Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / CSD_Strategy_Tests / TP_Test_2 / ClientApp.cpp
blob78cb71484d8a2a4d60789cd666aa2dfe71b36a65
1 #include "ClientApp.h"
2 #include "AppHelper.h"
3 #include "TestAppExceptionC.h"
4 #include "Foo_A_ClientEngine.h"
5 #include "ace/Get_Opt.h"
8 ClientApp::ClientApp()
9 : TestAppBase("TP_Test_2_Client"),
10 ior_(ACE_TEXT("Not Set")),
11 client_kind_(0),
12 client_id_(0)
17 ClientApp::~ClientApp()
22 int
23 ClientApp::run_i(int argc, ACE_TCHAR* argv[])
25 int result = this->init(argc, argv);
26 if (result != 0)
28 return result;
31 this->client_setup();
33 result = this->run_engine();
34 this->cleanup();
35 return result;
39 int
40 ClientApp::init(int argc, ACE_TCHAR* argv[])
42 this->orb_ = CORBA::ORB_init(argc, argv);
44 // Parse the command-line args for this application.
45 // * Raises -1 if problems are encountered.
46 // * Returns 1 if the usage statement was explicitly requested.
47 // * Returns 0 otherwise.
48 return this->parse_args(argc, argv);
52 void
53 ClientApp::client_setup()
55 // Turn the ior_ into a Foo_A obj ref.
56 Foo_A_var foo = RefHelper<Foo_A>::string_to_ref(this->orb_.in(),
57 this->ior_.c_str());
59 // Create the ClientEngine object, and give it the Foo_A obj ref.
60 this->engine_ = new Foo_A_ClientEngine(foo.in(), this->client_id_);
64 int
65 ClientApp::run_engine()
67 bool result = this->engine_->execute();
68 return result ? 0 : -1;
72 void
73 ClientApp::cleanup()
78 int
79 ClientApp::parse_args(int argc, ACE_TCHAR* argv[])
81 this->exe_name_ = argv[0];
83 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("i:k:n:"));
85 int c;
87 while ((c = get_opts()) != -1)
89 int result = 0;
90 switch (c)
92 case 'i':
93 this->ior_ = get_opts.opt_arg();
94 break;
96 case 'k':
97 result = set_arg(this->client_kind_,
98 get_opts.opt_arg(),
100 "client_kind");
101 break;
103 case 'n':
104 result = set_arg(this->client_id_,
105 get_opts.opt_arg(),
107 "client_id");
108 break;
110 case '?':
111 this->usage_statement();
112 return 1;
114 default:
115 this->usage_statement();
116 return -1;
119 if (result != 0)
121 return result;
125 return this->arg_dependency_checks();
128 void
129 ClientApp::usage_statement()
131 ACE_ERROR((LM_ERROR,
132 "Usage: %s [options]\n\n"
133 "OPTIONS:\n\n"
134 "\t[-i <ior>]\n"
135 "\t[-k <client_kind>]\n"
136 "\t[-n <client_id>]\n"
137 "\t[-?]\n\n",
138 this->exe_name_.c_str()));
143 ClientApp::arg_dependency_checks()
145 if (this->ior_ == ACE_TEXT("Not Set"))
147 ACE_ERROR((LM_ERROR,
148 "Error: Missing required command-line option (-i <ior>).\n"));
149 this->usage_statement();
150 return -1;
153 if (this->client_id_ <= 0)
155 ACE_ERROR((LM_ERROR,
156 "Error: Invalid command-line option (-n <client id>).\n"
157 " The client id should be positive integer.\n"));
158 this->usage_statement();
159 return -1;
162 return 0;
167 ClientApp::set_arg(unsigned& value,
168 const ACE_TCHAR* arg,
169 char opt,
170 const char* name,
171 int min)
173 int tmp = ACE_OS::atoi(arg);
175 if (tmp < min)
177 ACE_ERROR((LM_ERROR,
178 "Error: -%c <%s> must be integer type with a value of, "
179 "at least, %d.\n", opt, name, min));
180 this->usage_statement();
181 return -1;
184 value = tmp;
185 return 0;