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

#include "AMI_MyInterface_i.h"

namespace CCM_CORBA_AMI_MyInterface_Impl
{
  //============================================================
  // Implementation of the AMI CORBA INTERFACE reply handler
  //============================================================
  AMI_MyInterface_reply_handler::AMI_MyInterface_reply_handler (
    ::CCM_AMI::AMI_MyInterface_callback_ptr interface_callback)
    : interface_callback_ (::CCM_AMI::AMI_MyInterface_callback::_duplicate (interface_callback))
  {
  }
    
  AMI_MyInterface_reply_handler::~AMI_MyInterface_reply_handler ()
  {
  }
    
  void
  AMI_MyInterface_reply_handler::do_something_with_something (
    CORBA::Float result)
  {
    printf ("AMI CORBA :\tMyInterface Reply Handler::do_something_with_something\n");
    interface_callback_->do_something_with_something_callback_handler (result);
    this->_remove_ref ();
  }

  void
  AMI_MyInterface_reply_handler::do_something_with_something_excep (
    ::Messaging::ExceptionHolder * excep_holder)
  {
    printf ("AMI CORBA :\tMyInterface Reply Handler::do_something_with_something_excep\n");

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

      interface_callback_->do_something_with_something_callback_excep (ex.ex);

      if (ex.ex.id != 42)
      {
        printf ("ERROR (INTERFACE) :\tReceived unexpected ID received in exception handler\n");
      }
      if (ACE_OS::strcmp (ex.ex.error_string.in (), "Hello world") != 0)
      {
        printf ("ERROR (INTERFACE) :\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_MyInterface_i::AMI_MyInterface_i (CORBA::ORB_ptr orb, ::CCM_AMI::MyInterface_ptr interface_receiver)
    : orb_ (CORBA::ORB::_duplicate (orb)),
      interface_receiver_ (::CCM_AMI::MyInterface::_duplicate (interface_receiver))
  {
  }

  CORBA::Float
  AMI_MyInterface_i::do_something_with_something (CORBA::Short something)
  {
    try
      {
        printf ("AMI CORBA (INTERFACE) :\tReceived short <%d>. Try passing it to the Receiver component\n", something);
        CORBA::Float result = interface_receiver_->do_something_with_something (something);
        return result;
      }
    catch (CCM_AMI::InternalError& ex)
      {
        printf ("AMI CORBA (INTERFACE) :\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 (INTERFACE) :\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_MyInterface_server::CORBA_MyInterface_server (::CCM_AMI::MyInterface_ptr interface_receiver)
    : interface_receiver_ (::CCM_AMI::MyInterface::_duplicate (interface_receiver))
  {
  }

  int CORBA_MyInterface_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_MyInterface_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_MyInterface_i AMI_MyInterface_i (orb.in (), interface_receiver_);

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

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

      CCM_AMI::MyInterface_var ami_interface_var =
          CCM_AMI::MyInterface::_narrow (object.in ());

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

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

      poa_manager->activate ();

      printf ("AMI CORBA (INTERFACE) :\tServer is activated\n");

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

    return 0;
  }
}