KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.test.notification;
2
3 import junit.framework.Assert;
4
5 import org.omg.CORBA.Any JavaDoc;
6 import org.omg.CORBA.BooleanHolder JavaDoc;
7 import org.omg.CORBA.IntHolder JavaDoc;
8 import org.omg.CORBA.ORB JavaDoc;
9 import org.omg.CosEventChannelAdmin.AlreadyConnected;
10 import org.omg.CosEventChannelAdmin.TypeError;
11 import org.omg.CosEventComm.Disconnected;
12 import org.omg.CosNotification.EventType;
13 import org.omg.CosNotifyChannelAdmin.AdminLimitExceeded;
14 import org.omg.CosNotifyChannelAdmin.AdminNotFound;
15 import org.omg.CosNotifyChannelAdmin.ClientType;
16 import org.omg.CosNotifyChannelAdmin.EventChannel;
17 import org.omg.CosNotifyChannelAdmin.InterFilterGroupOperator;
18 import org.omg.CosNotifyChannelAdmin.ProxyPullConsumer;
19 import org.omg.CosNotifyChannelAdmin.ProxyPullConsumerHelper;
20 import org.omg.CosNotifyChannelAdmin.ProxyType;
21 import org.omg.CosNotifyChannelAdmin.SupplierAdmin;
22 import org.omg.CosNotifyComm.PullSupplierPOA;
23
24 /**
25  * @author Alphonse Bendt
26  */

27
28 public class AnyPullSender extends PullSupplierPOA implements TestClientOperations
29 {
30     Any JavaDoc event_;
31
32     Any JavaDoc invalidAny_;
33
34     ProxyPullConsumer myConsumer_;
35
36     boolean connected_ = false;
37
38     boolean available_ = false;
39
40     SupplierAdmin myAdmin_;
41
42     private ORB JavaDoc orb_;
43
44     public AnyPullSender(ORB JavaDoc orb, Any JavaDoc event)
45     {
46         event_ = event;
47         orb_ = orb;
48     }
49
50     void reset()
51     {
52         event_ = null;
53     }
54
55     public boolean isConnected()
56     {
57         return connected_;
58     }
59
60     public void connect(EventChannel channel, boolean useOrSemantic) throws AdminLimitExceeded,
61             AlreadyConnected, TypeError, AdminNotFound
62     {
63
64         IntHolder JavaDoc _proxyId = new IntHolder JavaDoc();
65         IntHolder JavaDoc _adminId = new IntHolder JavaDoc();
66
67         invalidAny_ = orb_.create_any();
68
69         if (useOrSemantic)
70         {
71             myAdmin_ = channel.new_for_suppliers(InterFilterGroupOperator.OR_OP, _adminId);
72             Assert.assertEquals(InterFilterGroupOperator.OR_OP, myAdmin_.MyOperator());
73         }
74         else
75         {
76             myAdmin_ = channel.new_for_suppliers(InterFilterGroupOperator.AND_OP, _adminId);
77             Assert.assertEquals(InterFilterGroupOperator.AND_OP, myAdmin_.MyOperator());
78         }
79
80         Assert.assertEquals(myAdmin_, channel.get_supplieradmin(_adminId.value));
81
82         myConsumer_ = ProxyPullConsumerHelper.narrow(myAdmin_.obtain_notification_pull_consumer(
83                 ClientType.ANY_EVENT, _proxyId));
84
85         Assert.assertEquals(ProxyType._PULL_ANY, myConsumer_.MyType().value());
86
87         myConsumer_.connect_any_pull_supplier(_this(orb_));
88         connected_ = true;
89     }
90
91     public void shutdown()
92     {
93         myConsumer_.disconnect_pull_consumer();
94     }
95
96     public void run()
97     {
98         available_ = true;
99     }
100
101     public boolean isEventHandled()
102     {
103         return sent_;
104     }
105
106     boolean sent_ = false;
107
108     public boolean isError()
109     {
110         return false;
111     }
112
113     public void subscription_change(EventType[] e1, EventType[] e2)
114     {
115         // ignored
116
}
117
118     public Any JavaDoc pull() throws Disconnected
119     {
120         BooleanHolder JavaDoc _b = new BooleanHolder JavaDoc();
121         Any JavaDoc _event;
122         while (true)
123         {
124             _event = try_pull(_b);
125             if (_b.value)
126             {
127                 return _event;
128             }
129             Thread.yield();
130         }
131     }
132
133     public Any JavaDoc try_pull(BooleanHolder JavaDoc success) throws Disconnected
134     {
135         try
136         {
137             Any JavaDoc _event = invalidAny_;
138             success.value = false;
139
140             synchronized (this)
141             {
142                 if (available_)
143                 {
144                     _event = event_;
145                     event_ = null;
146                     success.value = true;
147
148                     sent_ = true;
149                     available_ = false;
150                 }
151             }
152
153             return _event;
154         } catch (Throwable JavaDoc t)
155         {
156             t.printStackTrace();
157             throw new RuntimeException JavaDoc();
158         }
159     }
160
161     public void disconnect_pull_supplier()
162     {
163         connected_ = false;
164     }
165 }
Popular Tags