summaryrefslogblamecommitdiffstats
path: root/emacs.d/lisp/rudel/obby/rudel-obby-server.el
blob: 5bf115851810922216538efac66b4deb828ac0d9 (plain) (tree)
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798





























































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                  
;;; rudel-obby-server.el --- Server component of the Rudel obby backend
;;
;; Copyright (C) 2008, 2009 Jan Moringen
;;
;; Author: Jan Moringen <scymtym@users.sourceforge.net>
;; Keywords: Rudel, obby, backend, server
;; X-RCS: $Id:$
;;
;; This file is part of Rudel.
;;
;; Rudel is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; Rudel is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Rudel. If not, see <http://www.gnu.org/licenses>.


;;; Commentary:
;;
;; This file contains the server part of the obby backend for Rudel.
;;
;; It is implemented using one state machine (class
;; `rudel-obby-client') for each client connection. These state
;; machines have the following states:
;;
;; + new                  `rudel-obby-server-state-new'
;; + encryption-negotiate `rudel-obby-server-state-encryption-negotiate'
;; + before-join          `rudel-obby-server-state-before-join'
;; + idle                 `rudel-obby-server-state-idle'


;;; History:
;;
;; 0.2 - State machine.
;;
;; 0.1 - Initial revision.


;;; Code:
;;

(eval-when-compile
  (require 'cl))

(require 'eieio)

(require 'jupiter)

(require 'rudel-state-machine)

(require 'rudel-obby-errors)
(require 'rudel-obby-util)
(require 'rudel-obby-state)


;;; Class rudel-obby-server-state-new
;;

(defclass rudel-obby-server-state-new
  (rudel-obby-server-connection-state)
  ()
  "State in which new connections start out.")

(defmethod rudel-enter ((this rudel-obby-server-state-new))
  "Sends welcome messages to the client and starts the session
timeout timer."
  ;; Send greeting sequence to the client.
  (rudel-send this
	      "obby_welcome"
	      (number-to-string rudel-obby-protocol-version))

  ;; Switch to encryption negotiation state.
  'encryption-negotiate)


;;; Class rudel-obby-server-state-encryption-negotiate
;;

(defclass rudel-obby-server-state-encryption-negotiate
  (rudel-obby-server-connection-state)
  ()
  "Encryption negotiation state.")

(defmethod rudel-enter ((this rudel-obby-server-state-encryption-negotiate))
  "Send net6 'encryption' message requesting to not enable encryption."
  (rudel-send this "net6_encryption" "0")
  nil)

(defmethod rudel-obby/net6_encryption_ok
  ((this rudel-obby-server-state-encryption-negotiate))
  "Handle net6 'encryption_ok' message.
Even if the client requests an encrypted connection, we cancel
the negotiation."
  (rudel-send this "net6_encryption_failed")
  'before-join)

(defmethod rudel-obby/net6_encryption_failed
  ((this rudel-obby-server-state-encryption-negotiate))
  "Handle net6 'encryption_failed' message.
No action has to be taken, since the client simply proceeds after
failed encryption negotiation."
  'before-join)


;;; Class rudel-obby-server-state-before-join
;;

(defclass rudel-obby-server-state-before-join
  (rudel-obby-server-connection-state)
  ()
  "Waiting for client request joining the session.")

(defmethod rudel-obby/net6_client_login
  ((this rudel-obby-server-state-before-join) username color
   &optional global-password user-password)
  "Handle net6 'client_login' message."
  (with-parsed-arguments ((color color))
    (with-slots (server
		 (client-id :id)
		 user
		 encryption) (oref this :connection)
      ;; Make sure USERNAME and COLOR are valid.
      (let ((error (rudel-check-username-and-color
		    server username color)))
	(if error
	    ;; If USERNAME or COLOR are invalid, send the error code
	    ;; to the client and stay in the current state.
	    (progn
	      (rudel-send this
			  "net6_login_failed"
			  (format "%x" error))
	      nil)

	  ;; Create a user object for this client and add it to the
	  ;; server.
	  (setq user (rudel-make-user
		      server
		      username client-id color encryption))
	  (rudel-add-user server user)

	  ;; Broadcast the join event to all clients (including the
	  ;; new one).
	  (with-slots ((name :object-name) color (user-id :user-id)) user
	    (rudel-broadcast this (list 'exclude (oref this :connection))
			     "net6_client_join"
			     (format "%x" client-id)
			     name
			     "0"
			     (format "%x" user-id)
			     (rudel-obby-format-color color)))

	  ;; Get the new client up to date:
	  ;; - transmit user list
	  ;;   - connected users
	  ;;   - disconnected users
	  ;; - transmit document list
	  (with-slots (users clients documents) server
	    ;; Send number of synchronization items: sum of numbers of
	    ;; offline users and documents.
	    (let ((number-of-items (+ (length users) (length documents))))
	      (rudel-send this
			  "obby_sync_init"
			  (format "%x" number-of-items)))

	    ;; Transmit list of connected users.
	    (dolist (client clients)
	      (with-slots ((client-id :id) user) client
		(when user
		  (with-slots ((name    :object-name)
			       color
			       (user-id :user-id)) user
		    (rudel-send this
				"net6_client_join"
				(format "%x" client-id)
				name
				"0"
				(format "%x" user-id)
				(rudel-obby-format-color color))))))

	    ;; Transmit list of disconnected users.
	    (let ((offline-users (remove-if #'rudel-connected users)))
	      (dolist (user offline-users)
		(with-slots ((name :object-name) user-id color) user
		  (rudel-send this
			      "obby_sync_usertable_user"
			      (format "%x" user-id)
			      name
			      (rudel-obby-format-color color)))))

	    ;; Transmit document list
	    (dolist (document documents)
	      (with-slots ((name      :object-name)
			   (doc-id    :id)
			   owner-id
			   suffix
			   subscribed) document
		(apply #'rudel-send
		       this
		       "obby_sync_doclist_document"
		       (format "%x" owner-id)
		       (format "%x" doc-id)
		       name
		       (format "%x" suffix)
		       "UTF-8"
		       (mapcar
			(lambda (user1) ;; TODO we could use `user' here, but there is a bug in cl
			  (format "%x" (rudel-id user1)))
			subscribed)))))

	  (rudel-send this "obby_sync_final")
	  'idle))))
  )


