summaryrefslogtreecommitdiff
path: root/CIAO/connectors/ami4ccm/tests/UsesMulti/Sender/UsesMulti_Sender_exec.cpp
blob: 64159e36be83b24a535753bb48d9b2e866939cd3 (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
// -*- C++ -*-
// $Id$

// test use multiple porttypes.
#include "UsesMulti_Sender_exec.h"
#include "ace/OS_NS_unistd.h"

namespace CIAO_UsesMulti_Sender_Impl
{
  // Keeps the messages received back.
  Atomic_UShort nr_of_received = 0;
  // Keeps the messages on the go.
  Atomic_UShort nr_of_sent = 0;
  CORBA::Boolean asynch = false;
  //============================================================
  // Facet Executor Implementation Class: One_callback_exec_i
  //============================================================
  One_callback_exec_i::One_callback_exec_i (void)
  {
  }

  One_callback_exec_i::~One_callback_exec_i (void)
  {
  }
  //============================================================
  // Operations from ::CCM_AMI::One_callback
  //============================================================
  // FOO methods
  void
  One_callback_exec_i::foo (
    ::CORBA::Long /*ami_return_val*/,
    const char * answer)
  {
    ACE_DEBUG ((LM_DEBUG, "Sender: Get asynchronous callback from foo,"
                           " answer = <%C>\n",
                           answer));
     ++nr_of_received;
     --nr_of_sent;
  }

  void
  One_callback_exec_i::foo_excep (
      ::CCM_AMI::ExceptionHolder * excep_holder)
  {
    excep_holder->raise_exception ();
  }
  //============================================================
  // Worker thread for asynchronous invocations for One
  //============================================================
  asynch_foo_generator::asynch_foo_generator (
    ::UsesMulti::CCM_Sender_Context_ptr context)
  : context_(::UsesMulti::CCM_Sender_Context::_duplicate (context))
  {
  }

  int asynch_foo_generator::svc ()
  {
    ::UsesMulti::Sender::sendc_run_my_um_oneConnections_var  my_one_ami_ =
      context_->get_connections_sendc_run_my_um_one();

    // Invoke Asynchronous calls to test
    for (CORBA::ULong i = 0; i < my_one_ami_->length (); ++i)
      {
        CORBA::String_var test;
        switch (i)
          {
            case 0:
              test = CORBA::string_dup ("Asynch. call one.");
              break;
            case 1:
              test = CORBA::string_dup ("Asynch. call two");
              break;
            case 2:
              test = CORBA::string_dup ("Asynch. call three");
              break;
            default:
              break;
          }
        ++nr_of_sent;
        my_one_ami_[i].objref->sendc_foo (new One_callback_exec_i (),
                                          test.in (),
                                          i); 
        ACE_DEBUG ((LM_DEBUG, "Sender (ASYNCH) : send <%C> !\n",
                    test.in ()));
        // There is more than 1 message sent, without receiving callbacks,
        // so it is asynchronous
        if (nr_of_sent.value() > 1)
          {
            asynch = true;
          }
      }
    return 0;
  }
  //============================================================
  // Worker thread for synchronous invocations for One
  //============================================================
  synch_foo_generator::synch_foo_generator (
     ::UsesMulti::CCM_Sender_Context_ptr context)
  : context_(::UsesMulti::CCM_Sender_Context::_duplicate (context))
 {
  }

  int synch_foo_generator::svc ()
  {
    ::UsesMulti::Sender::run_my_um_oneConnections_var my_one_ami_ =
         context_->get_connections_run_my_um_one ();

    for(CORBA::ULong i = 0; i < my_one_ami_->length(); ++i)
      {
        CORBA::String_var test;
        switch (i)
          {
            case 0:
              test = CORBA::string_dup ("Synch. call 0.");
              break;
            case 1:
              test = CORBA::string_dup ("Synch. call 1");
              break;
            case 2:
              test = CORBA::string_dup ("Synch. call 2");
              break;
            default:
              break;
          }

        ACE_DEBUG ((LM_DEBUG,"Sender (SYNCH) : send <%C> !\n",
                    test.in ()));

        CORBA::String_var answer;
        CORBA::ULong result = my_one_ami_[i].objref->foo( test,
                              i,
                              answer.out ());
        if (result != i)
          {
            ACE_ERROR ((LM_ERROR, 
                        "ERROR Sender (SYNCH) : CLASS One foo !\n"));
          }
        else
          { 
            ++nr_of_received;
            ACE_DEBUG ((LM_DEBUG,
                        "Sender (SYNCH) : received answer = <%C> !\n",
                        answer.in ()));
          }
      }
    return 0;
  }
  //============================================================
  // Component Executor Implementation Class: Sender_exec_i
  //============================================================
  Sender_exec_i::Sender_exec_i (void)
  {
  }

  Sender_exec_i::~Sender_exec_i (void)
  {
  }

  // Supported operations and attributes.
  // Component attributes.
  // Operations from Components::SessionComponent.
  void
  Sender_exec_i::set_session_context (
    ::Components::SessionContext_ptr ctx)
  {
    this->context_ =
      ::UsesMulti::CCM_Sender_Context::_narrow (ctx);
    if ( ::CORBA::is_nil (this->context_.in ()))
      {
        throw ::CORBA::INTERNAL ();
      }
  }

  void
  Sender_exec_i::configuration_complete (void)
  {
  }

  void
  Sender_exec_i::ccm_activate (void)
  {
    asynch_foo_generator* asynch_foo_gen =
      new asynch_foo_generator (this->context_.in());
     asynch_foo_gen->activate (THR_NEW_LWP | THR_JOINABLE, 1);

     synch_foo_generator* synch_foo_gen =
         new synch_foo_generator (this->context_.in());
    synch_foo_gen->activate (THR_NEW_LWP | THR_JOINABLE, 1);
  }

  void
  Sender_exec_i::ccm_passivate (void)
  {
  }

  void
  Sender_exec_i::ccm_remove (void)
  {
    if (asynch == false)
      {
        ACE_ERROR ((LM_ERROR,
                   "ERROR: Uses multiple test not asynchronous!\n"));
      }
    if (nr_of_received.value() != 6)
      {
        ACE_ERROR ((LM_ERROR, "ERROR: Did not get all callbacks from "
                              "multiple ports."
                              " Received = %u of 6\n",
                              nr_of_received.value()));
      }
    else
      {
        ACE_DEBUG ((LM_DEBUG,
                    "OK: All messages received back by Sender\n"));
      }
  }

  extern "C"  ::Components::EnterpriseComponent_ptr
  create_UsesMulti_AMI_Sender_Impl (void)
  {
    ::Components::EnterpriseComponent_ptr retval =
      ::Components::EnterpriseComponent::_nil ();

    ACE_NEW_RETURN (
      retval,
      Sender_exec_i,
      ::Components::EnterpriseComponent::_nil ());

    return retval;
  }
}