summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Notify_SequenceProxyPushSupplier_i.cpp
blob: 68af758ef3a5a55e4b585a8bc4fc05ec42e59a0e (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*- C++ -*- */
// $Id$

#include "Notify_SequenceProxyPushSupplier_i.h"
#include "Notify_ConsumerAdmin_i.h"
#include "Notify_StructuredEvents.h"
#include "Notify_Factory.h"
#include "Notify_Channel_Objects_Factory.h"

#include "tao/ORB_Core.h"
#include "tao/PortableServer/Object_Adapter.h"

ACE_RCSID(Notify, Notify_SequenceProxyPushSupplier_i, "$Id$")

typedef ACE_Reverse_Lock<ACE_Lock> TAO_Notify_Unlock;

TAO_Notify_SequenceProxyPushSupplier_i::TAO_Notify_SequenceProxyPushSupplier_i (TAO_Notify_ConsumerAdmin_i* consumer_admin)
  : proxy_inherited (consumer_admin),
    from_timeout_ (0),
    reset_timeout_flag_ (0),
    batch_lock_ (0)
{
}

TAO_Notify_SequenceProxyPushSupplier_i::~TAO_Notify_SequenceProxyPushSupplier_i (void)
{
  delete batch_lock_;
}

void
TAO_Notify_SequenceProxyPushSupplier_i::connect_sequence_push_consumer (CosNotifyComm::SequencePushConsumer_ptr push_consumer ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((
                   CORBA::SystemException,
                   CosEventChannelAdmin::AlreadyConnected,
                   CosEventChannelAdmin::TypeError
                   ))
{
  ACE_GUARD_THROW_EX (ACE_Lock, ace_mon, *this->lock_,
                      CORBA::INTERNAL ());
  ACE_CHECK;

  if (CORBA::is_nil (push_consumer))
    ACE_THROW (CosEventChannelAdmin::TypeError ());
  else if (this->is_connected_ == 1)
    ACE_THROW (CosEventChannelAdmin::AlreadyConnected ());
  else
    {
      this->push_consumer_ =
        CosNotifyComm::SequencePushConsumer::_duplicate (push_consumer);

      this->is_connected_ = 1;
    }

  ACE_TRY
    {
      TAO_Notify_Unlock reverse_lock (*this->lock_);

      {
        ACE_GUARD_THROW_EX (TAO_Notify_Unlock, ace_mon, reverse_lock,
                            CORBA::INTERNAL ());
        ACE_CHECK;

        this->on_connected (ACE_ENV_SINGLE_ARG_PARAMETER);
        ACE_TRY_CHECK;
      }
    }
  ACE_CATCHALL
    {
     this->push_consumer_ =
       CosNotifyComm::SequencePushConsumer::_nil ();
     ACE_RE_THROW;
    }
 ACE_ENDTRY;
}

void
TAO_Notify_SequenceProxyPushSupplier_i::dispatch_event_i (TAO_Notify_Event& events
                                                          ACE_ENV_ARG_DECL)
{
  // If we are in this method then we are a SequenceProxyPushSupplier
  // connected up to a SequenceProxyPushConsumer and the only thing
  // that events can actually be is a TAO_Notify_StructuredEvents object.

  ACE_TRY
    {
      // We only have a batch_lock_ if
      // the PacingInterval policy was used.
      if (this->batch_lock_ != 0)
        {
          // I don't use ACE_GUARD here, because I don't want
          // to block if I can't get this lock.  The timeout handler
          // in the same thread would cause a deadlock.
          ACE_Guard<ACE_Lock> ace_mon (*this->batch_lock_, 0);
          if (ace_mon.locked () == 0) return;

          // Only set this if we have successfully obtained the lock.
          // This way, if from_timeout_ is ever set to 1 it will
          // only be set back to 0 if do_push () is called when timeout_
          // is set to 1 (causing the cache to be flushed).
          this->reset_timeout_flag_ = 1;
          events.do_push (this->push_consumer_.in (),
                          this->qos_admin (),
                          this->event_cache_,
                          this->from_timeout_
                          ACE_ENV_ARG_PARAMETER);
        }
      else
        {
          events.do_push (this->push_consumer_.in (),
                          this->qos_admin (),
                          this->event_cache_,
                          this->from_timeout_
                          ACE_ENV_ARG_PARAMETER);
        }
      ACE_TRY_CHECK;
    }
  ACE_CATCHALL
    {
      ACE_DEBUG ((LM_DEBUG, "Exception dispatching structured event\n"));
      // misbehaving client, ignore for now.
    }
  ACE_ENDTRY;
}

void
TAO_Notify_SequenceProxyPushSupplier_i::dispatch_update_i (CosNotification::EventTypeSeq added, CosNotification::EventTypeSeq removed ACE_ENV_ARG_DECL)
{
  ACE_TRY
    {
      this->push_consumer_->offer_change (added, removed ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHALL
    {
      // misbehaving client, ignore for now.
    }
  ACE_ENDTRY;
}

void
TAO_Notify_SequenceProxyPushSupplier_i::disconnect_sequence_push_supplier(ACE_ENV_SINGLE_ARG_DECL)
  ACE_THROW_SPEC ((
                   CORBA::SystemException
                   ))
{
  this->on_disconnected (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  // ask our parent to deactivate us.
  this->consumer_admin_->
    deactivate_proxy_pushsupplier (this ACE_ENV_ARG_PARAMETER);
}


void
TAO_Notify_SequenceProxyPushSupplier_i::shutdown (ACE_ENV_SINGLE_ARG_DECL)
{
  // Tell the consumer that we're going away ...
  // @@ Later, lookup a "notify_on_disconnect" option.

  {
    ACE_GUARD (ACE_Lock, ace_mon, *this->lock_);

    if (this->is_connected_ == 0)
      return;
  }

  this->disconnect_sequence_push_supplier (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  ACE_TRY
    {
      this->push_consumer_->disconnect_sequence_push_consumer (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHALL
    {
      // ignore
    }
  ACE_ENDTRY;
}


void
TAO_Notify_SequenceProxyPushSupplier_i::setup_qos_policies (
                                          ACE_ENV_SINGLE_ARG_DECL)
{
# if defined (ACE_CONFIG_WIN32_H)
  ACE_Time_Value interval (
      ACE_static_cast (long, this->qos_admin ().pacing_interval ()));
# else
  ACE_Time_Value interval (this->qos_admin ().pacing_interval () / 1);
# endif /* ACE_CONFIG_WIN32_H */

  if (interval != ACE_Time_Value::zero)
    {
      // Only create the batch_lock_ if we are going to have a timer
      TAO_Notify_CO_Factory* cof =
        TAO_Notify_Factory::get_channel_objects_factory ();

      this->batch_lock_ = cof->create_proxy_supplier_lock (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      // Get back to the reactor to schedule the timer
      TAO_POA_Current_Impl *poa_current_impl =
        ACE_static_cast (TAO_POA_Current_Impl *,
                         TAO_TSS_RESOURCES::instance ()->poa_current_impl_);
      if (poa_current_impl != 0)
        {
          ACE_Reactor* reactor = poa_current_impl->orb_core ().reactor ();
          reactor->schedule_timer (this, 0, interval, interval);
        }
      else
        {
          ACE_ERROR ((LM_ERROR,
                      "(%P|%t) Unable to schedule "
                      "timer for the pacing interval\n"));
        }
    }
}


int
TAO_Notify_SequenceProxyPushSupplier_i::handle_timeout (
                                  const ACE_Time_Value& /*current_time*/,
                                  const void* /*act*/)
{

  ACE_TRY_NEW_ENV
    {
      // We don't need to bother if there is nothing in the cache
      if (this->event_cache_.length () > 0)
        {
          // We need a TAO_Notify_StructuredEvents when we call
          // dispatch_event ().  It doesn't matter that there's nothing
          // in it.
          CosNotification::EventBatch notifications;
          TAO_Notify_StructuredEvents dummy_events (notifications);

          // Set the flag saying that we are comming from the timeout.
          // The worst that could happen if this is set while
          // dispatch_event_i was called by the event processor is that
          // the event cache will be flushed a split second before it
          // normally would.
          this->from_timeout_ = 1;
          this->dispatch_event (dummy_events ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
    }
  ACE_CATCHALL
    {
      ACE_DEBUG ((LM_DEBUG, "Exception dispatching handle_timeout\n"));
      // We must eat the exception in this case
    }
  ACE_ENDTRY;

  if (this->reset_timeout_flag_)
    {
      this->from_timeout_ = 0;
      this->reset_timeout_flag_ = 0;
    }
  return 0;
}


#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class TAO_Notify_ProxySupplier<POA_CosNotifyChannelAdmin::SequenceProxyPushSupplier>;
template class TAO_Notify_Proxy<POA_CosNotifyChannelAdmin::SequenceProxyPushSupplier>;

#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate TAO_Notify_ProxySupplier<POA_CosNotifyChannelAdmin::SequenceProxyPushSupplier>
#pragma instantiate TAO_Notify_Proxy<POA_CosNotifyChannelAdmin::SequenceProxyPushSupplier>

#endif /*ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */