001: package demo.notification.whiteboard;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.omg.CORBA.Any;
007: import org.omg.CORBA.IntHolder;
008: import org.omg.CORBA.ORB;
009: import org.omg.CosEventChannelAdmin.AlreadyConnected;
010: import org.omg.CosEventComm.Disconnected;
011: import org.omg.CosNaming.*;
012: import org.omg.CosNotification.EventHeader;
013: import org.omg.CosNotification.EventType;
014: import org.omg.CosNotification.FixedEventHeader;
015: import org.omg.CosNotification.Property;
016: import org.omg.CosNotification.StructuredEvent;
017: import org.omg.CosNotifyChannelAdmin.ClientType;
018: import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumer;
019: import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumerHelper;
020: import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplier;
021: import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplierHelper;
022: import org.omg.CosNotifyComm.StructuredPushConsumer;
023: import org.omg.CosNotifyComm.StructuredPushConsumerPOA;
024: import org.omg.CosNotifyComm.StructuredPushSupplier;
025: import org.omg.CosNotifyComm.StructuredPushSupplierPOA;
026: import org.omg.CosNotifyFilter.ConstraintExp;
027: import org.omg.CosNotifyFilter.Filter;
028: import org.omg.CosNotifyFilter.FilterFactory;
029: import org.omg.PortableServer.*;
030:
031: import org.apache.log.Hierarchy;
032: import org.apache.log.Logger;
033:
034: /**
035: * @author Alphonse Bendt
036: * @version $Id: Workgroup.java,v 1.7 2004/04/28 13:52:02 alphonse.bendt Exp $ Workgroup.java,v 1.6 2004/04/28 12:37:27 brose Exp $
037: */
038:
039: public class Workgroup extends IWorkgroupPOA implements
040: WorkgroupController, WhiteboardVars {
041:
042: Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor(
043: getClass().getName());
044:
045: protected ORB orb_;
046: protected POA poa_;
047:
048: // mein Malfenster
049: protected WorkgroupFrame workgroupFrame_;
050: protected BrushSizePixelImage image_;
051:
052: // meine Id
053: protected int myId;
054: protected IWhiteBoard whiteboard_;
055: protected IFactory factory_;
056:
057: TotalImageHandler totalImageHandler_;
058:
059: TotalImageHandler getTotalImageHandler() {
060: if (totalImageHandler_ == null) {
061: synchronized (this ) {
062: if (totalImageHandler_ == null) {
063: totalImageHandler_ = new TotalImageHandler(this );
064: }
065: }
066: }
067: return totalImageHandler_;
068: }
069:
070: LineHandler lineHandler_;
071:
072: LineHandler getLineHandler() {
073: if (lineHandler_ == null) {
074: synchronized (this ) {
075: if (lineHandler_ == null) {
076: lineHandler_ = new LineHandler(this );
077: }
078: }
079: }
080: return lineHandler_;
081: }
082:
083: ClearHandler clearHandler_;
084:
085: ClearHandler getClearHandler() {
086: if (clearHandler_ == null) {
087: synchronized (this ) {
088: if (clearHandler_ == null) {
089: clearHandler_ = new ClearHandler(this );
090: }
091: }
092: }
093: return clearHandler_;
094: }
095:
096: ImageHandler imageHandler_;
097:
098: ImageHandler getImageHandler() {
099: if (imageHandler_ == null) {
100: synchronized (this ) {
101: if (imageHandler_ == null) {
102: imageHandler_ = new ImageHandler(this );
103: }
104: }
105: }
106: return imageHandler_;
107: }
108:
109: public Workgroup(ORB orb, POA poa, IFactory factory) {
110: orb_ = orb;
111: poa_ = poa;
112: _this (orb);
113: factory_ = factory;
114: image_ = new BrushSizePixelImage(400, 400);
115: workgroupFrame_ = new WorkgroupFrame(this , "Whiteboard");
116: clearAll();
117: }
118:
119: public String[] getListOfWhiteboards() {
120: return factory_.listAllWhiteboards();
121: }
122:
123: public void selectWhiteboard(String name) {
124: logger_.info("join " + name);
125:
126: // Referenz auf WhiteBoard holen
127: whiteboard_ = factory_.getCreateWhiteboard(name);
128:
129: // an board anmelden dabei Id merken
130: IRegistrationInfo _info = whiteboard_.join(_this (orb_));
131:
132: myId = _info.workgroup_identifier;
133:
134: IntHolder imageListenerId_ = new IntHolder();
135:
136: try {
137: StructuredProxyPushConsumer _consumer = StructuredProxyPushConsumerHelper
138: .narrow(_info.supplier_admin
139: .obtain_notification_push_consumer(
140: ClientType.STRUCTURED_EVENT,
141: imageListenerId_));
142:
143: imageHandler_ = getImageHandler();
144: imageHandler_.connect(_consumer);
145:
146: // totalImageHandler_ = new FetchTotalImage(this,
147: // orb_,
148: // StructuredProxyPushSupplierHelper.narrow(_info.consumer_admin.obtain_notification_push_supplier(ClientType.STRUCTURED_EVENT, imageListenerId_)));
149: // totalImageHandler_.connect();
150:
151: lineHandler_ = getLineHandler();
152:
153: lineHandler_.connect(StructuredProxyPushSupplierHelper
154: .narrow(_info.consumer_admin
155: .obtain_notification_push_supplier(
156: ClientType.STRUCTURED_EVENT,
157: imageListenerId_)),
158: _info.filter_factory);
159:
160: clearHandler_ = getClearHandler();
161: clearHandler_.connect(StructuredProxyPushSupplierHelper
162: .narrow(_info.consumer_admin
163: .obtain_notification_push_supplier(
164: ClientType.STRUCTURED_EVENT,
165: imageListenerId_)),
166: _info.filter_factory);
167:
168: workgroupFrame_.setCurrentBoardText(name);
169: workgroupFrame_.setLeaveMenuItem(true);
170:
171: logger_.debug("imagelistener connected successful");
172: } catch (Exception e) {
173: e.printStackTrace();
174: }
175: workgroupFrame_.repaint();
176: }
177:
178: // von board abmelden
179: // keine updates mehr versenden
180: // lokale update queue lo"schen
181: public void leaveWhiteboard() {
182: imageHandler_.shutdown();
183: lineHandler_.shutdown();
184: clearHandler_.shutdown();
185:
186: imageHandler_ = null;
187: lineHandler_ = null;
188: clearHandler_ = null;
189:
190: whiteboard_.leave(myId);
191: }
192:
193: public void exit() {
194: try {
195: leaveWhiteboard();
196: } finally {
197: System.exit(0);
198: }
199: }
200:
201: public int getWorkgroupId() {
202: return myId;
203: }
204:
205: public void drawLine(int x0, int y0, int x1, int y1, int red,
206: int green, int blue) {
207: image_.drawLine(x0, y0, x1, y1, red, green, blue);
208: if (imageHandler_ != null) {
209: imageHandler_.drawLine(x0, y0, x1, y1, red, green, blue,
210: image_.getBrushSize());
211: }
212: }
213:
214: public void drawLineLocal(int x0, int y0, int x1, int y1, int red,
215: int green, int blue, int brushsize) {
216: int _savedBrushsize = image_.getBrushSize();
217: image_.setBrushSize(brushsize);
218: image_.drawLine(x0, y0, x1, y1, red, green, blue);
219: image_.setBrushSize(_savedBrushsize);
220: workgroupFrame_.getDrawCanvas().repaint();
221: }
222:
223: public PixelImage getImage() {
224: return image_;
225: }
226:
227: public void clearAll() {
228: if (imageHandler_ != null) {
229: imageHandler_.clear();
230: }
231: clearAllLocal();
232: }
233:
234: public void clearAllLocal() {
235: image_.clearAll();
236: workgroupFrame_.getDrawCanvas().repaint();
237: }
238:
239: public void setBrushSize(int i) {
240: image_.setBrushSize(i);
241: }
242:
243: public ORB getOrb() {
244: return orb_;
245: }
246:
247: public void updateWholeImage(int[] data) {
248: image_.setPixelBuffer(data);
249: workgroupFrame_.getDrawCanvas().repaint();
250:
251: try {
252: totalImageHandler_.disconnect();
253: } catch (Exception e) {
254: e.printStackTrace();
255: totalImageHandler_ = null;
256: }
257: }
258:
259: public static void main(String[] args) {
260: ORB _orb = ORB.init(args, null);
261: try {
262: // CORBA stuff
263: POA _poa = POAHelper.narrow(_orb
264: .resolve_initial_references("RootPOA"));
265:
266: NamingContext nc = NamingContextHelper.narrow(_orb
267: .resolve_initial_references("NameService"));
268:
269: NameComponent[] name = new NameComponent[1];
270: name[0] = new NameComponent("WhiteBoard", "Factory");
271:
272: IFactory _factory = IFactoryHelper.narrow(nc.resolve(name));
273:
274: // OK Workgroup starten
275: Workgroup wg = new Workgroup(_orb, _poa, _factory);
276:
277: _poa.the_POAManager().activate();
278:
279: wg.workgroupFrame_.show();
280: } catch (Exception e) {
281: e.printStackTrace();
282: System.exit(1);
283: }
284: }
285:
286: } // Workgroup
287:
288: class TotalImageHandler extends StructuredPushConsumerPOA implements
289: WhiteboardVars {
290:
291: boolean connected_ = false;
292: StructuredProxyPushSupplier mySupplier_;
293: StructuredPushConsumer this Ref_;
294: WorkgroupController control_;
295:
296: Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor(
297: "fetch_total_image");
298:
299: Filter filter_;
300: int filterId_;
301:
302: TotalImageHandler(WorkgroupController control) {
303: control_ = control;
304: this Ref_ = _this (control.getOrb());
305: }
306:
307: void connect(StructuredProxyPushSupplier supplier,
308: FilterFactory filterFactory) throws Exception {
309:
310: filter_ = filterFactory.create_filter("EXTENDED_TCL");
311: ConstraintExp[] _filter = new ConstraintExp[1];
312: _filter[0] = new ConstraintExp();
313: _filter[0].constraint_expr = "$type_name == 'IMAGE'";
314: _filter[0].event_types = new EventType[1];
315: _filter[0].event_types[0] = new EventType("*", "*");
316: filter_.add_constraints(_filter);
317:
318: mySupplier_ = supplier;
319: filterId_ = mySupplier_.add_filter(filter_);
320: mySupplier_.connect_structured_push_consumer(this Ref_);
321: connected_ = true;
322:
323: }
324:
325: void disconnect() throws Exception {
326: mySupplier_.remove_filter(filterId_);
327: mySupplier_.disconnect_structured_push_supplier();
328: connected_ = false;
329: }
330:
331: public void push_structured_event(StructuredEvent event)
332: throws Disconnected {
333:
334: if (!connected_) {
335: throw new Disconnected();
336: }
337:
338: WhiteboardUpdate _update = WhiteboardUpdateHelper
339: .extract(event.remainder_of_body);
340:
341: control_.updateWholeImage(_update.image());
342: }
343:
344: public void disconnect_structured_push_consumer() {
345: connected_ = false;
346: }
347:
348: public void offer_change(EventType[] e1, EventType[] e2) {
349: }
350: }
351:
352: class LineHandler extends StructuredPushConsumerPOA implements
353: WhiteboardVars {
354: ORB orb_;
355: StructuredProxyPushSupplier mySupplier_;
356: WorkgroupController control_;
357: StructuredPushConsumer ref_;
358:
359: Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor(
360: "UpdateHandler");
361:
362: Filter filter_;
363:
364: LineHandler(WorkgroupController control) {
365: control_ = control;
366: ref_ = _this (control.getOrb());
367: }
368:
369: void connect(StructuredProxyPushSupplier supplier,
370: FilterFactory filterFactory) throws Exception {
371:
372: filter_ = filterFactory.create_filter("EXTENDED_TCL");
373: ConstraintExp[] _filter = new ConstraintExp[1];
374: _filter[0] = new ConstraintExp();
375: _filter[0].constraint_expr = "$type_name == 'LINE'";
376:
377: _filter[0].event_types = new EventType[1];
378: _filter[0].event_types[0] = new EventType("*", "*");
379: filter_.add_constraints(_filter);
380:
381: mySupplier_ = supplier;
382: mySupplier_.connect_structured_push_consumer(ref_);
383: mySupplier_.add_filter(filter_);
384: }
385:
386: void shutdown() {
387: mySupplier_.disconnect_structured_push_supplier();
388: }
389:
390: public void push_structured_event(StructuredEvent event) {
391: WhiteboardUpdate _update = WhiteboardUpdateHelper
392: .extract(event.remainder_of_body);
393:
394: LineData _lineData = _update.line();
395:
396: control_.drawLineLocal(_lineData.x0, _lineData.y0,
397: _lineData.x1, _lineData.y1, _lineData.red,
398: _lineData.green, _lineData.blue, _lineData.brushSize);
399:
400: }
401:
402: public void disconnect_structured_push_consumer() {
403: }
404:
405: public void offer_change(EventType[] e1, EventType[] e2) {
406: }
407: }
408:
409: class ClearHandler extends StructuredPushConsumerPOA implements
410: WhiteboardVars {
411: ORB orb_;
412: StructuredProxyPushSupplier mySupplier_;
413: WorkgroupController control_;
414: StructuredPushConsumer this Ref_;
415: Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor(
416: "ClearHandler");
417: Filter myFilter_;
418:
419: ClearHandler(WorkgroupController control) {
420: control_ = control;
421: orb_ = control.getOrb();
422: this Ref_ = _this (orb_);
423: }
424:
425: void connect(StructuredProxyPushSupplier mySupplier,
426: FilterFactory factory) throws Exception {
427: myFilter_ = factory.create_filter("EXTENDED_TCL");
428: ConstraintExp[] _filter = new ConstraintExp[1];
429: _filter[0] = new ConstraintExp();
430:
431: _filter[0].constraint_expr = "$type_name == 'CLEAR'";
432: _filter[0].event_types = new EventType[1];
433: _filter[0].event_types[0] = new EventType("*", "*");
434: myFilter_.add_constraints(_filter);
435:
436: mySupplier_ = mySupplier;
437: mySupplier_.connect_structured_push_consumer(this Ref_);
438: mySupplier_.add_filter(myFilter_);
439: }
440:
441: void shutdown() {
442: mySupplier_.disconnect_structured_push_supplier();
443: }
444:
445: public void push_structured_event(StructuredEvent event) {
446: WhiteboardUpdate _update = WhiteboardUpdateHelper
447: .extract(event.remainder_of_body);
448:
449: boolean _clear = _update.clear();
450:
451: control_.clearAllLocal();
452: }
453:
454: public void disconnect_structured_push_consumer() {
455: }
456:
457: public void offer_change(EventType[] e1, EventType[] e2) {
458: }
459: }
460:
461: class ImageHandler extends StructuredPushSupplierPOA implements
462: WhiteboardVars, Runnable {
463:
464: ORB orb_;
465: boolean connected_ = false;
466: boolean active_ = false;
467:
468: StructuredProxyPushConsumer imageListener_;
469: StructuredPushSupplier this Ref_;
470: StructuredEvent event_;
471: Thread this Thread_;
472:
473: List queue_ = new ArrayList();
474: Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor(
475: "ImageHandler");
476: WorkgroupController control_;
477:
478: public void run() {
479: while (active_) {
480: WhiteboardUpdate _update = null;
481: synchronized (queue_) {
482: while (queue_.isEmpty()) {
483: try {
484: queue_.wait();
485: } catch (InterruptedException ie) {
486: if (!active_) {
487: return;
488: }
489: }
490: }
491: _update = (WhiteboardUpdate) queue_.get(0);
492: queue_.remove(0);
493: }
494:
495: StructuredEvent _event = getEvent();
496: if (_update.discriminator() == UpdateType.clear) {
497: _event.header.fixed_header.event_type.type_name = "CLEAR";
498: } else if (_update.discriminator() == UpdateType.line) {
499: _event.header.fixed_header.event_type.type_name = "LINE";
500: }
501:
502: try {
503: WhiteboardUpdateHelper.insert(_event.remainder_of_body,
504: _update);
505: imageListener_.push_structured_event(_event);
506: } catch (Disconnected d) {
507: d.printStackTrace();
508: connected_ = false;
509: }
510: }
511: }
512:
513: ImageHandler(WorkgroupController control) {
514: orb_ = control.getOrb();
515: this Ref_ = _this (orb_);
516: this Thread_ = new Thread(this );
517: this Thread_.setPriority(3);
518: control_ = control;
519: }
520:
521: void shutdown() {
522: active_ = false;
523: this Thread_.interrupt();
524:
525: if (connected_) {
526: imageListener_.disconnect_structured_push_consumer();
527: connected_ = false;
528: }
529: }
530:
531: StructuredEvent getEvent() {
532: if (event_ == null) {
533: event_ = new StructuredEvent();
534: event_.header = new EventHeader();
535: event_.header.fixed_header = new FixedEventHeader();
536: event_.header.fixed_header.event_type = new EventType();
537: event_.header.fixed_header.event_type.domain_name = EVENT_DOMAIN;
538:
539: event_.header.fixed_header.event_name = "";
540: event_.header.variable_header = new Property[1];
541: Any _any = orb_.create_any();
542: _any.insert_long(control_.getWorkgroupId());
543: event_.header.variable_header[0] = new Property(
544: WORKGROUP_ID, _any);
545: event_.filterable_data = new Property[0];
546: event_.remainder_of_body = orb_.create_any();
547: }
548: return event_;
549: }
550:
551: void connect(StructuredProxyPushConsumer myConsumer)
552: throws AlreadyConnected {
553: imageListener_ = myConsumer;
554: myConsumer.connect_structured_push_supplier(this Ref_);
555: active_ = true;
556: this Thread_.start();
557: }
558:
559: public void disconnect_structured_push_supplier() {
560: connected_ = false;
561: }
562:
563: public void subscription_change(EventType[] e1, EventType[] e2) {
564: }
565:
566: public void clear() {
567: if (!active_) {
568: return;
569: }
570:
571: WhiteboardUpdate _update = new WhiteboardUpdate();
572: _update.clear(true);
573: logger_.debug("clear()");
574:
575: synchronized (queue_) {
576: queue_.add(_update);
577: queue_.notifyAll();
578: }
579: }
580:
581: public void drawLine(int x0, int y0, int x1, int y1, int red,
582: int green, int blue, int brushSize) {
583:
584: if (!active_) {
585: return;
586: }
587:
588: WhiteboardUpdate _update = new WhiteboardUpdate();
589: LineData _data = new LineData();
590:
591: _data.x0 = x0;
592: _data.y0 = y0;
593: _data.x1 = x1;
594: _data.y1 = y1;
595: _data.red = red;
596: _data.green = green;
597: _data.blue = blue;
598: _data.brushSize = brushSize;
599:
600: _update.line(_data);
601:
602: synchronized (queue_) {
603: queue_.add(_update);
604: queue_.notifyAll();
605: }
606: }
607: }
|