summaryrefslogtreecommitdiff
path: root/src/tcpclient/__tcpclient_properties__.cc
blob: a9e64aaad194c86f79db0a1de1eaca25bd298603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (C) 2021   John Donoghue   <john.donoghue@ieee.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.

#include <octave/oct.h>
#include <octave/ov-struct.h>

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#ifdef BUILD_TCP
#  include "tcpclient_class.h"
#endif

static octave_value_list get_terminator (octave_tcpclient* tcp)
{
  // may have a single terminator or a start and stop
  octave_value in = tcp->get_input_terminator ();
  octave_value out = tcp->get_output_terminator ();

  if(in.is_string() && out.is_string() && in.string_value() == out.string_value())
    return in;
  else if(in.is_scalar_type() && out.is_scalar_type() && in.int_value() == out.int_value())
    return in;
  else
    {
      Cell ret = Cell(dim_vector(1, 2));
      ret(0) = in;
      ret(1) = out;
      return octave_value (ret);
    }
}

static octave_value set_terminator(octave_tcpclient* tcp, const octave_value_list& args)
{
  if (args.length () == 1)
    {
      if ( !(args (0).is_string ()) && !(args (0).is_scalar_type ()))
        (*current_liboctave_error_handler) ("argument must be a number or string");

      tcp->set_input_terminator (args (0));
      tcp->set_output_terminator (args (0));

      return octave_value (); // Should it return by default?
    }
  else if (args.length () == 2)
    {
      if ( !(args (0).is_string ()) && !(args (0).is_scalar_type ()))
        (*current_liboctave_error_handler) ("argument must be a number or string");
      if ( !(args (1).is_string ()) && !(args (1).is_scalar_type ()))
        (*current_liboctave_error_handler) ("argument must be a number or string");

      tcp->set_input_terminator (args (0));
      tcp->set_output_terminator (args (1));

      return octave_value (); // Should it return by default?
    }
  else if (args.length () > 2)
    (*current_liboctave_error_handler) ("wrong number of arguments");

  return octave_value();
}

// PKG_ADD: autoload ("__tcpclient_properties__", "tcpclient.oct");
DEFUN_DLD (__tcpclient_properties__, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {varargout =} __tcpclient_properties__ (@var{octave_tcpclient}, @var{property}, @var{varargin})\n\
Undocumented internal function.\n\
@end deftypefn")
{
#ifdef BUILD_TCP
  if (args.length () < 2 ||
    args(0).type_id () != octave_tcpclient::static_type_id () || 
    !args(1).is_string ())
      (*current_liboctave_error_handler) ("wrong number of arguments");
    
  const octave_base_value& rep = args(0).get_rep ();
  octave_tcpclient* tcpclient = &((octave_tcpclient &)rep);
    
  std::string property = args(1).string_value ();
  std::transform (property.begin (), property.end (), property.begin (), ::tolower);

  int maxinputs = 3;
  if (property == "terminator")
    maxinputs = 4;

  if (args.length () > maxinputs)
    (*current_liboctave_error_handler) ("wrong number of arguments");

  if (args.length () == 2) // get
    {
      if (property == "name")
        return octave_value (tcpclient->get_name ());
      else if (property == "type")
        return octave_value (tcpclient->get_type ());
      else if (property == "port")
        return octave_value (tcpclient->get_port ());
      else if (property == "address")
        return octave_value (tcpclient->get_address ());
      else if (property == "status")
        return octave_value (tcpclient->get_status ());
      else if (property == "timeout")
        return octave_value (tcpclient->get_timeout ());
      else if (property == "numbytesavailable")
        return octave_value (tcpclient->get_numbytesavailable ());
      else if (property == "numbyteswritten")
        return octave_value (tcpclient->get_numbyteswritten ());
      else if (property == "byteorder")
        return octave_value (tcpclient->get_byteorder ());
      else if (property == "userdata")
        return octave_value (tcpclient->get_userdata ());
      else if (property == "terminator")
        return get_terminator (tcpclient);
      else
        (*current_liboctave_error_handler) ("invalid property name");
    }
  else // set
    {
      if (property == "name")
        return octave_value (tcpclient->set_name (args(2).string_value ()));
      else if (property == "type")
        (*current_liboctave_error_handler) ("can not set this property");
      else if (property == "port")
        (*current_liboctave_error_handler) ("can not set this property");
      else if (property == "address")
        (*current_liboctave_error_handler) ("can not set this property");
      else if (property == "status")
        (*current_liboctave_error_handler) ("can not set this property");
      else if (property == "timeout")
        return octave_value (tcpclient->set_timeout (args(2).double_value ()));
      else if (property == "userdata")
        { tcpclient->set_userdata (args(2)); return octave_value (); }
      else if (property == "byteorder")
        return octave_value (tcpclient->set_byteorder (args(2).string_value ()));
      else if (property == "flush")
        return octave_value (tcpclient->flush (args(2).int_value ()));
      else if (property == "terminator")
        return set_terminator (tcpclient, args.slice (2, args.length ()-2));
      else
        (*current_liboctave_error_handler) ("invalid property name");
    }

#endif
    /* never reached in normal operation */
  (*current_liboctave_error_handler) ("Your system doesn't support the TCP interface");
}
#if 0
%!shared ip
%! ip = resolvehost("www.octave.org", "address");

%!test
%! # test get
%! a = tcpclient (ip, 80);
%! assert (__tcpclient_properties__ (a,"type"), "tcpclient");
%! assert (__tcpclient_properties__ (a,"port"), 80);
%! assert (__tcpclient_properties__ (a,"address"), ip);
%! assert (__tcpclient_properties__ (a,"timeout"), -1);
%! assert (__tcpclient_properties__ (a,"status"), "open");
%! assert (__tcpclient_properties__ (a,"name"), ["TCP-" ip]);
%! fail ("__tcpclient_properties__ (a,'invalid')", "invalid property name");
%! clear a

%!test
%! # test set
%! a = tcpclient(ip, 80);
%! __tcpclient_properties__ (a, 'name', "mytest");
%! assert (__tcpclient_properties__ (a,"name"), "mytest");
%! fail ("__tcpclient_properties__ (a,'invalid', 1)", "invalid property name");
%! clear a

%!test
%! # test flush
%! a = tcpclient(ip, 80);
%! __tcpclient_properties__ (a, 'flush', 0);
%! __tcpclient_properties__ (a, 'flush', 1);
%! __tcpclient_properties__ (a, 'flush', 2);
%! fail ("__tcpclient_properties__ (a,'flush')", "invalid property name");
%! clear a


%!error <wrong number of arguments> __tcpclient_properties__ ()

%!error <wrong number of arguments> __tcpclient_properties__ (1)

%!test
%! a = tcpclient (ip, 80);
%! fail ("__tcpclient_properties__ (a, 'name', 'test', 0)", "wrong number of arguments");
%! clear a

#endif