Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Content_Server / AMI_Observer / client.cpp
blob0d9e236c5113154c6693b274dc89d6176d66a38b
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 "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,
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);
31 // Initialize the ORB.
32 CORBA::ORB_var orb = CORBA::ORB_init (argc,
33 argv,
34 "Mighty ORB");
36 // Get the Root POA.
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 ();
45 mgr->activate ();
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"),
57 argv[1]),
58 -1);
61 // Variable used to keep track of when file retrieval has
62 // completed.
63 int request_count = 0;
65 // Activate and run the reply handlers.
66 ::invoke_requests (argc,
67 argv,
68 &request_count,
69 factory.in ());
71 // 1 millisecond delay to reduce "busy waiting" in ORB event
72 // loop.
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 ();
82 if (more_work)
84 orb->perform_work ();
86 else
87 ACE_OS::sleep (tv);
90 orb->shutdown (0);
92 //orb->destroy ();
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"),
99 exc.status),
100 -1);
102 catch (const CORBA::Exception& ex)
104 ex._tao_print_exception (ACE_TEXT ("Caught unexpected exception:"));
106 return -1;
109 // Wait for all spawned viewers to exit.
110 ACE_Process_Manager::instance ()->wait ();
112 return 0;
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 ();
135 // Create a name.
136 CosNaming::Name name;
137 name.length (1);
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 ());
146 return factory;
149 void invoke_requests (int argc,
150 ACE_TCHAR *argv[],
151 int *request_count,
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.
158 for (int i = 0;
159 i < argc - 1; // Don't include the program name.
160 ++i)
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]),
173 factory.in ());