summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/ImplRepo_Service/AsyncStartupWaiter_i.cpp
blob: 2cebbc590bdbbaf1ab9e4d6ac497eb02b50bb6cd (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
// -*- C++ -*-
//
// $Id$

#include "AsyncStartupWaiter_i.h"
#include "ImR_Locator_i.h"

using namespace ImplementationRepository;

AsyncStartupWaiter_i::PendingData::PendingData (const char* p, const char* i)
: partial_ior (p)
, ior( i)
{
}

AsyncStartupWaiter_i::PendingData::PendingData ()
{
}


void AsyncStartupWaiter_i::debug (bool dbg)
{
  debug_ = dbg;
}

void AsyncStartupWaiter_i::wait_for_startup (AMH_AsyncStartupWaiterResponseHandler_ptr rh,
   const char* name ACE_ENV_ARG_DECL_NOT_USED) ACE_THROW_SPEC ((CORBA::SystemException))
{
  PendingListPtr plst;
  pending_.find(name, plst);
  if (! plst.null () && plst->size () > 0)
    {
      PendingList& tmp = *plst;
      PendingData& pd = tmp[tmp.size () - 1];
      tmp.pop_back ();

      if (debug_)
        ACE_DEBUG ((LM_DEBUG, "ImR: Skipping wait due to queued startup info for <%s>.\n", name));

      send_response (*rh, name, pd.partial_ior.c_str(), pd.ior.c_str());

    }
  else
    {
      RHListPtr lst;
      waiting_.find (name, lst);
      if (lst.null ())
      {
        lst = RHListPtr (new RHList);
        int err = waiting_.bind (name, lst);
        ACE_ASSERT (err == 0);
        ACE_UNUSED_ARG (err);
      }
      lst->push_back (AMH_AsyncStartupWaiterResponseHandler::_duplicate (rh));
    }
}

void
AsyncStartupWaiter_i::send_response (ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler& rh,
                                    const char* name, const char* partial_ior, const char* ior)
{
  StartupInfo_var si = new StartupInfo();
  si->name = name;
  si->partial_ior = partial_ior;
  si->ior = ior;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      rh.wait_for_startup (si.in () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      if (debug_)
        ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "AsyncStartupWaiter_i::send_response ()");
    }
  ACE_ENDTRY;
}

void
AsyncStartupWaiter_i::unblock_one (const char* name, const char* partial_ior, const char* ior, bool queue) {
  ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler_var rh = get_one_waiter(name);
  if (! CORBA::is_nil(rh.in ()))
    {
      send_response (*rh.in (), name, partial_ior, ior);
    }
  else if (queue)
    {
      if (debug_)
        ACE_DEBUG((LM_DEBUG, "ImR: Queuing startup info.\n"));

      PendingListPtr lst;
      pending_.find (name, lst);
      if (lst.null ())
        {
          lst = PendingListPtr (new PendingList);
          int err = pending_.bind (name, lst);
          ACE_ASSERT (err == 0);
          ACE_UNUSED_ARG (err);
        }
      lst->push_back (PendingData (partial_ior, ior));
    }
}

void
AsyncStartupWaiter_i::unblock_all (const char* name) {
  RHList tmp;

  get_all_waiters (name, tmp);

  // This startup info should be ignored when unblocking all, because we
  // don't know the ior or partial_ior at this point.
  StartupInfo_var si = new StartupInfo ();
  si->name = name;

  // Note : This method may be called when there are no waiters.

  for (size_t i = 0; i < tmp.size(); ++i)
  {
    ACE_DECLARE_NEW_CORBA_ENV;
    ACE_TRY
      {
        ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler_var& rh = tmp[i];

        rh->wait_for_startup (si.in () ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;
      }
    ACE_CATCHANY
      {
        if (debug_)
          ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "AsyncStartupWaiter_i::unblock_all ()");
      }
    ACE_ENDTRY;
  }
}

ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler_ptr
AsyncStartupWaiter_i::get_one_waiter (const char* name)
{
  RHListPtr lst;
  waiting_.find (name, lst);
  if (! lst.null() && lst->size () > 0)
    {
      RHList& rhlst = *lst;
      ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler_var& tmp = rhlst[rhlst.size () - 1];
      ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler_ptr ret = tmp._retn ();
      rhlst.pop_back ();
      return ret;
    }
  return ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler::_nil ();
}

void
AsyncStartupWaiter_i::get_all_waiters (const char* name, RHList& ret)
{
  RHListPtr lst;
  waiting_.find (name, lst);
  if (! lst.null ()) {
    for (size_t i = 0; i < lst->size (); ++i)
      {
        RHList& tmp = *lst;
        ret.push_back (tmp[i]);
        // The ACE_Vector will not destruct the elements when cleared, so we must
        // make sure to do so here.
        tmp[i] = ImplementationRepository::AMH_AsyncStartupWaiterResponseHandler::_nil ();
      }
    lst->clear ();
  }
}