summaryrefslogtreecommitdiff
path: root/TAO/tests/Bug_3674_Regression/test.cpp
blob: d506290225fc7c690a8350f2187844abdc2980d7 (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
// $Id$

#include "tao/ORB_Core.h"
#include "tao/ORBInitializer_Registry.h"
#include "tao/PI/PI.h"
#include "tao/PI_Server/PI_Server.h"
#include "tao/PI/ORBInitInfo.h"

#include "testS.h"

ACE_RCSID (PICurrent,
           test,
           "test.cpp,v 1.7 2003/11/01 11:15:11 dhinton Exp")

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4250)
#endif /* _MSC_VER */

namespace
{
  //------------------------------------------------------------------
  // For simplicity most of the main variables are available GLOBALLY.
  int Result= 0;
  CORBA::ORB_var orb;
  PortableInterceptor::Current_var PICurrent;
  PortableInterceptor::SlotId slot_id= 2093843221;
  testInterface_var server;

  void isCorrect (const CORBA::Any &data, const CORBA::Long correct, const ACE_TCHAR *const ID)
    {
      CORBA::Long actual= 0;
      if (data >>= actual)
        if (correct == actual)
          ACE_DEBUG ((LM_DEBUG,
                      ". %s retrieved CORRECT long (%d) from the TSC.\n",
                      ID, actual));
        else
          {
            ACE_DEBUG ((LM_DEBUG,
                        ". %s retrieved **INCORRECT** long (%d should be %d) from TSC.\n",
                        ID, actual, correct));
            Result= 1;
          }
      else
        {
           ACE_DEBUG ((LM_DEBUG,
                       ". %s can't retrieved long from the TSC.\n",
                       ID));
            Result= 1;
        }
    }

