summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/ImplRepo_Service/AsyncListManager.cpp
blob: 959e2879ebfca37426ab96900fa3e8bd0666d163 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// -*- C++ -*-
#include "AsyncListManager.h"
#include "Iterator.h"
#include "Locator_Repository.h"
#include "ImR_Locator_i.h"

#include "orbsvcs/Log_Macros.h"

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

AsyncListManager::AsyncListManager (const Locator_Repository *repo,
                                    PortableServer::POA_ptr poa,
                                    LiveCheck *pinger)
  :repo_ (repo),
   poa_ (PortableServer::POA::_duplicate (poa)),
   primary_ (ImplementationRepository::AMH_AdministrationResponseHandler::_nil ()),
   secondary_ (ImplementationRepository::AMH_ServerInformationIteratorResponseHandler::_nil ()),
   pinger_ (pinger),
   server_list_ (0),
   first_ (0),
   how_many_ (0),
   waiters_ (0),
   refcount_ (1),
   lock_ ()
{
}

AsyncListManager::~AsyncListManager (void)
{
  if (ImR_Locator_i::debug() > 4)
    {
      ORBSVCS_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) AsyncListManager(%@)::dtor\n"),
                      this));
    }
}

PortableServer::POA_ptr
AsyncListManager::poa (void)
{
  return PortableServer::POA::_duplicate (this->poa_.in());
}

void
AsyncListManager::init_list (void)
{
  CORBA::ULong const len =
    static_cast<CORBA::ULong> (this->repo_->servers ().current_size ());
  Locator_Repository::SIMap::ENTRY* entry = 0;
  Locator_Repository::SIMap::CONST_ITERATOR it (this->repo_->servers ());
  this->server_list_.length (len);
  this->waiters_ = 0;
  for (CORBA::ULong i = 0; i < len; i++)
    {
      it.next (entry);
      it.advance ();

      Server_Info_Ptr info = entry->int_id_;
      info->setImRInfo (&this->server_list_[i]);
      if (this->pinger_ != 0)
        {
          ListLiveListener *l = 0;
          ACE_NEW (l, ListLiveListener (info->ping_id (),
                                        info->pid,
                                        i,
                                        this,
                                        *this->pinger_));

          LiveListener_ptr llp (l);
          if (!l->start ())
            {
              this->server_list_[i].activeStatus =
                ImplementationRepository::ACTIVE_NO;
              l->cancel ();
            }
          else
            {
              if (!evaluate_status (i, l->status(), info->pid))
                {
                  this->waiters_++;
                }
              else
                {
                  l->cancel ();
                }
            }
        }
     }

  if (ImR_Locator_i::debug() > 4)
    {
      ORBSVCS_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) AsyncListManager(%@)::init_list, <%d> waiters")
                      ACE_TEXT (" out of <%d> registered servers\n"),
                      this, this->waiters_, len));
    }
}

bool
AsyncListManager::make_iterator (ImplementationRepository::ServerInformationIterator_out si, CORBA::ULong start)
{
  si = ImplementationRepository::ServerInformationIterator::_nil ();
  try
    {
      ImR_AsyncIterator* imr_iter = 0;
      ACE_NEW_THROW_EX (imr_iter,
                        ImR_AsyncIterator (start, this),
                        CORBA::NO_MEMORY ());

      PortableServer::ServantBase_var tmp (imr_iter);

      PortableServer::ObjectId_var id =
        this->poa_->activate_object (imr_iter);
      CORBA::Object_var obj = this->poa_->id_to_reference (id.in ());
      si = ImplementationRepository::
        ServerInformationIterator::_unchecked_narrow (obj.in ());
      return true;
    }
  catch (const CORBA::SystemException& ex)
    {
      ex._tao_print_exception ("AsyncListManager:final state constructing iterator\n");
      ImplementationRepository::AMH_AdministrationExceptionHolder h (ex._tao_duplicate());
      try
        {
          this->primary_->list_excep (&h);
        }
      catch (const CORBA::Exception& ex2)
        {
          ex2._tao_print_exception ("AsyncListManager:final calling list_excep\n");
        }
    }
  catch (const CORBA::UserException& ex)
    {
      ex._tao_print_exception ("AsyncListManager:final state constructing iterator\n");
      ImplementationRepository::AMH_AdministrationExceptionHolder h (new CORBA::INTERNAL);
      try
        {
          this->primary_->list_excep (&h);
        }
      catch (const CORBA::Exception& ex2)
        {
          ex2._tao_print_exception ("AsyncListManager:final calling list_excep\n");
        }
    }
  return false;
}

