summaryrefslogtreecommitdiff
path: root/modules/CIAO/connectors/AMI_CCM/AMI/AMI_exec.cpp
blob: a86cdf2998dcdf941b7460be9e45a523e276530e (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
341
342
343
344
345
346
347
348
349
// -*- C++ -*-
//
// $Id$

// ****  Code generated by the The ACE ORB (TAO) IDL Compiler v1.7.1 ****
// TAO and the TAO IDL Compiler have been developed by:
//       Center for Distributed Object Computing
//       Washington University
//       St. Louis, MO
//       USA
//       http://www.cs.wustl.edu/~schmidt/doc-center.html
// and
//       Distributed Object Computing Laboratory
//       University of California at Irvine
//       Irvine, CA
//       USA
//       http://doc.ece.uci.edu/
// and
//       Institute for Software Integrated Systems
//       Vanderbilt Universityaaa
//       Nashville, TN
//       USA
//       http://www.isis.vanderbilt.edu/
//
// Information about TAO is available at:
//     http://www.cs.wustl.edu/~schmidt/TAO.html

// TAO_IDL - Generated from
// be/be_codegen.cpp:1278

#include "AMI_exec.h"
#include "AMI_perform_work.h"
#include "AMI_CORBA_handler.h"
#include "ciao/CIAO_common.h"


namespace CIAO_Hello_AMI_AMI_Impl
{
#if !defined (AMI_CORBA_IMPLEMENTATION)
  //============================================================
  // Implementation of the basic form of AMI: a thread to handle
  // asynchronous invocations.
  //============================================================
  ami_handler::ami_handler (
    ::CCM_AMI::Cookie ck,
    const char * in_str,
    ::CCM_AMI::AMI_foo_ptr foo_receiver,
    ::CCM_AMI::AMI_foo_callback_ptr foo_callback) :
      ck_ (ck),
      in_str_ (in_str),
      foo_receiver_ (::CCM_AMI::AMI_foo::_duplicate (foo_receiver)),
      foo_callback_ (::CCM_AMI::AMI_foo_callback::_duplicate (foo_callback))
  {
  }

  int ami_handler::svc ()
  {
    char* out_str;
    long  result;
    printf ("AMI :\tThread started for cookie <%ld> sending string <%s>\n", ck_, in_str_);
    result = foo_receiver_->asynch_foo (CORBA::string_dup (in_str_), out_str);
    printf ("AMI :\tCookie <%ld> received : result <%ld> answer <%s>\n",
            ck_, result, out_str);
    foo_callback_->foo_callback_handler (ck_, result, CORBA::string_dup (out_str));

    return 0;
  }
#endif /* AMI_CORBA_IMPLEMENTATION */

  //============================================================
  // Implementation of the AMI CORBA reply handler
  //============================================================
  class AMI_CORBA_handler : public POA_INTERNAL_CCM_AMI::AMI_AMI_fooHandler
  {
  public:
    AMI_CORBA_handler (void)
      : foo_callback_ (0)
    {
    };

    void set_callback (::CCM_AMI::AMI_foo_callback_ptr foo_callback)
      {
        foo_callback_ = ::CCM_AMI::AMI_foo_callback::_duplicate (foo_callback);
      };

    void asynch_foo (CORBA::Long result,
              const char * out_str)
      {
        printf ("AMI CORBA :\tHandler::asynch_foo\n");
        foo_callback_->foo_callback_handler (1, result, CORBA::string_dup (out_str));
      };

    void asynch_foo_excep (::Messaging::ExceptionHolder * excep_holder)
      {
        printf ("AMI CORBA :\tHandler::foo_excep\n");

        try
          {
            excep_holder->raise_exception ();
          }
        catch (const CORBA::Exception& ex)
          {
            ex._tao_print_exception ("Caught exception:");
          }
      };

    ~AMI_CORBA_handler (void)
    {
    };
  private:
    ::CCM_AMI::AMI_foo_callback_var foo_callback_;
  };

  // ReplyHandler.
  AMI_CORBA_handler handler;

  //============================================================
  // Facet Executor Implementation Class: AMI_ami_foo_exec_i
  //============================================================

#if !defined (AMI_CORBA_IMPLEMENTATION)
  AMI_ami_foo_exec_i::AMI_ami_foo_exec_i (
    ::CCM_AMI::AMI_foo_ptr foo_receiver,
    ::CCM_AMI::AMI_foo_callback_ptr foo_callback) :
      foo_receiver_ (::CCM_AMI::AMI_foo::_duplicate (foo_receiver)),
      foo_callback_ (::CCM_AMI::AMI_foo_callback::_duplicate (foo_callback)),
      cookie_ (0)
  {
  }
#else
  AMI_ami_foo_exec_i::AMI_ami_foo_exec_i (
    ::CCM_AMI::AMI_foo_callback_ptr foo_callback) :
      foo_callback_ (::CCM_AMI::AMI_foo_callback::_duplicate (foo_callback)),
      cookie_ (0)
  {
    //initilize AMI client
    int argc = 2;
    ACE_TCHAR **argv = new ACE_TCHAR *[argc];
    argv[0] = ACE::strnew (ACE_TEXT (""));
    argv[1] = ACE::strnew (ACE_TEXT (""));
    orb_ = CORBA::ORB_init (argc, argv);

    CORBA::Object_var object =
      orb_->string_to_object ("file://server.ior");
    ami_foo_var_ = INTERNAL_CCM_AMI::AMI_foo::_narrow (object.in ());

    if (CORBA::is_nil (ami_foo_var_.in ()))
      {
        printf ("Server is NIL\n");
      }

    // Activate POA to handle the call back.
    CORBA::Object_var poa_object =
      orb_->resolve_initial_references("RootPOA");

    if (CORBA::is_nil (poa_object.in ()))
      printf ("POA is NIL!\n");

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

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

    poa_manager->activate ();

    printf ("Start the AMI CORBA handler\n");
    handler.set_callback (foo_callback);
    the_handler_var_ = handler._this (/* */);
  }
#endif /* AMI_CORBA_IMPLEMENTATION */

  AMI_ami_foo_exec_i::~AMI_ami_foo_exec_i (void)
  {
  }

  // Operations from ::CCM_AMI::AMI_ami_foo

  ::CCM_AMI::Cookie
  AMI_ami_foo_exec_i::sendc_asynch_foo (
    const char * in_str)
  {
    printf ("AMI :\tsendc_asynch_foo <%s>\n", in_str);
    ::CCM_AMI::Cookie ck = ++cookie_;
#if !defined (AMI_CORBA_IMPLEMENTATION)
    //single thread to perform asynchronous actions
    printf ("AMI :\tReceived string <%s> for <%d>\n", in_str, ck);
    ami_handler* ah = new ami_handler (ck, in_str, foo_receiver_, foo_callback_);
    ah->activate ();
#else
    //AMI CORBA implementation.
    printf ("AMI :\tSending string <%s> for cookie <%d> to AMI CORBA server\n", in_str, ck);
    ami_foo_var_->sendc_asynch_foo (the_handler_var_.in (), in_str);
#endif /* AMI_CORBA_IMPLEMENTATION */
    return ck;
  }
  
  //============================================================
  // Component Executor Implementation Class: AMI_exec_i
  //============================================================
  
  AMI_exec_i::AMI_exec_i (void)
  {
  }
  
  AMI_exec_i::~AMI_exec_i (void)
  {
  }
  
  // Supported operations and attributes.
  
  // Component attributes.
  
  // Port operations.
  
  ::CCM_AMI::CCM_AMI_ami_foo_ptr
  AMI_exec_i::get_perform_asynch_foo (void)
  {
    ::CCM_AMI::AMI_foo_callback_var foo_callback =
      this->context_->get_connection_callback_foo ();

#if !defined (AMI_CORBA_IMPLEMENTATION)
    ::CCM_AMI::AMI_foo_var receiver_foo =
      this->context_->get_connection_receiver_foo ();
    return new AMI_ami_foo_exec_i (receiver_foo, foo_callback);
#else
    return new AMI_ami_foo_exec_i (foo_callback);
#endif /* AMI_CORBA_IMPLEMENTATION */
  }
  
  // Operations from Components::SessionComponent.
  
  void
  AMI_exec_i::set_session_context (
    ::Components::SessionContext_ptr ctx)
  {
    this->context_ =
      ::Hello_AMI::CCM_AMI_Context::_narrow (ctx);
    
    if ( ::CORBA::is_nil (this->context_.in ()))
      {
        throw ::CORBA::INTERNAL ();
      }
  }
  
  void
  AMI_exec_i::configuration_complete (void)
  {
    /* Your code here. */
  }
  
  void
  AMI_exec_i::ccm_activate (void)
  {
    ::CCM_AMI::AMI_foo_var receiver_foo =
      this->context_->get_connection_receiver_foo ();

    printf ("AMI :\tCCM_ACTIVATE\n");
    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);
    AMI_server *srv = new AMI_server (orb.in (), receiver_foo);
    AMI_perform_work *pw = new AMI_perform_work (orb.in ());
    printf ("AMI :\tStarting server and CORBA thread.\n");
    srv->activate ();
    pw->activate ();
  }
  
  void
  AMI_exec_i::ccm_passivate (void)
  {
    /* Your code here. */
  }
  
  void
  AMI_exec_i::ccm_remove (void)
  {
    /* Your code here. */
  }
  
  extern "C"  ::Components::EnterpriseComponent_ptr
  create_Hello_AMI_AMI_Impl (void)
  {
    ::Components::EnterpriseComponent_ptr retval =
      ::Components::EnterpriseComponent::_nil ();
    
    ACE_NEW_RETURN (
      retval,
      AMI_exec_i,
      ::Components::EnterpriseComponent::_nil ());
    
    return retval;
  }
}

namespace CIAO_Hello_AMI_AMI_Impl
{
  //============================================================
  // Home Executor Implementation Class: AMIHome_exec_i
  //============================================================
  
  AMIHome_exec_i::AMIHome_exec_i (void)
  {
  }
  
  AMIHome_exec_i::~AMIHome_exec_i (void)
  {
  }
  
  // All operations and attributes.
  
  // Factory operations.
  
  // Finder operations.
  
  // Implicit operations.
  
  ::Components::EnterpriseComponent_ptr
  AMIHome_exec_i::create (void)
  {
    ::Components::EnterpriseComponent_ptr retval =
      ::Components::EnterpriseComponent::_nil ();
    
    ACE_NEW_THROW_EX (
      retval,
      AMI_exec_i,
      ::CORBA::NO_MEMORY ());
    
    return retval;
  }
  
  extern "C"  ::Components::HomeExecutorBase_ptr
  create_Hello_AMI_AMIHome_Impl (void)
  {
    ::Components::HomeExecutorBase_ptr retval =
      ::Components::HomeExecutorBase::_nil ();
    
    ACE_NEW_RETURN (
      retval,
      AMIHome_exec_i,
      ::Components::HomeExecutorBase::_nil ());
    
    return retval;
  }
}