summaryrefslogtreecommitdiff
path: root/trunk/TAO/tests/ORB_Local_Config/Bug_2612/DllOrb.cpp
blob: 2b90056d91714b6a654ac164f4991c6fa8776d60 (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
/*
 * @author Iliyan jeliazkov <iliyan@ociweb.com>
 * @author Lothar Werzinger <lothar@tradescape.biz>
 *
 * $Id$
 */


#include "ace/Arg_Shifter.h"
#include "ace/SString.h"
#include "ace/OS_NS_unistd.h"

#include "tao/corba.h"
#include "tao/TAO_Singleton_Manager.h"

#include "DllOrb.h"


DllOrb::DllOrb (int nthreads)
:
  m_nthreads_ (nthreads),
  m_failPrePostInit(0),
#if defined (ACE_HAS_THREADS)
  mp_barrier(0),
#endif
  mv_orb(),
  mv_rootPOA()
{
}

DllOrb::~DllOrb ( )
  throw ()
{
#if defined (ACE_HAS_THREADS)
  delete mp_barrier;
#endif
}


int DllOrb::init (int argc, char *argv[])
{
  int result = 0;
  int threadCnt = this->m_nthreads_;

  try
  {
    ACE_Arg_Shifter as(argc, argv);
    const ACE_TCHAR *currentArg = 0;
    while(as.is_anything_left())
      {
        if((currentArg = as.get_the_parameter("-t")))
          {
            int num = ACE_OS::atoi(currentArg);
            if(num >= 1)
              threadCnt = num;
            as.consume_arg();
          }
        else
          as.ignore_arg();
      }

    if (m_failPrePostInit < 3)
      {
        ACE_DEBUG((LM_DEBUG, "TEST (%P|%t) Pre-ORB initialization ...\n"));

        // -----------------------------------------------------------------
        // Pre-ORB initialization steps necessary for proper DLL ORB
        // support.
        // -----------------------------------------------------------------
        // Make sure TAO's singleton manager is initialized, and set to not
        // register itself with the ACE_Object_Manager since it is under the
        // control of the Service Configurator.  If we register with the
        // ACE_Object_Manager, then the ACE_Object_Manager will still hold
        // (dangling) references to instances of objects created by this
        // module and destroyed by this object when it is dynamically
        // unloaded.
        int register_with_object_manager = 0;
        TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance();
        result = p_tsm->init(register_with_object_manager);

        if (result == -1 && m_failPrePostInit == 0)
          return -1;
      }

    // Initialize the ORB
    mv_orb = CORBA::ORB_init(argc, argv);
    if (CORBA::is_nil(mv_orb.in()))
      return -1;

    CORBA::Object_var v_poa = mv_orb->resolve_initial_references("RootPOA");

    mv_rootPOA = PortableServer::POA::_narrow(v_poa.in ());
    if (CORBA::is_nil(mv_rootPOA.in()))
      return -1;

    mv_poaManager = mv_rootPOA->the_POAManager();
    if (CORBA::is_nil(mv_poaManager.in()))
      return -1;

    mv_poaManager->activate();
  }
  catch(CORBA::Exception& ex)
  {
    ex._tao_print_exception (ACE_TEXT ("(%P|%t) init failed:"));
    return -1;
  }
  catch(...)
  {
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%P|%t) init failed\n")),
                      -1);
  }

#if defined (ACE_HAS_THREADS)
  mp_barrier = new ACE_Thread_Barrier(threadCnt + 1);

  this->activate(
                 THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED,
                 threadCnt
                 );
  mp_barrier->wait();
#endif

  return 0;
}


int DllOrb::fini (void)
{
  int result;

  try
    {
#if !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO) && !defined (TAO_HAS_MINIMUM_POA)
      mv_poaManager->deactivate(1, 1);
#endif
      mv_poaManager = PortableServer::POAManager::_nil();

      // attempt to protect against sporadic BAD_INV_ORDER exceptions
      ACE_OS::sleep(ACE_Time_Value(0, 500));

      mv_orb->shutdown(1);
    }
  catch(CORBA::Exception& ex)
  {
    ex._tao_print_exception (ACE_TEXT ("(%P|%t) fini failed:"));
    return -1;
  }
  catch(...)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%P|%t) fini failed to deactivate/shutdown\n")),
                        -1);
    }


  // wait for our threads to finish
  wait();

#if defined (ACE_HAS_THREADS)
  delete mp_barrier;
  mp_barrier = 0;
#endif

  try
    {
      mv_orb->destroy();
      mv_orb = CORBA::ORB::_nil();
    }
  catch(CORBA::Exception& ex)
  {
    ex._tao_print_exception (
      ACE_TEXT (
        "(%P|%t) init failed to destroy the orb:"));
    return -1;
  }

  if (m_failPrePostInit < 3)
    {
      ACE_DEBUG((LM_DEBUG, "TEST (%P|%t) Post-ORB finalization ...\n"));

      // -----------------------------------------------------------------
      // Post-ORB finalization steps necessary for proper DLL ORB
      // support.
      // -----------------------------------------------------------------
      // Explicitly clean up singletons created by TAO before
      // unloading this module.
      TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance();
      result = p_tsm->fini();
      if (result == -1 && m_failPrePostInit == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("(%P|%t) fini failed to destroy TAO_Singleton_Manager\n")),
                          -1);
    } /* end of if */

  return 0;
}


int DllOrb::svc (void)
{
#if defined (ACE_HAS_THREADS)
  mp_barrier->wait();
#endif

  try
    {
      mv_orb->run();
    }
  catch(CORBA::BAD_INV_ORDER const & rc_ex)
    {
      const CORBA::ULong VMCID = rc_ex.minor() & 0xFFFFF000U;
      const CORBA::ULong minorCode = rc_ex.minor() & 0xFFFU;
      if (VMCID != CORBA::OMGVMCID || minorCode != 4)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("(%P|%t) svc exits (-1)\n")),
                          -1);
    }
  catch(CORBA::Exception& ex)
  {
    ex._tao_print_exception (ACE_TEXT ("(%P|%t) svc - orb->run() failed:"));
    return -1;
  }
  catch(...)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%P|%t) svc got some exception\n")),
                        -1);
    }
  return 0;
}


ACE_FACTORY_DEFINE (DllOrb, DllOrb)