summaryrefslogtreecommitdiff
path: root/CIAO/ciao/Servant_Activator.cpp
blob: 284cfadf4350bca2413fdaf0d9465b04149aaaaa (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
#include "Servant_Activator.h"
#include "CIAO_common.h"
#include "Port_Activator.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_string.h"

ACE_RCSID (ciao,
           Servant_Activator,
           "$Id$")

namespace CIAO
{
  Servant_Activator::Servant_Activator (CORBA::ORB_ptr o)
    : orb_ (CORBA::ORB::_duplicate (o))
    // @@ TODO, avoid this magic number
    , pa_ (64)
    , slot_index_ (0)
  {
  }

  Servant_Activator::~Servant_Activator (void)
  {
    {
      ACE_GUARD (TAO_SYNCH_MUTEX,
                 guard,
                 this->mutex_);

      size_t const sz =  this->slot_index_;

      for (size_t t = 0; t != sz; ++t)
        {
          Port_Activator *&tmp = this->pa_[t];

          delete tmp;
        }
    }
  }

  bool
  Servant_Activator::update_port_activator (
    const PortableServer::ObjectId &oid)
  {
    CORBA::String_var str =
      PortableServer::ObjectId_to_string (oid);
    {
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                        guard,
                        this->mutex_,
                        0);
      size_t const sz = this->slot_index_;
      for (size_t t = 0; t != sz; ++t)
        {
          Port_Activator *&tmp = this->pa_[t];
          if (ACE_OS::strcmp (tmp->oid (), str.in ()) == 0)
            {
              tmp->oid ("dummy");
              //delete tmp;
              //--this->slot_index_;
            }
        }
    }
    return true;
  }

  PortableServer::Servant
  Servant_Activator::incarnate (const PortableServer::ObjectId &oid,
                                PortableServer::POA_ptr)
  {
    CORBA::String_var str =
      PortableServer::ObjectId_to_string (oid);

    if (CIAO::debug_level () > 9)
      ACE_DEBUG ((LM_DEBUG,
                  "CIAO (%P|%t) - Servant_Activator::incarnate, "
                  "activating port name [%s] \n",
                  str.in ()));

    {
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                        guard,
                        this->mutex_,
                        0);

      size_t const sz = this->slot_index_;

      Port_Activator *tmp = 0;

      for (size_t t = 0; t != sz; ++t)
        {
          if (this->pa_.get (tmp, t) == -1)
            throw CORBA::OBJECT_NOT_EXIST ();

          if (tmp == 0)
            {
              if (CIAO::debug_level () > 9)
                ACE_ERROR ((LM_ERROR,
                            "CIAO (%P|%t) - Servant_Activator::incarnate (),"
                            " value from the array is null \n"));
              continue;
            }
          if (ACE_OS::strcmp (tmp->oid (),
                              str.in ()) == 0)
            {
              // We should try avoiding making outbound calls with the
              // lock held. Oh well, let us get some sense of sanity in
              // CIAO to do think about these.
              if (CIAO::debug_level () > 5)
                ACE_DEBUG ((LM_DEBUG, "Activating Port %s\n",
                            str.in ()));

              return this->pa_[t]->activate (oid);
            }
        }
    }
    throw CORBA::OBJECT_NOT_EXIST ();
  }

  void
  Servant_Activator::etherealize (const PortableServer::ObjectId &oid,
                                  PortableServer::POA_ptr ,
                                  PortableServer::Servant servant,
                                  CORBA::Boolean ,
                                  CORBA::Boolean)
  {
    CORBA::String_var str =
      PortableServer::ObjectId_to_string (oid);

    size_t const sz = this->slot_index_;

    Port_Activator *tmp = 0;

    for (size_t t = 0; t != sz; ++t)
      {
        if (this->pa_.get (tmp, t) == -1)
          {
            ACE_DEBUG ((LM_DEBUG, "Could not get Port Activator\n"));
            continue;
          }

        if (tmp == 0)
          {
            ACE_DEBUG ((LM_DEBUG, "Port Activator is NULL\n"));
            continue;
          }
        if (ACE_OS::strcmp (tmp->oid (),
                            str.in ()) == 0)
          {
            ACE_DEBUG ((LM_DEBUG, "Deactivating Port %s\n",
                        str.in ()));
            this->pa_[t]->deactivate (servant);
          }
      }
  }

  bool
  Servant_Activator::register_port_activator (Port_Activator *pa)
  {
    ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                      guard,
                      this->mutex_,
                      false);

    // @@ TODO, need to implement a better algorithm here.
    //
    if (this->slot_index_ >= this->pa_.size ())
      this->pa_.size ((this->slot_index_ + 1));

    if (this->pa_.set (pa, this->slot_index_) == 0)
      {
        ++this->slot_index_;

        if (CIAO::debug_level () > 9)
          ACE_DEBUG ((LM_DEBUG,
                      "CIAO (%P|%t) - Servant_Activator::register_port_activator"
                      " with port name [%s],"
                      " the slot_index_ is [%d] \n",
                      pa->name (),
                      this->slot_index_));

        return true;
      }

    return false;
  }
}