1 package demo.notification.whiteboard; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import org.omg.CORBA.Any ; 7 import org.omg.CORBA.IntHolder ; 8 import org.omg.CORBA.ORB ; 9 import org.omg.CosEventChannelAdmin.AlreadyConnected; 10 import org.omg.CosEventComm.Disconnected; 11 import org.omg.CosNaming.*; 12 import org.omg.CosNotification.EventHeader; 13 import org.omg.CosNotification.EventType; 14 import org.omg.CosNotification.FixedEventHeader; 15 import org.omg.CosNotification.Property; 16 import org.omg.CosNotification.StructuredEvent; 17 import org.omg.CosNotifyChannelAdmin.ClientType; 18 import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumer; 19 import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumerHelper; 20 import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplier; 21 import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplierHelper; 22 import org.omg.CosNotifyComm.StructuredPushConsumer; 23 import org.omg.CosNotifyComm.StructuredPushConsumerPOA; 24 import org.omg.CosNotifyComm.StructuredPushSupplier; 25 import org.omg.CosNotifyComm.StructuredPushSupplierPOA; 26 import org.omg.CosNotifyFilter.ConstraintExp; 27 import org.omg.CosNotifyFilter.Filter; 28 import org.omg.CosNotifyFilter.FilterFactory; 29 import org.omg.PortableServer.*; 30 31 import org.apache.log.Hierarchy; 32 import org.apache.log.Logger; 33 34 38 39 public class Workgroup 40 extends IWorkgroupPOA implements WorkgroupController, WhiteboardVars { 41 42 Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor(getClass().getName()); 43 44 protected ORB orb_; 45 protected POA poa_; 46 47 protected WorkgroupFrame workgroupFrame_; 49 protected BrushSizePixelImage image_; 50 51 protected int myId; 53 protected IWhiteBoard whiteboard_; 54 protected IFactory factory_; 55 56 TotalImageHandler totalImageHandler_; 57 TotalImageHandler getTotalImageHandler() { 58 if (totalImageHandler_ == null) { 59 synchronized(this) { 60 if (totalImageHandler_ == null) { 61 totalImageHandler_ = new TotalImageHandler(this); 62 } 63 } 64 } 65 return totalImageHandler_; 66 } 67 68 LineHandler lineHandler_; 69 LineHandler getLineHandler() { 70 if (lineHandler_ == null) { 71 synchronized(this) { 72 if (lineHandler_ == null) { 73 lineHandler_ = new LineHandler(this); 74 } 75 } 76 } 77 return lineHandler_; 78 } 79 80 ClearHandler clearHandler_; 81 ClearHandler getClearHandler() { 82 if (clearHandler_ == null) { 83 synchronized(this) { 84 if (clearHandler_ == null) { 85 clearHandler_ = new ClearHandler(this); 86 } 87 } 88 } 89 return clearHandler_; 90 } 91 92 ImageHandler imageHandler_; 93 ImageHandler getImageHandler() { 94 if (imageHandler_ == null) { 95 synchronized(this) { 96 if (imageHandler_ == null) { 97 imageHandler_ = new ImageHandler(this); 98 } 99 } 100 } 101 return imageHandler_; 102 } 103 104 public Workgroup(ORB orb, POA poa, IFactory factory) { 105 orb_ = orb; 106 poa_ = poa; 107 _this(orb); 108 factory_ = factory; 109 image_ = new BrushSizePixelImage(400, 400); 110 workgroupFrame_ = new WorkgroupFrame(this, "Whiteboard"); 111 clearAll(); 112 } 113 114 public String [] getListOfWhiteboards() { 115 return factory_.listAllWhiteboards(); 116 } 117 118 public void selectWhiteboard(String name) { 119 logger_.info("join " + name); 120 121 whiteboard_ = factory_.getCreateWhiteboard(name); 123 124 IRegistrationInfo _info = 126 whiteboard_.join(_this(orb_)); 127 128 myId = _info.workgroup_identifier; 129 130 IntHolder imageListenerId_ = new IntHolder (); 131 132 try { 133 StructuredProxyPushConsumer _consumer = 134 StructuredProxyPushConsumerHelper.narrow(_info.supplier_admin.obtain_notification_push_consumer(ClientType.STRUCTURED_EVENT,imageListenerId_)); 135 136 imageHandler_ = getImageHandler(); 137 imageHandler_.connect(_consumer); 138 139 144 lineHandler_ = getLineHandler(); 145 146 lineHandler_.connect(StructuredProxyPushSupplierHelper.narrow(_info.consumer_admin.obtain_notification_push_supplier(ClientType.STRUCTURED_EVENT, imageListenerId_)), _info.filter_factory); 147 148 clearHandler_ = getClearHandler(); 149 clearHandler_.connect(StructuredProxyPushSupplierHelper.narrow(_info.consumer_admin.obtain_notification_push_supplier(ClientType.STRUCTURED_EVENT, imageListenerId_)), _info.filter_factory); 150 151 workgroupFrame_.setCurrentBoardText(name); 152 workgroupFrame_.setLeaveMenuItem(true); 153 154 logger_.debug("imagelistener connected successful"); 155 } catch (Exception e) { 156 e.printStackTrace(); 157 } 158 workgroupFrame_.repaint(); 159 } 160 161 public void leaveWhiteboard() { 165 imageHandler_.shutdown(); 166 lineHandler_.shutdown(); 167 clearHandler_.shutdown(); 168 169 imageHandler_ = null; 170 lineHandler_ = null; 171 clearHandler_ = null; 172 173 whiteboard_.leave(myId); 174 } 175 176 public void exit() { 177 try { 178 leaveWhiteboard(); 179 } finally { 180 System.exit(0); 181 } 182 } 183 184 public int getWorkgroupId() { 185 return myId; 186 } 187 188 public void drawLine(int x0,int y0,int x1,int y1,int red,int green,int blue) { 189 image_.drawLine(x0, y0, x1, y1, red, green, blue); 190 if (imageHandler_ != null) { 191 imageHandler_.drawLine(x0, y0, x1, y1, red, green, blue, image_.getBrushSize()); 192 } 193 } 194 195 public void drawLineLocal(int x0, int y0, int x1, int y1, int red, int green, int blue, int brushsize) { 196 int _savedBrushsize = image_.getBrushSize(); 197 image_.setBrushSize(brushsize); 198 image_.drawLine(x0, y0, x1, y1, red, green, blue); 199 image_.setBrushSize(_savedBrushsize); 200 workgroupFrame_.getDrawCanvas().repaint(); 201 } 202 203 public PixelImage getImage() { 204 return image_; 205 } 206 207 public void clearAll() { 208 if (imageHandler_ != null) { 209 imageHandler_.clear(); 210 } 211 clearAllLocal(); 212 } 213 214 public void clearAllLocal() { 215 image_.clearAll(); 216 workgroupFrame_.getDrawCanvas().repaint(); 217 } 218 219 public void setBrushSize(int i) { 220 image_.setBrushSize(i); 221 } 222 223 public ORB getOrb() { 224 return orb_; 225 } 226 227 public void updateWholeImage(int[] data) { 228 image_.setPixelBuffer(data); 229 workgroupFrame_.getDrawCanvas().repaint(); 230 231 try { 232 totalImageHandler_.disconnect(); 233 } catch (Exception e) { 234 e.printStackTrace(); 235 totalImageHandler_ = null; 236 } 237 } 238 239 public static void main (String [] args) { 240 ORB _orb = ORB.init(args, null); 241 try { 242 POA _poa = POAHelper.narrow(_orb.resolve_initial_references("RootPOA")); 244 245 NamingContext nc = 246 NamingContextHelper.narrow(_orb.resolve_initial_references("NameService")); 247 248 NameComponent [] name = new NameComponent[1]; 249 name[0] = new NameComponent( "WhiteBoard", "Factory"); 250 251 IFactory _factory = IFactoryHelper.narrow(nc.resolve(name)); 252 253 Workgroup wg = new Workgroup(_orb, _poa, _factory); 255 256 _poa.the_POAManager().activate(); 257 258 wg.workgroupFrame_.show(); 259 } catch (Exception e) { 260 e.printStackTrace(); 261 System.exit(1); 262 } 263 } 264 265 } 267 class TotalImageHandler extends StructuredPushConsumerPOA implements WhiteboardVars { 268 269 boolean connected_ = false; 270 StructuredProxyPushSupplier mySupplier_; 271 StructuredPushConsumer thisRef_; 272 WorkgroupController control_; 273 274 Logger logger_ = 275 Hierarchy.getDefaultHierarchy().getLoggerFor("fetch_total_image"); 276 277 Filter filter_; 278 int filterId_; 279 280 TotalImageHandler(WorkgroupController control) { 281 control_ = control; 282 thisRef_ = _this(control.getOrb()); 283 } 284 285 void connect(StructuredProxyPushSupplier supplier, 286 FilterFactory filterFactory) throws Exception { 287 288 filter_ = filterFactory.create_filter("EXTENDED_TCL"); 289 ConstraintExp[] _filter = new ConstraintExp[1]; 290 _filter[0] = new ConstraintExp(); 291 _filter[0].constraint_expr = "$type_name == 'IMAGE'"; 292 _filter[0].event_types = new EventType[1]; 293 _filter[0].event_types[0] = new EventType("*", "*"); 294 filter_.add_constraints(_filter); 295 296 mySupplier_ = supplier; 297 filterId_ = mySupplier_.add_filter(filter_); 298 mySupplier_.connect_structured_push_consumer(thisRef_); 299 connected_ = true; 300 301 } 302 303 void disconnect() throws Exception { 304 mySupplier_.remove_filter(filterId_); 305 mySupplier_.disconnect_structured_push_supplier(); 306 connected_ = false; 307 } 308 309 public void push_structured_event(StructuredEvent event) 310 throws Disconnected { 311 312 if (!connected_) { 313 throw new Disconnected(); 314 } 315 316 WhiteboardUpdate _update = 317 WhiteboardUpdateHelper.extract(event.remainder_of_body); 318 319 control_.updateWholeImage(_update.image()); 320 } 321 322 public void disconnect_structured_push_consumer() { 323 connected_ = false; 324 } 325 326 public void offer_change(EventType[] e1, EventType[] e2) { 327 } 328 } 329 330 class LineHandler extends StructuredPushConsumerPOA implements WhiteboardVars { 331 ORB orb_; 332 StructuredProxyPushSupplier mySupplier_; 333 WorkgroupController control_; 334 StructuredPushConsumer ref_; 335 336 Logger logger_ = 337 Hierarchy.getDefaultHierarchy().getLoggerFor("UpdateHandler"); 338 339 Filter filter_; 340 341 LineHandler(WorkgroupController control) { 342 control_ = control; 343 ref_ = _this(control.getOrb()); 344 } 345 346 void connect(StructuredProxyPushSupplier supplier, 347 FilterFactory filterFactory) throws Exception { 348 349 filter_ = filterFactory.create_filter("EXTENDED_TCL"); 350 ConstraintExp[] _filter = new ConstraintExp[1]; 351 _filter[0] = new ConstraintExp(); 352 _filter[0].constraint_expr = 353 "$type_name == 'LINE'"; 354 355 _filter[0].event_types = new EventType[1]; 356 _filter[0].event_types[0] = new EventType("*", "*"); 357 filter_.add_constraints(_filter); 358 359 mySupplier_ = supplier; 360 mySupplier_.connect_structured_push_consumer(ref_); 361 mySupplier_.add_filter(filter_); 362 } 363 364 void shutdown() { 365 mySupplier_.disconnect_structured_push_supplier(); 366 } 367 368 public void push_structured_event(StructuredEvent event) { 369 WhiteboardUpdate _update = 370 WhiteboardUpdateHelper.extract(event.remainder_of_body); 371 372 LineData _lineData = _update.line(); 373 374 control_.drawLineLocal(_lineData.x0, 375 _lineData.y0, 376 _lineData.x1, 377 _lineData.y1, 378 _lineData.red, 379 _lineData.green, 380 _lineData.blue, 381 _lineData.brushSize); 382 383 } 384 385 public void disconnect_structured_push_consumer() { 386 } 387 388 public void offer_change(EventType[] e1, EventType[] e2) { 389 } 390 } 391 392 class ClearHandler extends StructuredPushConsumerPOA implements WhiteboardVars { 393 ORB orb_; 394 StructuredProxyPushSupplier mySupplier_; 395 WorkgroupController control_; 396 StructuredPushConsumer thisRef_; 397 Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor("ClearHandler"); 398 Filter myFilter_; 399 400 ClearHandler(WorkgroupController control) { 401 control_ = control; 402 orb_ = control.getOrb(); 403 thisRef_ = _this(orb_); 404 } 405 406 void connect(StructuredProxyPushSupplier mySupplier, FilterFactory factory) throws Exception { 407 myFilter_ = factory.create_filter("EXTENDED_TCL"); 408 ConstraintExp[] _filter = new ConstraintExp[1]; 409 _filter[0] = new ConstraintExp(); 410 411 _filter[0].constraint_expr = "$type_name == 'CLEAR'"; 412 _filter[0].event_types = new EventType[1]; 413 _filter[0].event_types[0] = new EventType("*", "*"); 414 myFilter_.add_constraints(_filter); 415 416 mySupplier_ = mySupplier; 417 mySupplier_.connect_structured_push_consumer(thisRef_); 418 mySupplier_.add_filter(myFilter_); 419 } 420 421 void shutdown() { 422 mySupplier_.disconnect_structured_push_supplier(); 423 } 424 425 public void push_structured_event(StructuredEvent event) { 426 WhiteboardUpdate _update = 427 WhiteboardUpdateHelper.extract(event.remainder_of_body); 428 429 boolean _clear = _update.clear(); 430 431 control_.clearAllLocal(); 432 } 433 434 public void disconnect_structured_push_consumer() { 435 } 436 437 public void offer_change(EventType[] e1, EventType[] e2) { 438 } 439 } 440 441 class ImageHandler extends StructuredPushSupplierPOA implements WhiteboardVars, Runnable { 442 443 ORB orb_; 444 boolean connected_ = false; 445 boolean active_ = false; 446 447 StructuredProxyPushConsumer imageListener_; 448 StructuredPushSupplier thisRef_; 449 StructuredEvent event_; 450 Thread thisThread_; 451 452 List queue_ = new ArrayList (); 453 Logger logger_ = Hierarchy.getDefaultHierarchy().getLoggerFor("ImageHandler"); 454 WorkgroupController control_; 455 456 public void run() { 457 while(active_) { 458 WhiteboardUpdate _update = null; 459 synchronized(queue_) { 460 while(queue_.isEmpty()) { 461 try { 462 queue_.wait(); 463 } catch (InterruptedException ie) { 464 if (!active_) { 465 return; 466 } 467 } 468 } 469 _update = (WhiteboardUpdate)queue_.get(0); 470 queue_.remove(0); 471 } 472 473 StructuredEvent _event = getEvent(); 474 if (_update.discriminator() == UpdateType.clear) { 475 _event.header.fixed_header.event_type.type_name = "CLEAR"; 476 } else if (_update.discriminator() == UpdateType.line) { 477 _event.header.fixed_header.event_type.type_name = "LINE"; 478 } 479 480 try { 481 WhiteboardUpdateHelper.insert(_event.remainder_of_body, _update); 482 imageListener_.push_structured_event(_event); 483 } catch (Disconnected d) { 484 d.printStackTrace(); 485 connected_ = false; 486 } 487 } 488 } 489 490 ImageHandler(WorkgroupController control) { 491 orb_ = control.getOrb(); 492 thisRef_ = _this(orb_); 493 thisThread_ = new Thread (this); 494 thisThread_.setPriority(3); 495 control_ = control; 496 } 497 498 void shutdown() { 499 active_ = false; 500 thisThread_.interrupt(); 501 502 if (connected_) { 503 imageListener_.disconnect_structured_push_consumer(); 504 connected_ = false; 505 } 506 } 507 508 StructuredEvent getEvent() { 509 if (event_ == null) { 510 event_ = new StructuredEvent(); 511 event_.header = new EventHeader(); 512 event_.header.fixed_header = new FixedEventHeader(); 513 event_.header.fixed_header.event_type = new EventType(); 514 event_.header.fixed_header.event_type.domain_name = EVENT_DOMAIN; 515 516 event_.header.fixed_header.event_name = ""; 517 event_.header.variable_header = new Property[1]; 518 Any _any = orb_.create_any(); 519 _any.insert_long(control_.getWorkgroupId()); 520 event_.header.variable_header[0] = new Property(WORKGROUP_ID, _any); 521 event_.filterable_data = new Property[0]; 522 event_.remainder_of_body = orb_.create_any(); 523 } 524 return event_; 525 } 526 527 void connect(StructuredProxyPushConsumer myConsumer) throws AlreadyConnected { 528 imageListener_ = myConsumer; 529 myConsumer.connect_structured_push_supplier(thisRef_); 530 active_ = true; 531 thisThread_.start(); 532 } 533 534 public void disconnect_structured_push_supplier() { 535 connected_ = false; 536 } 537 538 public void subscription_change(EventType[] e1, EventType[] e2) { 539 } 540 541 public void clear() { 542 if (!active_) { 543 return; 544 } 545 546 WhiteboardUpdate _update = new WhiteboardUpdate(); 547 _update.clear(true); 548 logger_.debug("clear()"); 549 550 synchronized(queue_) { 551 queue_.add(_update); 552 queue_.notifyAll(); 553 } 554 } 555 556 public void drawLine(int x0, 557 int y0, 558 int x1, 559 int y1, 560 int red, 561 int green, 562 int blue, 563 int brushSize) { 564 565 if (!active_) { 566 return; 567 } 568 569 WhiteboardUpdate _update = new WhiteboardUpdate(); 570 LineData _data = new LineData(); 571 572 _data.x0 = x0; 573 _data.y0 = y0; 574 _data.x1 = x1; 575 _data.y1 = y1; 576 _data.red = red; 577 _data.green = green; 578 _data.blue = blue; 579 _data.brushSize = brushSize; 580 581 _update.line(_data); 582 583 synchronized(queue_) { 584 queue_.add(_update); 585 queue_.notifyAll(); 586 } 587 } 588 } 589 | Popular Tags |