Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_App / FT_Creator.cpp
blob6251fb5b9f88e33c335249af201af876083623de
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");
133 int FTAPP::FT_Creator::init (CORBA::ORB_ptr orb)
135 int result = 0;
136 this->orb_ = CORBA::ORB::_duplicate (orb);
138 // if a factory IOR was specified on command line
139 if ( this->registry_ior_ != 0)
141 CORBA::Object_var registry_obj
142 = this->orb_->string_to_object (this->registry_ior_);
143 PortableGroup::FactoryRegistry_var registry
144 = PortableGroup::FactoryRegistry::_narrow(registry_obj.in ());
145 if (! CORBA::is_nil (registry.in ()))
147 result = this->creator_.set_factory_registry(registry.in());
151 if (result == 0)
153 result = this->creator_.init (orb);
157 if (result == 0 && this->ns_register_)
159 CORBA::Object_var naming_obj =
160 this->orb_->resolve_initial_references ("NameService");
162 if (CORBA::is_nil(naming_obj.in ()))
164 ACE_ERROR_RETURN ((LM_ERROR,
165 "%T %n (%P|%t) Unable to find the Naming Service\n"),
168 this->naming_context_=
169 CosNaming::NamingContext::_narrow (naming_obj.in ());
172 return result;
175 int FTAPP::FT_Creator::run (void)
177 int result = 0;
178 size_t typeCount = this->create_roles_.size();
179 size_t nType = 0;
180 for ( nType = 0; result == 0 && nType < typeCount; ++nType)
182 const char * role = this->create_roles_[nType].c_str();
183 ACE_OS::fprintf (stdout, "\nCreator: Creating group of %s\n", role);
184 PortableGroup::ObjectGroup_var group = this->creator_.create_group (
185 role,
186 this->write_iors_);
188 if (this->write_iogr_)
190 CORBA::String_var iogr = this->orb_->object_to_string (group.in ());
192 char iogr_filename[1000];
193 ACE_OS::snprintf (iogr_filename, sizeof(iogr_filename),
194 "%s%s_" ACE_SIZE_T_FORMAT_SPECIFIER_ASCII ".iogr",
195 this->prefix_.c_str (),
196 role,
197 this->iogr_seq_);
198 FILE * iogr_file = ACE_OS::fopen (iogr_filename, "w");
199 if (iogr_file != 0)
201 char const * siogr = static_cast<const char *> (iogr.in ());
202 ACE_OS::fwrite (siogr, 1, ACE_OS::strlen(siogr), iogr_file);
203 ACE_OS::fclose (iogr_file);
205 else
207 ACE_OS::fprintf (stderr, "Can't open iogr output file %s\n", iogr_filename);
208 result = 1;
212 if(this->ns_register_)
214 char iogr_name[1000];
215 ACE_OS::snprintf (iogr_name, sizeof(iogr_name),
216 "%s_%s_" ACE_SIZE_T_FORMAT_SPECIFIER_ASCII,
217 this->prefix_.c_str (),
218 role,
219 this->iogr_seq_);
221 CosNaming::Name this_name (1);
222 this_name.length (1);
223 this_name[0].id = CORBA::string_dup (iogr_name);
225 this->naming_context_->rebind (this_name, group.in());
228 iogr_seq_ += 1;
232 typeCount = this->unregister_roles_.size();
233 for ( nType = 0; result == 0 && nType < typeCount; ++nType)
235 const char * role = this->unregister_roles_[nType].c_str();
236 result = this->creator_.unregister_role (role);
239 return result;
242 int FTAPP::FT_Creator::fini ()
244 return this->creator_.fini();
248 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
250 int result = 0;
253 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
254 FTAPP::FT_Creator app;
255 result = app.parse_args(argc, argv);
256 if (result == 0)
258 result = app.init (orb.in ());
259 if (result == 0)
261 result = app.run ();
263 if (result == 0)
265 result = app.fini();
269 catch (const CORBA::Exception& ex)
271 ex._tao_print_exception ("FT_Creator::main\t\n");
272 result = -1;
274 return result;