summaryrefslogtreecommitdiff
path: root/TAO/tests/Bug_3171_Regression/server.cpp
blob: 92c4e1532bbd4dcf9f2bf4904fdf0f8c2908c0c2 (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
#include "ace/Get_Opt.h"
#include "ace/Task.h"
#include "tao/corba.h"
#include "tao/Utils/ORB_Manager.h"
#include "tao/AnyTypeCode/Any.h"
#include "tao/Messaging/Messaging.h"
#include "tao/ORBInitializer_Registry.h"
#include "tao/PI/PI.h"
#include "tao/LocalObject.h"

int g_nthreads = 1;
bool g_setTimeout = true;
bool g_registerORBinitializer = true;
bool g_initInMain = false;
int g_argc;
ACE_TCHAR ** g_argv;

int initORB(int threadID);

int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("min:"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'm':
        g_initInMain = true;
        break;

      case 'i':
        g_registerORBinitializer = false;
        break;

      case 'n':
        g_nthreads = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-m (Initialize an ORB from the main thread first) "
                           "-i (Do not register ORB initializer) "
                           "-n <numberOfThreads>"
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates successful parsing of the command line
  return 0;
}

class Worker : public ACE_Task_Base
{
public:
  Worker ();

  virtual int svc ();

private:
};

class MyORBinitializer
  : public virtual PortableInterceptor::ORBInitializer
  , public virtual CORBA::LocalObject
{
public:
  MyORBinitializer( ACE_CString orbID )
    : orbID_( orbID ) {};

  virtual
  void
  pre_init( PortableInterceptor::ORBInitInfo_ptr )
  {
    ACE_DEBUG ((LM_DEBUG,
                "MyORBinitializer::pre_init() called for ORB \"%C\"\n",
                orbID_.c_str()));
  };

  virtual
  void
  post_init( PortableInterceptor::ORBInitInfo_ptr )
  {
    ACE_DEBUG ((LM_DEBUG,
                "MyORBinitializer::post_init() called for ORB \"%C\"\n",
                orbID_.c_str()));
  };

  private:
    ACE_CString orbID_;
};

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  g_argc = argc;
  g_argv = argv;

  try
  {
    if (parse_args (argc, argv) != 0)
      return 1;

    if ( g_initInMain )
    {
      initORB(0);
    }
//  Run in both main and background thread
//    else
    {
      Worker worker;
      if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
                           g_nthreads) != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot activate ORB thread(s)\n"),
                          1);

      worker.thr_mgr ()->wait ();

    }
    ACE_DEBUG ((LM_DEBUG, "Event loop finished\n"));
  }
  catch (const CORBA::Exception& ex)
  {
    ex._tao_print_exception ("Exception caught:");
    return 1;
  }

  return 0;
}

// ****************************************************************

Worker::Worker ()
{
}

int
Worker::svc (void)
{
  static int threadID = 0;

  initORB(++threadID);

  return 0;
}

int
initORB(int threadID)
{
  try
  {
    char ORBid[10];
    ACE_OS::sprintf (ORBid, "ORB_%d", threadID);

    ACE_DEBUG ((LM_DEBUG, "Initializing ORB \"%C\"\n", ORBid));

    if ( g_registerORBinitializer )
    {
      ACE_DEBUG ((LM_DEBUG, "Creating ORB initializer\n"));
      PortableInterceptor::ORBInitializer_var rCOI(
        new MyORBinitializer( ORBid ) );
      PortableInterceptor::register_orb_initializer( rCOI.in() );
    }

    ACE_DEBUG ((LM_DEBUG, "Creating TAO_ORB_Manager\n"));
    TAO_ORB_Manager* pORBmgr = new TAO_ORB_Manager;

    if ( -1 == pORBmgr->init( g_argc, g_argv, ORBid ) )
    {
      ACE_DEBUG ((LM_DEBUG, "Failed to initialize ORB \"%C\"\n", ORBid));
      throw CORBA::INTERNAL();
    }

    ACE_DEBUG ((LM_DEBUG, "ORB \"%C\" initialized\n", ORBid));

    if ( g_setTimeout )
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Setting connection timeout policy for ORB \"%C\"\n",
                  ORBid));

      CORBA::PolicyList policyList;

      CORBA::ORB_var orb = pORBmgr->orb();

      TimeBase::TimeT connectionTimeout = 100*10000;
      CORBA::Any any;
      any <<= connectionTimeout;

      CORBA::ULong l( policyList.length() );
      policyList.length( l+1 );

      policyList[l] = orb->create_policy(
        TAO::CONNECTION_TIMEOUT_POLICY_TYPE,
        any );

      ACE_DEBUG ((LM_DEBUG, "Connection timeout policy set for ORB \"%C\"\n",
                  ORBid));
    }

    pORBmgr->fini();

    delete pORBmgr;
  }
  catch (const CORBA::Exception& ex)
  {
    ACE_ERROR ((LM_ERROR, "Caught exception: %C\n", ex._info().c_str()));
    return 1;
  }

  return 0;
}