Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_App / StubFaultAnalyzer.cpp
blob5541c294eaa80667223abbc28646529b8b037ee3
1 // -*- C++ -*-
2 #include "StubFaultAnalyzer.h"
3 #include "ace/Get_Opt.h"
4 #include "orbsvcs/PortableGroup/PG_Properties_Encoder.h"
5 // FUZZ: disable check_for_streams_include
6 #include "ace/streams.h"
7 #include "ace/OS_NS_stdio.h"
9 StubFaultAnalyzer::StubFaultAnalyzer ()
10 : readyFile_(0)
11 , detector_ior_(0)
12 , notifier_ior_(0)
17 StubFaultAnalyzer::~StubFaultAnalyzer ()
22 int StubFaultAnalyzer::parse_args (int argc, ACE_TCHAR * argv[])
24 int optionError = 0;
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:r:d:n:"));
26 int c;
27 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'r':
33 this->replicaIORs.push_back (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
34 break;
36 case 'd':
38 this->detector_ior_ = get_opts.opt_arg ();
39 break;
41 case 'n':
43 this->notifier_ior_ = get_opts.opt_arg ();
44 break;
46 case 'o':
48 this->readyFile_ = get_opts.opt_arg ();
49 break;
52 default:
53 // fall thru
54 case '?':
56 break;
61 if(! optionError)
63 if (0 == replicaIORs.size())
65 ACE_ERROR ((LM_ERROR,
66 "at least one -r option is required.\n"
67 ));
68 optionError = -1;
71 if (0 == this->detector_ior_)
73 ACE_ERROR ((LM_ERROR,
74 "-d option is required.\n"
75 ));
76 optionError = -1;
78 if (0 == this->notifier_ior_)
80 ACE_ERROR ((LM_ERROR,
81 "-n option is required.\n"
82 ));
83 optionError = -1;
87 if(optionError)
89 ACE_ERROR ((LM_ERROR,
90 "usage: %s"
91 " -r <replica.ior[,replica.ior]>"
92 " -d <detector.ior>"
93 " -o <ready.file>" // note: not an IOR file. just an "I'm alive" indicator."
94 " -n <notifier.ior>"
95 "\n",
96 argv [0]
97 ));
99 return optionError;
103 * Register this object as necessary
105 int StubFaultAnalyzer::init (CORBA::ORB_ptr orb)
107 int result = 0;
108 this->orb_ = CORBA::ORB::_duplicate (orb);
109 //////////////////////////////////////////
110 // resolve reference to detector factory
111 CORBA::Object_var detector_obj = this->orb_->string_to_object(this->detector_ior_);
112 this->factory_ = ::FT::FaultDetectorFactory::_narrow(detector_obj.in ());
113 if (CORBA::is_nil(this->factory_.in ()))
115 ACE_OS::fprintf (stderr, "Can't resolve Detector Factory IOR %s\n",
116 ACE_TEXT_ALWAYS_CHAR (this->detector_ior_));
117 result = -1;
120 //////////////////////////////////////////
121 // resolve references to notifier
122 CORBA::Object_var not_obj = this->orb_->string_to_object(this->notifier_ior_);
123 this->notifier_ = ::FT::FaultNotifier::_narrow(not_obj.in ());
124 if (CORBA::is_nil(this->notifier_.in ()))
126 ACE_OS::fprintf (stderr, "Can't resolve Notifier IOR %s\n",
127 ACE_TEXT_ALWAYS_CHAR (this->notifier_ior_));
128 result = -1;
132 //////////////////////////////////
133 // register fault consumers
134 if (result == 0)
136 result = this->faultConsumer_.init (orb, this->notifier_);
139 if (result == 0)
141 result = this->batchConsumer_.init (orb, this->notifier_);
144 /////////////////////////
145 // Set up fault detectors
146 if (result == 0)
148 ////////////////////////////////////
149 // resolve references to replicas
150 // create a fault detector for each replica
151 size_t replicaCount = this->replicaIORs.size();
152 for(size_t nRep = 0; result == 0 && nRep < replicaCount; ++nRep)
154 const char * iorName = this->replicaIORs[nRep];
155 CORBA::Object_var rep_obj = this->orb_->string_to_object(iorName);
156 FT::PullMonitorable_var replica = FT::PullMonitorable::_narrow(rep_obj.in ());
157 if (CORBA::is_nil(replica.in ()))
159 ACE_OS::fprintf (stderr, "Can't resolve Replica IOR %s\n", iorName);
160 result = -1;
162 else
164 this->replicas_.push_back(replica);
166 CORBA::String_var type_id = CORBA::string_dup("FaultDetector");
168 TAO_PG::Properties_Encoder encoder;
170 PortableGroup::Value value;
171 value <<= this->notifier_.in ();
172 encoder.add(::FT::FT_NOTIFIER, value);
174 value <<= replica.in ();
175 encoder.add(::FT::FT_MONITORABLE, value);
177 FT::FTDomainId domain_id = 0;
178 value <<= domain_id;
179 encoder.add(::FT::FT_DOMAIN_ID, value);
181 PortableGroup::Location object_location;
182 object_location.length(1);
183 object_location[0].id = CORBA::string_dup("Test location");
184 value <<= object_location;
185 encoder.add(::FT::FT_LOCATION, value);
187 PortableGroup::TypeId object_type = CORBA::string_dup("dummy_type");
188 value <<= object_type;
189 encoder.add(::FT::FT_TYPE_ID, value);
191 FT::ObjectGroupId group_id = 0;
192 value <<= group_id;
193 encoder.add(::FT::FT_GROUP_ID, value);
195 // allocate and populate the criteria
196 PortableGroup::Criteria_var criteria;
197 ACE_NEW_NORETURN (criteria,
198 PortableGroup::Criteria);
199 if (criteria.ptr() == 0)
201 ACE_ERROR((LM_ERROR,
202 "Error cannot allocate criteria.\n"
204 result = -1;
206 else
208 encoder.encode(criteria);
209 PortableGroup::GenericFactory::FactoryCreationId_var factory_creation_id;
211 ACE_DEBUG((LM_DEBUG,
212 "Call create_object with type: %s\n", type_id.in()
214 this->factory_->create_object (
215 type_id.in(),
216 criteria.in(),
217 factory_creation_id);
222 if (result == 0 && this->readyFile_ != 0)
224 FILE *ready = ACE_OS::fopen (this->readyFile_, "w");
225 if ( ready )
227 ACE_OS::fprintf (ready, "ready\n");
228 ACE_OS::fclose (ready);
232 return result;
237 * Return a string to identify this object for logging/console message purposes.
239 const char * StubFaultAnalyzer::identity () const
241 return this->identity_.c_str();
245 * Clean house for process shut down.
247 int StubFaultAnalyzer::fini (void)
249 this->faultConsumer_.fini();
250 this->batchConsumer_.fini();
251 return 0;
255 int StubFaultAnalyzer::idle(int & result)
257 ACE_UNUSED_ARG(result);
258 int quit = 0;
260 if (this->replicas_.size() == this->faultConsumer_.notifications())
262 quit = 1;
264 return quit;