  //------------------------------------------------------------------
  // Need either a Client OR Server OR BOTH Request Interceptors
  // registered for the use of PICurrent to make sense in the application.
  struct ClientRequestInterceptor
    : public virtual PortableInterceptor::ClientRequestInterceptor,
      public virtual CORBA::LocalObject
  {
    /// Constructor.
    ClientRequestInterceptor () {}

    /// Return the name of this ClientRequestInterceptor.
    virtual char * name ()
      {
        return CORBA::string_dup ("ClientRequestInterceptor");
      }

    virtual void destroy () {}

    virtual void send_request (
      PortableInterceptor::ClientRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". CLIENT Interception, send_request\n"));
      }

    virtual void send_poll (
      PortableInterceptor::ClientRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". CLIENT Interception, send_poll\n"));
      }

    virtual void receive_reply (
      PortableInterceptor::ClientRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". CLIENT Interception, receive_reply\n"));
      }

    virtual void receive_exception (
      PortableInterceptor::ClientRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". CLIENT Interception, receive_exception\n"));
      }

    virtual void receive_other (
      PortableInterceptor::ClientRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". CLIENT Interception, receive_other\n"));
      }
  };

  //------------------------------------------------------------------
  // This is our server side request interceptor.
  // Need either a Client OR Server OR BOTH Request Interceptors
  // registered for the use of PICurrent to make sense in the application.
  struct ServerRequestInterceptor
    : public virtual PortableInterceptor::ServerRequestInterceptor,
      public virtual CORBA::LocalObject
    {
      /// Constructor.
      ServerRequestInterceptor () {}

      /// Return the name of this ServerRequestinterceptor.
      virtual char * name ()
      {
        return CORBA::string_dup ("ServerRequestInterceptor");
      }

      virtual void destroy () {}

    #if TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1
      virtual void tao_ft_interception_point (
          PortableInterceptor::ServerRequestInfo_ptr,
          CORBA::OctetSeq_out)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". SERVER Interception, tao_ft_interception_point\n"));
      }
    #endif // TAO_HAS_EXTENDED_FT_INTERCEPTORS == 1

      virtual void receive_request_service_contexts (
          PortableInterceptor::ServerRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". SERVER Interception, receive_request_service_contexts\n"));
      }

      virtual void receive_request (
          PortableInterceptor::ServerRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". SERVER Interception, receive_request\n"));
      }

      virtual void send_reply (
          PortableInterceptor::ServerRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". SERVER Interception, send_reply\n"));
      }

      virtual void send_exception (
          PortableInterceptor::ServerRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". SERVER Interception, send_exception\n"));
      }

      virtual void send_other (
          PortableInterceptor::ServerRequestInfo_ptr)
      {
        ACE_DEBUG ((LM_DEBUG,
                   ". SERVER Interception, send_other\n"));
      }
    };

  //------------------------------------------------------------------
  // This is the test ORB initializer that registers the IORInterceptor
  // for this test.
  class MyORBInitializer :
    public virtual PortableInterceptor::ORBInitializer,
    public virtual CORBA::LocalObject
    {
    public:
      ~MyORBInitializer() {slot_id= 2093843221;} // Invalidate slot
      /// The pre-initialization hook.
      virtual void pre_init (
        PortableInterceptor::ORBInitInfo_ptr)
      {}

      /// The post-initialization hook.
      virtual void post_init (
        PortableInterceptor::ORBInitInfo_ptr info)
      {
        CORBA::Object_var
          obj= info->resolve_initial_references ("PICurrent");
        PICurrent= PortableInterceptor::Current::_narrow (obj.in ());
        if (CORBA::is_nil (PICurrent.in ()))
          {
            ACE_ERROR ((LM_ERROR,
                        "ERROR: Could not resolve PICurrent object.\n"));
            throw ::CORBA::INTERNAL ();
          }
        slot_id= info->allocate_slot_id ();

        // Need either a Client OR Server OR BOTH Request Interceptors
        // Registered for the use of PICurrent to make sense in the application.
        PortableInterceptor::ClientRequestInterceptor_ptr client_tmp;
        ACE_NEW_THROW_EX (client_tmp,
                          ClientRequestInterceptor (),
                          CORBA::NO_MEMORY (
                            CORBA::SystemException::_tao_minor_code (
                              TAO::VMCID,
                              ENOMEM),
                            CORBA::COMPLETED_NO));
        PortableInterceptor::ClientRequestInterceptor_var
          client_interceptor= client_tmp;
        info->add_client_request_interceptor (client_interceptor.in ());

        // Need either a Client OR Server OR BOTH Request Interceptors
        // Registered for the use of PICurrent to make sense in the application.
        PortableInterceptor::ServerRequestInterceptor_ptr server_tmp;
        ACE_NEW_THROW_EX (server_tmp,
                          ServerRequestInterceptor,
                          CORBA::NO_MEMORY (
                            CORBA::SystemException::_tao_minor_code (
                              TAO::VMCID,
                              ENOMEM),
                            CORBA::COMPLETED_NO));
        PortableInterceptor::ServerRequestInterceptor_var
          server_interceptor= server_tmp;
        info->add_server_request_interceptor (server_interceptor.in ());

        // Disable collocation identification -- TAO-specific!!!
        // The fix needs to work even if collocation is not optomized for.
        TAO_ORBInitInfo_var
          tao_info= TAO_ORBInitInfo::_narrow (info);
        tao_info->orb_core ()->optimize_collocation_objects (0);
      }
    };

  //------------------------------------------------------------------
  // This is the implimentation of our test server.
  class testInterface_i : public virtual POA_testInterface
    {
    public:
      /// Constructor & Destructor.
      testInterface_i () {}
      ~testInterface_i () {}
      /// Main servant test methods.
      virtual void firstCalling ()
        {
          // Insert some data into the allocated PICurrent slot BEFORE we start.
          CORBA::Any data;
          CORBA::Long number = 2;  // Different from the client's data
          data <<= number;
          PICurrent.in()->set_slot (slot_id, data);
          ACE_DEBUG ((LM_DEBUG,
                      ". SERVER firstCalling() stored long (%d) to TSC.\n",
                      number));
          // Perform the next nested collocated call.
          server->secondCalling();
          // Check that the data in the PICurrent slot has not been corrupted.
          CORBA::Any_var data2= PICurrent.in()->get_slot (slot_id);
          isCorrect (data2.in (), 2, ACE_TEXT ("SERVER firstCalling()"));
        }
      virtual void secondCalling ()
        {
          // Insert some data into the allocated PICurrent slot.
          CORBA::Any data;
          CORBA::Long number = 3;  // Different from other server's data
          data <<= number;
          PICurrent.in()->set_slot (slot_id, data);
          ACE_DEBUG ((LM_DEBUG,
                      ". SERVER secondCalling() stored long (%d) to TSC.\n",
                      number));
        }
    };
}

