summaryrefslogtreecommitdiff
path: root/modules/CIAO/connectors/ami4ccm/examples/AMI/AMI_MyFoo_i.cpp
blob: 5bab675fbfd7b05dedfbb00e51c2a061867cef06 (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
// $Id$

#include "AMI_MyFoo_i.h"

namespace CCM_CORBA_AMI_MyFoo_Impl
{
  //============================================================
  // Implementation of the AMI CORBA FOO reply handler
  //============================================================
  AMI_MyFoo_reply_handler::AMI_MyFoo_reply_handler (::CCM_AMI::AMI_MyFoo_callback_ptr foo_callback)
  : foo_callback_ (::CCM_AMI::AMI_MyFoo_callback::_duplicate (foo_callback))
  {
  }

  AMI_MyFoo_reply_handler::~AMI_MyFoo_reply_handler ()
  {
  }

  // FOO methods
  void
  AMI_MyFoo_reply_handler::foo (
    CORBA::Long result,
    const char * out_str)
  {
    printf ("AMI CORBA (FOO) :\tMyFoo Foo Reply Handler::foo\n");
    foo_callback_->foo_callback_handler (result, CORBA::string_dup (out_str));
    this->_remove_ref ();
  }

  void
  AMI_MyFoo_reply_handler::foo_excep (
    ::Messaging::ExceptionHolder * excep_holder)
  {
    printf ("AMI CORBA (FOO) :\tMyFoo Foo Reply Handler::foo_excep\n");

    try
    {
      excep_holder->raise_exception ();
    }
    catch (const CCM_AMI::InternalError& ex)
    {
      printf ("AMI CORBA (FOO) :\tCaught the correct exception type (CCM_AMI::InternalError) <%d> <%s>\n",
              ex.ex.id, ex.ex.error_string.in ());

      foo_callback_->foo_callback_excep (ex.ex);

      if (ex.ex.id != 42)
      {
        printf ("ERROR (FOO):\tReceived unexpected ID received in exception handler\n");
      }
      if (ACE_OS::strcmp (ex.ex.error_string.in (), "Hello world") != 0)
      {
        printf ("ERROR (FOO):\tReceived unexpected error string received in exception handler\n");
      }
    }
    catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("ERROR (FOO) :\tCaught the WRONG exception:");
    }
    this->_remove_ref ();
  }
  
  // HELLO methods
  void
  AMI_MyFoo_reply_handler::hello (
    CORBA::Long answer)
  {
    printf ("AMI CORBA (FOO) :\tMyFoo Hello Reply Handler::foo\n");
    foo_callback_->hello_callback_handler (answer);
    this->_remove_ref ();
  }

  void
  AMI_MyFoo_reply_handler::hello_excep (
    ::Messaging::ExceptionHolder * excep_holder)
  {
    printf ("AMI CORBA (FOO) :\tMyFoo Hello Reply Handler::foo_excep\n");

    try
      {
        excep_holder->raise_exception ();
      }
    catch (const CCM_AMI::InternalError& ex)
      {
        printf ("AMI CORBA (FOO) :\tCaught the correct exception type (CCM_AMI::InternalError) <%d> <%s>\n",
                ex.ex.id, ex.ex.error_string.in ());

        foo_callback_->hello_callback_excep (ex.ex);

        if (ex.ex.id != 42)
          {
            printf ("ERROR (FOO):\tReceived unexpected ID received in exception handler\n");
          }
        if (ACE_OS::strcmp (ex.ex.error_string.in (), "Hello world") != 0)
          {
            printf ("ERROR (FOO):\tReceived unexpected error string received in exception handler\n");
          }
      }
    catch (const CORBA::Exception& ex)
      {
        ex._tao_print_exception ("ERROR (FOO) :\tCaught the WRONG exception:");
      }
    this->_remove_ref ();
  }

  //============================================================
  // Implementation of the AMI CORBA FOO interface
  //============================================================
  AMI_MyFoo_i::AMI_MyFoo_i (CORBA::ORB_ptr orb, ::CCM_AMI::MyFoo_ptr foo_receiver)
    : orb_ (CORBA::ORB::_duplicate (orb)),
      foo_receiver_ (::CCM_AMI::MyFoo::_duplicate (foo_receiver))
  {
  }

  CORBA::Long
  AMI_MyFoo_i::foo (const char * in_str,
                  CORBA::String_out out_str)
  {
    try
      {
        printf ("AMI CORBA (FOO) :\tReceived string <%s>. Try passing it to the Receiver component\n", in_str);
        CORBA::Long result = foo_receiver_->foo (CORBA::string_dup (in_str), out_str);
        return result;
      }
    catch (CCM_AMI::InternalError& ex)
      {
        printf ("AMI CORBA (FOO) :\tCORRECT EXCEPTION -> re-throwing\n");
        CCM_AMI::InternalException excep;
        excep.id = ex.ex.id;
        excep.error_string = CORBA::string_dup (ex.ex.error_string);
        throw CCM_AMI::InternalError (excep);
      }
    catch (...)
    {
        printf ("AMI CORBA (FOO) :\t!!!!!UNKNOWN EXCEPTION!!!!!\n");
        CCM_AMI::InternalException excep;
        excep.id = 43;
        excep.error_string = CORBA::string_dup ("UNKNOWN");
        throw CCM_AMI::InternalError (excep);
    }
  }

  void
  AMI_MyFoo_i::hello (CORBA::Long_out answer)
  {
    try
      {
        printf ("AMI CORBA (FOO) :\tHello. Try calling the Receiver component\n");
        foo_receiver_->hello (answer);
      }
    catch (CCM_AMI::InternalError& ex)
      {
        printf ("AMI CORBA (FOO) :\tCORRECT EXCEPTION -> re-throwing\n");
        CCM_AMI::InternalException excep;
        excep.id = ex.ex.id;
        excep.error_string = CORBA::string_dup (ex.ex.error_string);
        throw CCM_AMI::InternalError (excep);
      }
    catch (...)
    {
        printf ("AMI CORBA (FOO) :\t!!!!!UNKNOWN EXCEPTION!!!!!\n");
        CCM_AMI::InternalException excep;
        excep.id = 43;
        excep.error_string = CORBA::string_dup ("UNKNOWN");
        throw CCM_AMI::InternalError (excep);
    }
  }

  //============================================================
  // Worker thread to service the AMI CORBA FOO interface
  //============================================================
  CORBA_MyFoo_server::CORBA_MyFoo_server (::CCM_AMI::MyFoo_ptr foo_receiver)
    : foo_receiver_ (::CCM_AMI::MyFoo::_duplicate (foo_receiver))
  {
  }

  int CORBA_MyFoo_server::svc ()
  {
    try
    {
      int argc = 2;
      ACE_TCHAR **argv = new ACE_TCHAR *[argc];
      argv[0] = ACE::strnew (ACE_TEXT (""));
      argv[1] = ACE::strnew (ACE_TEXT (""));
      CORBA::ORB_var orb =
          CORBA::ORB_init (argc, argv, ACE_TEXT ("CORBA_MyFoo_server"));

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

      if (CORBA::is_nil (poa_object.in ()))
      {
        printf ("Server :\tERROR creating POA \n");
        return 1;
      }
      PortableServer::POA_var root_poa =
          PortableServer::POA::_narrow (poa_object.in ());

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

      AMI_MyFoo_i AMI_MyFoo_i (orb.in (), foo_receiver_);

      PortableServer::ObjectId_var id =
          root_poa->activate_object (&AMI_MyFoo_i);

      CORBA::Object_var object = root_poa->id_to_reference (id.in ());

      CCM_AMI::MyFoo_var ami_foo_var =
          CCM_AMI::MyFoo::_narrow (object.in ());

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

        // If the ior_output_file exists, output the ior to it
      FILE *output_file= ACE_OS::fopen ("foo.ior", "w");
      if (output_file == 0)
      {
        printf ("Cannot open output file for writing IOR: server.ior\n");
        return 1;
      }
      ACE_OS::fprintf (output_file, "%s", ior.in ());
      ACE_OS::fclose (output_file);

      poa_manager->activate ();

      printf ("AMI CORBA (FOO) :\tFOO Server is activated\n");

      orb->run ();
    }
    catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Caught exception:");
      return 1;
    }

    return 0;
  }
}