001: package demo.notification.office;
002:
003: /**
004: * A simple notification demo client for the Printer service.
005: * It will send a few commands to the printer and wait for
006: * structured events to see what happens. Serves to show that
007: * the Notification Service is set up correctly and works.
008: *
009: * @author Gerald Brose, 08/01/2003
010: */
011:
012: import org.omg.CosNotification.*;
013: import org.omg.CosNotifyComm.*;
014: import org.omg.CosNotifyFilter.*;
015: import org.omg.CosNotifyChannelAdmin.*;
016:
017: import org.omg.CosNaming.*;
018: import org.omg.CORBA.Any;
019:
020: import demo.notification.office.PrinterPackage.*;
021:
022: import org.omg.PortableServer.*;
023: import org.omg.CORBA.NO_IMPLEMENT;
024:
025: public class PrintClient extends StructuredPullConsumerPOA {
026: /**
027: * releases any resources, none in this case
028: */
029: public void disconnect_structured_pull_consumer() {
030: System.out.println("Disconnected!");
031: }
032:
033: public void offer_change(EventType added[], EventType removed[]) {
034: }
035:
036: /**
037: * main
038: */
039:
040: static public void main(String argv[]) {
041: EventChannel channel = null;
042: FilterFactory filterFactory = null;
043: Filter filter = null;
044: ConsumerAdmin consumerAdmin;
045: StructuredProxyPullSupplier proxyPullSupplier = null;
046: StructuredPullConsumer structuredPullConsumer;
047: Printer printer = null;
048: String userid = "MeMyselfAndI";
049:
050: if (argv.length > 0)
051: userid = argv[0];
052:
053: // initialize ORB
054:
055: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(argv, null);
056: POA poa = null;
057:
058: try {
059: // inititialize POA
060: poa = POAHelper.narrow(orb
061: .resolve_initial_references("RootPOA"));
062:
063: // get naming service reference
064: NamingContextExt nc = NamingContextExtHelper.narrow(orb
065: .resolve_initial_references("NameService"));
066:
067: // find the event channel reference and the Printer
068: channel = EventChannelHelper.narrow(nc.resolve(nc
069: .to_name("office_event.channel")));
070:
071: printer = PrinterHelper.narrow(nc.resolve(nc
072: .to_name("Printer")));
073:
074: poa.the_POAManager().activate();
075:
076: // create and implicitly activate the client
077: structuredPullConsumer = (StructuredPullConsumer) new PrintClient()
078: ._this (orb);
079:
080: // get the admin interface and the supplier proxy
081: consumerAdmin = channel.default_consumer_admin();
082:
083: proxyPullSupplier = StructuredProxyPullSupplierHelper
084: .narrow(consumerAdmin
085: .obtain_notification_pull_supplier(
086: ClientType.STRUCTURED_EVENT,
087: new org.omg.CORBA.IntHolder()));
088:
089: // connect ourselves to the event channel
090: proxyPullSupplier
091: .connect_structured_pull_consumer(structuredPullConsumer);
092:
093: // get the default filter factory
094: filterFactory = channel.default_filter_factory();
095: if (filterFactory == null) {
096: System.err.println("No default filter Factory!");
097: } else {
098: filter = filterFactory.create_filter("EXTENDED_TCL");
099: EventType[] eventTypes = new EventType[] {
100: new EventType("Office", "Printed"),
101: new EventType("Office", "Canceled") };
102:
103: ConstraintExp constraint = new ConstraintExp(
104: eventTypes, "TRUE");
105:
106: filter
107: .add_constraints(new ConstraintExp[] { constraint });
108: proxyPullSupplier.add_filter(filter);
109: }
110:
111: Property[] qos = new Property[1];
112: org.omg.CORBA.Any data = org.omg.CORBA.ORB.init()
113: .create_any();
114: data.insert_short(PriorityOrder.value);
115: qos[0] = new Property(OrderPolicy.value, data);
116:
117: try {
118: consumerAdmin.set_qos(qos);
119: } catch (UnsupportedQoS ex) {
120: System.err.println("Unsupported QoS");
121: } catch (NO_IMPLEMENT e) {
122: // this method is not supported yet
123: }
124:
125: } catch (Exception e) {
126: e.printStackTrace();
127: System.exit(1);
128: }
129:
130: // print a couple of jobs
131:
132: for (int i = 0; i < 5; i++) {
133: try {
134: System.out.println("Sending job, ID #"
135: + printer.print("A test job", userid));
136: } catch (OffLine ol) {
137: System.err
138: .println("Printer found off line when printing job.");
139: }
140: }
141:
142: // wait a sec...
143:
144: try {
145: System.out.println("Sleep...");
146: Thread.sleep(5000);
147: } catch (Exception e) {
148: }
149:
150: // try to cancel the last job
151:
152: int job = 4;
153: try {
154: System.out.println("Cancelling job ID #" + job);
155: printer.cancel(job, userid);
156: } catch (UnknownJobID ol) {
157: System.err.println("Unknown job ID #" + job);
158: } catch (AlreadyPrinted ap) {
159: System.err.println("Could not cancel, job #" + job
160: + " already printed");
161: } catch (org.omg.CORBA.NO_PERMISSION np) {
162: System.err.println("Could not cancel, job #" + job
163: + ", no permission");
164: }
165:
166: int eventsReceived = 0;
167:
168: for (int i = 0; i < 5; i++) {
169: org.omg.CORBA.BooleanHolder bh = new org.omg.CORBA.BooleanHolder();
170:
171: try {
172: System.out.println("Looking for structured events....");
173: // try to pull an event
174: StructuredEvent event = proxyPullSupplier
175: .try_pull_structured_event(bh);
176:
177: if (bh.value) {
178: System.out.println("got structured event.");
179: FixedEventHeader fixed_header = event.header.fixed_header;
180: System.out.println("\t"
181: + fixed_header.event_type.domain_name + "."
182: + fixed_header.event_type.type_name + "#"
183: + fixed_header.event_name);
184:
185: Property properties[] = event.filterable_data;
186: System.out.println("\t" + properties[0].name
187: + " : "
188: + properties[0].value.extract_long());
189: System.out.println("\t" + properties[1].name
190: + " : "
191: + properties[1].value.extract_string());
192: }
193: Thread.currentThread().sleep(2000);
194: } catch (Exception e) {
195: e.printStackTrace();
196: }
197: }
198: // disconnect and shutdown
199: proxyPullSupplier.disconnect_structured_pull_supplier();
200: orb.shutdown(true);
201: }
202: }
|