1 package demo.notification.office; 2 3 6 7 import org.omg.CosNotification.*; 8 import org.omg.CosNotifyComm.*; 9 import org.omg.CosNotifyChannelAdmin.*; 10 11 import org.omg.CosNaming.*; 12 import org.omg.CosNaming.NamingContextPackage.*; 13 import org.omg.CORBA.Any ; 14 import org.omg.CORBA.ORB ; 15 import org.omg.PortableServer.*; 16 17 import java.util.Hashtable ; 18 import demo.notification.office.PrinterPackage.*; 19 20 class PrinterImpl 21 extends PrinterPOA 22 implements StructuredPushSupplierOperations 23 { 24 private EventChannel channel; 25 private SupplierAdmin supplierAdmin ; 26 private StructuredProxyPushConsumer pushConsumer; 27 private ORB orb; 28 private POA poa; 29 30 private Hashtable queue; 31 private int jobId; 32 private int printIdx; 33 private int eventId; 34 private boolean offline; 35 private boolean disconnected; 36 private PrintThread printThread; 37 38 static class JobInfo 39 { 40 public int jobId; 41 public String userId; 42 public String text; 43 44 public JobInfo(int jobId, String userId, String text) 45 { 46 this.jobId = jobId; 47 this.userId = userId; 48 this.text = text; 49 } 50 } 51 52 public int getEventId() 53 { 54 return eventId++; 55 } 56 57 58 61 62 class PrintThread extends Thread 63 { 64 public PrintThread() 65 { 66 start(); 67 } 68 69 72 73 public synchronized void tell() 74 { 75 super.notify(); 76 } 77 78 public void run() 79 { 80 while( true ) 81 { 82 while( printIdx >= jobId || offline ) 84 { 85 try 86 { 87 synchronized( this ) 88 { 89 this.wait(); 90 } 91 } 92 catch( InterruptedException ie ) 93 {} 94 } 95 96 JobInfo job = (JobInfo)queue.remove( new Integer ( printIdx )); 98 if( job != null && generateEvents() ) 99 { 100 System.out.println("--Printing Job # " + job.jobId + " --\n" + job.text + "\n--END JOB---"); 101 StructuredEvent printedEvent = new StructuredEvent(); 103 104 EventType type = new EventType("Office", "Printed"); 106 FixedEventHeader fixed = new FixedEventHeader(type, "" + getEventId() ); 107 108 Property variable[] = new Property[0]; 110 111 printedEvent.header = new EventHeader(fixed, variable); 112 113 printedEvent.filterable_data = new Property[3]; 115 116 Any jobAny = orb.create_any(); 117 jobAny.insert_long( job.jobId ); 118 printedEvent.filterable_data[0] = new Property("job_id", jobAny ); 119 120 Any userAny = orb.create_any(); 121 userAny.insert_string( job.userId ); 122 printedEvent.filterable_data[1] = new Property("user_id", userAny ); 123 124 Any urgentAny = orb.create_any(); 125 urgentAny.insert_boolean( false ); 126 printedEvent.filterable_data[2] = new Property( "urgent", urgentAny ); 127 128 printedEvent.remainder_of_body = orb.create_any(); 130 131 try 132 { 133 boolean exist = false; 134 try 135 { 136 exist = ! pushConsumer._non_existent(); 137 } 138 catch( org.omg.CORBA.SystemException e ) 139 { 140 } 142 143 if( exist ) 144 pushConsumer.push_structured_event(printedEvent); 145 else 146 System.err.println("Object " + pushConsumer + " not existent"); 147 } 148 catch( org.omg.CosEventComm.Disconnected d ) 149 { 150 } 152 } 153 printIdx++; 155 try 156 { 157 Thread.sleep(5000); 158 } 159 catch( Exception e ) 160 { 161 } 163 } 164 } 165 } 166 167 168 public PrinterImpl(EventChannel e, ORB orb, POA poa) 169 { 170 this.orb = orb; 172 this.poa = poa; 173 channel = e; 174 } 175 176 public void connect() 177 { 178 StructuredPushSupplierPOATie thisTie = new StructuredPushSupplierPOATie( this ); 179 180 supplierAdmin = channel.default_supplier_admin(); 182 183 ClientType ctype = ClientType.STRUCTURED_EVENT; 184 org.omg.CORBA.IntHolder proxyIdHolder = new org.omg.CORBA.IntHolder (); 185 186 try 187 { 188 pushConsumer = 189 StructuredProxyPushConsumerHelper.narrow( 190 supplierAdmin.obtain_notification_push_consumer(ctype, proxyIdHolder)); 191 } 192 catch (AdminLimitExceeded ex) 193 { 194 System.err.println("Could not get consumer proxy, maximum number of proxies exceeded!"); 195 System.exit(1); 196 } 197 198 199 try 201 { 202 pushConsumer.connect_structured_push_supplier( StructuredPushSupplierHelper.narrow( poa.servant_to_reference( thisTie ))); 203 } 204 catch( Exception e ) 205 { 206 e.printStackTrace(); 207 } 208 queue = new Hashtable (); 210 printThread = new PrintThread(); 211 } 212 213 216 217 public synchronized int print( String text, String uid) 218 throws OffLine 219 { 220 if( offline ) 221 throw new OffLine(); 222 223 queue.put( new Integer (jobId), new JobInfo( jobId, uid, text )); 224 printThread.tell(); 225 return jobId++; 226 } 227 228 231 232 public void cancel(int id, String uid ) 233 throws UnknownJobID, AlreadyPrinted 234 { 235 236 if( id > jobId || id < 0) 237 throw new UnknownJobID(); 238 239 if( id < printIdx ) 240 throw new AlreadyPrinted(); 241 242 JobInfo job = (JobInfo)queue.get( new Integer ( id )); 243 if( job != null ) 244 { 245 if( !job.userId.equals( uid )) 246 throw new org.omg.CORBA.NO_PERMISSION (); 247 248 queue.remove( new Integer ( id )); 249 250 System.out.println("--CANCELLED JOB #" + id + "--"); 251 252 if( generateEvents() ) 253 { 254 StructuredEvent cancelEvent = new StructuredEvent(); 256 257 EventType type = new EventType("Office", "Canceled"); 259 FixedEventHeader fixed = new FixedEventHeader(type, "" + getEventId() ); 260 261 Property variable[] = new Property[0]; 263 cancelEvent.header = new EventHeader(fixed, variable); 264 265 cancelEvent.filterable_data = new Property[3]; 267 268 Any jobAny = orb.create_any(); 269 jobAny.insert_long( job.jobId ); 270 cancelEvent.filterable_data[0] = new Property("job_id ", jobAny ); 271 272 Any userAny = orb.create_any(); 273 userAny.insert_string( job.userId ); 274 cancelEvent.filterable_data[1] = new Property("user_id ", userAny ); 275 276 Any urgentAny = orb.create_any(); 277 urgentAny.insert_boolean( true ); 278 cancelEvent.filterable_data[2] = new Property( "urgent", urgentAny ); 279 280 cancelEvent.remainder_of_body = orb.create_any(); 281 282 try 283 { 284 pushConsumer.push_structured_event( cancelEvent ); 285 } 286 catch( org.omg.CosEventComm.Disconnected d ) 287 { 288 } 290 } 291 } 292 } 293 294 297 298 public void setOffLine(boolean flag) 299 { 300 offline = flag; 301 if( !offline ) 302 printThread.tell(); 303 304 if( generateEvents() ) 305 { 306 StructuredEvent lineEvent = new StructuredEvent(); 308 309 String typeSuffix = ( offline ? "offline" : "online" ); 310 311 EventType type = new EventType("Office", "Printer" + typeSuffix); 313 FixedEventHeader fixed = new FixedEventHeader(type, "" + getEventId() ); 314 315 319 Property variable[] = new Property[0]; 320 lineEvent.header = new EventHeader(fixed, variable); 321 322 lineEvent.filterable_data = new Property[1]; 324 325 Any urgentAny = orb.create_any(); 326 urgentAny.insert_boolean( false ); 327 lineEvent.filterable_data[0] = new Property( "urgent", urgentAny ); 328 329 lineEvent.remainder_of_body = orb.create_any(); 330 try 331 { 332 pushConsumer.push_structured_event( lineEvent ); 333 } 334 catch( org.omg.CosEventComm.Disconnected d ) 335 { 336 } 338 } 339 } 340 341 342 boolean generateEvents() 343 { 344 return !disconnected; 345 } 346 347 351 352 public void disconnect_structured_push_supplier() 353 { 354 disconnected = true; 355 System.out.println("Disconnected!"); 356 } 357 358 361 362 public void subscription_change(EventType added[], EventType removed[]) 363 { 364 } 366 367 368 369 } 370 | Popular Tags |