nikhilm / brainstorm

A whiteboard in Java, made for Apps Dreamer at Synapse 2010. ( http://synapse.daiict.ac.in/index.php/events/te... )

Clone this repository (size: 385.5 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/nikhilm/brainstorm/

Changed (Δ11.6 KB):

raw changeset »

doc/brainstorm_design.lyx (649 lines added, 0 lines removed)

doc/brainstorm_design_and_code.lyx

Up to file-list doc/brainstorm_design.lyx:

1
#LyX 1.6.5 created this file. For more info see http://www.lyx.org/
2
\lyxformat 345
3
\begin_document
4
\begin_header
5
\textclass article
6
\use_default_options true
7
\language english
8
\inputencoding auto
9
\font_roman default
10
\font_sans default
11
\font_typewriter default
12
\font_default_family default
13
\font_sc false
14
\font_osf false
15
\font_sf_scale 100
16
\font_tt_scale 100
17
18
\graphics default
19
\paperfontsize 12
20
\spacing single
21
\use_hyperref true
22
\pdf_title "Brainstorm"
23
\pdf_author "Nikhil Marathe"
24
\pdf_bookmarks true
25
\pdf_bookmarksnumbered false
26
\pdf_bookmarksopen false
27
\pdf_bookmarksopenlevel 2
28
\pdf_breaklinks false
29
\pdf_pdfborder true
30
\pdf_colorlinks true
31
\pdf_backref false
32
\pdf_pdfusetitle true
33
\papersize default
34
\use_geometry true
35
\use_amsmath 1
36
\use_esint 1
37
\cite_engine basic
38
\use_bibtopic false
39
\paperorientation portrait
40
\secnumdepth 3
41
\tocdepth 3
42
\paragraph_separation indent
43
\defskip medskip
44
\quotes_language english
45
\papercolumns 1
46
\papersides 1
47
\paperpagestyle default
48
\tracking_changes false
49
\output_changes false
50
\author "" 
51
\end_header
52
53
\begin_body
54
55
\begin_layout Title
56
Brainstorm
57
\end_layout
58
59
\begin_layout Author
60
\begin_inset CommandInset href
61
LatexCommand href
62
name "Nikhil Marathe"
63
target "nsm.nikhil@gmail.com"
64
type "mailto:"
65
66
\end_inset
67
68
, 200801011
69
\begin_inset Newline newline
70
\end_inset
71
72
DA-IICT
73
\end_layout
74
75
\begin_layout Part
76
Brainstorm Architecture
77
\end_layout
78
79
\begin_layout Section
80
Introduction
81
\end_layout
82
83
\begin_layout Standard
84
Brainstorm is a collaborative drawing application.
85
 It uses a client-server model, and a custom protocol.
86
 All communication is over TCP port 
87
\emph on
88
6491
89
\emph default
90
.
91
\end_layout
92
93
\begin_layout Section
94
Protocol
95
\end_layout
96
97
\begin_layout Standard
98
The Brainstorm protocol is a custom protocol over the Bencode
99
\begin_inset Foot
100
status open
101
102
\begin_layout Plain Layout
103
\begin_inset CommandInset href
104
LatexCommand href
105
target "http://wiki.theory.org/BitTorrentSpecification#bencoding"
106
107
\end_inset
108
109
110
\end_layout
111
112
\end_inset
113
114
 wire protocol used by Bittorrent.
115
 It is human readable yet terse, leading to compact messages.
116
 At the same time it remains extensible.
117
\end_layout
118
119
\begin_layout Standard
120
All messages are sent as 
121
\emph on
122
length prefixed
123
\emph default
124
 bencode 
125
\emph on
126
dictionaries 
127
\emph default
128
parsed by the 
129
\emph on
130
brainstorm.MessageEmitter
131
\emph default
132
 class.
133
 
134
\end_layout
135
136
\begin_layout LyX-Code
137
Message format = <message len>:d<pairs>e
138
\end_layout
139
140
\begin_layout Standard
141
Encoding and decoding is done by the classes in 
142
\emph on
143
package bencode 
144
\emph default
145
and is not covered here.
146
 All
147
\emph on
148
 
149
\emph default
150
messages contain the following:
151
\end_layout
152
153
\begin_layout LyX-Code
154
type : CHAT | SHAPE | PROTOCOL (integers)
155
\end_layout
156
157
\begin_layout LyX-Code
158
nick : nickname of sender
159
\end_layout
160
161
\begin_layout LyX-Code
162
origin : IP address of sender
163
\end_layout
164
165
\begin_layout LyX-Code
166
originport : ephemeral TCP port used by sending Client
167
\end_layout
168
169
\begin_layout Standard
170
In addition server out bound messages have an integer 
171
\emph on
172
servertime
173
\emph default
174
.
175
 servertime is set to 0 when the server first starts and is incremented
176
 periodically and broadcast with every out going message.
177
 Right now it is not used, but it can be used to implement a timeline feature
178
 and undo, by Clients queuing all messages from the beginning of the session.
179
\end_layout
180
181
\begin_layout Subsection
182
Chat messages
183
\end_layout
184
185
\begin_layout Standard
186
Messages of type 
187
\emph on
188
chat 
189
\emph default
190
contain
191
\end_layout
192
193
\begin_layout LyX-Code
194
message : string containing actual message
195
\end_layout
196
197
\begin_layout Subsection
198
Protocol
199
\end_layout
200
201
\begin_layout Standard
202
Protocol messages are used to handle network related actions like clients
203
 joining and leaving.
204
 Protocol messages contain
205
\end_layout
206
207
\begin_layout LyX-Code
208
message :  HELLO - sent by a new client to server
209
\end_layout
210
211
\begin_layout LyX-Code
212
         | JOINED - sent by server to clients to notify new arrival
213
\end_layout
214
215
\begin_layout LyX-Code
216
         | LEFT - sent by server to clients to notify leave
217
\end_layout
218
219
\begin_layout LyX-Code
220
         | BYE - sent by a client when it leaves nicely
221
\end_layout
222
223
\begin_layout LyX-Code
224
         | HELLO_ACK - sent to HELLO client by server
225
\end_layout
226
227
\begin_layout LyX-Code
228
         | SERVER_BYE - server shutdown
229
\end_layout
230
231
\begin_layout Standard
232
These constants are specified in 
233
\emph on
234
brainstorm.Message
235
\emph default
236
.
237
 Below are enumerated the types which have further data.
238
\end_layout
239
240
\begin_layout Subsubsection
241
JOINED and LEFT
242
\end_layout
243
244
\begin_layout LyX-Code
245
nick : nickname of client which joined, forwarded by server
246
\end_layout
247
248
\begin_layout Subsubsection
249
HELLO_ACK
250
\end_layout
251
252
\begin_layout LyX-Code
253
width : image width in pixels, integer
254
\end_layout
255
256
\begin_layout LyX-Code
257
height : image height in pixels, height
258
\end_layout
259
260
\begin_layout Subsection
261
Shape
262
\end_layout
263
264
\begin_layout Standard
265
Shapes are the primary objective of Brainstorm and have a type class of
266
 their own.
267
 All shapes have
268
\end_layout
269
270
\begin_layout LyX-Code
271
shape : a string specifying the shape with values:
272
\end_layout
273
274
\begin_layout LyX-Code
275
        
276
\begin_inset Quotes eld
277
\end_inset
278
279
line
280
\begin_inset Quotes erd
281
\end_inset
282
283
284
\end_layout
285
286
\begin_layout LyX-Code
287
        
288
\begin_inset Quotes eld
289
\end_inset
290
291
rectangle
292
\begin_inset Quotes erd
293
\end_inset
294
295
296
\end_layout
297
298
\begin_layout LyX-Code
299
        
300
\begin_inset Quotes eld
301
\end_inset
302
303
polygon
304
\begin_inset Quotes erd
305
\end_inset
306
307
308
\end_layout
309
310
\begin_layout LyX-Code
311
        
312
\begin_inset Quotes eld
313
\end_inset
314
315
ellipse
316
\begin_inset Quotes erd
317
\end_inset
318
319
320
\end_layout
321
322
\begin_layout LyX-Code
323
        
324
\begin_inset Quotes eld
325
\end_inset
326
327
text
328
\begin_inset Quotes erd
329
\end_inset
330
331
332
\end_layout
333
334
\begin_layout LyX-Code
335
color : dictionary specifying RGB colour of Shape
336
\end_layout
337
338
\begin_layout LyX-Code
339
        r : integer 0-255
340
\end_layout
341
342
\begin_layout LyX-Code
343
        g : integer 0-255
344
\end_layout
345
346
\begin_layout LyX-Code
347
        b : integer 0-255
348
\end_layout
349
350
\begin_layout Standard
351
A colour has to be broadcast with each Message since different clients can
352
 have have different active colours in their session.
353
\end_layout
354
355
\begin_layout Subsubsection
356
line
357
\end_layout
358
359
\begin_layout LyX-Code
360
Coordinates of line
361
\end_layout
362
363
\begin_layout LyX-Code
364
x1 : integer
365
\end_layout
366
367
\begin_layout LyX-Code
368
y1 : integer
369
\end_layout
370
371
\begin_layout LyX-Code
372
x2 : integer
373
\end_layout
374
375
\begin_layout LyX-Code
376
y2 : integer
377
\end_layout
378
379
\begin_layout Subsubsection
380
rectangle and ellipse
381
\end_layout
382
383
\begin_layout LyX-Code
384
Top left corner
385
\end_layout
386
387
\begin_layout LyX-Code
388
x : integer
389
\end_layout
390
391
\begin_layout LyX-Code
392
y : integer
393
\end_layout
394
395
\begin_layout LyX-Code
396
width : integer
397
\end_layout
398
399
\begin_layout LyX-Code
400
height : integer
401
\end_layout
402
403
\begin_layout Subsubsection
404
polygon
405
\end_layout
406
407
\begin_layout LyX-Code
408
points : list [ x1, y1, x2, y2, ..., xn, yn ]
409
\end_layout
410
411
\begin_layout LyX-Code
412
closed : [optional integer] if present, polygon is closed
413
\end_layout
414
415
\begin_layout Subsubsection
416
text
417
\end_layout
418
419
\begin_layout LyX-Code
420
Top left corner
421
\end_layout
422
423
\begin_layout LyX-Code
424
x : integer
425
\end_layout
426
427
\begin_layout LyX-Code
428
y : integer
429
\end_layout
430
431
\begin_layout LyX-Code
432
text : string containing text
433
\end_layout
434
435
\begin_layout Standard
436
At this point font information is not encoded in text and not presented
437
 to the user, this can be extended.
438
\end_layout
439
440
\begin_layout Section
441
Class Structure
442
\end_layout
443
444
\begin_layout Standard
445
This details the most important classes.
446
 Minor classes are skipped.
447
\end_layout
448
449
\begin_layout Subsection
450
Network handling
451
\end_layout
452
453
\begin_layout Standard
454
The 
455
\emph on
456
Server
457
\emph default
458
 and 
459
\emph on
460
Client
461
\emph default
462
 classes manage core networking dealing with Sockets.
463
 The Server class uses Java's NIO package which allows using a platforms
464
 event based functions like select().
465
 So it uses a single thread to manage all clients.
466
 Clients are maintained in a list, use to broadcast messages.
467
\end_layout
468
469
\begin_layout Standard
470
The Client class maintains a single Socket and so does not need NIO.
471
 It also starts another Thread to run the inner class MessageWriter, which
472
 continuously writes any queued messages to the Server.
473
 In addition all incoming messages are clued by the Client, so that pausing
474
 can be implemented.
475
\end_layout
476
477
\begin_layout Subsection
478
Message interpretation
479
\end_layout
480
481
\begin_layout Standard
482
Message interpretation is performed by the MessageEmitter class.
483
 It takes an InputStream.
484
 It is the job of Client/Server to 
485
\emph on
486
feed
487
\emph default
488
 its MessageEmitter when it receives new information.
489
 A MessageEmitter is 
490
\emph on
491
not meant 
492
\emph default
493
to be used with multiple streams.
494
 So the Server maintains different MessageEmitters for each Client.
495
\end_layout
496
497
\begin_layout Standard
498
A class interested in receiving Message notifications should register with
499
 the MessageEmitter using 
500
\emph on
501
connect()
502
\emph default
503
.
504
 Two interfaces, 
505
\emph on
506
MessageReceiver
507
\emph default
508
 and 
509
\emph on
510
SelectiveMessageReceiver
511
\emph default
512
 implements a Observer pattern.
513
 
514
\emph on
515
notifyMessage( Message m )
516
\emph default
517
 is invoked on the observers by the MessageEmitter.
518
 Selective receivers implement 
519
\emph on
520
interestOps()
521
\emph default
522
 to signify what kind of message ( chat, shape, protocol ) they are interested
523
 in.
524
 This is why the three have values in powers of two.
525
 They can be bitwise ORed and ANDed.
526
\end_layout
527
528
\begin_layout Subsection
529
Chat
530
\end_layout
531
532
\begin_layout Standard
533
Chat is handled by the 
534
\emph on
535
ChatWidget 
536
\emph default
537
class.
538
\end_layout
539
540
\begin_layout Subsection
541
Canvas
542
\end_layout
543
544
\begin_layout Standard
545
The 
546
\emph on
547
Canvas
548
\emph default
549
 class implements a canvas by inheriting from JPanel.
550
 It is interested in Shape events.
551
 Canvas double buffers the graphics as an offscreen Image.
552
 Tools
553
\begin_inset CommandInset ref
554
LatexCommand eqref
555
reference "sub:Tools-and-Tool"
556
557
\end_inset
558
559
 use canvas.graphics() to get the relevant 
560
\emph on
561
Graphics2D
562
\emph default
563
 context.
564
 In addition Canvas maintains a queue for temporary graphics.
565
 Due to Swing's painting mechanism, a temporary graphics object is only
566
 available in the 
567
\emph on
568
paintComponent()
569
\emph default
570
 method, so a queue is maintained using 
571
\emph on
572
canvas.tempDraw(Shape)
573
\emph default
574
 which is periodically emptied on 
575
\emph on
576
repaint()
577
\emph default
578
 calls.
579
\end_layout
580
581
\begin_layout Standard
582
In addition Canvas maintains the current color and tool after receiving
583
 notifications from the main class.
584
\end_layout
585
586
\begin_layout Subsection
587
\begin_inset CommandInset label
588
LatexCommand label
589
name "sub:Tools-and-Tool"
590
591
\end_inset
592
593
Tools and Tool Management
594
\end_layout
595
596
\begin_layout Standard
597
598
\emph on
599
ToolManager
600
\emph default
601
 maintains a dictionary of tools and allows the Brainstorm interface to
602
 be automatically generated.
603
\end_layout
604
605
\begin_layout Standard
606
The 
607
\emph on
608
Tool
609
\emph default
610
 class implements common methods.
611
 
612
\emph on
613
activated() 
614
\emph default
615
and 
616
\emph on
617
deactivated() 
618
\emph default
619
are called when the tool is selected and another tool is selected.
620
 All Tools implement the KeyListener and Mouse(Motion)Listener so that they
621
 can respond to events.
622
 All Tools have a reference to the Canvas.
623
 The 
624
\emph on
625
render() 
626
\emph default
627
function also takes a Message received and renders it on the Canvas.
628
 This way all processing -- input and output -- of a Shape is encapsulated
629
 in a single class.
630
\end_layout
631
632
\begin_layout Subsection
633
Brainstorm
634
\end_layout
635
636
\begin_layout Standard
637
The main class.
638
 This handles the interface.
639
 Three inner classes handle pausing the interaction, saving the canvas to
640
 a image file and choosing the colour.
641
 Other toolbar actions are dynamically generated using the ToolManager.
642
\end_layout
643
644
\begin_layout LyX-Code
645
646
\end_layout
647
648
\end_body
649
\end_document