KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > notification > whiteboard > WhiteBoard


1 package demo.notification.whiteboard;
2
3 import java.util.Hashtable JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.omg.CORBA.IntHolder JavaDoc;
7 import org.omg.CORBA.ORB JavaDoc;
8 import org.omg.CosEventChannelAdmin.AlreadyConnected;
9 import org.omg.CosEventComm.Disconnected;
10 import org.omg.CosNotification.EventHeader;
11 import org.omg.CosNotification.EventType;
12 import org.omg.CosNotification.FixedEventHeader;
13 import org.omg.CosNotification.Property;
14 import org.omg.CosNotification.StructuredEvent;
15 import org.omg.CosNotifyChannelAdmin.AdminLimitExceeded;
16 import org.omg.CosNotifyChannelAdmin.AdminNotFound;
17 import org.omg.CosNotifyChannelAdmin.ClientType;
18 import org.omg.CosNotifyChannelAdmin.ConsumerAdmin;
19 import org.omg.CosNotifyChannelAdmin.EventChannel;
20 import org.omg.CosNotifyChannelAdmin.InterFilterGroupOperator;
21 import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumer;
22 import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumerHelper;
23 import org.omg.CosNotifyChannelAdmin.SupplierAdmin;
24 import org.omg.CosNotifyComm.InvalidEventType;
25 import org.omg.CosNotifyComm.StructuredPushSupplierOperations;
26 import org.omg.CosNotifyComm.StructuredPushSupplierPOATie;
27 import org.omg.CosNotifyFilter.ConstraintExp;
28 import org.omg.CosNotifyFilter.Filter;
29 import org.omg.CosNotifyFilter.FilterFactory;
30 import org.omg.CosNotifyFilter.InvalidConstraint;
31 import org.omg.CosNotifyFilter.InvalidGrammar;
32
33 /**
34  * @author Alphonse Bendt
35  * @version $Id: WhiteBoard.java,v 1.4 2004/04/28 13:52:02 alphonse.bendt Exp $
36  */

37
38 public class WhiteBoard
39     extends IWhiteBoardPOA
40     implements IWhiteBoardOperations, WhiteboardVars {
41
42     // zur Erzeugung einer eindeutigen Id
43
static int COUNT = 0;
44
45     EventChannel channel_;
46     FilterFactory filterFactory_;
47
48     // alle angemeldeten Workgroups
49
protected Map JavaDoc workgroups_;
50
51     Map JavaDoc registrationInfo_ = new Hashtable JavaDoc();
52
53     // bisher gesammelte Updates
54
// Queue updates_;
55

56     // Dispatcher Thread
57
Dispatcher disp;
58
59     // prima"rkopie
60
BrushSizePixelImage image;
61
62     public WhiteBoard(ORB JavaDoc orb, EventChannel channel) throws AdminLimitExceeded {
63     this(orb, channel, 400, 400);
64     }
65
66     public WhiteBoard(ORB JavaDoc orb,
67               EventChannel channel,
68               int xsize,
69               int ysize) throws AdminLimitExceeded {
70
71     System.out.println("WhiteBoard.init()");
72
73     _this(orb);
74     workgroups_ = new Hashtable JavaDoc();
75     clear();
76     image = new ExtendedPixelImage(xsize,ysize);
77     disp = new Dispatcher(orb, this, channel.default_supplier_admin());
78     // disp.start();
79
System.out.println("done");
80     filterFactory_ = channel.default_filter_factory();
81     channel_ = channel;
82     }
83
84     public int[] getCurrentImage() {
85     return image.getPixelBuffer();
86     }
87
88     // Workgroup an Whiteboard anmelden
89
// Id zur Kommunikation mit dem Whiteboard zuru"ck
90
public IRegistrationInfo join(IWorkgroup group) {
91     System.out.println("Workgroup joins the Whiteboard ");
92
93     Integer JavaDoc _id = new Integer JavaDoc(COUNT++);
94     System.out.println("workgroups: " + workgroups_ + ".put(" + _id + ", " + group);
95
96     workgroups_.put(_id, group);
97
98     IRegistrationInfo _registrationInfo = new IRegistrationInfo();
99     _registrationInfo.workgroup_identifier = _id.intValue();
100
101     IntHolder JavaDoc
102         _supplierAdminId = new IntHolder JavaDoc(),
103         _consumerAdminId = new IntHolder JavaDoc();
104
105     Filter _filter = null;
106     try {
107         _filter = filterFactory_.create_filter("EXTENDED_TCL");
108
109         ConstraintExp[] _constraints = new ConstraintExp[1];
110         _constraints[0] = new ConstraintExp();
111         _constraints[0].event_types = new EventType[] {new EventType(EVENT_DOMAIN, "*")};
112         _constraints[0].constraint_expr = "$.header.variable_header(" + WORKGROUP_ID + ") != " + _id.intValue();
113
114
115         _filter.add_constraints(_constraints);
116
117         SupplierAdmin _supplierAdmin =
118         channel_.new_for_suppliers(InterFilterGroupOperator.AND_OP, _supplierAdminId);
119
120         ConsumerAdmin _consumerAdmin =
121         channel_.new_for_consumers(InterFilterGroupOperator.AND_OP, _consumerAdminId);
122         _consumerAdmin.add_filter(_filter);
123
124         _registrationInfo.supplier_admin = _supplierAdmin;
125         _registrationInfo.consumer_admin = _consumerAdmin;
126         _registrationInfo.filter_factory = filterFactory_;
127
128         LocalRegistrationInfo _localInfo = new LocalRegistrationInfo();
129         _localInfo.consumerAdmin_ = _consumerAdminId.value;
130         _localInfo.supplierAdmin_ = _supplierAdminId.value;
131
132         registrationInfo_.put(_id, _localInfo);
133
134         return _registrationInfo;
135     } catch (InvalidGrammar ig) {
136         ig.printStackTrace();
137     } catch (InvalidConstraint ic) {
138         ic.printStackTrace();
139         }
140     throw new RuntimeException JavaDoc();
141     }
142
143     public boolean leave(int workgroup) {
144     System.out.println("Bye");
145
146     Integer JavaDoc _id = new Integer JavaDoc(workgroup);
147
148     LocalRegistrationInfo _info =
149         (LocalRegistrationInfo)registrationInfo_.get(_id);
150
151     try {
152         channel_.get_consumeradmin(_info.consumerAdmin_).destroy();
153         channel_.get_supplieradmin(_info.supplierAdmin_).destroy();
154     } catch (AdminNotFound anf) {
155     }
156
157     return (workgroups_.remove(_id) != null);
158     }
159
160     // Whiteboard lo"schen
161
public void clear() {
162     // updates_ = new Queue();
163
}
164
165 } // WhiteBoard
166

