Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / FT_Naming / stress_storable / client.cpp
blobf6090ac3d136ff2f779b9c2c78ee8b08c920f6c0
1 #include "TestC.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Time_Value.h"
4 #include "orbsvcs/Naming/Naming_Client.h"
5 #include "orbsvcs/FT_NamingManagerC.h"
6 #include "orbsvcs/Naming/FaultTolerant/nsgroup_svc.h"
7 #include "ace/Task.h"
10 const ACE_TCHAR *group_names[] =
11 {ACE_TEXT("C++ Group"), ACE_TEXT("FORTRAN Group"), ACE_TEXT("Groovy Group"), ACE_TEXT("Basic Group"),
12 ACE_TEXT("Java Group"), ACE_TEXT("Forth Group"), ACE_TEXT("COBOL Group"), ACE_TEXT("Ruby Group"),
13 ACE_TEXT("wrong answer dude"), ACE_TEXT("Scala Group"), ACE_TEXT("R Group"), 0};
15 class Hammer : public ACE_Task_Base
17 NS_group_svc group_svc;
19 public:
20 Hammer ()
21 : group_svc(true)
24 int setup (CORBA::ORB_ptr orb, FT_Naming::NamingManager_ptr nm)
26 if (group_svc.set_orb (orb) != 0 || group_svc.set_naming_manager (nm))
28 ACE_DEBUG ((LM_DEBUG, "Hammer setup failed\n"));
29 return -1;
31 return 0;
34 int svc ()
36 for (int g = 0, i = 0; i < 100; i++)
38 try {
39 group_svc.member_list (group_names[g]);
41 catch (PortableGroup::ObjectGroupNotFound&) {
43 catch (CORBA::Exception &ex) {
44 ACE_DEBUG ((LM_DEBUG,
45 ACE_TEXT ("Hammer thread %t caught %C accessing %C\n"),
46 ex._name(), group_names[g]));
48 if (group_names[++g] == 0)
49 g = 0;
52 return 0;
56 const ACE_TCHAR *ior = ACE_TEXT("file://obj.ior");
57 int hammers = 10;
59 int
60 parse_args (int argc, ACE_TCHAR *argv[])
62 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:n:"));
63 int c;
65 while ((c = get_opts ()) != -1)
66 switch (c)
68 case 'k':
69 ior = get_opts.opt_arg ();
70 break;
71 case 'n':
72 hammers = ACE_OS::atoi (get_opts.opt_arg ());
73 break;
74 case '?':
75 default:
76 ACE_ERROR_RETURN ((LM_ERROR,
77 ACE_TEXT ("usage: %s ")
78 ACE_TEXT ("-k <ior> ")
79 ACE_TEXT ("\n"),
80 argv [0]),
81 -1);
83 // Indicates successful parsing of the command line
84 return 0;
87 int
88 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
90 try
92 CORBA::ORB_var orb =
93 CORBA::ORB_init (argc, argv);
95 if (parse_args (argc, argv) != 0)
96 return 1;
98 ACE_DEBUG ((LM_DEBUG,
99 ACE_TEXT ("(%P|%t) - Acquiring Name Service\n")));
102 CORBA::Object_var nmobj = orb->resolve_initial_references ("NameManager");
103 FT_Naming::NamingManager_var naming_manager =
104 FT_Naming::NamingManager::_narrow (nmobj.in());
106 TAO_Naming_Client name_svc;
107 try {
108 ACE_Time_Value timeout (10); // Wait up to 10 seconds for the naming service
109 if (name_svc.init (orb.in (), &timeout) != 0)
110 ACE_ERROR_RETURN ((LM_DEBUG,
111 ACE_TEXT ("client: Could not connect to ")
112 ACE_TEXT ("naming service.\n")),
115 catch (const CORBA::Exception& ex)
117 ex._tao_print_exception (
118 ACE_TEXT ("Exception caught while initializing name ")
119 ACE_TEXT ("service facade:"));
120 return 1;
124 Hammer hammer;
126 if (hammer.setup(orb.in(), naming_manager.in()) == -1)
128 return 1;
131 CosNaming::Name name (1);
132 name.length (1);
133 name[0].id = CORBA::string_dup ("basic_name");
135 CORBA::Object_var tmp;
136 Test::Basic_var basic;
138 // Iterate enough so we get a few wrap-arounds
139 for (int i = 0; i < 15; i++)
141 if (i == 3) {
142 hammer.activate (THR_NEW_LWP | THR_JOINABLE, hammers);
145 try {
146 // Each time we invoke resolve, we get a different member
147 tmp =
148 name_svc->resolve (name);
150 // Narrow it to a Basic object
151 basic =
152 Test::Basic::_narrow (tmp.in ());
154 catch (CORBA::Exception& ex)
156 ex._tao_print_exception (ACE_TEXT ("Error resolving name.\n"));
159 if (CORBA::is_nil (basic.in ()))
161 ACE_ERROR_RETURN ((LM_DEBUG,
162 ACE_TEXT ("Server obj ref not obtained ")
163 ACE_TEXT ("from Load Balancing Name Service\n"),
164 ior),
168 try {
169 CORBA::String_var the_string =
170 basic->get_string ();
172 ACE_DEBUG ((LM_DEBUG,
173 ACE_TEXT ("(%P|%t) - Client request handled ")
174 ACE_TEXT ("by object at <%C>\n"),
175 the_string.in ()));
177 catch (CORBA::Exception& ex)
179 ex._tao_print_exception (
180 ACE_TEXT ("Error invoking get_string on Basic object.\n"));
181 return 1;
184 // Remove one member after we wrapped around to make sure naming manager can
185 // handle it successufully
186 if (i == 7)
188 try {
189 // Try removing a member
190 basic->remove_member ();
192 catch (CORBA::Exception& ex)
194 ex._tao_print_exception (
195 ACE_TEXT ("Error invoking get_string on Basic object.\n"));
196 return 1;
201 hammer.wait ();
203 try {
204 basic->shutdown ();
206 ACE_DEBUG ((LM_DEBUG,
207 ACE_TEXT ("(%P|%t) - Shutting down server\n")));
209 catch (CORBA::Exception& ex)
211 ex._tao_print_exception (
212 ACE_TEXT ("Error invoking get_string on Basic object.\n"));
213 return 1;
215 orb->destroy ();
217 catch (const CORBA::Exception& ex)
219 ex._tao_print_exception (
220 ACE_TEXT ("Exception caught in client.cpp:"));
221 return 1;
224 return 0;