summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/DOVEBrowser/MTDataHandlerAdapter.java
blob: 5025c0a8e6db5b726d3aa07e0b99baac92f12467 (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
// $Id$

// This is an adapter class for a data handler to be used in a separate 
// thread.  The adapter provides a push method that places an event
// set into its synchronized internal MTQueue.  It runs a separate thread
// which blocks until there is an event in the queue, then dequeues the
// event and then unpacks it and updates the underlying data handler.

public class  MTDataHandlerAdapter extends Thread
{
  // Both the queue and the underlying data handler are private
  private MTQueue queue_ = null;
  private DataHandler dataHandler_ = null;
  private boolean use_queueing_ = false;

  // Constructor.
  MTDataHandlerAdapter (DataHandler dh, boolean use_queueing)
    {
      dataHandler_ = dh;
      use_queueing_ = use_queueing;
      if (use_queueing_)
        {
          queue_ = new MTQueue ();
        }
    }

  // Enqueue an event set for the handler thread.
  public void push (RtecEventComm.Event[] events)
    {
      if (use_queueing_)
        {
          queue_.enqueue_tail (events);
        }
      else
        {
          for (int i = 0; i < events.length; ++i)
            {
              if(events[i].header.type == 
                 PushConsumer.ACE_ES_EVENT_NOTIFICATION)
                {
                  dataHandler_.update (events[i]);
                }
            }
        }
    }

  // Process enqueued event sets in a separate thread.
  public void run ()
    {
      // Loop forever, handling events.
      for (;;)
        {
          try
            {
              // Pull an event set from the head of the queue
             RtecEventComm.Event[] events = 
                (RtecEventComm.Event[]) queue_.dequeue_head ();

              for (int i = 0; i < events.length; ++i)
                {
                  if(events[i].header.type == 
                     PushConsumer.ACE_ES_EVENT_NOTIFICATION)
                    {
                      dataHandler_.update (events[i]);
                    }
                }
            }
          catch(org.omg.CORBA.SystemException e)
            {
              System.err.println(e);
            }
        }
    }
}