167 class Dispatcher
168     extends Thread JavaDoc
169     implements WhiteboardVars, StructuredPushSupplierOperations {
170
171     boolean connected_ = false;
172     boolean active_ = true;
173     WhiteBoard master_;
174     StructuredProxyPushConsumer myConsumer_;
175     ORB JavaDoc orb_;
176     IntHolder JavaDoc myConsumerId_ = new IntHolder JavaDoc();
177     static long SLEEP = 10000L;
178
179     public Dispatcher(ORB JavaDoc orb,
180               WhiteBoard master,
181               SupplierAdmin supplierAdmin) throws AdminLimitExceeded {
182
183     master_ = master;
184     orb_ = orb;
185     myConsumer_ =
186         StructuredProxyPushConsumerHelper.narrow(supplierAdmin.obtain_notification_push_consumer(ClientType.STRUCTURED_EVENT, myConsumerId_));
187     connected_ = tryConnect();
188     }
189
190     boolean tryConnect() {
191     long BACKOFF = 100L;
192
193     for (int x=0; x<3; ++x) {
194         try {
195         myConsumer_.connect_structured_push_supplier(new StructuredPushSupplierPOATie(this)._this(orb_));
196         connected_ = true;
197         return true;
198         } catch (AlreadyConnected ac) {
199         }
200
201         try {
202         Thread.sleep(BACKOFF^(x+1));
203         } catch (InterruptedException JavaDoc ie) {
204         }
205     }
206     return false;
207     }
208
209     public void shutdown() {
210     active_ = false;
211     interrupt();
212     }
213
214     public void run() {
215     StructuredEvent _event = new StructuredEvent();
216     _event.header = new EventHeader();
217     _event.header.fixed_header = new FixedEventHeader();
218     _event.header.fixed_header.event_type = new EventType();
219     _event.header.fixed_header.event_type.domain_name = EVENT_DOMAIN;
220     _event.header.fixed_header.event_type.type_name = "UPDATE";
221     _event.header.fixed_header.event_name = "";
222     _event.header.variable_header = new Property[0];
223
224     _event.filterable_data = new Property[0];
225
226     while (active_) {
227         try {
228         Thread.sleep(SLEEP);
229         } catch (InterruptedException JavaDoc ie) {
230         if (!active_) {
231             return;
232         }
233         }
234
235         if (!connected_) {
236         connected_ = tryConnect();
237         if (!connected_) {
238             System.out.println("Giving Up");
239             return;
240         }
241         }
242
243         try {
244         WhiteboardUpdate _update = new WhiteboardUpdate();
245         _update.image(master_.getCurrentImage());
246         _event.remainder_of_body = orb_.create_any();
247         WhiteboardUpdateHelper.insert(_event.remainder_of_body, _update);
248         myConsumer_.push_structured_event(_event);
249         } catch (Disconnected d) {
250         connected_ = false;
251         }
252     }
253     }
254
255     public void disconnect_structured_push_supplier() {
256     connected_ = false;
257     }
258
259     public void subscription_change(EventType[] t1, EventType[] t2) throws InvalidEventType {
260     }
261 }
262
263 class LocalRegistrationInfo {
264     int consumerAdmin_;
265     int supplierAdmin_;
266 }
267
268
Popular Tags