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 "Push_Web_ServerC.h"
8 #include "Push_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")),
31 // Initialize the ORB.
32 CORBA::ORB_var orb
= CORBA::ORB_init (argc
,
37 CORBA::Object_var obj
=
38 orb
->resolve_initial_references ("RootPOA");
40 PortableServer::POA_var poa
=
41 PortableServer::POA::_narrow (obj
.in ());
43 // Activate the POA manager.
44 PortableServer::POAManager_var mgr
= poa
->the_POAManager ();
47 // Get an Iterator_Factory reference.
48 Web_Server::Iterator_Factory_var factory
=
49 ::get_iterator (orb
.in ());
51 if (CORBA::is_nil (factory
.in ()))
53 ACE_ERROR_RETURN ((LM_ERROR
,
54 ACE_TEXT ("Object pointed to by:\n %s\n")
55 ACE_TEXT ("is not an Iterator_Factory")
56 ACE_TEXT ("object.\n"),
61 // Variable used to keep track of when file retrieval has
63 int request_count
= 0;
65 // Activate and run the reply handlers.
66 ::invoke_requests (argc
,
71 // 1 millisecond delay to reduce "busy waiting" in ORB event
73 ACE_Time_Value
tv (0, 1000);
75 // Run the ORB event loop.
76 while (request_count
> 0)
78 CORBA::Boolean more_work
;
80 more_work
= orb
->work_pending ();
94 catch (const Web_Server::Error_Result
& exc
)
96 ACE_ERROR_RETURN ((LM_ERROR
,
97 ACE_TEXT ("Caught Web Server exception ")
98 ACE_TEXT ("status %d\n"),
102 catch (const CORBA::Exception
& ex
)
104 ex
._tao_print_exception (ACE_TEXT ("Caught unexpected exception:"));
109 // Wait for all spawned viewers to exit.
110 ACE_Process_Manager::instance ()->wait ();
115 Web_Server::Iterator_Factory_ptr
116 get_iterator (CORBA::ORB_ptr o
)
118 CORBA::ORB_var orb
= CORBA::ORB::_duplicate (o
);
120 // Get a reference to the Name Service.
121 CORBA::Object_var obj
=
122 orb
->resolve_initial_references ("NameService");
124 // Narrow to a Naming Context
125 CosNaming::NamingContext_var nc
=
126 CosNaming::NamingContext::_narrow (obj
.in ());
128 if (CORBA::is_nil (obj
.in ()))
130 ACE_ERROR ((LM_ERROR
,
131 ACE_TEXT ("Nil reference to Name Service\n")));
132 return Web_Server::Iterator_Factory::_nil ();
136 CosNaming::Name name
;
138 name
[0].id
= CORBA::string_dup ("Push_Iterator_Factory");
139 name
[0].kind
= CORBA::string_dup ("");
141 obj
= nc
->resolve (name
);
143 Web_Server::Iterator_Factory_ptr factory
=
144 Web_Server::Iterator_Factory::_narrow (obj
.in ());
149 void invoke_requests (int argc
,
152 Web_Server::Iterator_Factory_ptr f
)
154 Web_Server::Iterator_Factory_var factory
=
155 Web_Server::Iterator_Factory::_duplicate (f
);
157 // Activate and run the reply handlers.
159 i
< argc
- 1; // Don't include the program name.
162 Push_Iterator_Handler
*handler
= 0;
163 ACE_NEW_THROW_EX (handler
,
164 Push_Iterator_Handler
,
165 CORBA::NO_MEMORY ());
167 // Transfer ownership to the POA.
168 PortableServer::ServantBase_var
tmp (handler
);
170 // This ends up being an AMI call, so it won't block.
171 handler
->run (request_count
,
172 ACE_TEXT_ALWAYS_CHAR(argv
[i
+ 1]),