summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/Object_Reactivation/Object_Reactivation.cpp
blob: 98a931f25e03c2d5ce214515cdb6c7d8faf13997 (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
// $Id$

//========================================================================
//
// = LIBRARY
//     TAO/tests/POA/Object_Reactivation
//
// = FILENAME
//     Object_Reactivation.cpp
//
// = DESCRIPTION
//     This program tests the reactivation of a servant that has been
//     deactivated but not removed from the Active Object Map yet.
//
// = AUTHOR
//     Irfan Pyarali
//
//=========================================================================

#include "testS.h"
#include "ace/Task.h"
#include "ace/Get_Opt.h"
#include "ace/SString.h"
#include "ace/Auto_Event.h"
#include "ace/OS_NS_unistd.h"

static int debug = 1;

static int
parse_args (int argc, char **argv)
{
  ACE_Get_Opt get_opts (argc, argv, "d:");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':
        debug = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-d debug "
                           "\n",
                           argv [0]),
                          -1);
      }

  return 0;
}

class test_i : public POA_test
{
public:
  test_i (ACE_Auto_Event &event);

  void deactivate_self (void)
    ACE_THROW_SPEC ((CORBA::SystemException));

private:
  ACE_Auto_Event &event_;
};

test_i::test_i (ACE_Auto_Event &event)
  : event_ (event)
{
}

void
test_i::deactivate_self (void)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  PortableServer::POA_var poa = this->_default_POA ();

  PortableServer::ObjectId_var id = poa->servant_to_id (this);

  if (debug)
    ACE_DEBUG ((LM_DEBUG, "(%t) Deactivating servant\n"));

  poa->deactivate_object (id.in ());

  if (debug)
    ACE_DEBUG ((LM_DEBUG, "(%t) Deactivation complete: signaling main thread and going to sleep\n"));

  int result = this->event_.signal ();
  ACE_ASSERT (result == 0);
  ACE_UNUSED_ARG (result);

  ACE_OS::sleep (3);

  if (debug)
    ACE_DEBUG ((LM_DEBUG, "(%t) test_i::deactivate_self complete\n"));
}

class Activator : public ACE_Task_Base
{
public:
  Activator (test_ptr t,
             ACE_Auto_Event &event,
             PortableServer::POA_ptr poa,
             PortableServer::Servant servant,
             const ACE_CString &task_id,
             const PortableServer::ObjectId &id);
  int svc (void);

private:
  test_var test_;
  ACE_Auto_Event &event_;
  PortableServer::POA_var poa_;
  PortableServer::Servant servant_;
  ACE_CString task_id_;
  PortableServer::ObjectId id_;
};

Activator::Activator (test_ptr t,
                      ACE_Auto_Event &event,
                      PortableServer::POA_ptr poa,
                      PortableServer::Servant servant,
                      const ACE_CString &task_id,
                      const PortableServer::ObjectId &id)
  : test_ (test::_duplicate (t)),
    event_ (event),
    poa_ (PortableServer::POA::_duplicate (poa)),
    servant_ (servant),
    task_id_ (task_id),
    id_ (id)
{
}

int
Activator::svc (void)
{
  if (debug)
    ACE_DEBUG ((LM_DEBUG, "(%t) Waiting for deactivation to complete\n"));

  int result = this->event_.wait ();
  ACE_ASSERT (result == 0);
  ACE_UNUSED_ARG (result);

  if (debug)
    ACE_DEBUG ((LM_DEBUG, "(%t) Deactivation complete, trying to activate\n"));

  try
    {
      if (this->task_id_ == "first thread")
        {
          PortableServer::ObjectId_var id =
            this->poa_->activate_object (this->servant_);
        }
      else
        {
          this->poa_->activate_object_with_id (this->id_,
                                               this->servant_);
        }

      if (debug)
        ACE_DEBUG ((LM_DEBUG, "(%t) Activation complete\n"));
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Activator::svc");
      return -1;
    }
  return 0;
}

class Deactivator : public ACE_Task_Base
{
public:
  Deactivator (test_ptr t);
  int svc (void);

private:
  test_var test_;
};

Deactivator::Deactivator (test_ptr t)
  : test_ (test::_duplicate (t))
{
}

int
Deactivator::svc (void)
{
  try
    {
      this->test_->deactivate_self ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Deactivator::svc");
      return -1;
    }
  return 0;
}

int
main (int argc, char **argv)
{

  try
    {
      // Initialize the ORB first.
      CORBA::ORB_var orb = CORBA::ORB_init (argc,
                                            argv,
                                            0);

      int parse_args_result =
        parse_args (argc, argv);

      if (parse_args_result != 0)
        return parse_args_result;

      // Obtain the RootPOA.
      CORBA::Object_var obj =
        orb->resolve_initial_references ("RootPOA");

      // Get the POA_var object from Object_var.
      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (obj.in ());

      // Get the POAManager of the RootPOA.
      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager ();

      poa_manager->activate ();

      ACE_Auto_Event event1;
      test_i servant1 (event1);

      ACE_Auto_Event event2;
      test_i servant2 (event2);

      test_var test_object1 = servant1._this ();

      test_var test_object2 = servant2._this ();

      PortableServer::ObjectId_var id1 =
        root_poa->reference_to_id (test_object1.in ());

      PortableServer::ObjectId_var id2 =
        root_poa->reference_to_id (test_object2.in ());

      Activator activator1 (test_object1.in (),
                            event1,
                            root_poa.in (),
                            &servant1,
                            "first thread",
                            id1.in ());

      Activator activator2 (test_object2.in (),
                            event2,
                            root_poa.in (),
                            &servant2,
                            "second thread",
                            id2.in ());

      Deactivator deactivator1 (test_object1.in ());

      Deactivator deactivator2 (test_object2.in ());

      if (activator1.activate () != 0 ||
          activator2.activate () != 0 ||
          deactivator1.activate () != 0 ||
          deactivator2.activate () != 0)
        return -1;

      int result = ACE_Thread_Manager::instance ()->wait ();

      if (result != 0)
        return result;

      root_poa->destroy (1,
                         1);
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught");
      return -1;
    }

  return 0;
}