/
Draw.java
579 lines (479 loc) · 17.7 KB
/
Draw.java
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
package org.jgroups.demos;
import org.jgroups.*;
import org.jgroups.jmx.JmxConfigurator;
import org.jgroups.util.Util;
import javax.management.MBeanServer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.io.*;
/**
* Shared whiteboard, each new instance joins the same group. Each instance chooses a random color,
* mouse moves are broadcast to all group members, which then apply them to their canvas<p>
* @author Bela Ban, Oct 17 2001
*/
public class Draw extends ReceiverAdapter implements ActionListener, ChannelListener {
String groupname="draw-cluster";
private Channel channel=null;
private int member_size=1;
private JFrame mainFrame=null;
private JPanel sub_panel=null;
private DrawPanel panel=null;
private JButton clear_button, leave_button;
private final Random random=new Random(System.currentTimeMillis());
private final Font default_font=new Font("Helvetica",Font.PLAIN,12);
private final Color draw_color=selectColor();
private static final Color background_color=Color.white;
boolean no_channel=false;
boolean jmx;
private boolean use_state=false;
private long state_timeout=5000;
private boolean use_unicasts=false;
private final List<Address> members=new ArrayList<Address>();
public Draw(String props, boolean no_channel, boolean jmx, boolean use_state, long state_timeout,
boolean use_unicasts, String name) throws Exception {
this.no_channel=no_channel;
this.jmx=jmx;
this.use_state=use_state;
this.state_timeout=state_timeout;
this.use_unicasts=use_unicasts;
if(no_channel)
return;
channel=new JChannel(props);
if(name != null)
channel.setName(name);
channel.setReceiver(this);
channel.addChannelListener(this);
}
public Draw(Channel channel) throws Exception {
this.channel=channel;
channel.setReceiver(this);
channel.addChannelListener(this);
}
public Draw(Channel channel, boolean use_state, long state_timeout) throws Exception {
this.channel=channel;
channel.setReceiver(this);
channel.addChannelListener(this);
this.use_state=use_state;
this.state_timeout=state_timeout;
}
public String getGroupName() {
return groupname;
}
public void setGroupName(String groupname) {
if(groupname != null)
this.groupname=groupname;
}
public static void main(String[] args) {
Draw draw=null;
String props=null;
boolean no_channel=false;
boolean jmx=true;
boolean use_state=false;
String group_name=null;
long state_timeout=5000;
boolean use_unicasts=false;
String name=null;
for(int i=0; i < args.length; i++) {
if("-help".equals(args[i])) {
help();
return;
}
if("-props".equals(args[i])) {
props=args[++i];
continue;
}
if("-no_channel".equals(args[i])) {
no_channel=true;
continue;
}
if("-jmx".equals(args[i])) {
jmx=Boolean.parseBoolean(args[++i]);
continue;
}
if("-groupname".equals(args[i])) {
group_name=args[++i];
continue;
}
if("-state".equals(args[i])) {
use_state=true;
continue;
}
if("-timeout".equals(args[i])) {
state_timeout=Long.parseLong(args[++i]);
continue;
}
if("-bind_addr".equals(args[i])) {
System.setProperty("jgroups.bind_addr", args[++i]);
continue;
}
if("-use_unicasts".equals(args[i])) {
use_unicasts=true;
continue;
}
if("-name".equals(args[i])) {
name=args[++i];
continue;
}
help();
return;
}
try {
draw=new Draw(props, no_channel, jmx, use_state, state_timeout, use_unicasts, name);
if(group_name != null)
draw.setGroupName(group_name);
draw.go();
}
catch(Throwable e) {
e.printStackTrace(System.err);
System.exit(0);
}
}
static void help() {
System.out.println("\nDraw [-help] [-no_channel] [-props <protocol stack definition>]" +
" [-groupname <name>] [-state] [-timeout <state timeout>] [-use_unicasts] " +
"[-bind_addr <addr>] [-jmx <true | false>] [-name <logical name>]");
System.out.println("-no_channel: doesn't use JGroups at all, any drawing will be relected on the " +
"whiteboard directly");
System.out.println("-props: argument can be an old-style protocol stack specification, or it can be " +
"a URL. In the latter case, the protocol specification will be read from the URL\n");
}
private Color selectColor() {
int red=Math.abs(random.nextInt()) % 255;
int green=Math.abs(random.nextInt()) % 255;
int blue=Math.abs(random.nextInt()) % 255;
return new Color(red, green, blue);
}
private void sendToAll(byte[] buf) throws Exception {
for(Address mbr: members) {
Message msg=new Message(mbr, null, buf);
channel.send(msg);
}
}
public void go() throws Exception {
if(!no_channel && !use_state) {
channel.connect(groupname);
}
mainFrame=new JFrame();
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel=new DrawPanel(use_state);
panel.setBackground(background_color);
sub_panel=new JPanel();
mainFrame.getContentPane().add("Center", panel);
clear_button=new JButton("Clear");
clear_button.setFont(default_font);
clear_button.addActionListener(this);
leave_button=new JButton("Leave");
leave_button.setFont(default_font);
leave_button.addActionListener(this);
sub_panel.add("South", clear_button);
sub_panel.add("South", leave_button);
mainFrame.getContentPane().add("South", sub_panel);
mainFrame.setBackground(background_color);
clear_button.setForeground(Color.blue);
leave_button.setForeground(Color.blue);
mainFrame.pack();
mainFrame.setLocation(15, 25);
mainFrame.setBounds(new Rectangle(250, 250));
if(!no_channel && use_state) {
channel.connect(groupname, null, state_timeout);
}
mainFrame.setVisible(true);
setTitle();
}
void setTitle(String title) {
String tmp="";
if(no_channel) {
mainFrame.setTitle(" Draw Demo ");
return;
}
if(title != null) {
mainFrame.setTitle(title);
}
else {
if(channel.getAddress() != null)
tmp+=channel.getAddress();
tmp+=" (" + member_size + ")";
mainFrame.setTitle(tmp);
}
}
void setTitle() {
setTitle(null);
}
public void receive(Message msg) {
byte[] buf=msg.getRawBuffer();
if(buf == null) {
System.err.println("[" + channel.getAddress() + "] received null buffer from " + msg.getSrc() +
", headers: " + msg.printHeaders());
return;
}
try {
DrawCommand comm=(DrawCommand)Util.streamableFromByteBuffer(DrawCommand.class, buf, msg.getOffset(), msg.getLength());
switch(comm.mode) {
case DrawCommand.DRAW:
if(panel != null)
panel.drawPoint(comm);
break;
case DrawCommand.CLEAR:
clearPanel();
break;
default:
System.err.println("***** received invalid draw command " + comm.mode);
break;
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public void viewAccepted(View v) {
member_size=v.size();
if(mainFrame != null)
setTitle();
members.clear();
members.addAll(v.getMembers());
if(v instanceof MergeView) {
System.out.println("** " + v);
// This is an example of a simple merge function, which fetches the state from the coordinator
// on a merge and overwrites all of its own state
if(use_state && !members.isEmpty()) {
Address coord=members.get(0);
Address local_addr=channel.getAddress();
if(local_addr != null && !local_addr.equals(coord)) {
try {
System.out.println("fetching state from " + coord);
channel.getState(coord, 5000);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
else
System.out.println("** View=" + v);
}
public void getState(OutputStream ostream) throws Exception {
panel.writeState(ostream);
}
public void setState(InputStream istream) throws Exception {
panel.readState(istream);
}
/* --------------- Callbacks --------------- */
public void clearPanel() {
if(panel != null)
panel.clear();
}
public void sendClearPanelMsg() {
DrawCommand comm=new DrawCommand(DrawCommand.CLEAR);
try {
byte[] buf=Util.streamableToByteBuffer(comm);
if(use_unicasts)
sendToAll(buf);
else
channel.send(new Message(null, null, buf));
}
catch(Exception ex) {
System.err.println(ex);
}
}
public void actionPerformed(ActionEvent e) {
String command=e.getActionCommand();
if("Clear".equals(command)) {
if(no_channel) {
clearPanel();
return;
}
sendClearPanelMsg();
}
else if("Leave".equals(command)) {
stop();
}
else
System.out.println("Unknown action");
}
public void stop() {
if(!no_channel) {
try {
channel.close();
}
catch(Exception ex) {
System.err.println(ex);
}
}
mainFrame.setVisible(false);
mainFrame.dispose();
}
/* ------------------------------ ChannelListener interface -------------------------- */
public void channelConnected(Channel channel) {
if(jmx) {
Util.registerChannel((JChannel)channel, "jgroups");
}
}
public void channelDisconnected(Channel channel) {
if(jmx) {
MBeanServer server=Util.getMBeanServer();
if(server != null) {
try {
JmxConfigurator.unregisterChannel((JChannel)channel,server, groupname);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
public void channelClosed(Channel channel) {
}
/* --------------------------- End of ChannelListener interface ---------------------- */
private class DrawPanel extends JPanel implements MouseMotionListener {
final Dimension preferred_size=new Dimension(235, 170);
Image img=null; // for drawing pixels
Dimension d, imgsize=null;
Graphics gr=null;
final Map<Point,Color> state;
public DrawPanel(boolean use_state) {
if(use_state)
state=new LinkedHashMap<Point,Color>();
else
state=null;
createOffscreenImage(false);
addMouseMotionListener(this);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
if(getWidth() <= 0 || getHeight() <= 0) return;
createOffscreenImage(false);
}
});
}
public void writeState(OutputStream outstream) throws IOException {
if(state == null)
return;
synchronized(state) {
DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(outstream, 4096));
// DataOutputStream dos=new DataOutputStream(outstream);
dos.writeInt(state.size());
for(Map.Entry<Point,Color> entry: state.entrySet()) {
Point point=entry.getKey();
Color col=entry.getValue();
dos.writeInt(point.x);
dos.writeInt(point.y);
dos.writeInt(col.getRGB());
}
dos.flush();
}
}
public void readState(InputStream instream) throws IOException {
DataInputStream in=new DataInputStream(instream);
Map<Point,Color> new_state=new HashMap<Point,Color>();
int num=in.readInt();
for(int i=0; i < num; i++) {
Point point=new Point(in.readInt(), in.readInt());
Color col=new Color(in.readInt());
new_state.put(point, col);
}
synchronized(state) {
state.clear();
state.putAll(new_state);
System.out.println("read state: " + state.size() + " entries");
createOffscreenImage(true);
}
}
final void createOffscreenImage(boolean discard_image) {
d=getSize();
if(discard_image) {
img=null;
imgsize=null;
}
if(img == null || imgsize == null || imgsize.width != d.width || imgsize.height != d.height) {
img=createImage(d.width, d.height);
if(img != null) {
gr=img.getGraphics();
if(gr != null && state != null) {
drawState();
}
}
imgsize=d;
}
repaint();
}
/* ---------------------- MouseMotionListener interface------------------------- */
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
int x=e.getX(), y=e.getY();
DrawCommand comm=new DrawCommand(DrawCommand.DRAW, x, y,
draw_color.getRed(), draw_color.getGreen(), draw_color.getBlue());
if(no_channel) {
drawPoint(comm);
return;
}
try {
byte[] buf=Util.streamableToByteBuffer(comm);
if(use_unicasts)
sendToAll(buf);
else
channel.send(new Message(null, null, buf));
}
catch(Exception ex) {
System.err.println(ex);
}
}
/* ------------------- End of MouseMotionListener interface --------------------- */
/**
* Adds pixel to queue and calls repaint() whenever we have MAX_ITEMS pixels in the queue
* or when MAX_TIME msecs have elapsed (whichever comes first). The advantage compared to just calling
* repaint() after adding a pixel to the queue is that repaint() can most often draw multiple points
* at the same time.
*/
public void drawPoint(DrawCommand c) {
if(c == null || gr == null) return;
Color col=new Color(c.r, c.g, c.b);
gr.setColor(col);
gr.fillOval(c.x, c.y, 10, 10);
repaint();
if(state != null) {
synchronized(state) {
state.put(new Point(c.x, c.y), col);
}
}
}
public void clear() {
if(gr == null) return;
gr.clearRect(0, 0, getSize().width, getSize().height);
repaint();
if(state != null) {
synchronized(state) {
state.clear();
}
}
}
/** Draw the entire panel from the state */
public void drawState() {
// clear();
Map.Entry entry;
Point pt;
Color col;
synchronized(state) {
for(Iterator it=state.entrySet().iterator(); it.hasNext();) {
entry=(Map.Entry)it.next();
pt=(Point)entry.getKey();
col=(Color)entry.getValue();
gr.setColor(col);
gr.fillOval(pt.x, pt.y, 10, 10);
}
}
repaint();
}
public Dimension getPreferredSize() {
return preferred_size;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(img != null) {
g.drawImage(img, 0, 0, null);
}
}
}
}