KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > CosEventPullSender


1 package org.jacorb.test.notification;
2
3 import org.omg.CORBA.Any JavaDoc;
4 import org.omg.CORBA.BooleanHolder JavaDoc;
5 import org.omg.CORBA.ORB JavaDoc;
6 import org.omg.CosEventChannelAdmin.AlreadyConnected;
7 import org.omg.CosEventChannelAdmin.EventChannel;
8 import org.omg.CosEventChannelAdmin.EventChannelHelper;
9 import org.omg.CosEventChannelAdmin.ProxyPullConsumer;
10 import org.omg.CosEventChannelAdmin.SupplierAdmin;
11 import org.omg.CosEventChannelAdmin.TypeError;
12 import org.omg.CosEventComm.Disconnected;
13 import org.omg.CosEventComm.PullSupplierPOA;
14
15 import EDU.oswego.cs.dl.util.concurrent.Latch;
16
17 /**
18  * @author Alphonse Bendt
19  * @version $Id: CosEventPullSender.java,v 1.5 2005/05/01 21:14:29 alphonse.bendt Exp $
20  */

21
22 public class CosEventPullSender extends PullSupplierPOA implements TestClientOperations, Runnable JavaDoc
23 {
24     Any JavaDoc event_;
25
26     Any JavaDoc invalidAny_;
27
28     ProxyPullConsumer myConsumer_;
29
30     Latch latch_ = new Latch();
31
32     private boolean connected_;
33
34     private boolean error_;
35
36     private boolean sent_;
37
38     private final ORB JavaDoc orb_;
39
40     CosEventPullSender(ORB JavaDoc orb, Any JavaDoc event)
41     {
42         event_ = event;
43         orb_ = orb;
44     }
45
46     public void run()
47     {
48         try
49         {
50             latch_.acquire();
51         } catch (InterruptedException JavaDoc ie)
52         {
53             // ignored
54
}
55     }
56
57     public Any JavaDoc pull() throws Disconnected
58     {
59         BooleanHolder JavaDoc _b = new BooleanHolder JavaDoc();
60         Any JavaDoc _event;
61         while (true)
62         {
63             _event = try_pull(_b);
64             if (_b.value)
65             {
66                 return _event;
67             }
68             Thread.yield();
69         }
70     }
71
72     public Any JavaDoc try_pull(BooleanHolder JavaDoc success) throws Disconnected
73     {
74         Any JavaDoc _event = invalidAny_;
75         success.value = false;
76         if (event_ != null)
77         {
78             synchronized (this)
79             {
80                 if (event_ != null)
81                 {
82                     _event = event_;
83                     event_ = null;
84                     success.value = true;
85                     sent_ = true;
86                     latch_.release();
87                 }
88             }
89         }
90         return _event;
91     }
92
93     public void disconnect_pull_supplier()
94     {
95         connected_ = false;
96     }
97
98     public boolean isConnected()
99     {
100         return connected_;
101     }
102
103     public boolean isEventHandled()
104     {
105         return sent_;
106     }
107
108     public boolean isError()
109     {
110         return error_;
111     }
112
113     public void shutdown()
114     {
115         myConsumer_.disconnect_pull_consumer();
116     }
117
118     public void connect(org.omg.CosNotifyChannelAdmin.EventChannel channel, boolean useOrSemantic)
119             throws AlreadyConnected, TypeError
120     {
121         invalidAny_ = orb_.create_any();
122         EventChannel _channel = EventChannelHelper.narrow(channel);
123         SupplierAdmin _admin = _channel.for_suppliers();
124         myConsumer_ = _admin.obtain_pull_consumer();
125
126         myConsumer_.connect_pull_supplier(_this(orb_));
127         connected_ = true;
128     }
129
130 }
131
Popular Tags