Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / examples / Content_Server / AMI_Iterator / client.cpp
blob8ab5c5057c7a359ce830f140b9697a3fa88dc337
1 // -*- C++ -*-
2 // Ossama Othman <ossama@uci.edu>
4 #include "ace/Process_Manager.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "orbsvcs/CosNamingC.h"
7 #include "Web_ServerC.h"
8 #include "Iterator_Handler.h"
10 // Obtain reference to Iterator_Factory
11 Web_Server::Iterator_Factory_ptr
12 get_iterator (CORBA::ORB_ptr orb);
14 // Perform file requests
15 void invoke_requests (int argc,
16 ACE_TCHAR *argv[],
17 int *request_count,
18 Web_Server::Iterator_Factory_ptr f);
20 int
21 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
23 try
25 if (argc < 2)
26 ACE_ERROR_RETURN ((LM_ERROR,
27 ACE_TEXT ("Usage: client filename ")
28 ACE_TEXT ("[filename ...]\n")),
29 -1);
30 // Initialize the ORB.
31 CORBA::ORB_var orb = CORBA::ORB_init (argc,
32 argv,
33 "Mighty ORB");
35 // Get the Root POA.
36 CORBA::Object_var obj =
37 orb->resolve_initial_references ("RootPOA");
39 PortableServer::POA_var poa =
40 PortableServer::POA::_narrow (obj.in ());
42 // Activate the POA manager.
43 PortableServer::POAManager_var mgr = poa->the_POAManager ();
44 mgr->activate ();
46 // Get an Iterator_Factory reference.
47 Web_Server::Iterator_Factory_var factory =
48 ::get_iterator (orb.in ());
50 if (CORBA::is_nil (factory.in ()))
51 ACE_ERROR_RETURN ((LM_ERROR,
52 ACE_TEXT ("Object pointed to by:\n %s\n")
53 ACE_TEXT ("is not an Iterator_Factory object.\n"),
54 argv[1]),
55 -1);
57 // 1 millisecond delay to reduce "busy waiting" in ORB event
58 // loop. (simulating "work")
59 ACE_Time_Value tv (0, 1000);
61 // Variable used to keep track of when file retrieval has
62 // completed.
63 int request_count = 0;
65 ::invoke_requests (argc,
66 argv,
67 &request_count,
68 factory.in ());
70 // Run the ORB event loop.
71 while (request_count > 0)
73 CORBA::Boolean more_work;
75 more_work = orb->work_pending ();
77 if (more_work)
79 orb->perform_work ();
81 else
82 ACE_OS::sleep (tv);
85 orb->shutdown (false);
87 orb->destroy ();
89 catch (const Web_Server::Error_Result& exc)
91 ACE_ERROR_RETURN ((LM_ERROR,
92 ACE_TEXT ("Caught Web Server exception with ")
93 ACE_TEXT ("status %d\n"),
94 exc.status),
95 -1);
97 catch (const CORBA::Exception& ex)
99 ex._tao_print_exception (ACE_TEXT ("Caught unexpected exception:"));
101 return -1;
104 // Wait for all children to exit.
105 ACE_Process_Manager::instance ()->wait ();
107 return 0;
110 Web_Server::Iterator_Factory_ptr
111 get_iterator (CORBA::ORB_ptr o)
113 CORBA::ORB_var orb = CORBA::ORB::_duplicate (o);
115 // Get a reference to the Name Service.
116 CORBA::Object_var obj =
117 orb->resolve_initial_references ("NameService");
119 // Narrow to a Naming Context
120 CosNaming::NamingContext_var nc =
121 CosNaming::NamingContext::_narrow (obj.in ());
123 if (CORBA::is_nil (obj.in ()))
125 ACE_ERROR ((LM_ERROR,
126 ACE_TEXT ("Nil reference to Name Service\n")));
127 return Web_Server::Iterator_Factory::_nil ();
130 // Create a name.
131 CosNaming::Name name;
132 name.length (1);
133 name[0].id = CORBA::string_dup ("Iterator_Factory");
134 name[0].kind = CORBA::string_dup ("");
136 obj = nc->resolve (name);
138 Web_Server::Iterator_Factory_ptr factory =
139 Web_Server::Iterator_Factory::_narrow (obj.in ());
141 return factory;
144 void invoke_requests (int argc,
145 ACE_TCHAR *argv[],
146 int *request_count,
147 Web_Server::Iterator_Factory_ptr f)
149 Web_Server::Iterator_Factory_var factory =
150 Web_Server::Iterator_Factory::_duplicate (f);
152 // Activate and run the reply handlers.
153 for (int i = 0;
154 i < argc - 1; // Don't include the program name.
155 ++i)
157 Iterator_Handler *handler = 0;
158 ACE_NEW_THROW_EX (handler,
159 Iterator_Handler,
160 CORBA::NO_MEMORY ());
162 // Transfer ownership to the POA.
163 PortableServer::ServantBase_var tmp (handler);
165 // This ends up being an AMI call, so it won't block.
166 handler->run (request_count,
167 ACE_TEXT_ALWAYS_CHAR(argv[i + 1]),
168 factory.in ());