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
|
;;; rudel-obby-util.el --- Miscellaneous functions for the Rudel obby backend
;;
;; Copyright (C) 2008, 2009 Jan Moringen
;;
;; Author: Jan Moringen <scymtym@users.sourceforge.net>
;; Keywords: Rudel, obby, backend, miscellaneous
;; 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:
;;
;;
;;; History:
;;
;; 0.1 - Initial revision.
;;; Code:
;;
(eval-when-compile
(require 'cl))
(require 'eieio)
(require 'rudel)
(require 'rudel-util)
(require 'jupiter)
;;; Class rudel-obby-socket-owner
;;
(defclass rudel-obby-socket-owner (rudel-socket-owner)
((buffer :initarg :buffer
:type (or null string)
:initform nil
:documentation
"Stores message fragments until complete messages can
be assembled."))
"This class adds functions for sending and receiving obby
messages to the base class rudel-socket-owner.")
(defmethod rudel-send ((this rudel-obby-socket-owner)
name &rest arguments)
"Send obby message NAME with arguments ARGUMENTS through the socket associated to THIS."
(with-slots (socket) this
(rudel-obby-send socket name arguments)))
(defmethod rudel-receive ((this rudel-obby-socket-owner) data)
"Reassemble lines in DATA received on the socket associated with THIS and call message handler."
;; Assemble fragmented lines.
(with-slots (buffer) this
(rudel-assemble-line-fragments data buffer))
;; Process all available lines.
(rudel-loop-lines data line
;; `rudel-message' has to dispatch message to an appropriate
;; handler.
(let ((message (rudel-obby-parse-message line)))
(rudel-message this message)))
)
(defgeneric rudel-message ((this rudel-obby-socket-owner) message)
"Called when a message arrives.
Should be implemented in derived classes.")
;;; Message serialization
;;
(defgeneric rudel-operation->message ((this jupiter-operation))
"Generate a list obby message components from THIS operation.")
(defmethod rudel-operation->message ((this jupiter-insert))
"Serialize THIS insert operation."
(with-slots (from data) this
(list "ins" (format "%x" from) data)))
(defmethod rudel-operation->message ((this jupiter-delete))
"Serialize THIS delete operation."
(with-slots (from length) this
(list "del" (format "%x" from) (format "%x" length))))
(defmethod rudel-operation->message ((this jupiter-compound))
"Serialize THIS compound operation."
(with-slots (children) this
(apply #'append
(list "split" )
(mapcar #'rudel-operation->message children))))
(defmethod rudel-operation->message ((this jupiter-nop))
"Serialize THIS nop operation."
(list "nop"))
(defun rudel-message->operation (message local-revision remote-revision)
"Construct an operation object from MESSAGE and LOCAL-REVISION and REMOTE-REVISION.
LOCAL-REVISION and REMOTE-REVISION are only used in the
construction of the name of the new operation. "
(let ((type (car message)))
(cond
;; Insert operation
((string= type "ins")
(let ((position-numeric (string-to-number (nth 1 message) 16))
(data (nth 2 message)))
(jupiter-insert
(format "insert-%d-%d"
remote-revision local-revision)
:from position-numeric
:data data)))
;; Delete operation
((string= type "del")
(let ((position-numeric (string-to-number (nth 1 message) 16))
(length-numeric (string-to-number (nth 2 message) 16)))
(jupiter-delete
(format "delete-%d-%d"
remote-revision local-revision)
:from position-numeric
:to (+ position-numeric length-numeric))))
;; Compound operation
((string= type "split")
(let* ((rest (cdr message))
(offset (position-if
(lambda (item)
(member* item '("ins" "del" "nop")
:test #'string=))
rest
:start 1))
(first (subseq rest 0 offset))
(second (subseq rest offset)))
(jupiter-compound
(format "compound-%d-%d"
remote-revision local-revision)
:children
(list (rudel-message->operation
first local-revision remote-revision)
(rudel-message->operation
second local-revision remote-revision)))))
;; No operation
((string= type "nop")
(jupiter-nop
(format "nop-%d-%d"
remote-revision local-revision)))
;; Unknown operation type
(t (error "Unknown document record type: `%s'" type))))
)
;;; Character <-> byte position conversion
;;
(defgeneric rudel-obby-char->byte ((this jupiter-operation) buffer)
"Convert character positions and lengths in THIS to bytes.")
(defmethod rudel-obby-char->byte ((this jupiter-insert) buffer)
"Convert character positions and lengths in THIS insert to bytes."
(with-slots (from) this
(with-current-buffer buffer
(setq from (- (position-bytes (+ from 1)) 1)))))
(defmethod rudel-obby-char->byte ((this jupiter-delete) buffer)
"Convert character positions and lengths in THIS delete to bytes."
(with-slots (from to length) this
(let ((old-from (+ from 1))
(old-to (+ to 1)))
(with-current-buffer buffer
(destructuring-bind (change-from change-to string)
rudel-buffer-change-workaround-data
(setq from (- (position-bytes old-from) 1)
length (string-bytes
(substring string
(- old-from change-from)
(- old-to change-from))))))))
)
(defmethod rudel-obby-char->byte ((this jupiter-compound) buffer)
"Convert character positions and lengths in THIS compound to bytes.."
(with-slots (children) this
(mapc
(lambda (child)
(rudel-obby-char->byte child buffer))
children))
)
(defmethod rudel-obby-char->byte ((this jupiter-nop) buffer)
"Nothing to convert if THIS is a nop.")
(defgeneric rudel-obby-byte->char ((this jupiter-operation) buffer)
"Convert byte positions and lengths in THIS to character positions.")
(defmethod rudel-obby-byte->char ((this jupiter-insert) buffer)
"Convert byte positions and lengths in THIS insert to character positions."
(with-slots (from) this
(with-current-buffer buffer
(setq from (- (byte-to-position (+ from 1)) 1)))))
(defmethod rudel-obby-byte->char ((this jupiter-delete) buffer)
"Convert byte positions and lengths in THIS delete to character positions."
(with-slots (from to length) this
(let ((old-from from)
(old-length length))
(with-current-buffer buffer
(setq from (- (byte-to-position (+ old-from 1)) 1)
to (- (byte-to-position (+ old-from old-length 1)) 1)))))
)
(defmethod rudel-obby-byte->char ((this jupiter-compound) buffer)
"Convert byte positions and lengths in THIS compound to character positions."
(with-slots (children) this
(mapc
(lambda (child)
(rudel-obby-byte->char child buffer))
children))
)
(defmethod rudel-obby-byte->char ((this jupiter-nop) buffer)
"Nothing to convert if THIS is a nop.")
;;; Miscellaneous functions
;;
(defun rudel-obby-dispatch (object name arguments &optional prefix)
"Call method starting with PREFIX and ending in NAME of OBJECT with ARGUMENTS.
When PREFIX is not specified, \"rudel-obby/\" is used."
;; Default prefix.
(unless prefix
(setq prefix "rudel-obby/"))
;; Construct a matching symbol.
(let ((method (intern-soft (concat prefix name))))
;; If we found a suitable method, run it; Otherwise warn and do
;; nothing.
(unless (and method
(condition-case error
;; Try to call METHOD. If successful, always
;; return t.
(progn
(apply method object arguments)
t)
;; Warn only when the condition is
;; 'no-method-definition' and refers to METHOD,
;; otherwise continue unwinding.
(no-method-definition
(if (eq method (cadr error))
nil
(signal (car error) (cdr error))))))
(display-warning
'(rudel obby)
(format "%s: in context `%s': no method: `%s'; arguments: %s"
(object-name-string object) prefix name arguments)
:debug)))
)
(defmacro with-parsed-arguments (specs &rest forms)
"Execute FORMS with variable bindings according to SPECS.
SPECS is structured as follows:
SPECS ::= (BINDING*)
BINDING ::= (VAR TYPE)
VAR is a symbol and TYPE is one of number, color, document-id and
coding-system."
(declare (indent 1)
(debug (listp &rest form)))
(let ((bindings
(mapcar
(lambda (spec)
(destructuring-bind (var type) spec
(list var
(case type
;; Number
(number
`(string-to-number ,var 16))
;; Color
(color
`(rudel-obby-parse-color ,var))
;; Document Id
(document-id
`(mapcar
(lambda (string)
(string-to-number string 16))
(split-string ,var " " t)))
;; Coding System
(coding-system
`(rudel-get-coding-system (downcase ,var)))))))
specs)))
`(let (,@bindings)
,@forms))
)
(provide 'rudel-obby-util)
;;; rudel-obby-util.el ends here
|