summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/EC_Config/Consumer.cpp
blob: 8acacceeea6b7f54deb870f8f5b1250e5a63f9b8 (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
180
181
182
183
184
185
186
187
188
189
190
// $Id$

#include "Consumer.h"

#include <sstream> //for ostringstream

#include "ace/Thread.h"
#include "orbsvcs/Event_Utilities.h" //for ACE_Supplier/ConsumerQOS_Factory
#include "orbsvcs/RtecSchedulerC.h"

ACE_RCSID(EC_Examples, Consumer, "$Id$")

Consumer::Consumer (void)
  : _consumer(this)
  , _consumer_id(-1)
{
}

Consumer::~Consumer(void)
{
}

void
Consumer::connect (RtecScheduler::Scheduler_ptr scheduler,
                   const char *entry_prefix,
                   int consumer_id, //unique identifier
                   long event_type,
                   RtecEventChannelAdmin::EventChannel_ptr ec
                   ACE_ENV_ARG_DECL)
{
  this->connect_impl(false,
                     scheduler,
                     entry_prefix,
                     consumer_id,
                     event_type,
                     0, //period; ignored
                     RtecScheduler::VERY_LOW_IMPORTANCE, //ignored
                     RtecScheduler::VERY_LOW_CRITICALITY, //ignored
                     ec
                     ACE_ENV_ARG_PARAMETER);
}

void
Consumer::connect (RtecScheduler::Scheduler_ptr scheduler,
                   const char *entry_prefix,
                   int consumer_id, //unique identifier
                   long event_type,
                   TimeBase::TimeT period,
                   RtecScheduler::Importance_t importance,
                   RtecScheduler::Criticality_t criticality,
                   RtecEventChannelAdmin::EventChannel_ptr ec
                   ACE_ENV_ARG_DECL)
{
  this->connect_impl(true,
                     scheduler,
                     entry_prefix,
                     consumer_id,
                     event_type,
                     period,
                     importance,
                     criticality,
                     ec
                     ACE_ENV_ARG_PARAMETER);
}

void
Consumer::connect_impl (bool set_rtinfo, //true if should set RT_Info
                        RtecScheduler::Scheduler_ptr scheduler,
                        const char *entry_prefix,
                        int consumer_id, //unique identifier
                        long event_type,
                        TimeBase::TimeT period,
                        RtecScheduler::Importance_t importance,
                        RtecScheduler::Criticality_t criticality,
                        RtecEventChannelAdmin::EventChannel_ptr ec
                        ACE_ENV_ARG_DECL)
{
  this->_consumer_id = consumer_id;

  //create consumer RT_Info
  std::ostringstream cons_entry_pt;
  cons_entry_pt << entry_prefix; //unique RT_Info entry point
  ACE_DEBUG((LM_DEBUG,"Creating %s\n",cons_entry_pt.str().c_str()));
  RtecScheduler::handle_t rt_info = scheduler->create (cons_entry_pt.str().c_str()
                                                       ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
  if (set_rtinfo)
    {
      scheduler->set (rt_info,
                      criticality,
                      period, period, period,
                      period,
                      importance,
                      period,
                      0,
                      RtecScheduler::OPERATION
                      ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
      ACE_DEBUG((LM_DEBUG,"Set Consumer %d RT_Info\n",this->_consumer_id));
    } else
    {
      ACE_DEBUG((LM_DEBUG,"NOT Set Consumer %d RT_Info\n",this->_consumer_id));
    }

  // Register as consumer of appropriate event type
  ACE_ConsumerQOS_Factory consQoS;
  consQoS.insert_type(event_type,
                      rt_info);

  RtecEventChannelAdmin::ConsumerAdmin_var consumer_admin =
    ec->for_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->_supplier_proxy =
    consumer_admin->obtain_push_supplier (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  RtecEventComm::PushConsumer_var consumerv =
    this->_consumer._this (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->_supplier_proxy->connect_push_consumer (consumerv.in (),
                                                consQoS.get_ConsumerQOS ()
                                                ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  ACE_DEBUG((LM_DEBUG,"Consumer %d connected\n",this->_consumer_id));
  ACE_DEBUG((LM_DEBUG,"\tEvent type: %d\n",event_type));
}

void
Consumer::disconnect (ACE_ENV_SINGLE_ARG_DECL)
{
  //disconnect consumer

  if (! CORBA::is_nil (this->_supplier_proxy.in()))
    {
      this->_supplier_proxy->disconnect_push_supplier (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      this->_supplier_proxy = RtecEventChannelAdmin::ProxyPushSupplier::_nil();

      //Deactivate the servant
      PortableServer::POA_var poa =
        this->_consumer._default_POA(ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
      PortableServer::ObjectId_var id =
        poa->servant_to_id (&this->_consumer ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
      poa->deactivate_object(id.in() ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
      ACE_DEBUG((LM_DEBUG,"Consumer %d disconnected\n",this->_consumer_id));
    } else
    {
      ACE_DEBUG((LM_DEBUG,"Cannot disconnect; Consumer %d not connected!\n",this->_consumer_id));
    }
}

void
Consumer::push (const RtecEventComm::EventSet& events
                ACE_ENV_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
  if (events.length () == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Consumer (%P|%t) no events\n"));
      return;
    }

  int prio = -1;
  ACE_hthread_t handle;
  ACE_Thread::self(handle);
  ACE_Thread::getprio(handle,prio);
  //ACE_thread_t tid = ACE_Thread::self();
  ACE_DEBUG ((LM_DEBUG, "Consumer #%d @%d (%P|%t) we received event type %d\n",
              this->_consumer_id,prio,events[0].header.type));
}

void
Consumer::disconnect_push_consumer (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
}

// ****************************************************************

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */