File: xstream.gi

package info (click to toggle)
gap-scscp 2.2.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,696 kB
  • sloc: xml: 1,226; sh: 388; makefile: 19
file content (301 lines) | stat: -rw-r--r-- 9,135 bytes parent folder | download | duplicates (3)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
###########################################################################
##
#W xstream.gi               The SCSCP package             Alexander Konovalov
#W                                                               Steve Linton
##
###########################################################################

           
###########################################################################
##
##  InputOutputTCPStream( <hostname>, <port> )
##  InputOutputTCPStream( <socket_descriptor> )
##
##  The first usage is to be used by client and specifies hostname and port.
##  The second usage is to be used by server and specifies socket descriptor
##  which will be used to accept incoming connections.
##        
InstallGlobalFunction( InputOutputTCPStream,
function( arg )
# InputOutputLocalProcess has 3 arguments: cdir, exec, argts
# at least for now, we want to preserve four components of the internal 
# representation of input/output streams, with the following correspondence
# 1) ptynum --> object in the category IsFile
# 2) basename --> hostname
# 3) argts --> ??? arguments to be communicated to the hostname?
# 4) false --> false # what'is the meaning of this?
local hostname, port, lookup, sock, res, fio, socket_descriptor;
if Length( arg ) = 2 then # client case
  hostname := arg[1];
  port := arg[2];
  if not IsString( hostname ) then
    Error( "InputOutputTCPStream: <hostname> must be a string! \n");  
  fi;
  if not ( IsInt(port) and port >= 0 ) then
    Error( "InputOutputTCPStream: <port> must be a non-negative integer! \n");  
  fi;
  lookup := IO_gethostbyname( hostname );
  if lookup = fail then
    Print( "InputOutputTCPStream: cannot find hostname ", hostname, "\n");
    return fail;
  fi; 
  Info( InfoSCSCP, 1, "Creating a socket ..." );
  sock := IO_socket( IO.PF_INET, IO.SOCK_STREAM, "tcp" );
  Info( InfoSCSCP, 1, "Connecting to a remote socket via TCP/IP ..." );
  res := IO_connect( sock, IO_make_sockaddr_in( lookup.addr[1], port ) );  
  if res = fail then
    Print( "Error: ", LastSystemError(), "\n" );
    IO_close( sock );
    return fail;
  else
    fio := IO_WrapFD( sock, IO.DefaultBufSize, IO.DefaultBufSize );
    return Objectify( InputOutputTCPStreamDefaultType,
                      [ fio, hostname, [ port ], false ] );
  fi;
elif Length( arg ) = 1 then # server case
  socket_descriptor := arg[1];
  if not ( IsInt(socket_descriptor) and socket_descriptor >= 0 ) then
    Error( "InputOutputTCPStream: <socket_descriptor> must be a non-negative integer! \n");  
  fi;
  fio := IO_WrapFD( socket_descriptor, IO.DefaultBufSize, IO.DefaultBufSize );
  return Objectify( InputOutputTCPStreamDefaultType,
                    [ fio, "socket descriptor", [ socket_descriptor ], false ] );  
else
  Error( "InputOutputTCPStream: usage \n",
         "InputOutputTCPStream(<hostname>, <port>) for client, \n", 
         "InputOutputTCPStream(<socket_descriptor>) for server! \n"); 
fi;
end);


###########################################################################
##
#M  ViewObj( <ioTCPstream> )
##
InstallMethod( ViewObj, "for ioTCPstream",
[ IsInputOutputTCPStreamRep and IsInputOutputStream ],
function(stream)
    Print("< ");
    if IsClosedStream(stream) then
        Print("closed ");
    fi;
    Print("input/output TCP stream to ",stream![2],":", stream![3][1], " >");
end);


###########################################################################
##
#M  PrintObj( <ioTCPstream> )
##
InstallMethod( PrintObj, "for ioTCPstream",
[ IsInputOutputTCPStreamRep and IsInputOutputStream ],
function(stream)
    local i;
    Print("< ");
    if IsClosedStream(stream) then
        Print("closed ");
    fi;
    Print("input/output TCP stream to ",stream![2],":", stream![3][1], " >");
end);


###########################################################################
##
#M  ReadByte( <ioTCPstream> )
##
InstallMethod( ReadByte, "for ioTCPstream", 
[ IsInputOutputTCPStreamRep and IsInputOutputStream ],
function(stream)
local buf;
    buf := IO_Read( stream![1], 1 );
    if buf = fail or Length(buf) = 0 then
        stream![4] := true;
        return fail;
    else
        stream![4] := true;
        return INT_CHAR(buf[1]);
    fi;
end);


