KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > notification > office > PrintClient


1 package demo.notification.office;
2
3 /**
4  * A simple notification demo client for the Printer service.
5  * It will send a few commands to the printer and wait for
6  * structured events to see what happens. Serves to show that
7  * the Notification Service is set up correctly and works.
8  *
9  * @author Gerald Brose, 08/01/2003
10  */

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 JavaDoc;
19
20 import demo.notification.office.PrinterPackage.*;
21
22 import org.omg.PortableServer.*;
23 import org.omg.CORBA.NO_IMPLEMENT JavaDoc;
24
25 public class PrintClient
26     extends StructuredPullConsumerPOA
27 {
28     /**
29      * releases any resources, none in this case
30      */

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     /**
41      * main
42      */

43
44     static public void main( String JavaDoc 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 JavaDoc userid = "MeMyselfAndI";
54
55         if( argv.length > 0 )
56             userid = argv[0];
57
58         // initialize ORB
59

60         org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init( argv, null);
61         POA poa = null;
62
63         try
64         {
65             // inititialize POA
66
poa =
67                 POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
68
69             // get naming service reference
70
NamingContextExt nc =
71                 NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
72
73             // find the event channel reference and the Printer
74
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             // create and implicitly activate the client
82
structuredPullConsumer =
83                 (StructuredPullConsumer)new PrintClient()._this(orb);
84
85             // get the admin interface and the supplier proxy
86
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 JavaDoc() ) );
93
94             // connect ourselves to the event channel
95
proxyPullSupplier.connect_structured_pull_consumer( structuredPullConsumer );
96
97             // get the default filter factory
98
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 JavaDoc 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 JavaDoc e) {
131                 // this method is not supported yet
132
}
133
134         }
135         catch (Exception JavaDoc e)
136         {
137             e.printStackTrace();
138             System.exit(1);
139         }
140
141         // print a couple of jobs
142

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         // wait a sec...
157

158         try
159         {
160             System.out.println("Sleep...");
161             Thread.sleep(5000);
162         }
163         catch( Exception JavaDoc e)
164         {}
165
166         // try to cancel the last job
167

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 JavaDoc 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 JavaDoc bh =
192                 new org.omg.CORBA.BooleanHolder JavaDoc();
193
194             try
195             {
196                 System.out.println("Looking for structured events....");
197                 // try to pull an event
198
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 JavaDoc e )
218             {
219                 e.printStackTrace();
220             }
221         }
222         // disconnect and shutdown
223
proxyPullSupplier.disconnect_structured_pull_supplier();
224         orb.shutdown(true);
225     }
226 }
227
228
229
230
231
232
Popular Tags