-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.st
419 lines (351 loc) · 12.7 KB
/
server.st
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
Namespace current: Shampoo [
Error subclass: ClientDisconnected [
<category: 'Shampoo-Server'>
<comment: 'I am just an exception. My instances point out that a client
has disconnected from the server.'>
]
Error subclass: FatalError [
<category: 'Shampoo-Server'>
<comment: 'I am just an exception. My instances represent a fatal,
unrecoverable error.'>
]
Object subclass: ShampooTranscript [
<category: 'Shampoo-Server'>
<comment: 'I am a network-oriented version of the standard GNU Smalltalk
Transcript. I send every text being printed to a client, but also mirror the
text to the standard output, as the original Transcript does'>
servers := Set new.
serversMutex := Semaphore forMutualExclusion.
oldAssoc := nil.
ShampooTranscript class >> initialize [
<category: 'class initialization'>
self install
]
ShampooTranscript class >> message: aString [
<category: 'transcript'>
self fallback: aString.
serversMutex critical: [servers do: [:each | each echo: aString]]
]
ShampooTranscript class >> fallback: aString [
<category: 'transcript'>
oldAssoc ifNotNil:
[oldAssoc key perform: oldAssoc value with: aString]
]
ShampooTranscript class >> add: aServer [
<category: 'setup'>
serversMutex critical: [servers add: aServer]
]
ShampooTranscript class >> remove: aServer [
<category: 'setup'>
serversMutex critical:
[servers remove: aServer ifAbsent: []]
]
ShampooTranscript class >> install [
<category: 'installation'>
| message |
message := Transcript message.
(message key = ShampooTranscript and: [message value = #message:])
ifFalse: [oldAssoc := message.
Transcript message: (ShampooTranscript -> #message:)]
]
]
Object subclass: ConnectionState [
<category: 'Shampoo-Server'>
<comment: 'My subclasses represent various states of a user session on
the server. Yes, the server aims to look like as a finite state machine.'>
| connection |
ConnectionState class >> of: aConnection [
<category: 'instance creation'>
^(self new)
connection: aConnection;
yourself
]
connection: aConnection [
<category: 'private'>
connection := aConnection
]
switchTo: aConnectionStateClass [
<category: 'fsm'>
connection state: (aConnectionStateClass of: connection)
]
process: aRequest [
<category: 'fsm'>
self subclassResponsibility
]
echo: aString [
<category: 'fsm'>
"Do nothing by default. 'self subclassResponsibility' would be better?"
]
]
ConnectionState subclass: NotAuthorizedState [
<category: 'Shampoo-Server'>
<comment: 'My instances represent a non-authorized state of a user
session. When entered, I send a magic string to a client. If the client will
prove his identity, I will switch the session to the AutiorizedState.'>
| magic |
register [
<category: 'other'>
"Send a magic number to a client"
connection send: (MagicResponse number: self magic)
]
process: aRequest [
<category: 'fsm'>
"Anti-If campaign probably hates me."
aRequest class == LoginRequest
ifTrue: [^self checkLogin: aRequest]
ifFalse: [FatalError new signal]
]
checkLogin: aRequest [
<category: 'private'>
"Really hates."
(connection server authenticates: aRequest creds with: self magic)
ifTrue: [self switchTo: AuthorizedState.
^ServerInfoResponse id: aRequest id]
ifFalse: [FatalError new signal]
]
magic [
<category: 'private'>
^magic ifNil: [magic := Random between: 0 and: 16rFFFF]
]
]
ConnectionState subclass: AuthorizedState [
<category: 'Shampoo-Server'>
<comment: 'My instances represent authorized states of a user session.
Such state is also can be called "active". The most of Shampoo requests are
processed in this state.'>
process: aRequest [
<category: 'fsm'>
^aRequest execute
]
echo: aString [
<category: 'fsm'>
connection send: (EchoResponse id: -1 text: aString)
]
]
Object subclass: ProcessObject [
<category: 'Shampoo-Server'>
<comment: 'My instances represent objects bound to processes.
Such object do live inside its process and does the most of its operations
in the process context.'>
| process |
proc: aProcess [
<category: 'accessors'>
process := aProcess
]
suspend [
<category: 'suspend/resume'>
process ifNotNil: [process suspend]
]
resume [
<category: 'suspend/resume'>
process ifNotNil: [process resume]
]
]
Object subclass: ClientConnection [
<category: 'Shampoo-Server'>
<comment: 'My instances represent a client connection. A client connection
is actually a finite state machine, see ConnectionState class and its subclasses'>
| sock server disconnectedHandler state parser |
ClientConnection class >> on: aSocket onDisconnect: aBlock parent: aServer [
<category: 'instance creation'>
^aSocket
ifNil: [nil]
ifNotNil: [self new on: aSocket onDisconnect: aBlock parent: aServer]
]
on: aSocket onDisconnect: aBlock parent: aServer [
<category: 'private'>
sock := aSocket.
server := aServer.
disconnectedHandler := aBlock.
state := NotAuthorizedState of: self.
state register.
parser := MessageParser new.
]
go [
<category: 'loop'>
[sock isPeerAlive] whileTrue:
[[self fetchMessages do: [:m | self process: m]]
on: ClientDisconnected
do: [:e | ^self signalDisconnected]].
self signalDisconnected
]
fetchMessages [
<category: 'private'>
[sock ensureReadable] ifError: [ClientDisconnected new signal].
^parser process: (sock next: sock availableBytes)
]
signalDisconnected [
<category: 'private'>
disconnectedHandler ifNotNil: [:handler | handler value: self]
]
process: anXMLRequest [
<category: 'fsm'>
| r xml |
[xml := ShampooXML.ShNode from: anXMLRequest]
ifError:
[^server inform:
'Shampoo: failed to parse ', anXMLRequest printString].
r := Request from: xml.
[(self state process: r) do: [:resp | self send: resp]]
on: FatalError do: [:e | self close]
]
state: aClientConnectionState [
<category: 'fsm'>
state := aClientConnectionState
]
state [
<category: 'fsm'>
^state
]
echo: aString [
<category: 'fsm'>
self state echo: aString
]
send: aPacket [
<category: 'networking'>
| msg |
msg := aPacket asXML printString.
sock
nextPutAll: 'Content-Length: ', msg size printString;
crlf;
crlf;
nextPutAll: msg;
crlf.
sock flush
]
close [
<category: 'networking'>
sock close
]
server [
<category: 'accessors'>
^server
]
]
ProcessObject subclass: ShampooServer [
<category: 'Shampoo-Server'>
<comment: 'I am the heart of the Shampoo system. My instances manage
connections and... hmm, looks like thats all.'>
| server clients clientsMutex creds |
servers := Dictionary new.
serversMutex := Semaphore forMutualExclusion.
clients [
<category: 'private'>
^clients ifNil: [clients := OrderedCollection new]
]
acceptedClient [
<category: 'private'>
| client |
client := ClientConnection
on: server accept
onDisconnect: [:cl | self handleDisconnected: cl]
parent: self.
^client
]
authenticates: aCreds with: aMagic [
<category: 'authentication'>
^(creds with: aMagic) = aCreds
]
handleDisconnected: aClient [
<category: 'delegation'>
"This method is delegated to a client session in a block"
clientsMutex critical: [self clients remove: aClient]
]
startOn: aPort creds: aCreds [
<category: 'private'>
clientsMutex := Semaphore forMutualExclusion.
server := TCP.ServerSocket port: aPort.
creds := aCreds.
ShampooTranscript add: self.
[server isOpen] whileTrue:
[| conn |
[server waitForConnection]
ifError: [^self inform:
'Shampoo: failed to listen for incoming connections'].
conn := self acceptedClient.
conn ifNotNil:
[clientsMutex critical: [self clients add: conn].
[conn go] fork]]
]
echo: aString [
<category: 'transcript'>
clientsMutex critical:
[self clients do: [:each | each echo: aString]]
]
ShampooServer class >> startOn: aPort login: aLogin pass: aPass [
<category: 'instance creation'>
^self startOn: aPort creds: (AuthInfo login: aLogin pass: aPass)
]
ShampooServer class >> closeAll [
<category: 'networking'>
serversMutex critical:
[servers values do: [:each | each close].
servers empty]
]
ShampooServer class >> closeOn: aPort [
<category: 'networking'>
serversMutex critical:
[| srv |
srv := servers at: aPort ifAbsent: [^nil].
servers removeKey: aPort.
srv close]
]
ShampooServer class >> startOn: aPort creds: aCreds [
<category: 'instance creation'>
serversMutex critical:
[(servers includesKey: aPort) ifFalse:
[| srv |
srv := self new.
servers at: aPort put: srv.
srv proc: [srv startOn: aPort creds: aCreds] fork]]
]
ShampooServer class >> update: aspect [
<category: 'change and update'>
aspect == #aboutToSnapshot ifTrue: [^self broadcast: #suspend ].
aspect == #finishedSnapshot ifTrue: [^self broadcast: #resume ].
aspect == #returnFromSnapshot ifTrue: [^self broadcast: #restart ].
]
ShampooServer class >> initialize [
<category: 'class initialization'>
ObjectMemory addDependent: self
]
ShampooServer class >> broadcast: aMessage [
<category: 'private'>
serversMutex critical:
[servers values do: [:srv | srv perform: aMessage]]
]
suspend [
<category: 'private'>
self closeConnections.
super suspend.
]
restart [
<category: 'private'>
self close.
self proc: [self startOn: server port creds: creds] fork.
]
closeConnections [
<category: 'private'>
clientsMutex critical:
[self clients copy do: [:cl | cl close]].
"And that's all. Every client connection
will be removed from the set in the
ShampooServer>>handleDisconnected:."
]
close [
<category: 'private'>
[server close] ifError: [].
self closeConnections.
ShampooTranscript remove: self
]
inform: aMessage [
<category: 'error handling'>
ShampooTranscript fallback: aMessage.
^nil
]
]
]
Eval [
Shampoo.ShampooTranscript initialize.
Shampoo.ShampooServer initialize.
]