;;; Class rudel-obby-server-state-idle
;;

(defclass rudel-obby-server-state-idle
  (rudel-obby-server-connection-state)
  ()
  "Idle state of a server connection.

The connection enters this state when all setup work is finished,
the client has joined the session and no operation is in
progress. In this state, the connection waits for new messages
from the client that initiate operations. Simple (which means
stateless in this case) operations are performed without leaving
the idle state.")

(defmethod rudel-obby/obby_user_colour
  ((this rudel-obby-server-state-idle) color-)
  "Handle obby 'user_colour' message.
This method is called when the connected user requests a change
of her color to COLOR."
  (with-parsed-arguments ((color- color))
    (with-slots (user) (oref this :connection)
      (with-slots (color (user-id :user-id)) user
	;; Set color slot value.
	(setq color color-)

	;; Run change hook.
	(object-run-hook-with-args user 'change-hook)

	(rudel-broadcast this (list 'exclude (oref this :connection))
			 "obby_user_colour"
			 (format "%x" user-id)
			 (rudel-obby-format-color color)))))
  nil)

(defmethod rudel-obby/obby_document_create
  ((this rudel-obby-server-state-idle)
   doc-id name encoding content)
  "Handle obby 'document_create' message."
  (with-parsed-arguments ((doc-id   number)
			  (encoding coding-system))
    (with-slots (user server) (oref this :connection)
      (with-slots ((user-id :user-id)) user
	;; Create a (hidden) buffer for the new document.
	(let* ((buffer         (get-buffer-create
				(generate-new-buffer-name
				 (concat " *"  name "*"))))
	       ;; Create the new document object
	       (document       (rudel-obby-document
				name
				:buffer     buffer
				:subscribed (list user)
				:id         doc-id
				:owner-id   user-id
				:suffix     1)))

	  ;; Initialize the buffer's content
	  (with-current-buffer buffer
	    (insert content))

	  (with-slots (suffix) document
	    ;; Determine an appropriate suffix to provide an unique
	    ;; name for the new document.
	    (while (rudel-find-document server
					(if (= suffix 1)
					    name
					  (format "%s<%d>" name suffix))
					#'string= #'rudel-unique-name)
	      (incf suffix))

	    ;; Add the document to the server's document list
	    (rudel-add-document server document)

	    ;; Maybe notify the creating client of the changed suffix.
	    (unless (= suffix 1)
	      (rudel-send this
			  "obby_document"
			  (format "%x %x" user-id doc-id)
			  "rename"
			  (format "%x" user-id)
			  name
			  (format "%x" suffix)))

	    ;; Notify other clients of the new document
	    (rudel-broadcast this (list 'exclude  (oref this :connection))
			     "obby_document_create"
			     (format "%x" user-id)
			     (format "%x" doc-id)
			     name
			     (format "%x" suffix)
			     (upcase (symbol-name encoding))))

	  ;; Add a jupiter context for (THIS DOCUMENT).
	  (rudel-add-context server (oref this :connection) document))))
    nil)
  )

(defmethod rudel-obby/obby_document
  ((this rudel-obby-server-state-idle) doc-id action &rest arguments)
  "Handle obby 'document' messages."
  (with-parsed-arguments ((doc-id document-id))
    ;; Locate the document based on owner id and document id
    (let ((document (with-slots (server) (oref this :connection)
		      (rudel-find-document server doc-id
					   #'equal #'rudel-both-ids))))
      (rudel-obby-dispatch this action
			   (append (list document) arguments)
			   "rudel-obby/obby_document/")))
  )

(defmethod rudel-obby/obby_document/subscribe
  ((this rudel-obby-server-state-idle) document user-id)
  "Handle 'subscribe' submessage of obby 'document' message."
  (with-parsed-arguments ((user-id number))
    (let ((user (with-slots (server) (oref this :connection)
		  (rudel-find-user server user-id
				   #'= #'rudel-id))))
      (with-slots (owner-id (doc-id :id) subscribed buffer) document

	;; Track subscription, handle duplicate subscription requests.
	(when (memq user subscribed)
	  (error "User `%s' already subscribed to document `%s'"
		 (object-name user) (object-name document)))
	(rudel-add-user document user)

	;; Synchronize the buffer content to the client.
	(with-current-buffer buffer
	  ;; Send overall buffer size
	  (rudel-send this
		      "obby_document"
		      (format "%x %x" owner-id doc-id)
		      "sync_init"
		      (format "%x" (1- (position-bytes (point-max)))))

	  ;; Send buffer chunks with author ids
	  (dolist (chunk (rudel-chunks document))
	    (multiple-value-bind (from to author) chunk
	      (let ((string (buffer-substring (+ from 1) (+ to 1))))
		(rudel-send this
			    "obby_document"
			    (format "%x %x" owner-id doc-id)
			    "sync_chunk"
			    string
			    (format "%x"
				    (if author
					(oref author :user-id)
				      0)))))))

	;; Notify clients of the new subscription (including our own
	;; client, who requested the subscription).
	(with-slots ((user-id :user-id)) user
	  (rudel-broadcast this nil
			   "obby_document"
			   (format "%x %x" owner-id doc-id)
			   "subscribe"
			   (format "%x" user-id)))))

    ;; Add a jupiter context for (THIS document).
    (with-slots (server) (oref this :connection)
      (rudel-add-context server (oref this :connection) document))
    nil)
  )

(defmethod rudel-obby/obby_document/unsubscribe
  ((this rudel-obby-server-state-idle) document user-id)
  "Handle 'unsubscribe' submessage of 'obby_document' message."
  (with-parsed-arguments ((user-id number))
    (let ((user (with-slots (server) (oref this :connection)
		  (rudel-find-user server user-id
				   #'= #'rudel-id))))
      (with-slots (owner-id (doc-id :id) subscribed) document

	;; Track subscription, handle invalid unsubscribe requests
	(unless (memq user subscribed)
	  (error "User `%s' not subscribed to document `%s'"
		 (object-name user) (object-name document)))
	(rudel-remove-user document user)

	;; Notify clients of the canceled subscription (including our
	;; own client, who requested being unsubscribed).
	(with-slots ((user-id :user-id)) user
	  (rudel-broadcast this nil
			   "obby_document"
			   (format "%x %x" owner-id doc-id)
			   "unsubscribe"
			   (format "%x" user-id))))

      ;; Remove jupiter context for (THIS DOCUMENT).
      (with-slots (server) (oref this :connection)
	(rudel-remove-context server (oref this :connection) document)))
    nil)
  )

(defmethod rudel-obby/obby_document/record
  ((this rudel-obby-server-state-idle)
   document local-revision remote-revision action &rest arguments)
  "Handle 'record' submessages of 'obby_document' message."
  (with-parsed-arguments ((local-revision  number)
			  (remote-revision number))
    ;; Dispatch to specialized operation handlers.
    (rudel-obby-dispatch
     this action
     (append (list document local-revision remote-revision)
	     arguments)
     "rudel-obby/obby_document/record/"))
  )

(defmethod rudel-obby/obby_document/record/ins
  ((this rudel-obby-server-state-idle)
   document local-revision remote-revision position data)
  "Handle 'ins' submessage of 'record' submessages of 'obby_document' message."
  (with-parsed-arguments ((position number))
    ;; Construct the operation object and process it.
    (rudel-remote-operation
     (oref this :connection) document
     remote-revision local-revision
     (jupiter-insert
      (format "insert-%d-%d"
	      remote-revision local-revision)
      :from position
      :data data))
    nil)
  )

(defmethod rudel-obby/obby_document/record/del
  ((this rudel-obby-server-state-idle)
   document local-revision remote-revision position length)
  "Handle 'del' submessage of 'record' submessages of 'obby_document' message."
  (with-parsed-arguments ((position number)
			  (length   number))
    ;; Construct the operation object and process it.
    (rudel-remote-operation
     (oref this :connection) document
     remote-revision local-revision
     (jupiter-delete
      (format "delete-%d-%d"
	      remote-revision local-revision)
      :from position
      :to   (+ position length)))
    nil)
  )


;;; Client connection states.
;;

(defvar rudel-obby-server-connection-states
  '((new                  . rudel-obby-server-state-new)
    (encryption-negotiate . rudel-obby-server-state-encryption-negotiate)
    (before-join          . rudel-obby-server-state-before-join)
    (idle                 . rudel-obby-server-state-idle))
  "Name symbols and classes of connection states.")


;;; Class rudel-obby-client
;;

(defclass rudel-obby-client (rudel-obby-socket-owner
			     rudel-state-machine)
  ((server     :initarg  :server
	       :type     rudel-obby-server
	       :documentation
	       "")
   (id         :initarg  :id
	       :type     integer
	       :accessor rudel-id
	       :documentation
	       "")
   (user       :initarg  :user
	       :type     (or rudel-obby-user null)
	       :initform nil
	       :documentation
	       "")
   (encryption :initarg  :encryption
	       :type     boolean
	       :documentation
	       ""))
  "Each object of this class represents one client, that is
connected to the server. This object handles all direct
communication with the client, while broadcast messages are
handled by the server.")

(defmethod initialize-instance ((this rudel-obby-client) &rest slots)
  "Initialize slots of THIS and register state machine states."
  ;; Initialize slots of THIS
  (when (next-method-p)
    (call-next-method))

  ;; Register states.
  (rudel-register-states this rudel-obby-server-connection-states)
  )

(defmethod rudel-register-state ((this rudel-obby-client) symbol state)
  "Register SYMBOL and STATE and set connection slot of STATE."
  ;; Associate THIS connection to STATE.
  (oset state :connection this)

  ;; Register STATE.
  (call-next-method))

(defmethod rudel-end ((this rudel-obby-client))
  ""
  (rudel-disconnect this))

(defmethod rudel-close ((this rudel-obby-client))
  ""
  (with-slots (server) this
    (rudel-remove-client server this)))

(defmethod rudel-message ((this rudel-obby-client) message)
  "Dispatch MESSAGE to the active state of THIS state machine."
  ;; Dispatch message to state
  (rudel-accept this message))

(defmethod rudel-broadcast ((this rudel-obby-client)
			    receivers name &rest arguments)
  "Broadcast message NAME with arguments ARGUMENTS to RECEIVERS."
  (with-slots (server) this
    (apply #'rudel-broadcast server receivers name arguments)))

(defmethod rudel-remote-operation ((this rudel-obby-client)
				   document
				   local-revision remote-revision
				   operation)
  "Execute and relay OPERATION on DOCUMENT."
  (with-slots (server user) this
    ;; Transform OPERATION and find clients that need to receive
    ;; notifications.
    (let* ((context     (rudel-find-context server this document))
	   (transformed (jupiter-remote-operation
			 context
			 local-revision remote-revision
			 operation))
	   (receivers   (rudel-subscribed-clients-not-self
			 this document)))

      ;; Relay change notification to other clients. We use
      ;; TRANSFORMED before the byte -> char conversion which is what
      ;; the receivers expect.
      (with-slots (user-id) user
	(with-slots (owner-id (doc-id :id)) document
	  ;; Construct and send messages to all receivers individually
	  ;; since the contents of the messages depends on the state
	  ;; of the jupiter context associated the respective
	  ;; receiver.
	  (dolist (receiver receivers)

	    ;; Find the jupiter context for RECEIVER and use its
	    ;; revision information.
	    (let ((context (rudel-find-context server receiver document)))
	      ;; Construct and send one message.
	      (with-slots (local-revision remote-revision) context
		(apply #'rudel-send
		       receiver
		       "obby_document"
		       (format "%x %x" owner-id doc-id)
		       "record"
		       (format "%x" user-id)
		       (format "%x" local-revision)
		       (format "%x" remote-revision)
		       (rudel-operation->message transformed)))

	      ;; Submit the operation to the jupiter context.
	      (jupiter-local-operation context transformed)))))

      ;; Incorporate change into DOCUMENT (the server-side
      ;; document). We have to convert bytes -> chars before we can do
      ;; this.
      (with-slots (buffer) document
	(rudel-obby-byte->char transformed buffer))

      (rudel-remote-operation document user transformed)))
  )

(defmethod rudel-subscribed-clients-not-self ((this rudel-obby-client)
					      document)
  "Return a list of clients subscribed to DOCUMENT excluding THIS."
  (with-slots (clients) (oref this :server)
    (with-slots (subscribed) document
      (remove-if
       (lambda (client)
	 (with-slots (user) client
	   (or (eq client this)
	       (not (memq user subscribed)))))
       clients)))
  )


;;; Class rudel-obby-server
;;

(defclass rudel-obby-server (rudel-server-session
			     rudel-socket-owner)
  ((clients        :initarg  :clients
		   :type     list
		   :initform nil
		   :documentation
		   "")
   (next-client-id :initarg  :next-client-id
		   :type     integer
		   :initform 1
		   :documentation
		   "")
   (next-user-id   :initarg  :next-user-id
		   :type     integer
		   :initform 1
		   :documentation
		   "")
   (contexts       :initarg  :contexts
		   :type     hash-table
		   :documentation
		   ""))
  "Class rudel-obby-server ")

(defmethod initialize-instance ((this rudel-obby-server) &rest slots)
  ""
  (when (next-method-p)
    (call-next-method))

  (with-slots (contexts) this
    (setq contexts (make-hash-table :test 'equal))))

(defmethod rudel-end ((this rudel-obby-server))
  ""
  (rudel-disconnect this))

(defmethod rudel-broadcast ((this rudel-obby-server)
			    receivers name &rest arguments)
  "Send a message of type NAME with arguments ARGUMENTS to RECEIVERS.

RECEIVERS can be a object derived from rudel-obby-client, a list
of such objects or a list with car 'exclude and cdr a list of
such objects derived from rudel-obby-client."
  ;; Construct list of receivers.
  (let ((receiver-list
	 (cond
	  ;; If RECEIVERS is nil, the message should be broadcast to
	  ;; all clients.
	  ((null receivers) (oref this :clients))
	  ;; If RECEIVERS is a (non-empty) list of rudel-obby-client
	  ;; (or derived) objects, treat it as a list of receivers.
	  ((and (listp receivers)
		(rudel-obby-client-child-p (car receivers)))
	   receivers)
	  ;; If RECEIVERS is a (non-empty) list with cdr equal to
	  ;; 'exclude treat it as a list of receivers to exclude.
	  ((and (listp receivers)
		(eq (car receivers) 'exclude))
	   (with-slots (clients) this
	     (set-difference clients (cdr receivers)
			     :key #'rudel-id)))
	  ;; If RECEIVERS is a single rudel-obby-client (or derived)
	  ;; object, send the message to that client.
	  ((rudel-obby-client-child-p receivers)
	   (list receivers))
	  ;;
	  (t (signal 'wrong-type-argument (type-of receivers))))))

    ;; Send message to receivers.
    (dolist (receiver receiver-list)
      (apply #'rudel-send receiver name arguments)))
  )

(defmethod rudel-make-user ((this rudel-obby-server)
			    name client-id color encryption)
  ""
  (with-slots (next-user-id) this
    (let ((user (rudel-obby-user name
                 :color      color
		 :client-id  client-id
		 :user-id    next-user-id
		 :connected  t
		 :encryption encryption)))
      (incf next-user-id)
      user))
  )

(defmethod rudel-check-username-and-color ((this rudel-obby-server)
					   username color)
  "Check whether USERNAME and COLOR are valid.
USERNAME must not be empty and must not be used by another
user. COLOR has to be sufficiently different from used colors."
  (cond
   ;; The empty user name is not allowed
   ((string= username "")
    rudel-obby-error-username-invalid)

   ;; Make sure the user name is not already in use.
   ((rudel-find-user this username
		     #'string= #'object-name-string)
    rudel-obby-error-username-in-use)

   ;; Make sure the color is sufficiently dissimilar to all used
   ;; colors.
   ((rudel-find-user this color
		     (lambda (left right)
		       (< (color-distance left right) 20000)) ;; TODO constant
		     #'rudel-color)
    rudel-obby-error-color-in-use))
  )

(defmethod rudel-add-client ((this rudel-obby-server)
			     client-socket)
  ""
  (with-slots (next-client-id clients) this
    (let ((client (rudel-obby-client (process-name client-socket)
		   :server     this
		   :socket     client-socket
		   :id         next-client-id
		   :encryption nil)))
      (push client clients))
    (incf next-client-id))
  )

(defmethod rudel-remove-client ((this rudel-obby-server)
				client)
  ""
  (with-slots ((client-id :id) user) client
    ;; Broadcast the part event to all remaining clients.
    (rudel-broadcast this (list 'exclude client)
		     "net6_client_part"
		     (format "%x" client-id))

    ;; If the client has an associated user object, set the status of
    ;; the user object to offline.
    (when user
      ;; Set slot value.
      (with-slots (connected) user
	(setq connected nil))

      ;; Run change hook.
      (object-run-hook-with-args user 'change-hook)))

  (object-remove-from-list this :clients client)
  )

(defmethod rudel-find-context ((this rudel-obby-server) client document)
  "Return the jupiter context associated to (CLIENT DOCUMENT) in THIS."
  (with-slots (contexts) this
    (gethash (rudel-obby-context-key client document) contexts)))

(defmethod rudel-add-context ((this rudel-obby-server) client document)
  "Add a jupiter context for (CLIENT DOCUMENT) to THIS."
  (with-slots (contexts) this
    (with-slots ((client-id :id)) client
      (with-slots ((doc-name :object-name)) document
	(puthash
	 (rudel-obby-context-key client document)
	 (jupiter-context (format "%d-%s" client-id doc-name))
	 contexts))))
  )

(defmethod rudel-remove-context ((this rudel-obby-server) client document)
  "Remove the jupiter context associated to (CLIENT DOCUMENT) from THIS."
  (with-slots (contexts) this
    (remhash
     (rudel-obby-context-key client document)
     contexts)))

(defun rudel-obby-context-key (client document)
  "Generate hash key based on CLIENT and DOCUMENT."
  (with-slots ((client-id :id)) client
    (with-slots ((doc-id :id)) document
      (list client-id doc-id))))

(defmethod object-print ((this rudel-obby-server) &rest strings)
  "Print THIS with number of clients."
  (with-slots (clients) this
    (apply #'call-next-method
	   this
	   (format " clients: %d"
		   (length clients))
	   strings))
  )

(provide 'rudel-obby-server)
;;; rudel-obby-server.el ends here