###########################################################################
##
#M  ReadLine( <ioTCPstream> )
##
InstallMethod( ReadLine, "for ioTCPstream",
[ IsInputOutputTCPStreamRep and IsInputOutputStream ],
function( stream )
    local sofar, chunk;
    sofar := IO_Read( stream![1], 1 );
    if sofar = fail or Length(sofar) = 0 then
        stream![4] := true;
        return fail;
    fi;
    while sofar[ Length(sofar) ] <> '\n' do
        chunk := IO_Read( stream![1], 1);
        if chunk = fail or Length(chunk) = 0 then
            stream![4] := true;
            return sofar;
        fi;
        Append( sofar, chunk );
    od;
    return sofar;
end);


###########################################################################
##
#M  ReadAllIoTCPStream( <ioTCPstream> )
##
BindGlobal( "ReadAllIoTCPStream", function(stream, limit)
    local sofar, chunk, csize;
    if limit = -1 then
        csize := 20000;
    else
        csize := Minimum(20000,limit);
        limit := limit - csize;
    fi;
    sofar := IO_Read(stream![1], csize);
    if sofar = fail or Length(sofar) = 0 then
        stream![4] := true;
        return fail;
    fi;
    while limit <> 0  do
        if limit = -1 then
            csize := 20000;
        else
            csize := Minimum(20000,limit);
            limit := limit - csize;
        fi;
        chunk := IO_Read( stream![1], csize);
        if chunk = fail or Length(chunk) = 0 then
            stream![4] := true;
            return sofar;
        fi;
        Append(sofar,chunk);
    od;
    return sofar;
end);


InstallMethod( ReadAll, "for ioTCPstream", 
    [ IsInputOutputTCPStreamRep and IsInputOutputStream ],
    stream ->  ReadAllIoTCPStream(stream, -1) );


InstallMethod( ReadAll, "for ioTCPstream", 
[ IsInputOutputTCPStreamRep and IsInputOutputStream, IsInt ],
function( stream, limit )
    if limit < 0 then
        Error("ReadAll: negative limit not allowed");
    fi;
    return  ReadAllIoTCPStream(stream, limit);
end);


###########################################################################
##
#M  WriteByte( <ioTCPstream> )
##
InstallMethod( WriteByte, "for ioTCPstream", 
[ IsInputOutputTCPStreamRep and IsInputOutputStream, IsInt ],
function(stream, byte)
    local ret,s;
    if byte < 0 or 255 < byte  then
        Error( "<byte> must an integer between 0 and 255" );
    fi;
    s := [CHAR_INT(byte)];
    ConvertToStringRep( s );
    ret := IO_Write( stream![1], s );
    if ret <> 1 then
        return fail;
    else
        return true;
    fi;
end);


###########################################################################
##
#M  WriteLine( <ioTCPstream>, <string> ) . . . write plus newline and flush
##
InstallMethod( WriteLine, "for ioTCPstream",
[ IsInputOutputTCPStreamRep and IsInputOutputStream, IsString ],
function( stream, string )
#    local res;
#    res := WriteAll( stream, string );
#    if res <> true  then 
#      return res;  
#    fi;
#    WriteByte( stream, INT_CHAR('\n') );
#    return IO_Flush( stream![1] );
return IO_WriteLine( stream![1], string );
end );


###########################################################################
##
#M  WriteAll( <ioTCPstream>, <string> )  . . . . . . . . .  write all bytes
##
InstallMethod( WriteAll, "for ioTCPstream",
[ IsInputOutputTCPStreamRep and IsInputOutputStream, IsString ],
function( stream, string )
    local   byte;
    for byte in string  do
        if WriteByte( stream, INT_CHAR(byte) ) <> true  then
            return fail;
        fi;
    od;
    return true;
end );


###########################################################################
##
#M IsEndOfStream( <ioTCPstream> )
##
InstallMethod( IsEndOfStream, "iostream",
    [ IsInputOutputTCPStreamRep and IsInputOutputStream ],
    stream -> not IO_HasData( stream![1] ) );
    # or IS_BLOCKED_IOSTREAM(stream![1]) );
        

###########################################################################
##
#M  CloseStream( <ioTCPstream> )
##
InstallMethod( CloseStream, "for ioTCPstream",
[ IsInputOutputTCPStreamRep and IsInputOutputStream ],
function(stream)
    IO_Close( stream![1] );
    SetFilterObj( stream, IsClosedStream );
end);


###########################################################################
##
#M  FileDescriptorOfStream( <ioTCPstream> )
##
InstallMethod( FileDescriptorOfStream, "for ioTCPstream",
[ IsInputOutputTCPStreamRep and IsInputOutputStream ],
stream -> IO_GetFD( stream![1] ) );


###########################################################################
##
#F  SCSCPwait( <ioTCPstream>, [ <timeout>] )
##
InstallGlobalFunction( SCSCPwait,
function( arg )
if Length(arg) = 2 then
    IO_Select( [ arg[1]![1] ], [ ], [ ], [ ], arg[2], 0 );
elif Length(arg) = 1 then
    IO_Select( [ arg[1]![1] ], [ ], [ ], [ ], 3600, 0 );
fi;
end);    

###########################################################################
##
#E 
##