summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/LWFT/StateSynchronizationAgent_i.cpp
blob: 76634ae121755b74c9a51f3b9c9ed5e71a527569 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// -*- C++ -*-

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

#include <string>

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

#ifdef FLARE_USES_DDS
# include "DDSStateUpdate_T.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),
#if defined (FLARE_USES_DDS)
    domain_id_ (0),//"FLAREDomain"),
    domain_participant_ (DDS::DomainParticipant::_nil ()),
    publisher_ (DDS::Publisher::_nil ()),
    subscriber_ (DDS::Subscriber::_nil ()),
#endif /* FLARE_USES_DDS */
    use_corba_ (use_corba)
{
#if defined (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 (void)
{
#ifdef FLARE_USES_DDS
  if (!use_corba_)
    {
      DDS::ReturnCode_t status =
        domain_participant_->delete_contained_entities ();
        
      if (status != DDS::RETCODE_OK)
        {
          ACE_ERROR ((LM_ERROR,
                      "SSA - "
                      "Bad retcode from delete_contained_entities()\n"));
        }
        
      if (! this->delete_participant ())
        {
          ACE_ERROR ((LM_ERROR,
                      "SSA - "
                      "Bad retcode from delete_participant()\n"));
        }
    }
#endif /* FLARE_USES_DDS */
}

void 
StateSynchronizationAgent_i::state_changed (const char * object_id)
{
  /*
  ACE_DEBUG ((LM_TRACE, 
              "SSA::state_changed (%s) called.\n", 
              object_id));
  */
  // get application reference
  ReplicatedApplication_var app;

  if (application_map_.find (ACE_CString (object_id),
			                       app) != 0)
    {
      ACE_ERROR ((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 while 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_.
  ReplicaGroup replica_group;
  
  if (replica_map_.find (ACE_CString (object_id),
			 replica_group) != 0)
    {
      /*
      ACE_ERROR ((LM_WARNING, 
                  "(%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.replicas.begin ();
       it != replica_group.replicas.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_)
    {
      // If only corba is used, we can simply reset the map.
      replica_map_.close ();
      replica_map_.open ();
    }
  else
    {
      // only remove entries replicated by CORBA
      for (OBJECTID_REPLICA_MAP::iterator it = replica_map_.begin ();
           it != replica_map_.end ();
           ++it)
        {
          if (!it->item ().use_dds)
            {
              replica_map_.unbind (it);
            }
        }
    }

  ACE_DEBUG ((LM_INFO,
              "SSA::update_rank_list with:\n"));

  // for each replication group in the replica group list
  for (size_t i = 0; i < rank_list.length (); ++i)
    {
      ACE_DEBUG ((LM_INFO,
                  "\toid = %s (%d entries)\n", 
                  rank_list[i].object_id.in (),
                  rank_list[i].ior_list.length ()));

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

      // If there is already an entry for this object_id it means that
      // we have a registered DDS object and should not override it.
      if (replica_map_.find (oid) != 0)
        {
          // Create a new list for every replication group.
          ReplicaGroup replica_group;
          replica_group.use_dds = false;      

          // 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_group.replicas.push_back (
                    STATEFUL_OBJECT_PTR (
                      new CorbaStateUpdate (
                          rank_list[i].ior_list[j])));
                }
              catch (const CORBA::SystemException& ex)
                {
		  /*
                  ACE_DEBUG ((
                    LM_WARNING, 
                    "(%P|%t) SSA::"
                    "update_rank_list could not resolve stringified "
                    "object reference for %s : %s\n",
                    oid.c_str (),
                    ex._info ().c_str ()));
		  */
                }
            }

          // Add one replication group to the map.
          replica_map_.bind (oid, replica_group);
        }
    }
}

void
StateSynchronizationAgent_i::register_application (
  const char * object_id,
  ReplicatedApplication_ptr app)
{
  //ACE_DEBUG ((LM_TRACE,
  //            "SSA::register_application (%s) called.\n", object_id));

  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

bool
StateSynchronizationAgent_i::create_participant (void)
{
  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 (void)
{
  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 (void)
{
  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 (void)
{
  DDS::ReturnCode_t status = 
    domain_participant_->delete_publisher (publisher_.in ());

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

bool
StateSynchronizationAgent_i::create_subscriber (void)
{
  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 (void)
{
  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;
}