summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Bug_2377_Regression/Hello.cpp
blob: 03cb59b88e1c2320566352f20e4b00236b4ba7b3 (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
//
// $Id$
//

#include "HelloS.h"

#include "ace/SString.h"
#include "ace/Log_Msg.h"
#include "ace/Task.h"
#include "ace/Mutex.h"
#include "ace/streams.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_unistd.h"
#include "tao/debug.h"
#include "tao/corba.h"
#include "orbsvcs/PortableGroup/MIOP.h"
#include "orbsvcs/PortableGroup/GOA.h"

#define CLIENT_SLEEP_TIME       100     /* in milliseconds */
#define NB_HELLO_CLIENT_THREAD  8
#define NB_HELLO_CALLS          100

void
sleep(int millisec)
{
    ACE_Time_Value tv(millisec / 1000, (millisec % 1000) * 1000);

    ACE_OS::sleep ((const ACE_Time_Value &) tv);
}

class MessageLog
{
  private:
    bool *sent;
    bool *recv;
    int extent;

  public:
    MessageLog(int num)
    {
        extent = num + 1;
        sent = new bool[extent];
        recv = new bool[extent];

        int i;

        for (i = 0; i < extent; i++)
        {
            sent[i] = false;
            recv[i] = false;
        }
    }

    void
    register_message_send(int message_num)
    {
        sent[message_num] = true;
    }

    void
    register_message_recv(int message_num)
    {
        recv[message_num] = true;
    }

    bool
    lost_messages()
    {
        bool lm = false;
        int i;

        for (i = 0; i < extent; i++)
        {
            if (sent[i] && !recv[i])
            {
                // message lost
                lm = true;
                break;
            }
        }
        return lm;
    }

    void
    report_lost_messages(const ACE_TCHAR *int_format_string)
    {
        int i;

        for (i = 0; i < extent; i++)
        {
            if (sent[i] && !recv[i])
            {
                ACE_DEBUG ((LM_DEBUG,
                            int_format_string,
                            i));
            }
        }
    }
};

namespace Test
{
    class Hello_impl
    : public virtual POA_Test::Hello
    {
      private:
        MessageLog *logger;
      public:
        Hello_impl(MessageLog *log)
        : logger(log)
        {
        }

        void say_hello(CORBA::Short count) throw(CORBA::SystemException)
        {
            logger->register_message_recv(count);
            if (TAO_debug_level == 0)
            {
                ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("R")));
            }
        }
    };
}

class OrbRunThread
: public ACE_Task_Base
{
  public:
    OrbRunThread(CORBA::ORB_ptr orb)
    : m_orb(CORBA::ORB::_duplicate(orb))
    {
    }
    virtual int svc()
    {
        m_orb->run();
        return 1;
    }
  private:
    CORBA::ORB_var m_orb;
};

class HelloClientThread
: public ACE_Task_Base
{
  public:
    HelloClientThread(Test::Hello_ptr hello, MessageLog *log)
    : m_hello(Test::Hello::_duplicate(hello)),
      logger(log),
      m_count(0)
    {
    }
    virtual int svc()
    {
        while (m_count < NB_HELLO_CALLS)
        {
            ACE_TRY_NEW_ENV
            {
                int count;
                {
                    // get your message number here
                    ACE_Guard < ACE_Mutex > guard(m_mutex);
                    count = m_count++;
                    if (TAO_debug_level == 0)
                    {
                        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("s")));
                    }
                }
                {
                    // send your message number here
#ifdef SEQUENCED_SAY_HELLO_REQUIRED
                    ACE_Guard < ACE_Mutex > guard(m_mutex);
#endif /* SEQUENCED_SAY_HELLO_REQUIRED */
                    logger->register_message_send(count);
                    m_hello->say_hello(count);
                }
                sleep(CLIENT_SLEEP_TIME);
            }
            ACE_CATCHANY
            {
                ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                     ACE_TEXT ("Exception thrown during say_hello()\n"));
            }
            ACE_ENDTRY;
        }
        return 1;
    }
  private:
    Test::Hello_var m_hello;
    MessageLog *logger;
    int m_count;
    ACE_Mutex m_mutex;
};