#if defined(_MSC_VER)
#pragma warning(pop)
#endif /* _MSC_VER */

//------------------------------------------------------------------
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  try
    {
      // Initialise the ORBInitializer for server side interception points
      PortableInterceptor::ORBInitializer_ptr
        temp_initializer= PortableInterceptor::ORBInitializer::_nil ();
      ACE_NEW_RETURN (temp_initializer,
                      MyORBInitializer,
                      -1);  // No CORBA exceptions yet we haven't yet ORB_init!
      PortableInterceptor::ORBInitializer_var
        orb_initializer= temp_initializer;
      PortableInterceptor::register_orb_initializer (orb_initializer.in ());

      // Start up the ORB and start the POAManager
      struct guardORB
        {
          guardORB(int argc, ACE_TCHAR *argv[])
            {orb= CORBA::ORB_init (argc, argv, "test_orb");}
          ~guardORB() {PICurrent= 0; orb= 0;}
        } guardORB_(argc, argv);

      CORBA::Object_var
        obj= orb->resolve_initial_references ("RootPOA");
      PortableServer::POA_var
        RootPOA= PortableServer::POA::_narrow (obj.in ());
      if (CORBA::is_nil (RootPOA.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to obtain RootPOA reference.\n"),
                          -1);
      PortableServer::POAManager_var
        POAManager= RootPOA->the_POAManager ();
      if (CORBA::is_nil (POAManager.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to obtain POAManager reference.\n"),
                          -1);
      POAManager->activate ();

      // OK now start our collocated client/server and activate it on the default POA
      testInterface_i *testInt;
      ACE_NEW_THROW_EX (testInt,
                         testInterface_i,
                         CORBA::NO_MEMORY (
                           CORBA::SystemException::_tao_minor_code (
                             TAO::VMCID,
                             ENOMEM),
                         CORBA::COMPLETED_NO));
      PortableServer::ServantBase_var
        server_impl= testInt;
      obj= testInt->_this ();
      struct serverGuard
        {
          serverGuard(CORBA::Object_ptr obj)
            {server= testInterface::_narrow (obj);}
          ~serverGuard() {server= 0;}
        } serverGuard_ (obj.in ());
      if (CORBA::is_nil (server.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to obtain reference to testInterface object.\n"),
                          -1);

      ACE_DEBUG ((LM_DEBUG, "Start of actual regression test...\n"));
      // Insert some data into the allocated PICurrent slot BEFORE we start.
      CORBA::Any data;
      CORBA::Long number = 1;
      data <<= number;
      PICurrent.in ()->set_slot (slot_id, data);
      ACE_DEBUG ((LM_DEBUG,
                  ". CLIENT stored long (%d) to TSC.\n",
                  number));
      // Perform the collocated call.
      server->firstCalling();
      // Check that the data in the PICurrent slot has not been corrupted.
      CORBA::Any_var data2= PICurrent.in ()->get_slot (slot_id);
      isCorrect (data2.in (), 1, ACE_TEXT ("CLIENT"));
      ACE_DEBUG ((LM_DEBUG, "End of actual regression test...\n"));

      RootPOA->destroy (1, 1);
      orb->destroy ();
    }
  catch (const ::CORBA::Exception &ex)
    {
      ex._tao_print_exception ("Corba exception");
      Result= 1;
    }
  catch (...)
    {
      ACE_DEBUG ((LM_DEBUG, "Unknown exception caught\n"));
      Result= 1;
    }

  ACE_DEBUG ((LM_DEBUG,
              "Test %s\n",
              (Result ?
               ACE_TEXT ("***FAILURE***") :
               ACE_TEXT ("Success"))));
  return Result;
}