Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_App / FT_Creator.cpp
blobf20db9c38807b13c1458b1ed8b909307c11e8fe9
1 /* -*- C++ -*- */
2 //=============================================================================
3 /**
4 * @file FT_Creator.cpp
6 * This file is part of Fault Tolerant CORBA.
7 * Main wrapped around TAO_Object_Group_Creator
9 * @author Dale Wilson <wilson_d@ociweb.com>
11 //=============================================================================
13 #include "FT_Creator.h"
14 // FUZZ: disable check_for_streams_include
15 #include "ace/streams.h"
16 #include <orbsvcs/PortableGroup/PG_Properties_Encoder.h>
18 #include <ace/Get_Opt.h>
19 #include <ace/OS_NS_stdio.h>
21 FTAPP::FT_Creator::FT_Creator ()
22 : creator_ ()
23 , orb_ (CORBA::ORB::_nil ())
24 , registry_ior_(0)
25 , replication_manager_ (::FT::ReplicationManager::_nil ())
26 , have_replication_manager_ (0)
27 , write_iors_ (0)
28 , write_iogr_ (0)
29 , ns_register_ (1)
30 , iogr_seq_ (0)
31 , prefix_ ("")
35 FTAPP::FT_Creator::~FT_Creator ()
39 int
40 FTAPP::FT_Creator::parse_args (int argc, ACE_TCHAR *argv[])
42 int result = 0;
44 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("r:ignf:u:p:"));
45 int c;
47 while (result == 0 && (c = get_opts ()) != -1)
49 switch (c)
51 case 'r':
53 this->create_roles_.push_back (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
54 break;
56 case 'u':
58 this->unregister_roles_.push_back (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
59 break;
61 case 'f':
63 this->registry_ior_ = get_opts.opt_arg ();
64 break;
67 case 'g':
69 this->write_iogr_ = !this->write_iogr_;
70 break;
73 case 'i':
75 this->write_iors_ = ! this->write_iors_;
76 break;
79 case 'n':
81 this->ns_register_ = !this->ns_register_;
82 break;
85 case 'p':
87 this->prefix_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg());
88 break;
91 default:
93 ACE_OS::fprintf (stderr, "Creator: Unknown argument -%c\n", (char) c);
94 usage(stderr);
95 result = 1;
96 break;
98 case '?':
100 usage(stderr);
101 result = 1;
102 break;
107 if ( this->create_roles_.size() == 0 && this->unregister_roles_.size() == 0)
109 //FUZZ: disable check_for_lack_ACE_OS
110 ACE_OS::fprintf (stderr, "Creator: neither create (-t) nor kill (-u) specified. Nothing to do.\n");
111 //FUZZ: enable check_for_lack_ACE_OS
112 usage (stderr);
113 result = -1;
116 return result;
119 void FTAPP::FT_Creator::usage(FILE* out)const
121 ACE_OS::fprintf (out, "usage\n"
122 " -r <role for objects to be created>\n"
123 " -f <factory registry ior file> (if not specified, ReplicationManager is used.)\n"
124 " -u <role to be unregistered (for testing factory registry)>\n"
125 " -i (toggle write ior for each object (default false))\n"
126 " -p <prefix for registration & file names>\n"
127 " -g (toggle write iogr to file (default false))\n"
128 " -n (toggle register iogr with name service (default true))\n");
132 int FTAPP::FT_Creator::init (CORBA::ORB_ptr orb)
134 int result = 0;
135 this->orb_ = CORBA::ORB::_duplicate (orb);
137 // if a factory IOR was specified on command line
138 if ( this->registry_ior_ != 0)
140 CORBA::Object_var registry_obj
141 = this->orb_->string_to_object (this->registry_ior_);
142 PortableGroup::FactoryRegistry_var registry
143 = PortableGroup::FactoryRegistry::_narrow(registry_obj.in ());
144 if (! CORBA::is_nil (registry.in ()))
146 result = this->creator_.set_factory_registry(registry.in());
150 if (result == 0)
152 result = this->creator_.init (orb);
156 if (result == 0 && this->ns_register_)
158 CORBA::Object_var naming_obj =
159 this->orb_->resolve_initial_references ("NameService");
161 if (CORBA::is_nil(naming_obj.in ()))
163 ACE_ERROR_RETURN ((LM_ERROR,
164 "%T %n (%P|%t) Unable to find the Naming Service\n"),
167 this->naming_context_=
168 CosNaming::NamingContext::_narrow (naming_obj.in ());
171 return result;
174 int FTAPP::FT_Creator::run ()
176 int result = 0;
177 size_t typeCount = this->create_roles_.size();
178 size_t nType = 0;
179 for ( nType = 0; result == 0 && nType < typeCount; ++nType)
181 const char * role = this->create_roles_[nType].c_str();
182 ACE_OS::fprintf (stdout, "\nCreator: Creating group of %s\n", role);
183 PortableGroup::ObjectGroup_var group = this->creator_.create_group (
184 role,
185 this->write_iors_);
187 if (this->write_iogr_)
189 CORBA::String_var iogr = this->orb_->object_to_string (group.in ());
191 char iogr_filename[1000];
192 ACE_OS::snprintf (iogr_filename, sizeof(iogr_filename),
193 "%s%s_" ACE_SIZE_T_FORMAT_SPECIFIER_ASCII ".iogr",
194 this->prefix_.c_str (),
195 role,
196 this->iogr_seq_);
197 FILE * iogr_file = ACE_OS::fopen (iogr_filename, "w");
198 if (iogr_file != 0)
200 char const * siogr = static_cast<const char *> (iogr.in ());
201 ACE_OS::fwrite (siogr, 1, ACE_OS::strlen(siogr), iogr_file);
202 ACE_OS::fclose (iogr_file);
204 else
206 ACE_OS::fprintf (stderr, "Can't open iogr output file %s\n", iogr_filename);
207 result = 1;
211 if(this->ns_register_)
213 char iogr_name[1000];
214 ACE_OS::snprintf (iogr_name, sizeof(iogr_name),
215 "%s_%s_" ACE_SIZE_T_FORMAT_SPECIFIER_ASCII,
216 this->prefix_.c_str (),
217 role,
218 this->iogr_seq_);
220 CosNaming::Name this_name (1);
221 this_name.length (1);
222 this_name[0].id = CORBA::string_dup (iogr_name);
224 this->naming_context_->rebind (this_name, group.in());
227 iogr_seq_ += 1;
230 typeCount = this->unregister_roles_.size();
231 for ( nType = 0; result == 0 && nType < typeCount; ++nType)
233 const char * role = this->unregister_roles_[nType].c_str();
234 result = this->creator_.unregister_role (role);
237 return result;
240 int FTAPP::FT_Creator::fini ()
242 return this->creator_.fini();
246 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
248 int result = 0;
251 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
252 FTAPP::FT_Creator app;
253 result = app.parse_args(argc, argv);
254 if (result == 0)
256 result = app.init (orb.in ());
257 if (result == 0)
259 result = app.run ();
261 if (result == 0)
263 result = app.fini();
267 catch (const CORBA::Exception& ex)
269 ex._tao_print_exception ("FT_Creator::main\t\n");
270 result = -1;
272 return result;