summaryrefslogtreecommitdiff
path: root/CIAO/connectors/dds4ccm/tutorials/Shapes/Shapes_asm/ShapesSender_comp/src/ShapesSender_exec.cpp
blob: e451a61422ed57d107e8da09a56002e7cfecf565 (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
// -*- C++ -*-
// $Id$

#include "ShapesSender_exec.h"
#include "ace/Guard_T.h"
#include "ciao/Logger/Log_Macros.h"
#include "tao/ORB_Core.h"

namespace CIAO_Shapes_Sender_comp_Impl
{
  //============================================================
  // Controller_exec_i
  //============================================================
  Controller_exec_i::Controller_exec_i (Sender_exec_i &callback)
    : callback_ (callback)
  {
  }

  Controller_exec_i::~Controller_exec_i (void)
  {
  }

  ::Shapes::ReturnStatus
  Controller_exec_i::setSize (::CORBA::UShort size)
  {
    return this->callback_.setSize (size);
  }

  ::Shapes::ReturnStatus
  Controller_exec_i::setLocation (::CORBA::UShort x,
                                  ::CORBA::UShort y)
  {
    return this->callback_.setLocation (x, y);
  }


  //============================================================
  // Sender_exec_i
  //============================================================
  Sender_exec_i::Sender_exec_i (void)
    : instance_handle_ (::DDS::HANDLE_NIL)
  {
    this->square_.x = 10;
    this->square_.y = 10;
    this->square_.shapesize = 1;
    this->square_.color = CORBA::string_dup("GREEN");
  }

  Sender_exec_i::~Sender_exec_i (void)
  {
  }

  // Supported operations and attributes.
  ::Shapes::CCM_Control_obj_ptr
  Sender_exec_i::get_control (void)
  {
    return new Controller_exec_i (*this);
  }

  ::Shapes::ReturnStatus
  Sender_exec_i::setSize (::CORBA::UShort size)
  {
    this->square_.shapesize = size;
    return this->update_square ();
  }

  ::Shapes::ReturnStatus
  Sender_exec_i::setLocation (::CORBA::UShort x,
                              ::CORBA::UShort y)
  {
    this->square_.x = x;
    this->square_.y = y;
    return this->update_square ();
  }

  ::Shapes::ReturnStatus
  Sender_exec_i::update_square ()
  {
    ::Shapes::ShapesConnector::Writer_var writer =
      this->context_->get_connection_info_write_data ();
    if (::CORBA::is_nil (writer.in ()))
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Sender_exec_i::update_square - ")
                              ACE_TEXT ("Unable to write: write_data is nil.\n")));
        return ::Shapes::RETURN_ERROR;
      }

    try
      {
        writer->write_one (this->square_, this->instance_handle_);
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("UPDATED Shape_info for <%C> %u:%u:%u\n"),
                  this->square_.color.in (),
                  this->square_.x,
                  this->square_.y,
                  this->square_.shapesize));
      }
    catch (const CCM_DDS::NonExistent& )
      {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Shape_info for <%C> not updated: <%C> didn't exist.\n"),
                    this->square_.color.in (), this->square_.color.in ()));
      }
    catch (const CCM_DDS::InternalError& )
      {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Internal Error while updating Shape_info for <%C>.\n"),
                    this->square_.color.in ()));
      }
    return ::Shapes::RETURN_OK;
  }

  void
  Sender_exec_i::set_session_context (::Components::SessionContext_ptr ctx)
  {
    this->context_ =
      ::Shapes::CCM_Sender_comp_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)
  {
    ::Shapes::ShapesConnector::Writer_var writer =
      this->context_->get_connection_info_write_data ();
    if (::CORBA::is_nil (writer.in ()))
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Sender_exec_i::update_square - ")
                              ACE_TEXT ("Unable to write: write_data is nil.\n")));
        throw CORBA::INTERNAL ();
      }

    try
      {
        this->instance_handle_ = writer->register_instance (this->square_);
        //Register shape with dds.
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("REGISTER Shape_info for <%C> %u:%u:%u\n"),
                      this->square_.color.in (),
                      this->square_.x,
                      this->square_.y,
                      this->square_.shapesize));
      }
    catch (const CCM_DDS::AlreadyCreated& )
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Shape_info for <%C> already created.\n"),
                    this->square_.color.in ()));
      }
    catch (const CCM_DDS::InternalError& )
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Internal Error while creating Shape_info for <%C>.\n"),
                      this->square_.color.in ()));
      }
  }

  void
  Sender_exec_i::ccm_passivate (void)
  {
    ::Shapes::ShapesConnector::Writer_var writer =
      this->context_->get_connection_info_write_data ();
    if (::CORBA::is_nil (writer.in ()))
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Sender_exec_i::update_square - ")
                              ACE_TEXT ("Unable to write: write_data is nil.\n")));
        throw CORBA::INTERNAL ();
      }

    try
      {
        writer->unregister_instance (this->square_,
                                     this->instance_handle_);
      }
    catch (const CCM_DDS::NonExistent& )
      {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Shape_info for <%C> not deleted: <%C> didn't exist.\n"),
                    this->square_.color.in (),
                    this->square_.color.in ()));
      }
    catch (const CCM_DDS::InternalError& )
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("Internal Error while deleting Shape_info for <%C>.\n"),
                    this->square_.color.in ()));
      }
  }

  void
  Sender_exec_i::ccm_remove (void)
  {
  }

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

    ACE_NEW_NORETURN (
      retval,
      Sender_exec_i);

    return retval;
  }
}