int
main(int argc, char *argv[])
{
    MessageLog *logger = 0;
    CORBA::ORB_var orb = CORBA::ORB::_nil();
    int exit_code = 0;

    OrbRunThread *orbThread = 0;
    Test::Hello_impl * hello_i = 0;

    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("<<< %s Started - \"s\" = message sent \"R\" = message received >>>\n"), argv[0]));

    ACE_TRY_NEW_ENV
    {
        /*** in svc.conf
dynamic UIPMC_Factory Service_Object * TAO_PortableGroup:_make_TAO_UIPMC_Protocol_Factory() ""
static Resource_Factory "-ORBProtocolFactory IIOP_Factory -ORBProtocolFactory UIPMC_Factory"
dynamic PortableGroup_Loader Service_Object * TAO_PortableGroup:_make_TAO_PortableGroup_Loader() ""
static Server_Strategy_Factory "-ORBConcurrency reactive -ORBPOALock thread -ORBAllowReactivationOfSystemids 1"
static Client_Strategy_Factory "-ORBProfileLock thread -ORBClientConnectionHandler MT"
         ***/

        // Initialize the ORB
        if (TAO_debug_level > 0)
        {
            ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("   - ORB_init\n")));
        }
        orb = CORBA::ORB_init(argc, argv);

        // Get the root POA
        CORBA::Object_var obj_root =
            orb->resolve_initial_references("RootPOA");
        PortableGroup::GOA_var rootPOA =
            PortableGroup::GOA::_narrow(obj_root.in());

        // Activate the POA manager
        if (TAO_debug_level > 0)
        {
            ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("   - POA manager activation\n")));
        }
        PortableServer::POAManager_var poaManager = rootPOA->the_POAManager();

        poaManager->activate();

        // create Message Log
        logger = new MessageLog(NB_HELLO_CALLS);

        // create Hello servant
        if (TAO_debug_level > 0)
        {
            ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("   - create Hello servant\n")));
        }
        hello_i = new Test::Hello_impl(logger);

        // activate Hello Object
        if (TAO_debug_level > 0)
        {
            ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("   - activate Hello object\n")));
        }
        PortableServer::ObjectId_var obj_id =
            rootPOA->activate_object(hello_i);

        // create MIOP reference
        if (TAO_debug_level > 0)
        {
            ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("   - create MIOP reference\n")));
        }
        ACE_CString multicast_addr("corbaloc:miop:1.0@1.0-cdmwftdomain-1/225.1.1.8:5555");
        CORBA::Object_var miop_ref =
            orb->string_to_object(multicast_addr.c_str());

        // associate MIOP reference with Hello object
        if (TAO_debug_level > 0)
        {
            ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("   - associate MIOP reference with Hello object\n")));
        }
        rootPOA->associate_reference_with_id(miop_ref.in(), obj_id.in ());

        // create Hello reference
        if (TAO_debug_level > 0)
        {
            ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("   - create Hello reference\n")));
        }
        Test::Hello_var hello = Test::Hello::_unchecked_narrow(miop_ref.in());

        // start ORB run() in a thread
        orbThread = new OrbRunThread(orb.in());
        orbThread->activate(THR_NEW_LWP, 1);

        // create client threads
        HelloClientThread *helloThread = new HelloClientThread(hello.in(),
                                                               logger);

        // start client threads
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("--------------------------------------------------\n")));
        helloThread->activate(THR_NEW_LWP, NB_HELLO_CLIENT_THREAD);
        helloThread->wait();
        delete helloThread;
    }
    ACE_CATCHANY
    {
        ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                             ACE_TEXT ("Exception thrown during main()\n"));
        exit_code = 1;
    }
    ACE_ENDTRY;

    ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("\n--------------------------------------------------\n")));
    if (logger->lost_messages())
    {
        logger->report_lost_messages(ACE_TEXT ("LOST ==> say_hello() %d\n"));
        exit_code = 1;
    }
    else if (exit_code == 0)
    {
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("<<< %s - no lost messages >>>\n"), argv[0]));
    }

    delete logger;

    if (orbThread != 0)
    {
        orb->shutdown(true);
        orbThread->wait();
        delete orbThread;
        delete hello_i;
    }
    if (!CORBA::is_nil(orb.in()))
    {
        ACE_TRY_NEW_ENV
        {
            orb->destroy();
        }
        ACE_CATCHANY
        {
            ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                         ACE_TEXT ("Exception thrown during orb check\n"));
            exit_code = 1;
        }
        ACE_ENDTRY;
    }
    exit(exit_code);
    return 0;
}