summaryrefslogtreecommitdiff
path: root/CIAO/ciao/FTComponentServer/StateSynchronizationAgent/StateSynchronizationAgent_i.cpp
blob: b126338c5527e45cf70c2cab5636b954f3e359ca (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// -*- C++ -*-

//=============================================================================
/**
 *  @file    StateSynchronizationAgent_i.cpp
 *
 *  $Id$
 *
 *  @author  Friedhelm Wolf (fwolf@dre.vanderbilt.edu)
 */
//=============================================================================

#include "StateSynchronizationAgent_i.h"
#include "CorbaStateUpdate.h"

#ifdef FLARE_USES_DDS
# include "DDSStateUpdate_T.h"
# include "StateDcps_impl.h"
#endif

StateSynchronizationAgent_i::StateSynchronizationAgent_i (
    CORBA::ORB_ptr orb,
    const std::string & host_id,
    const std::string & process_id,
    bool use_corba)
  : orb_ (CORBA::ORB::_duplicate (orb)),
    host_id_ (host_id),
    process_id_ (process_id),
#ifdef FLARE_USES_DDS
    domain_id_ (0),
    domain_participant_ (DDS::DomainParticipant::_nil ()),
    publisher_ (DDS::Publisher::_nil ()),
    subscriber_ (DDS::Subscriber::_nil ()),
#endif /* FLARE_USES_DDS */
    use_corba_ (use_corba)
{
#ifdef FLARE_USES_DDS
  if (!use_corba_)
    {
      if (!this->create_participant ())
	throw DDSFailure ("SSA could not create DDS participant\n");

      if (!this->create_publisher ())
	throw DDSFailure ("SSA could not create DDS publisher\n");

      if (!this->create_subscriber ())
	throw DDSFailure ("SSA could not create DDS subscriber\n");
    }
#endif /* FLARE_USES_DDS */
}

StateSynchronizationAgent_i::~StateSynchronizationAgent_i ()
{
#ifdef FLARE_USES_DDS
  if (!use_corba_)
    {
      this->delete_subscriber ();
      this->delete_publisher ();
      this->delete_participant ();
    }
#endif /* FLARE_USES_DDS */
}

void 
StateSynchronizationAgent_i::state_changed (const char * object_id)
{
  std::cout << "SSA::state_changed (%s) called." << std::endl;

  // get application reference
  ReplicatedApplication_var app;

  if (application_map_.find (ACE_CString (object_id),
			     app) != 0)
    {
      ACE_DEBUG ((LM_ERROR, 
		  "(%P|%t) SSA::state_changed () "
		  "could not find application for object id %s\n",
		  object_id));
      return;
    }

  // get state from the application
  CORBA::Any_var state;
  try 
    {
      state = app->get_state ();
    }
   catch (const CORBA::SystemException& ex)
    {
      ACE_DEBUG ((LM_ERROR, 
		  "(%P|%t) SSA::state_changed () "
		  "exception whil calling the get_state method for application %s:\n"
		  "%s",
		  object_id, ex._info ().c_str ()));
      return;
    }

  // send state to each element in the replica_map_
  REPLICA_OBJECT_LIST replica_group;
  if (replica_map_.find (ACE_CString (object_id),
			 replica_group) != 0)
    {
      ACE_DEBUG ((LM_ERROR, 
		  "(%P|%t) SSA::state_changed () "
		  "could not find replicas for the application %s\n",
		  object_id));
      return;
    }

  ReplicatedApplication_var replica;
  for (REPLICA_OBJECT_LIST::iterator it = replica_group.begin ();
       it != replica_group.end ();
       ++it)
    {
      try
	{
	  // set the state on this replica
	  (*it)->set_state (state.in ());
	}
      catch (const CORBA::SystemException& ex)
	{
	  ACE_DEBUG ((LM_WARNING, 
		      "(%P|%t) SSA::state_changed () "
		      "exception while contacting a server replica for %s.\n",
		      object_id));
	}
    }
}

void 
StateSynchronizationAgent_i::update_rank_list (const RankList & rank_list)
{
  if (use_corba_)
    {
      // protect operations on the map
      ACE_Guard <ACE_Thread_Mutex> guard (replica_map_mutex_);

      // reset content of the internal map
      replica_map_.close();
      replica_map_.open();

      std::cout << "SSA::update_rank_list with:" 
		<< std::endl;

      // for each replication group in the replica group list
      for (size_t i = 0; i < rank_list.length (); ++i)
	{
	  std::cout << "\toid = "<< rank_list[i].object_id.in ()
		    << " (" << rank_list.length () << " entries)"
		    << std::endl;

	  // use the application id as a key for the map
	  ACE_CString oid (rank_list[i].object_id);

	  // create a new list for every replication group
	  REPLICA_OBJECT_LIST replica_object_list;

	  // for each entry of a replica group
	  for (size_t j = 0; j < rank_list[i].ior_list.length (); ++j)
	    {
	      try
		{
		  // it is assumed that the strings identifying rank_list are
		  // stringified object references and can be resolved
		  // and used to contact the corresponding StateSynchronizationAgent
		  replica_object_list.push_back (
                    STATEFUL_OBJECT_PTR (
		      new CorbaStateUpdate (
                        ReplicatedApplication::_narrow (rank_list[i].ior_list[j]))));
		}
	      catch (const CORBA::SystemException& ex)
		{
		  ACE_DEBUG ((LM_WARNING, 
			      "(%P|%t) SSA::"
			      "update_replica_groups could not resolve stringified "
			      "object reference %s\n",
			      rank_list[i].ior_list[j].in ()));
		}
	    }

	  // add one replication group to the map
	  replica_map_.bind (oid, replica_object_list);
	}
    } // end if (use_corba_)
}

void 
StateSynchronizationAgent_i::register_application (const char * object_id,
						   ReplicatedApplication_ptr app)
{
  std::cout << "SSA::register_application (" << object_id << ") called." << std::endl;

  ACE_CString oid (object_id);

  if (application_map_.bind (oid, ReplicatedApplication::_duplicate (app)) < 0)
    {
      ACE_DEBUG ((LM_WARNING, 
		  "(%P|%t) SSA::register_application () "
		  "could not bind application %s to the map successfully\n",
		  object_id));
    }  

#ifdef FLARE_USES_DDS

  // if we use DDS for communication
  if (!use_corba_)
    {
      try
	{
	  // protect operations on the map
	  ACE_Guard <ACE_Thread_Mutex> guard (replica_map_mutex_);
	  
	  ACE_DEBUG ((LM_TRACE, "SSA::register_application add DDS participant"
		      " for application %s\n", object_id));

	  // create a new list which will have only one entry for DDS
	  REPLICA_OBJECT_LIST replica_object_list;

	  // register a DDS participant for this application
	  replica_object_list.push_back (
            STATEFUL_OBJECT_PTR (
              new DDSStateUpdate_T <CORBA::Long,
                                    State,
                                    StateTypeSupport,
                                    StateDataWriter,
	                            StateDataReader,
                                    StateSeq> (
	        oid.c_str (),
	        this->get_unique_id (oid.c_str ()),
	        domain_participant_.in (),
	        publisher_.in (),
	        subscriber_.in (),
                app)));

	  ACE_CString oid (object_id);

	  // this should work without doing a rebind, since there is only
	  // one application of the same type per process
	  replica_map_.bind (oid, replica_object_list);
	}
      catch (const DDSFailure & ex)
	{
	  std::cerr << "SSA::register_application () DDS problem : "
		    << ex.description ()
		    << std::endl;
	}
    }

#endif /* FLARE_USES_DDS */
}

#ifdef FLARE_USES_DDS

bool
StateSynchronizationAgent_i::create_participant ()
{
  DDS::DomainParticipantFactory_var dpf
    = DDS::DomainParticipantFactory::get_instance ();
    
  if (CORBA::is_nil (dpf.in ()))
    {
      return false;
    }

  domain_participant_ =
    dpf->create_participant (domain_id_,
			     PARTICIPANT_QOS_DEFAULT,
			     DDS::DomainParticipantListener::_nil (),
			     DDS::ANY_STATUS);

  if (CORBA::is_nil (domain_participant_.in ()))
    {
      return false;
    }

  return true;
}

bool
StateSynchronizationAgent_i::delete_participant ()
{
  DDS::DomainParticipantFactory_var dpf
    = DDS::DomainParticipantFactory::get_instance ();
    
  if (CORBA::is_nil (dpf.in ()))
    {
      return false;
    }

  DDS::ReturnCode_t status = 
    dpf->delete_participant (domain_participant_.in ());
  
  if (status != DDS::RETCODE_OK)
    {
      return false;
    }
    
  return true;
}

bool
StateSynchronizationAgent_i::create_publisher ()
{
  DDS::PublisherQos pub_qos;
  domain_participant_->get_default_publisher_qos (pub_qos);

  publisher_ =
    domain_participant_->create_publisher (pub_qos,
					   DDS::PublisherListener::_nil (),
					   DDS::ANY_STATUS);

  if (CORBA::is_nil (publisher_.in ()))
    {
      return false;
    }    

  return true;
}

bool
StateSynchronizationAgent_i::delete_publisher ()
{
  DDS::ReturnCode_t status = 
    domain_participant_->delete_publisher (publisher_.in ());

  if (status != DDS::RETCODE_OK)
    {
      return false;
    }
    
  return true;  
}

bool
StateSynchronizationAgent_i::create_subscriber ()
{
  subscriber_ =
    domain_participant_->create_subscriber (SUBSCRIBER_QOS_DEFAULT,
					    DDS::SubscriberListener::_nil (),
					    DDS::ANY_STATUS);

  if (CORBA::is_nil (subscriber_.in ()))
    {
      return false;
    }    

  return true;
}

bool
StateSynchronizationAgent_i::delete_subscriber ()
{
  DDS::ReturnCode_t status = 
    domain_participant_->delete_subscriber (subscriber_.in ());

  if (status != DDS::RETCODE_OK)
    {
      return false;
    }
    
  return true;  
}

#endif /* FLARE_USES_DDS */

std::string
StateSynchronizationAgent_i::get_unique_id (const std::string & app_name)
{
  std::string unique_id (app_name);

  // make name unique by adding host and process id
  unique_id += "_" + host_id_ + "_" + process_id_;

  return unique_id;
}