summaryrefslogtreecommitdiff
path: root/TAO/tests/UNKNOWN_Exception/server.cpp
blob: 0d5321adee3420acb0dadfcf1bbd0bd0adf31847 (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
// $Id$

#include "ace/Get_Opt.h"
#include "ace/Argv_Type_Converter.h"
#include "testS.h"
#include "tao/PortableServer/Root_POA.h"
#include "ace/OS_NS_stdio.h"

ACE_RCSID (UNKNOWN_Exception, server, "$Id$")

const char *ior_output_file = "ior";
static int done = 0;

void
throw_exception (void)
{
  throw 1;
}

class test_i :
  public POA_test
{
public:

  test_i (CORBA::ORB_ptr orb);

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

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

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

  void _add_ref (ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS);
  void _remove_ref (ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS);

  CORBA::ORB_var orb_;

  int reference_count_;
};

test_i::test_i (CORBA::ORB_ptr orb)
  : orb_ (CORBA::ORB::_duplicate (orb)),
    reference_count_ (1)
{
}

void
test_i::normal_method (void)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  ACE_DEBUG ((LM_DEBUG,
              "test_i::normal_method() called\n"));
}

void
test_i::unknown_exception_in_method (void)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  ACE_DEBUG ((LM_DEBUG,
              "test_i::unknown_exception_in_method() called\n"));

  ACE_DEBUG ((LM_DEBUG,
              "Unknown exception being generated: should be propagated to the client\n"));

  throw_exception ();
}

void
test_i::unknown_exception_during_deactivation (void)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  ACE_DEBUG ((LM_DEBUG,
              "test_i::unknown_exception_during_deactivation() called\n"));

  PortableServer::POA_var poa =
    this->_default_POA ();

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

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

void
test_i::_add_ref (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "test_i::_add_ref() called; current refcount = %d\n",
              this->reference_count_++));
}

void
test_i::_remove_ref (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "test_i::_remove_ref() called; current refcount = %d\n",
              this->reference_count_--));

  if (this->reference_count_ == 0)
    {
      delete this;

      ACE_DEBUG ((LM_DEBUG,
                  "Unknown exception being generated: should be gobbled up by the POA\n"));

      throw_exception ();
    }
}

class test_factory_i :
  public POA_test_factory
{
public:

  test_factory_i (CORBA::ORB_ptr orb);

  test_ptr create_test (void)
    ACE_THROW_SPEC ((CORBA::SystemException));

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

  CORBA::ORB_var orb_;
};

test_factory_i::test_factory_i (CORBA::ORB_ptr orb)
  : orb_ (CORBA::ORB::_duplicate (orb))
{
}

test_ptr
test_factory_i::create_test (void)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  test_i *servant =
    new test_i (this->orb_.in ());

  PortableServer::ServantBase_var safe_servant (servant);
  ACE_UNUSED_ARG (safe_servant);

  test_var test =
    servant->_this ();

  return test._retn ();
}

void
test_factory_i::shutdown (void)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  ACE_DEBUG ((LM_DEBUG,
              "factory_i::shutdown() called\n"));

  done = 1;
  this->orb_->shutdown (0);
}

int
parse_args (int argc, char *argv[])
{
  ACE_Get_Arg_Opt<char> get_opts (argc, argv, "o:");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'o':
        ior_output_file = get_opts.opt_arg ();
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "\nusage %s:\n"
                           "\t-o <ior output file> [defaults to %s]\n"
                           "\n",
                           argv [0],
                           ior_output_file),
                          -1);
      }

  return 0;
}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  ACE_Argv_Type_Converter convert (argc, argv);

  try
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (convert.get_argc(),
                         convert.get_ASCII_argv(),
                         "");

      CORBA::Object_var poa_object =
        orb->resolve_initial_references ("RootPOA");

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in ());

      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager ();

      if (parse_args (convert.get_argc(), convert.get_ASCII_argv()) != 0)
        return -1;

      {
        test_factory_i *servant =
          new test_factory_i (orb.in ());

        PortableServer::ServantBase_var safe_servant (servant);
        ACE_UNUSED_ARG (safe_servant);

        test_factory_var test_factory =
          servant->_this ();

        CORBA::String_var ior =
          orb->object_to_string (test_factory.in ());

        FILE *output_file = ACE_OS::fopen (ior_output_file, ACE_TEXT("w"));
        if (output_file == 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Cannot open output file for writing IOR: %s",
                             ior_output_file),
                            -1);
        ACE_OS::fprintf (output_file, "%s", ior.in ());
        ACE_OS::fclose (output_file);
      }

      poa_manager->activate ();

      TAO_Root_POA *tao_poa = dynamic_cast <TAO_Root_POA*> (root_poa.in ());

      while (!done)
        {
          CORBA::ULong outstanding_requests =
            tao_poa->outstanding_requests ();

          ACE_DEBUG ((LM_DEBUG,
                      "Number of outstanding requests before ORB::perform_work(): %d\n",
                      outstanding_requests));

          ACE_ASSERT (outstanding_requests == 0);

          orb->perform_work ();
        }
    }
  catch (...)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Failure: Unexpected exception caught\n"),
                        -1);
    }

  return 0;
}