void
AsyncListManager::final_state (void)
{
  if (ImR_Locator_i::debug() > 4)
    {
      ORBSVCS_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) AsyncListManager(%@)::final_state, ")
                      ACE_TEXT ("waiters count = %d, has pinger? %d\n"),
                      this, this->waiters_, (this->pinger_ != 0)));
    }

  if (this->pinger_ != 0 && this->waiters_ > 0)
    {
      return;
    }

  bool excepted = false;
  CORBA::ULong const len = this->server_list_.length ();
  ImplementationRepository::ServerInformationList alt_list (this->how_many_);
  ImplementationRepository::ServerInformationList *sil = &this->server_list_;
  if (this->first_ > 0 || this->how_many_ < len)
    {
      alt_list.length (this->how_many_);
      for (CORBA::ULong i = 0; i < this->how_many_; i++)
        {
          alt_list[i] = this->server_list_[i + this->first_];
        }
      sil = &alt_list;
    }

  if (!CORBA::is_nil (this->primary_.in ()))
    {
      ImplementationRepository::ServerInformationIterator_var server_iterator;
      if (sil !=  &this->server_list_)
        {
          excepted = !this->make_iterator (server_iterator.out(), this->how_many_);
        }
      else
        {
          server_iterator =
            ImplementationRepository::ServerInformationIterator::_nil ();
        }

      if (!excepted)
        {
          try
            {
              this->primary_->list (*sil, server_iterator.in ());
            }
          catch (const CORBA::Exception &ex)
            {
              ex._tao_print_exception ("AsyncListManager:final state sending list\n");
              ImplementationRepository::AMH_AdministrationExceptionHolder h (ex._tao_duplicate());
              this->primary_->list_excep (&h);
            }
        }
      this->primary_ =
        ImplementationRepository::AMH_AdministrationResponseHandler::_nil ();
    }
  else if (!CORBA::is_nil (this->secondary_.in()))
    {
      CORBA::Boolean done = this->first_ + sil->length() == len;
      try
        {
          this->secondary_->next_n (done, *sil);
        }
      catch (const CORBA::Exception &ex)
        {
          ex._tao_print_exception ("AsyncListManager:final state sending secondary list\n");
          ImplementationRepository::AMH_ServerInformationIteratorExceptionHolder h (ex._tao_duplicate());
          this->secondary_->next_n_excep (&h);
        }
      this->secondary_ =
        ImplementationRepository::AMH_ServerInformationIteratorResponseHandler::_nil ();
    }
}

CORBA::ULong
AsyncListManager::list
(ImplementationRepository::AMH_ServerInformationIteratorResponseHandler_ptr _tao_rh,
 CORBA::ULong start,
 CORBA::ULong count)
{
  this->secondary_ =
    ImplementationRepository::AMH_ServerInformationIteratorResponseHandler::_duplicate (_tao_rh);
  this->list_i (start, count);
  return this->first_ + this->how_many_;
}

void
AsyncListManager::list
(ImplementationRepository::AMH_AdministrationResponseHandler_ptr _tao_rh,
 CORBA::ULong count)
{
  this->primary_ =
    ImplementationRepository::AMH_AdministrationResponseHandler::_duplicate (_tao_rh);
  this->list_i (0, count);
}

void
AsyncListManager::list_i (CORBA::ULong start, CORBA::ULong count)
{
  if (this->server_list_.length () == 0)
    {
      this->init_list ();
    }

  this->first_ = start;
  this->how_many_ = this->server_list_.length () - start;
  if (start > 0 || (count > 0 && count < this->how_many_))
    {
      if (count > 0 && count < this->how_many_)
        {
          this->how_many_ = count;
        }
    }

  if (this->waiters_ == 0)
    {
      this->final_state ();
    }
}

bool
AsyncListManager::evaluate_status (CORBA::ULong index, LiveStatus status, int pid)
{
  bool is_final = true;
  switch (status)
    {
    case LS_ALIVE:
    case LS_LAST_TRANSIENT:
      this->server_list_[index].activeStatus =
        ImplementationRepository::ACTIVE_YES;
      break;
    case LS_TIMEDOUT:
      this->server_list_[index].activeStatus =
        ImplementationRepository::ACTIVE_MAYBE;
      break;
    case LS_DEAD:
      this->server_list_[index].activeStatus = (pid == 0) ?
        ImplementationRepository::ACTIVE_NO : ImplementationRepository::ACTIVE_MAYBE;
      break;
    default:
      is_final = false;
    }
  return is_final;
}

void
AsyncListManager::ping_replied (CORBA::ULong index, LiveStatus status, int pid)
{
  if (ImR_Locator_i::debug() > 4)
    {
      ORBSVCS_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) AsyncListManager(%@)::ping_replied, index <%d> ")
                      ACE_TEXT ("status <%C> server pid <%d> waiters <%d>\n"),
                      this,index, LiveEntry::status_name (status), pid, this->waiters_));
    }
  if (evaluate_status (index, status, pid))
    {
      if (--this->waiters_ == 0)
        {
          this->final_state ();
        }
    }
}

AsyncListManager *
AsyncListManager::_add_ref (void)
{
  ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, mon, this->lock_, 0);
  ++this->refcount_;
  return this;
}

void
AsyncListManager::_remove_ref (void)
{
  int count = 0;
  {
    ACE_GUARD (TAO_SYNCH_MUTEX, mon, this->lock_);
    count = --this->refcount_;
  }
  if (count == 0)
    {
      delete this;
    }
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

ListLiveListener::ListLiveListener (const char *server,
                                    int pid,
                                    CORBA::ULong index,
                                    AsyncListManager *owner,
                                    LiveCheck &pinger)
  :LiveListener (server),
   owner_ (owner->_add_ref ()),
   pinger_ (pinger),
   status_ (LS_UNKNOWN),
   index_ (index),
   started_ (false),
   pid_ (pid)
{
}

ListLiveListener::~ListLiveListener (void)
{
}

bool
ListLiveListener::start (void)
{
  bool rtn = this->pinger_.add_poll_listener (this);
  this->started_ = true;
  return rtn;
}

LiveStatus
ListLiveListener::status (void)
{
  return this->status_;
}

void
ListLiveListener::cancel (void)
{
  this->pinger_.remove_listener (this);
}

bool
ListLiveListener::status_changed (LiveStatus status)
{
  this->status_ = status;
  if (status == LS_TRANSIENT)
    {
      return false;
    }
  else
    {
      if (this->started_)
        this->owner_->ping_replied (this->index_, status, this->pid_);
    }
  return true;
}