File: client.m2

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (164 lines) | stat: -rw-r--r-- 5,058 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
----------------------------------
---------- CLIENT CODE -----------
----------------------------------
net SCSCPConnection := x -> x#"nicedesc";

closeConnection := s -> (
	s#"nicedesc" = "Closed SCSCP connection";
	try close s#"fd";
)

Manipulator SCSCPConnection := (m, s) -> (
	if m === close then closeConnection s
	else error concatenate("Cannot apply Manipulator ", toString m, " to SCSCPConnection");
)

newConnection = method();
newConnection (String, String) := (host, port) -> (
	hostport := host|":"|port;
	dbgout(1) << "[Client] Connecting to " << hostport << endl;

	s := new SCSCPConnection;
	s#"nicedesc" = "Uninitialized SCSCP connection to " | hostport;
	s#"fd" = openInOut ("$"|hostport);

	ans := ""; buf := "";
	while #ans === 0 or ans#-1 =!= "\n" do (
		buff := read s#"fd";
		buf = buff|"";
		if atEndOfFile s#"fd" then ( 
			dbgout(0) << "[Client]  atEndOFFile" << endl; 
			closeConnection s;		
			error("atEndOfFile during connection"); 
		);

		ans = ans|buf;
	);

	dbgout(2) << "[Client] Received: '" << ans << "'" << endl;
	re := "<\\?scscp (service_name)=\"([^\"]+)\" (service_version)=\"([^\"]+)\" (service_id)=\"([^\"]+)\" (scscp_versions)=\"([^\"]+)\" \\?>";
	if (mtch := regex(re, ans)) === null then (
		dbgout(1) << "[Client] Received invalid HELO" << endl;
		s#"nicedesc" = "SCSCP Connection to " | hostport;
	) else (
		for i in 1..4 do s#(substring(ans, mtch#(2*i-1)#0, mtch#(2*i-1)#1)) = substring(ans, mtch#(2*i)#0, mtch#(2*i)#1);
		s#"nicedesc" = "SCSCP Connection to " | s#"service_name" | " (" | s#"service_version" | ") on " | hostport;
	);

	versionstr := ///<?scscp version="/// | ProtVersion | ///" ?>///;
	dbgout(2) << "[Client] Requesting '" << versionstr << "'" << endl;
	s#"fd" << versionstr << endl << flush;
	
	dbgout(2) << "[Client] Waiting for response to version request..." << endl;
	ans = "";
	while #ans == 0 or ans#-1 =!= "\n" do (
		buff = read s#"fd";
		buf = buff|"";
		if atEndOfFile s#"fd" then ( 
			dbgout(0) << "[Client]  atEndOFFile" << endl; 
			closeConnection s;		
			error("atEndOfFile during version negotiation"); 
		);

		ans = ans|buf;
	);
	
	dbgout(2) << "[Client] Received: '" << ans << "'" << endl;
	
	if ans =!= versionstr|"\n" then (
		dbgout(0) << "[Client] Incompatible." << endl;
		closeConnection s;
		error("SCSCP connection failed.");
	);
	
	s	
);
newConnection (String, ZZ) := (host, port) -> newConnection(host, toString port);
newConnection (String) := s -> (
	mtch := regex("^(.*)\\:(.*)$", s);
	if mtch === null then 
		newConnection(s, "26133")
	else
		newConnection(substring(s, mtch#1#0, mtch#1#1),substring(s, mtch#2#0, mtch#2#1))
)


compute := method()
compute (SCSCPConnection, XMLnode, String) := (s,x, ret) -> (

	if not isOpen s#"fd" then error("Connection is closed.");

	dbgout(5) << "[Compute] replaceMultipleIDs..." << endl;
	x = replaceMultipleIDs x;

	dbgout(2) << "[Compute] Constructing procedure call..." << endl;
	if x.tag =!= "OMA" then x = OMA("fns1", "identity", {x});
	pc := OMA("scscp1", "procedure_call", { x });
	setOMAttr(pc, OMS("scscp1", "call_id"), OMSTR(toString (callIDCounter = callIDCounter+1)));
	setOMAttr(pc, OMS("scscp1", "option_return_"|ret), OMSTR(""));
	pc = OMOBJ(pc);
	pc = createOMATTRObj(pc);
	dbgout(5) << endl << pc << endl;

	dbgout(2) << "[Compute] Sending procedure call..." << endl;
	tspc := toString toLibxmlNode pc;
	dbgout(5) << endl << tspc << endl;
	s#"fd" << "<?scscp start ?>" << endl << flush;
	s#"fd" << tspc << endl;
	s#"fd" << "<?scscp end ?>" << endl << flush;

	dbgout(2) << "[Compute] Waiting for response..." << endl;
	ans := "";
	waitfor := "(.*)<\\?scscp end \\?>\n$";
	while not match(waitfor, ans) do (
		buff := read s#"fd";
		buf := buff|"";
		if atEndOfFile s#"fd" then ( dbgout(0) << "[Client]  atEndOFFile" << endl; return null; );

		ans = ans|buf;
	);
	
	dbgout(2) << "[Client] Answer received..." << endl;
	dbgout(5) << endl << ans << endl;
	
	dbgout(2) << "[Client] Parsing answer..." << endl;
	y := parse ans;
	if class(y) =!= XMLnode then
		error "Parsing failed.";

	dbgout(2) << "[Client] Identifying remote objects..." << endl;
	y = identifyRemoteObjects(s, y);
		
	y
)
compute (SCSCPConnection, Thing, String) := (s,x, ret) -> (
	if not isOpen s#"fd" then error("Connection is closed.");

	dbgout(2) << "[Compute] Constructing OpenMath object..." << endl;
	o := openMath x;
	if class(o) === XMLnode and o.tag === "OME" then (
		stderr << o << endl;
		error(concatenate("Could not convert '", toString x, "' of type '",toString class x,"'to OpenMath"));
	);
	
	a := compute(s, o, ret);
	
	dbgout(2) << "[Compute] Evaluating answer..." << endl;
	dbgout(5) << endl << a << endl;
	t := value a;

	t
)
compute (SCSCPConnection, XMLnode) := (s,x) -> compute(s, x, "object");
compute (SCSCPConnection, Thing) := (s,t) -> compute(s, t, "object");



-- This allows for s( .. something .. )
SCSCPConnection Thing := (s,t) -> (
	if      class(t) === Sequence and #t === 1 then compute(s, t#0)
	else if class(t) === Sequence and #t === 2 then compute(s, t#0, t#1)
	else compute(s,t)
)