forked from kennytm/ozdss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection2.oz
280 lines (241 loc) · 9.01 KB
/
Connection2.oz
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
functor
import
System
Pickle
Open
DSSCommon
ReflectionEx
export
OfferOnce
offer: OfferOnce
OfferMany
offerUnlimited: OfferMany
Take
define
%%% Store all tickets that can be taken. The key is the ticket ID, and the
%%% values can be `once#X#Y#Z`, meaning `X#Y#Z` should be sent to the other
%%% side and be deleted after taken, or `many#X#Y#Z`, for it won't be
%%% deleted.
%%%
%%% The `X`, `Y` and `Z` are
%%% X. The value itself.
%%% Y. The list of names which will become ReflectiveVariable when sent to
%%% the other side.
%%% Z. Similar to Y, but is for ReflectiveEntity.
TicketStore = {NewDictionary}
NextTicketID = {NewCell 0}
Initialized = {NewCell false}
%%% Initialize the connection module.
proc {Init}
if {Not @Initialized} then
{RunServer}
Initialized := true
end
end
DebugSerialization = true
% {{{ Low-level Serialization/Deserialization.
%%% Serialize a status code and the containing data of the form `ok(...)`
%%% or `notFound` etc. to in a VBS suitable for sending. This is mainly for
%%% sending a reply from server to client.
fun {SerializeResponse StatusCodeAndData}
if DebugSerialization then
{System.show [server send {Thread.this} StatusCodeAndData]}
end
{Pickle.pack StatusCodeAndData}
end
%%% Deserialize a VBS into a status code and containg data of the form
%%% `ok(...)` or `notFound` etc. This is mainly for receiving a reply from
%%% server to client.
fun {DeserializeResponse RawData}
Result = {Pickle.unpack RawData}
in
if DebugSerialization then
{System.show [client recv {Thread.this} Result]}
end
Result
end
%%% Deserialize a VBS into a request. This is mainly for receiving a request
%%% from client to server.
%%%
%%% Returns whether the site ID of the request matches this site's real ID.
fun {DeserializeRequest RawData ?Action ?ReplyIP ?ReplyPort ?ReplySiteID ?Data}
SiteID
in
post(SiteID Action ReplyIP ReplyPort ReplySiteID Data) = {Pickle.unpack RawData}
if DebugSerialization then
{System.show [server recv {Thread.this} Action Data]}
end
SiteID == {DSSCommon.mySiteID}
end
%%% Serialize a request into VBS. This is mainly for sending a request from
%%% client to server.
fun {SerializeRequest SiteID Action ReplyIP ReplyPort ReplySiteID Data}
if DebugSerialization then
{System.show [client send {Thread.this} Action Data]}
end
{Pickle.pack post(SiteID Action ReplyIP ReplyPort ReplySiteID Data)}
end
% }}}
% {{{ Reflection related.
%%% Create a callback function for the reflective entities. When the
%%% callback function runs, a `perform` action will be sent to the client at
%%% the corresponding IP:Port.
proc {ReflectionCallback SiteID IP#Port SrcName EncodedAction VarNames TokenNames}
_ = {RunClient IP Port SiteID perform EncodedAction#VarNames#TokenNames#SrcName}
end
% }}}
% {{{ Processors
%%% An interface for all action precessors.
class Processor from BaseObject
%%% Reply a message received from the client.
meth reply(Info ip:IP port:Port siteID:SiteID result:?ReplyStatusCodeAndData)
ReplyStatusCodeAndData = badRequest
end
meth onReply(IP Port SiteID StatusCodeAndData ?Reply)
if {Label StatusCodeAndData} \= ok then
{Exception.raiseError StatusCodeAndData}
else
Message = receive(ip:IP port:Port siteID:SiteID result:?Reply)
in
{self {Adjoin StatusCodeAndData Message}}
end
end
%%% Receive a message from the server
% meth receive(... ip:IP port:Port siteID:SiteID result:?Reply)
% skip
% end
end
class TakeProcessor from Processor
meth reply(TicketID ip:IP port:Port siteID:SiteID result:?Result)
case {Dictionary.condGet TicketStore TicketID unit}
of Persistence#Value#VarNames#TokenNames then
% Found a ticket. We register those proxies to the client's IP
% and port, so they could transparently receive the updates.
{ReflectionEx.registerRemoteObjects variable VarNames SiteID ReflectionCallback IP#Port}
{ForAll TokenNames ReflectionEx.tokenAddRef}
% Remove the ticket if it can only be offered once.
if Persistence == once then
{Dictionary.remove TicketStore TicketID}
end
Result = ok(Value#VarNames#TokenNames)
else
% Ticket not found.
Result = notFound
end
end
meth receive(Info ip:IP port:Port siteID:SiteID result:?Result)
Value#VarNames#TokenNames = Info
in
{ReflectionEx.registerRemoteObjects variable VarNames SiteID ReflectionCallback IP#Port}
{ReflectionEx.registerRemoteObjects token TokenNames SiteID ReflectionCallback IP#Port}
Result = {ReflectionEx.decode Value}
end
end
class PerformProcessor from Processor
meth reply(Info ip:IP port:Port siteID:SiteID result:?Result)
Action#VarNames#TokenNames#SrcName = Info
in
{System.show perform#Action#VarNames#TokenNames#SrcName}
{ReflectionEx.registerRemoteObjects variable VarNames SiteID ReflectionCallback IP#Port}
{ReflectionEx.registerRemoteObjects token TokenNames SiteID ReflectionCallback IP#Port}
{ReflectionEx.performAction SrcName {ReflectionEx.decode Action} SiteID}
Result = ok
end
meth receive(...)
skip
end
end
Processors = r(
take: {New TakeProcessor noop}
perform: {New PerformProcessor noop}
)
%}}}
% {{{ Server/client
ServerBlockingQueue = {NewCell nil}
proc {RunServer}
S = {New Open.socket init}
in
{S bind(takePort:{DSSCommon.myPort})}
{S listen}
thread
for do
C = {S accept(acceptClass:Open.socket accepted:$)}
in
thread
Data Action IP Port SiteID StatusCodeAndData
in
% Block until the server is free to read anything.
{ForAll {Exchange ServerBlockingQueue $ nil} Wait}
StatusCodeAndData = try
if {DeserializeRequest {C read(list:$)} ?Action ?IP ?Port ?SiteID ?Data} then
{Processors.Action reply(Data ip:IP port:Port siteID:SiteID result:$)}
else
badRequest
end
catch E then
{System.show E}
internalServerError
end
{C write(vs:{SerializeResponse StatusCodeAndData})}
end
end
end
end
fun {WithBlockingServer F}
OldValue
Blocker
RetVal
in
{Exchange ServerBlockingQueue OldValue (!!Blocker)|OldValue}
RetVal = {F}
Blocker = unit % unblock the server by making it deterministic.
RetVal
end
fun {RunClient IP Port SiteID Action Info}
C = {New Open.socket client(host:IP port:Port)}
Result
in
% Block the server until we have atomically sent and received the data
% we need. This avoids race condition when the server gives us something
% we aren't ready to digest.
Result = {WithBlockingServer fun {$}
Data
in
{C send(vs:{SerializeRequest SiteID Action
{DSSCommon.myIP} {DSSCommon.myPort}
{DSSCommon.mySiteID}
Info})}
Data = {DeserializeResponse {C read(list:$)}}
{Processors.Action onReply(IP Port SiteID Data $)}
end}
{C close}
Result
end
% }}}
fun {OfferWithPeristence Persistence V}
TicketID
NewTicketID
EncocdedData VarNames TokenNames
in
{Init}
% Atomically increase the ticket ID.
{Exchange NextTicketID ?TicketID ?NewTicketID}
NewTicketID = TicketID + 1
EncocdedData = {ReflectionEx.encode V ?VarNames ?TokenNames}
{Dictionary.put TicketStore TicketID Persistence#EncocdedData#VarNames#TokenNames}
{DSSCommon.getTicketPrefix}#TicketID
end
fun {OfferOnce V}
{OfferWithPeristence once V}
end
fun {OfferMany V}
{OfferWithPeristence many V}
end
fun {Take TicketURL}
IP Port SiteID TicketID
in
{Init}
{DSSCommon.parseTicketURL TicketURL ?IP ?Port ?SiteID ?TicketID}
{RunClient IP Port SiteID take TicketID}
end
end