1 package demo.notification.office; 2 3 11 12 import org.omg.CosNotification.*; 13 import org.omg.CosNotifyComm.*; 14 import org.omg.CosNotifyFilter.*; 15 import org.omg.CosNotifyChannelAdmin.*; 16 17 import org.omg.CosNaming.*; 18 import org.omg.CORBA.Any ; 19 20 import demo.notification.office.PrinterPackage.*; 21 22 import org.omg.PortableServer.*; 23 import org.omg.CORBA.NO_IMPLEMENT ; 24 25 public class PrintClient 26 extends StructuredPullConsumerPOA 27 { 28 31 public void disconnect_structured_pull_consumer() 32 { 33 System.out.println("Disconnected!"); 34 } 35 36 public void offer_change( EventType added[], EventType removed[] ) 37 { 38 } 39 40 43 44 static public void main( String argv[] ) 45 { 46 EventChannel channel = null; 47 FilterFactory filterFactory = null; 48 Filter filter = null; 49 ConsumerAdmin consumerAdmin; 50 StructuredProxyPullSupplier proxyPullSupplier = null; 51 StructuredPullConsumer structuredPullConsumer; 52 Printer printer = null; 53 String userid = "MeMyselfAndI"; 54 55 if( argv.length > 0 ) 56 userid = argv[0]; 57 58 60 org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init( argv, null); 61 POA poa = null; 62 63 try 64 { 65 poa = 67 POAHelper.narrow(orb.resolve_initial_references("RootPOA")); 68 69 NamingContextExt nc = 71 NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService")); 72 73 channel = 75 EventChannelHelper.narrow(nc.resolve(nc.to_name("office_event.channel"))); 76 77 printer = PrinterHelper.narrow( nc.resolve(nc.to_name("Printer"))); 78 79 poa.the_POAManager().activate(); 80 81 structuredPullConsumer = 83 (StructuredPullConsumer)new PrintClient()._this(orb); 84 85 consumerAdmin = channel.default_consumer_admin(); 87 88 proxyPullSupplier = 89 StructuredProxyPullSupplierHelper.narrow( 90 consumerAdmin.obtain_notification_pull_supplier( 91 ClientType.STRUCTURED_EVENT, 92 new org.omg.CORBA.IntHolder () ) ); 93 94 proxyPullSupplier.connect_structured_pull_consumer( structuredPullConsumer ); 96 97 filterFactory = channel.default_filter_factory(); 99 if( filterFactory == null ) 100 { 101 System.err.println("No default filter Factory!"); 102 } 103 else 104 { 105 filter = filterFactory.create_filter("EXTENDED_TCL"); 106 EventType [] eventTypes = 107 new EventType[] { new EventType("Office", "Printed"), 108 new EventType("Office", "Canceled") }; 109 110 ConstraintExp constraint = 111 new ConstraintExp ( eventTypes, "TRUE" ); 112 113 filter.add_constraints( new ConstraintExp[]{ constraint } ); 114 proxyPullSupplier.add_filter(filter); 115 } 116 117 Property[] qos = new Property[1]; 118 org.omg.CORBA.Any data = org.omg.CORBA.ORB.init().create_any(); 119 data.insert_short( PriorityOrder.value ); 120 qos[0] = new Property( OrderPolicy.value, data); 121 122 try 123 { 124 consumerAdmin.set_qos(qos); 125 } 126 catch (UnsupportedQoS ex) 127 { 128 System.err.println("Unsupported QoS"); 129 } 130 catch (NO_IMPLEMENT e) { 131 } 133 134 } 135 catch (Exception e) 136 { 137 e.printStackTrace(); 138 System.exit(1); 139 } 140 141 143 for( int i = 0; i < 5; i++ ) 144 { 145 try 146 { 147 System.out.println("Sending job, ID #" + 148 printer.print("A test job", userid)); 149 } 150 catch( OffLine ol ) 151 { 152 System.err.println("Printer found off line when printing job."); 153 } 154 } 155 156 158 try 159 { 160 System.out.println("Sleep..."); 161 Thread.sleep(5000); 162 } 163 catch( Exception e) 164 {} 165 166 168 int job = 4; 169 try 170 { 171 System.out.println("Cancelling job ID #" + job ); 172 printer.cancel( job, userid ); 173 } 174 catch( UnknownJobID ol ) 175 { 176 System.err.println("Unknown job ID #" + job ); 177 } 178 catch( AlreadyPrinted ap) 179 { 180 System.err.println("Could not cancel, job #" + job + " already printed"); 181 } 182 catch( org.omg.CORBA.NO_PERMISSION np) 183 { 184 System.err.println("Could not cancel, job #" + job + ", no permission"); 185 } 186 187 int eventsReceived = 0; 188 189 for( int i = 0; i < 5; i++ ) 190 { 191 org.omg.CORBA.BooleanHolder bh = 192 new org.omg.CORBA.BooleanHolder (); 193 194 try 195 { 196 System.out.println("Looking for structured events...."); 197 StructuredEvent event = 199 proxyPullSupplier.try_pull_structured_event(bh); 200 201 if( bh.value ) 202 { 203 System.out.println("got structured event."); 204 FixedEventHeader fixed_header = event.header.fixed_header; 205 System.out.println("\t" + fixed_header.event_type.domain_name + "." + 206 fixed_header.event_type.type_name + "#" + 207 fixed_header.event_name ); 208 209 Property properties [] = event.filterable_data; 210 System.out.println("\t" + properties[0].name + 211 " : " + properties[0].value.extract_long() ); 212 System.out.println("\t" + properties[1].name + 213 " : " + properties[1].value.extract_string() ); 214 } 215 Thread.currentThread().sleep(2000); 216 } 217 catch( Exception e ) 218 { 219 e.printStackTrace(); 220 } 221 } 222 proxyPullSupplier.disconnect_structured_pull_supplier(); 224 orb.shutdown(true); 225 } 226 } 227 228 229 230 231 232 | Popular Tags |