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
,
18 Web_Server::Iterator_Factory_ptr f
);
21 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
26 ACE_ERROR_RETURN ((LM_ERROR
,
27 ACE_TEXT ("Usage: client filename ")
28 ACE_TEXT ("[filename ...]\n")),
30 // Initialize the ORB.
31 CORBA::ORB_var orb
= CORBA::ORB_init (argc
,
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 ();
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"),
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
63 int request_count
= 0;
65 ::invoke_requests (argc
,
70 // Run the ORB event loop.
71 while (request_count
> 0)
73 CORBA::Boolean more_work
;
75 more_work
= orb
->work_pending ();
85 orb
->shutdown (false);
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"),
97 catch (const CORBA::Exception
& ex
)
99 ex
._tao_print_exception (ACE_TEXT ("Caught unexpected exception:"));
104 // Wait for all children to exit.
105 ACE_Process_Manager::instance ()->wait ();
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 ();
131 CosNaming::Name name
;
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 ());
144 void invoke_requests (int argc
,
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.
154 i
< argc
- 1; // Don't include the program name.
157 Iterator_Handler
*handler
= 0;
158 ACE_NEW_THROW_EX (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]),