summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/DOVEBrowser/PushConsumer.java.JDK1.2
blob: 49a665569c306519d29c7172c52475c71ee3a6a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// $Id$
//
// ============================================================================
//
// 
// = FILENAME
//    PushConsumer.java
//
// = AUTHOR
//    Michael Kircher (mk1@cs.wustl.edu)
//
// = DESCRIPTION
//    This is a Push Consumer which takes the data field of the
//    event and updates with it a Data Handler.
// 
//
// ============================================================================


public class PushConsumer extends RtecEventComm._PushConsumerImplBase
{
  
  public static final int ACE_ES_EVENT_ANY = 0;
  public static final int ACE_ES_EVENT_SHUTDOWN = 1;
  public static final int ACE_ES_EVENT_ACT = 2;
  public static final int ACE_ES_EVENT_NOTIFICATION = 3;
  public static final int ACE_ES_EVENT_TIMEOUT = 4;
  public static final int ACE_ES_EVENT_INTERVAL_TIMEOUT = 5;
  public static final int ACE_ES_EVENT_DEADLINE_TIMEOUT = 6;
  public static final int ACE_ES_GLOBAL_DESIGNATOR = 7;
  public static final int ACE_ES_CONJUNCTION_DESIGNATOR = 8;
  public static final int ACE_ES_DISJUNCTION_DESIGNATOR = 9;
  public static final int ACE_ES_EVENT_UNDEFINED = 16;
  public static final int TOTAL_MESSAGES = 30;

  // Store the number of received events
  private int total_received_ = 0;
  private org.omg.CORBA.ORB orb_;
  private DataHandler dataHandler_;
  private int rt_info_;
  private RtecEventChannelAdmin.EventChannel channel_admin_;
  private RtecEventChannelAdmin.ConsumerAdmin consumer_admin_;
  private RtecEventChannelAdmin.ProxyPushSupplier suppliers_;
  
  public PushConsumer (org.omg.CORBA.ORB orb, DataHandler dataHandler)
    {
      orb_ = orb;
      dataHandler_ = dataHandler;
    }
  
  
  public void push (RtecEventComm.Event[] events)
    {
      if (total_received_ < 5)	  
	System.out.println ("Demo Consumer: Received an event! ->Number: " + total_received_);
      else if (total_received_ == 5)
	System.out.println ("Demo Consumer: Everything is fine. Going to be mute.");
      
      
      if (events.length == 0)
	{
	  System.err.println ("No events");
	}
      else
	{
	  total_received_++;
	  
	  for (int i = 0; i < events.length; ++i)
	    {
	      if(events[i].type_ == ACE_ES_EVENT_NOTIFICATION) 
		{
		  try
		    {
		      dataHandler_.update (events[i].data_.any_value);
		    }
		  catch(org.omg.CORBA.SystemException e) 
		    {
		      System.err.println(e);
		    }
		}		
	    }
	}
    }
  
  public void disconnect_push_consumer() 
    {
      System.out.println ("Demo Consumer: Have to disconnect!");
    }
  
  public void open_consumer (RtecEventChannelAdmin.EventChannel event_channel,
			     RtecScheduler.Scheduler scheduler,
			     String name)
    {
      
      try 
	{
	  
	  // Define Real-time information
	  
	  rt_info_ = scheduler.create (name);
	  
	  scheduler.set (rt_info_,
			  RtecScheduler.Criticality.VERY_LOW_CRITICALITY,
			  new TimeBase.ulonglong (0,0), 
			  new TimeBase.ulonglong (0,0),
			  new TimeBase.ulonglong (0,0),
			  2500000,  // period
			  RtecScheduler.Importance.VERY_LOW_IMPORTANCE,
			  new TimeBase.ulonglong (0,0),
			  1,
			  RtecScheduler.Info_Type.OPERATION);	  	        
	  
	  // Register for Notification and Shutdown events
	  RtecEventComm.Event notification_event = 
	    new RtecEventComm.Event (ACE_ES_EVENT_NOTIFICATION,  0, 
				     1,        // ttl
				     new TimeBase.ulonglong (0,0),
				     new TimeBase.ulonglong (0,0),
				     new TimeBase.ulonglong (0,0),
				     new RtecEventComm.EventData (orb_.create_any ()));
	  
	  RtecEventChannelAdmin.Dependency dependencies[] = new RtecEventChannelAdmin.Dependency[1];  
	  dependencies[0] = new RtecEventChannelAdmin.Dependency (notification_event, rt_info_);
	  	 	  
	  RtecEventChannelAdmin.ConsumerQOS qos = new RtecEventChannelAdmin.ConsumerQOS (dependencies,
                                                                                         false); 
	  
	  // The channel administrator is the event channel we got from the invocation
	  // of this routine
	  channel_admin_ = event_channel;
	  
	  // Connect as a consumer
	  consumer_admin_ = channel_admin_.for_consumers ();
	
	  // Obtain a reference to the proxy push supplier
	  suppliers_ = consumer_admin_.obtain_push_supplier ();
	  
	  suppliers_.connect_push_consumer (this, qos);
	  
	  System.out.println ("Registered the consumer successfully.");
	  
	}
      catch (RtecEventChannelAdmin.TypeError e)
	{
	  System.err.println ("Demo_Consumer.open_consumer: Type error");
	  System.err.println (e);
	}
      catch (RtecEventChannelAdmin.AlreadyConnected e)
	{
	  System.err.println ("Demo_Consumer.open_consumer: Already connected");
	  System.err.println (e);
	}

      catch (RtecScheduler.UNKNOWN_TASK e)
	{
	  System.err.println ("Demo_Consumer.open_consumer: Unknown task");
	  System.err.println (e);
	}
      catch (RtecScheduler.DUPLICATE_NAME e)
	{
	  System.err.println ("Demo_Consumer.open_consumer: Duplicate names");
	  System.err.println (e);
	}
      catch(org.omg.CORBA.SystemException e) 
	{
	  System.err.println(e);
	}		
    }  
}