KickJava   Java API By Example, From Geeks To Geeks.

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


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.CosNotification.StructuredEvent;
12 import org.omg.CosNotifyChannelAdmin.AdminLimitExceeded;
13 import org.omg.CosNotifyChannelAdmin.ClientType;
14 import org.omg.CosNotifyChannelAdmin.ConsumerAdmin;
15 import org.omg.CosNotifyChannelAdmin.EventChannel;
16 import org.omg.CosNotifyChannelAdmin.ProxyType;
17 import org.omg.CosNotifyChannelAdmin.StructuredProxyPullSupplier;
18 import org.omg.CosNotifyChannelAdmin.StructuredProxyPullSupplierHelper;
19 import org.omg.CosNotifyComm.StructuredPullConsumerHelper;
20 import org.omg.CosNotifyComm.StructuredPullConsumerOperations;
21 import org.omg.CosNotifyComm.StructuredPullConsumerPOATie;
22 import org.omg.PortableServer.POA JavaDoc;
23
24 public class StructuredPullReceiver extends Thread JavaDoc implements StructuredPullConsumerOperations,
25         TestClientOperations
26 {
27     StructuredEvent event_ = null;
28
29     ORB JavaDoc orb_;
30
31     POA JavaDoc poa_;
32
33     boolean connected_;
34
35     StructuredProxyPullSupplier pullSupplier_;
36
37     boolean received_;
38
39     long TIMEOUT = 2000;
40
41     boolean error_;
42
43     public StructuredPullReceiver(ORB JavaDoc orb)
44     {
45         super();
46         orb_ = orb;
47     }
48
49     public boolean isConnected()
50     {
51         return connected_;
52     }
53
54     public void connect(EventChannel channel, boolean useOrSemantic) throws AdminLimitExceeded,
55             AlreadyConnected
56     {
57
58         StructuredPullConsumerPOATie _receiverTie = new StructuredPullConsumerPOATie(this);
59         ConsumerAdmin _consumerAdmin = channel.default_consumer_admin();
60         IntHolder JavaDoc _proxyId = new IntHolder JavaDoc();
61
62         pullSupplier_ = StructuredProxyPullSupplierHelper.narrow(_consumerAdmin
63                 .obtain_notification_pull_supplier(ClientType.STRUCTURED_EVENT, _proxyId));
64
65         Assert.assertEquals(pullSupplier_.MyType(), ProxyType.PULL_STRUCTURED);
66
67         pullSupplier_.connect_structured_pull_consumer(StructuredPullConsumerHelper
68                 .narrow(_receiverTie._this(orb_)));
69         connected_ = true;
70     }
71
72     public void shutdown()
73     {
74         Assert.assertTrue(!pullSupplier_._non_existent());
75         pullSupplier_.disconnect_structured_pull_supplier();
76         Assert.assertTrue(pullSupplier_._non_existent());
77         pullSupplier_ = null;
78     }
79
80     public boolean isEventHandled()
81     {
82         return event_ != null;
83     }
84
85     public boolean isError()
86     {
87         return false;
88     }
89
90     public void run()
91     {
92         BooleanHolder JavaDoc _success = new BooleanHolder JavaDoc();
93         _success.value = false;
94         long _startTime = System.currentTimeMillis();
95
96         try
97         {
98             while (true)
99             {
100                 event_ = pullSupplier_.try_pull_structured_event(_success);
101
102                 if (_success.value)
103                 {
104                     received_ = true;
105                     break;
106                 }
107
108                 if (System.currentTimeMillis() < _startTime + TIMEOUT)
109                 {
110                     Thread.yield();
111                 }
112                 else
113                 {
114                     received_ = false;
115                     break;
116                 }
117             }
118         } catch (Disconnected d)
119         {
120             d.printStackTrace();
121             error_ = true;
122         }
123     }
124
125     public void push_structured_event(StructuredEvent event)
126     {
127         synchronized (this)
128         {
129             event_ = event;
130             this.notifyAll();
131         }
132     }
133
134     public void disconnect_structured_pull_consumer()
135     {
136         connected_ = false;
137     }
138
139     public void offer_change(EventType[] e1, EventType[] e2)
140     {
141         // ignored
142
}
143 }
144
Popular Tags