Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / IORTable_Locator / async_server.cpp
blob9c7f7d49b39481ff1b325c1b4866dd9004914964
1 #include "simple_test_i.h"
2 #include "ace/OS_NS_stdio.h"
3 #include "ace/OS_NS_strings.h"
5 #include "tao/ORB_Core.h"
6 #include "tao/IORTable/Async_IORTable.h"
7 #include "tao/IORTable/IORTable.h"
8 #include "tao/LocalObject.h"
9 #include "tao/PortableServer/PortableServer.h"
11 CORBA::String_var testIOR;
13 ACE_CString ior_table_name = "IORTable";
14 bool use_async_locator = false;
16 //*************************************************************************
17 class AsyncLocatorTask
18 : public virtual IORTable::AsyncLocator,
19 public virtual CORBA::LocalObject
21 public:
22 AsyncLocatorTask (CORBA::ORB_ptr orb);
24 void async_locate (IORTable::Locate_ResponseHandler rh, const char *);
25 char * locate (const char *);
27 private:
28 CORBA::ORB_var orb_;
31 void
32 AsyncLocatorTask::async_locate (IORTable::Locate_ResponseHandler rh, const char * id)
34 CORBA::Object_var fwdObj = orb_->string_to_object(testIOR.in ());
35 CORBA::String_var fwdString = orb_->object_to_string(fwdObj.in ());
37 ACE_DEBUG ((LM_DEBUG,
38 ACE_TEXT ("in async_locate! Forwarding client %C to: %C\n"),
39 id, fwdString.in ()));
41 rh->forward_ior (fwdString._retn(), false);
44 char *
45 AsyncLocatorTask::locate (const char * id)
47 ACE_DEBUG ((LM_DEBUG,
48 ACE_TEXT ("In async locator::locate, id = %C, throwing\n"),
49 id));
50 throw IORTable::NotFound ();
51 return 0;
55 AsyncLocatorTask::AsyncLocatorTask (CORBA::ORB_ptr orb)
56 : orb_ (CORBA::ORB::_duplicate (orb))
60 //*************************************************************************
61 class SyncLocatorTask
62 : public virtual IORTable::Locator,
63 public virtual CORBA::LocalObject
65 public:
66 SyncLocatorTask (CORBA::ORB_ptr orb);
68 char * locate (const char *);
70 private:
71 CORBA::ORB_var orb_;
74 char *
75 SyncLocatorTask::locate (const char * id)
77 CORBA::Object_var fwdObj = orb_->string_to_object(testIOR.in ());
78 CORBA::String_var fwdString = orb_->object_to_string(fwdObj.in ());
80 ACE_DEBUG ((LM_DEBUG,
81 ACE_TEXT ("in synch locate: Forwarding client %C to: %C\n"),
82 id,
83 fwdString.in ()));
85 return fwdString._retn();
88 SyncLocatorTask::SyncLocatorTask (CORBA::ORB_ptr orb)
89 : orb_ (CORBA::ORB::_duplicate (orb))
93 int
94 parse_args (int argc, ACE_TCHAR *argv[])
96 for (int c = 1; c < argc; c++)
98 if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-aa")) == 0)
100 ior_table_name = "AsyncIORTable";
101 use_async_locator = true;
103 else if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-as")) == 0)
105 ior_table_name = "AsyncIORTable";
106 use_async_locator = false;
108 else
110 return -1;
113 return 0;
117 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
121 CORBA::ORB_var orb =
122 CORBA::ORB_init (argc, argv);
124 if (parse_args (argc, argv) == -1)
125 return -1;
127 CORBA::Object_var poa_object =
128 orb->resolve_initial_references ("RootPOA");
130 PortableServer::POA_var root_poa =
131 PortableServer::POA::_narrow (poa_object.in ());
133 if (CORBA::is_nil (root_poa.in ()))
134 ACE_ERROR_RETURN ((LM_ERROR,
135 " (%P|%t) Panic: nil RootPOA\n"),
138 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
140 Simple_Test_i *test_impl = 0;
141 ACE_NEW_RETURN (test_impl,
142 Simple_Test_i (orb.in ()),
144 PortableServer::ServantBase_var owner_transfer (test_impl);
146 PortableServer::ObjectId_var id =
147 root_poa->activate_object (test_impl);
149 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
151 simple::SimpleTest_T_var theServer =
152 simple::SimpleTest_T::_narrow (object.in ());
154 testIOR = orb->object_to_string (theServer.in ());
156 poa_manager->activate ();
158 // Create task.
159 IORTable::Locator_ptr iorloc_ptr;
160 if (use_async_locator)
162 ACE_NEW_RETURN (iorloc_ptr,
163 AsyncLocatorTask (orb.in ()),
166 else
168 ACE_NEW_RETURN (iorloc_ptr,
169 SyncLocatorTask (orb.in ()),
172 IORTable::Locator_var iorloc = iorloc_ptr;
174 ACE_DEBUG ((LM_DEBUG,
175 ACE_TEXT ("Server - using ior table object: %C\n"),
176 ior_table_name.c_str()));
177 // Resolve the IORTable and set the locator
178 CORBA::Object_var iorTableObj =
179 orb->resolve_initial_references(ior_table_name.c_str());
181 IORTable::Table_var local_ior_table =
182 IORTable::Table::_narrow (iorTableObj.in ());
184 local_ior_table->set_locator (iorloc.in());
186 orb->run ();
188 ACE_DEBUG ((LM_DEBUG,
189 ACE_TEXT ("Server - event loop finished\n")));
191 root_poa->destroy (1, 1);
193 orb->destroy ();
195 catch (const CORBA::Exception& ex)
197 ex._tao_print_exception (ACE_TEXT ("Server - Exception caught:"));
198 return 1;
201 return 0;