summaryrefslogtreecommitdiff
path: root/src/tcpclient/tcpclient.cc
blob: 7a6f9f5824a9919a6b776488f1a6f89991eabe65 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// 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/Matrix.h>

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

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

// PKG_ADD: autoload ("tcpclient", "tcpclient.oct");
DEFUN_DLD (tcpclient, args, nargout,
        "-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{tcpclient} = } tcpclient (@var{ipaddress}, @var{port})\n \
@deftypefnx {Loadable Function} {@var{tcpclient} = } tcpclient (@var{ipaddress}, @var{port}, [@var{propertyname}, @var{propertyvalue}])\n \
\n\
Open tcpclient interface.\n \
\n\
@subsubheading Inputs\n \
@var{ipaddress} - the ip address of type String.@* \
@var{port} - the port number to connect.@* \
@var{propname},@var{propvalue} - property name/value pairs.\n \
\n \
Known input properties:\n \
@table @asis\n \
@item Name\n \
name value\n \
@item Timeout\n \
Numeric timeout value or -1 to wait forever\n \
@item UserData\n \
User data value.\n \
@end table\n \
\n\
@subsubheading Outputs\n \
The tcpclient() shall return instance of @var{octave_tcpclient} class as the result @var{tcpclient}.\n \
\n \
@subsubheading Properties\n \
The tcpclient object has the following public properties:\n \
@table @asis\n \
@item Name\n \
name assigned to the tcpclient object\n \
@item Type\n \
instrument type 'tcpclient' (readonly)\n \
@item Port\n \
remote port number (Readonly)\n \
@item Address\n \
remote host address (Readonly)\n \
@item Status\n \
status of the object 'open' or 'closed' (readonly)\n \
@item Timeout\n \
timeout value in seconds used for waiting for data\n \
@item NumBytesAvailable\n \
number of bytes currently available to read (readonly)\n \
@item NumBytesWritten\n \
number of bytes currently available to read (readonly)\n \
@item ByteOrder\n \
Byte order  for data (currently not used)\n \
@item Terminator\n \
Terminator value used for string data (currently not used)\n \
@item UserData\n \
User data\n \
@end table \n \
@end deftypefn")
{
#ifndef BUILD_TCP
  error("tcpclient: Your system doesn't support the TCP interface");
  return octave_value ();
#else
  // Do not open interface if return value is not assigned
  if (nargout != 1)
    {
      print_usage ();
      return octave_value ();
    }

  // Default values
  std::string address ("127.0.0.1");
  std::string name = "";
  int port = 23;
  double timeout = -1;
  octave_value userdata = Matrix();

  // Parse the function arguments
  if (args.length () < 2)
    {
      print_usage ();
      return octave_value ();
    }

  if (args (0).is_string ())
    {
      address = args (0).string_value ();
    }
  else
    {
       error ("Expected address as a string");
       return octave_value ();
    }

  if (args (1).OV_ISINTEGER () || args (1).OV_ISFLOAT ())
    {
      port = args (1).int_value ();
    }
  else
    {
       error ("Expected port as a number");
       return octave_value ();
    }
 
  if (args.length () > 2)
    {
       if ((args.length () & 1) == 1)
         {
           error ("Expected property name/value pairs");
           return octave_value ();
         }

      // go through the properties
      for(int i=2;i<args.length();i+=2)
        {
          std::string propname = args(i).string_value();
          octave_value propval = args(i+1);

          std::transform (propname.begin (), propname.end (), propname.begin (), ::tolower);

          if (propname == "name")
            {
              if (propval.is_string ())
                name = propval.string_value ();
              else
                {
                  error ("name must be a string");
                  return octave_value ();
                }
            }
          else if (propname == "timeout")
            {
              if (propval.OV_ISINTEGER () || propval.OV_ISFLOAT ())
                timeout = propval.double_value ();
              else
                {
                  error ("timeout must be a integer or double");
                  return octave_value ();
                }
            }
          else if (propname == "userdata")
	    {
              userdata = propval;
	    }
          else
            {
              error ("unknown property '%s'", propname.c_str ());
              return octave_value ();
            }
        }

    }

  // Open the interface and connect
  octave_tcpclient* retval = new octave_tcpclient ();

  if (retval->open (address, port) < 0)
    {
      return octave_value ();
    }

  retval->set_timeout (timeout);

  //retval->flush (2);
  if (name.length() > 0)
    retval->set_name (name);

  retval->set_userdata (userdata);

  return octave_value (retval);
#endif
}

#if 0
%!test
%! addr = resolvehost ('gnu.org', 'address');
%! a = tcpclient (addr, 80);
%! assert (! isnull (a));
%! assert (isa (a, 'octave_tcpclient'));
%! clear a

%!test
%! addr = resolvehost ('gnu.org', 'address');
%! a = tcpclient (addr, 80, 'name', 'test', 'timeout', 2.5);
%! assert (! isnull (a));
%! assert (isa (a, 'octave_tcpclient'));
%! assert (get(a, 'Name'), 'test');
%! assert (get(a, 'Timeout'), 2.5);
%! clear a

%!error <Invalid call to tcpclient> tcpclient (1)

%!error <Invalid call to tcpclient> tcpclient (1, 1)

%!error <Invalid call to tcpclient> tcpclient ('127.0.0.1', '80', 'prop1')

#endif