File: client.g

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 (190 lines) | stat: -rw-r--r-- 6,122 bytes parent folder | download | duplicates (4)
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
###########################################################################
##
#W client.g                The SCSCP package            Alexander Konovalov
#W                                                             Steve Linton
##
###########################################################################


###########################################################################
#
# PingSCSCPservice( server, port )
#
InstallGlobalFunction( PingSCSCPservice,
function( server, port )
local stream, initmessage, rt;
if IN_SCSCP_TRACING_MODE then SCSCPTraceSendMessage( port ); fi;
stream := InputOutputTCPStream( server, port );
if stream <> fail then
  initmessage := ReadLine( stream );
  Info( InfoSCSCP, 1, "Got connection initiation message" );
  CloseStream(stream); 
  return true;
else
  return fail;
fi;    
end);


###########################################################################
#
# PingStatistic( server, port, nr )
#
InstallGlobalFunction( PingStatistic,
function( server, port, nr )
local stream, initmessage, i, rt, rt1, rt2, res, t, 
      nr_good, nr_lost, min_time, max_time;
nr_good := 0;
nr_lost := 0;
rt:=0;
max_time := 0;
min_time := infinity;
for i in [ 1 .. nr ] do
  rt1:=Runtime();
  stream := InputOutputTCPStream( server, port );
  if stream <> fail then
    initmessage := ReadLine( stream );
    Info( InfoSCSCP, 1, "Got connection initiation message nr ", i );
    rt2:=Runtime();
    t:=rt2-rt1;
    CloseStream(stream); 
    res := true;
  else
    res := false;
  fi; 
  if res then 
    nr_good := nr_good + 1;  
    rt := rt + t;
    if t < min_time then
      min_time := t;
    elif t > max_time then
      max_time := t;
    fi;     
  else
    nr_lost := nr_lost + 1;  
  fi;
od;
Print( nr, " packets transmitted, ", 
       nr_good, " received, ", 
       100*(nr_lost/nr), "% packet loss, time ", rt , "ms\n" );
if nr_good > 0 then       
       Print( "min/avg/max = ", [ min_time, rt/nr_good, max_time], "\n" );
fi;      
end);


###########################################################################
#
# StartSCSCPsession( stream )
#
InstallGlobalFunction( StartSCSCPsession,
function( stream )
local initmessage, session_id, pos1, pos2, server_scscp_version, suggested_versions;
initmessage := ReadLine( stream );
NormalizeWhitespace( initmessage );
Info( InfoSCSCP, 1, "Got connection initiation message" );
Info( InfoSCSCP, 2, initmessage );
session_id := initmessage{ [ PositionSublist(initmessage,"service_id=")+12 .. 
                             PositionSublist(initmessage,"\" scscp_versions")-1 ] };
server_scscp_version:=initmessage{ [ PositionSublist(initmessage,"scscp_versions=")+16 .. 
                                     PositionSublist(initmessage,"\" ?>")-1 ] };
server_scscp_version := SplitString( server_scscp_version, " " );
if not SCSCP_VERSION in server_scscp_version then
  # we select the highest compatible version of the protocol or insist on our version
  suggested_versions := Intersection( server_scscp_version, SCSCP_COMPATIBLE_VERSIONS );
  if Length( suggested_versions ) > 0 then
    SCSCP_VERSION := Maximum( suggested_versions );
  fi;
fi;
Info(InfoSCSCP, 1, "Requesting version ", SCSCP_VERSION, " from the server ..."); 
WriteLine( stream, Concatenation( "<?scscp version=\"", SCSCP_VERSION, "\" ?>" ) );
server_scscp_version := ReadLine( stream );
pos1 := PositionNthOccurrence(server_scscp_version,'\"',1);
pos2 := PositionNthOccurrence(server_scscp_version,'\"',2);
if pos1=fail or pos2=fail then
  CloseStream( stream );
  Error( "Incompatible protocol versions, the server requires ", server_scscp_version );
else 
  server_scscp_version := server_scscp_version{[ pos1+1 .. pos2-1 ]};
  if server_scscp_version <> SCSCP_VERSION then
    CloseStream( stream );
    Error("Incompatible protocol versions, the server requires ", server_scscp_version );
  else
    Info(InfoSCSCP, 1, "Server confirmed version ", SCSCP_VERSION, " to the client ..."); 
    return session_id;          
  fi;  
fi;
end);


###########################################################################
#
# EvaluateBySCSCP( command, listargs, <connection | server, port> : 
#                  output:=object/coookie/nothing/tree, 
#                  cd:="cdname", debuglevel:=N );
#
# Options object/coookie/nothing/tree are incompatible.
#
# For object/coookie/nothing see definions in the SCSCP specification.
#
# Option output:="tree" is used when it is necessary to suppress evaluation
# of the XML tree representing the OpenMath object (for example, to be used
# with "get_allowed_heads").
#
# The last option "cd" is used to specify the name of the OpenMath content
# dictionary when it is different from the transient CD used by default.
#
InstallGlobalFunction( EvaluateBySCSCP,
function( arg )

local output_option, debug_option, opt, cdname, process, result;

if ValueOption("output") <> fail then
  output_option := ValueOption("output");
else
  output_option := "object";  
fi;

if not output_option in 
           [ "object", "cookie", "nothing", "tree", "deferred" ] then
	Error( "output must be one of ", 
	       [ "object", "cookie", "nothing", "tree", "deferred" ], "\n" );
fi;

if ValueOption("cd") <> fail then
  cdname := ValueOption("cd");
else
  cdname := "";
fi;

if ValueOption("debuglevel") <> fail then
  debug_option := ValueOption("debuglevel");
else
  debug_option := 0;
fi;

if Length(arg)=3 then
    process := NewProcess( arg[1], arg[2], arg[3] : output := output_option, 
                                    cd:=cdname, debuglevel := debug_option );
elif Length(arg)=4 then
    process := NewProcess( arg[1], arg[2], arg[3], arg[4] : output := output_option, 
                                    cd:=cdname, debuglevel := debug_option );
else
    Error("EvaluateBySCSCP : wrong number of arguments\n");
fi;

Info( InfoSCSCP, 1, "Waiting for reply ...");
if output_option = "tree" then
  result := CompleteProcess( process : output:="tree" );
else
  result := CompleteProcess( process );
fi;

return result;

end);

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