Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Simulator / DOVEBrowser / PushConsumer.java.JDK1.2
blob53e611055a5ba562211746a31faa02346358bfe1
1 // ============================================================================
2 //
3 //
4 // = FILENAME
5 //    PushConsumer.java
6 //
7 // = AUTHOR
8 //    Michael Kircher (mk1@cs.wustl.edu)
9 //
10 // = DESCRIPTION
11 //    This is a Push Consumer which takes the data field of the
12 //    event and updates with it a Data Handler.
15 // ============================================================================
18 public class PushConsumer extends RtecEventComm._PushConsumerImplBase
21   public static final int ACE_ES_EVENT_ANY = 0;
22   public static final int ACE_ES_EVENT_SHUTDOWN = 1;
23   public static final int ACE_ES_EVENT_ACT = 2;
24   public static final int ACE_ES_EVENT_NOTIFICATION = 3;
25   public static final int ACE_ES_EVENT_TIMEOUT = 4;
26   public static final int ACE_ES_EVENT_INTERVAL_TIMEOUT = 5;
27   public static final int ACE_ES_EVENT_DEADLINE_TIMEOUT = 6;
28   public static final int ACE_ES_GLOBAL_DESIGNATOR = 7;
29   public static final int ACE_ES_CONJUNCTION_DESIGNATOR = 8;
30   public static final int ACE_ES_DISJUNCTION_DESIGNATOR = 9;
31   public static final int ACE_ES_EVENT_UNDEFINED = 16;
32   public static final int TOTAL_MESSAGES = 30;
34   // Store the number of received events
35   private int total_received_ = 0;
36   private org.omg.CORBA.ORB orb_;
37   private DataHandler dataHandler_;
38   private int rt_info_;
39   private RtecEventChannelAdmin.EventChannel channel_admin_;
40   private RtecEventChannelAdmin.ConsumerAdmin consumer_admin_;
41   private RtecEventChannelAdmin.ProxyPushSupplier suppliers_;
43   public PushConsumer (org.omg.CORBA.ORB orb, DataHandler dataHandler)
44     {
45       orb_ = orb;
46       dataHandler_ = dataHandler;
47     }
50   public void push (RtecEventComm.Event[] events)
51     {
52       if (total_received_ < 5)
53         System.out.println ("Demo Consumer: Received an event! ->Number: " + total_received_);
54       else if (total_received_ == 5)
55         System.out.println ("Demo Consumer: Everything is fine. Going to be mute.");
58       if (events.length == 0)
59         {
60           System.err.println ("No events");
61         }
62       else
63         {
64           total_received_++;
66           for (int i = 0; i < events.length; ++i)
67             {
68               if(events[i].type_ == ACE_ES_EVENT_NOTIFICATION)
69                 {
70                   try
71                     {
72                       dataHandler_.update (events[i].data_.any_value);
73                     }
74                   catch(org.omg.CORBA.SystemException e)
75                     {
76                       System.err.println(e);
77                     }
78                 }
79             }
80         }
81     }
83   public void disconnect_push_consumer()
84     {
85       System.out.println ("Demo Consumer: Have to disconnect!");
86     }
88   public void open_consumer (RtecEventChannelAdmin.EventChannel event_channel,
89                              RtecScheduler.Scheduler scheduler,
90                              String name)
91     {
93       try
94         {
96           // Define Real-time information
98           rt_info_ = scheduler.create (name);
100           scheduler.set (rt_info_,
101                           RtecScheduler.Criticality.VERY_LOW_CRITICALITY,
102                           new TimeBase.ulonglong (0,0),
103                           new TimeBase.ulonglong (0,0),
104                           new TimeBase.ulonglong (0,0),
105                           2500000,  // period
106                           RtecScheduler.Importance.VERY_LOW_IMPORTANCE,
107                           new TimeBase.ulonglong (0,0),
108                           1,
109                           RtecScheduler.Info_Type.OPERATION);
111           // Register for Notification and Shutdown events
112           RtecEventComm.Event notification_event =
113             new RtecEventComm.Event (ACE_ES_EVENT_NOTIFICATION,  0,
114                                      1,        // ttl
115                                      new TimeBase.ulonglong (0,0),
116                                      new TimeBase.ulonglong (0,0),
117                                      new TimeBase.ulonglong (0,0),
118                                      new RtecEventComm.EventData (orb_.create_any ()));
120           RtecEventChannelAdmin.Dependency dependencies[] = new RtecEventChannelAdmin.Dependency[1];
121           dependencies[0] = new RtecEventChannelAdmin.Dependency (notification_event, rt_info_);
123           RtecEventChannelAdmin.ConsumerQOS qos = new RtecEventChannelAdmin.ConsumerQOS (dependencies,
124                                                                                          false);
126           // The channel administrator is the event channel we got from the invocation
127           // of this routine
128           channel_admin_ = event_channel;
130           // Connect as a consumer
131           consumer_admin_ = channel_admin_.for_consumers ();
133           // Obtain a reference to the proxy push supplier
134           suppliers_ = consumer_admin_.obtain_push_supplier ();
136           suppliers_.connect_push_consumer (this, qos);
138           System.out.println ("Registered the consumer successfully.");
140         }
141       catch (RtecEventChannelAdmin.TypeError e)
142         {
143           System.err.println ("Demo_Consumer.open_consumer: Type error");
144           System.err.println (e);
145         }
146       catch (RtecEventChannelAdmin.AlreadyConnected e)
147         {
148           System.err.println ("Demo_Consumer.open_consumer: Already connected");
149           System.err.println (e);
150         }
152       catch (RtecScheduler.UNKNOWN_TASK e)
153         {
154           System.err.println ("Demo_Consumer.open_consumer: Unknown task");
155           System.err.println (e);
156         }
157       catch (RtecScheduler.DUPLICATE_NAME e)
158         {
159           System.err.println ("Demo_Consumer.open_consumer: Duplicate names");
160           System.err.println (e);
161         }
162       catch(org.omg.CORBA.SystemException e)
163         {
164           System.err.println(e);
165         }
166     }