KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.test.notification;
2
3 import junit.framework.Assert;
4
5 import org.omg.CORBA.BooleanHolder JavaDoc;
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.EventType;
11 import org.omg.CosNotifyChannelAdmin.AdminLimitExceeded;
12 import org.omg.CosNotifyChannelAdmin.AdminNotFound;
13 import org.omg.CosNotifyChannelAdmin.ClientType;
14 import org.omg.CosNotifyChannelAdmin.ConsumerAdmin;
15 import org.omg.CosNotifyChannelAdmin.EventChannel;
16 import org.omg.CosNotifyChannelAdmin.InterFilterGroupOperator;
17 import org.omg.CosNotifyChannelAdmin.ProxyPullSupplier;
18 import org.omg.CosNotifyChannelAdmin.ProxyPullSupplierHelper;
19 import org.omg.CosNotifyChannelAdmin.ProxyType;
20 import org.omg.CosNotifyComm.PullConsumerPOA;
21
22 /**
23  * @author Alphonse Bendt
24  */

25
26 public class AnyPullReceiver
27             extends PullConsumerPOA
28             implements Runnable JavaDoc,
29             TestClientOperations
30 {
31     boolean received_ = false;
32     boolean error_ = false;
33     ORB JavaDoc orb_;
34     ProxyPullSupplier mySupplier_;
35     long TIMEOUT = 5000;
36     boolean connected_;
37     
38     ConsumerAdmin myAdmin_;
39     NotificationTestCase testCase_;
40
41     public AnyPullReceiver(ORB JavaDoc orb)
42     {
43         orb_ = orb;
44     }
45
46     public void connect(EventChannel channel,
47                         boolean useOrSemantic) throws AdminNotFound, AlreadyConnected, AdminLimitExceeded
48     {
49         IntHolder JavaDoc _proxyId = new IntHolder JavaDoc();
50         IntHolder JavaDoc _adminId = new IntHolder JavaDoc();
51
52         if (useOrSemantic)
53         {
54             myAdmin_ = channel.new_for_consumers(InterFilterGroupOperator.OR_OP, _adminId);
55             Assert.assertEquals(InterFilterGroupOperator.OR_OP, myAdmin_.MyOperator());
56         }
57         else
58         {
59             myAdmin_ = channel.new_for_consumers(InterFilterGroupOperator.AND_OP, _adminId);
60             Assert.assertEquals(InterFilterGroupOperator.AND_OP, myAdmin_.MyOperator());
61         }
62         
63         Assert.assertEquals(myAdmin_, channel.get_consumeradmin(_adminId.value));
64
65         mySupplier_ =
66             ProxyPullSupplierHelper.narrow(myAdmin_.
67                                            obtain_notification_pull_supplier(ClientType.ANY_EVENT, _proxyId));
68
69         Assert.assertEquals(ProxyType._PULL_ANY,
70                                mySupplier_.MyType().value());
71
72
73         mySupplier_.connect_any_pull_consumer(_this(orb_));
74         connected_ = true;
75     }
76
77     public void shutdown()
78     {
79         mySupplier_.disconnect_pull_supplier();
80         mySupplier_ = null;
81
82         if (myAdmin_ != null)
83         {
84             myAdmin_.destroy();
85             myAdmin_ = null;
86         }
87     }
88
89     void reset()
90     {
91         error_ = false;
92         received_ = false;
93     }
94
95     public boolean isConnected()
96     {
97         return connected_;
98     }
99
100     public boolean isEventHandled()
101     {
102         return received_;
103     }
104
105     public boolean isError()
106     {
107         return error_;
108     }
109
110     public void run()
111     {
112         BooleanHolder JavaDoc _success = new BooleanHolder JavaDoc();
113         _success.value = false;
114         long _startTime = System.currentTimeMillis();
115
116         try
117         {
118             while (true)
119             {
120                 mySupplier_.try_pull(_success);
121
122                 if (_success.value)
123                 {
124                     received_ = true;
125                     break;
126                 }
127
128                 if (System.currentTimeMillis() < _startTime + TIMEOUT)
129                 {
130                     Thread.yield();
131                 }
132                 else
133                 {
134                     received_ = false;
135                     break;
136                 }
137             }
138         }
139         catch (Disconnected d)
140         {
141             d.printStackTrace();
142             error_ = true;
143         }
144     }
145
146     public void offer_change(EventType[] e1, EventType[] e2)
147     {
148         // ignored
149
}
150
151     public void disconnect_pull_consumer()
152     {
153         connected_ = false;
154     }
155 }
156
Popular Tags