Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / POA / On_Demand_Act_Direct_Coll / Server_Task.cpp
blob8d7e87ee6899b3d6dbafd57574da77dad4fb27a8
1 #include "Server_Task.h"
2 #include "Servant_Activator.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/Manual_Event.h"
6 static const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
8 static int
9 write_iors_to_file (const char *first_ior)
11 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
12 if (output_file == 0 )
13 ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output files for writing IORs: %Cn",
14 ior_output_file),
15 -1);
17 int result = ACE_OS::fprintf (output_file,
18 "%s",
19 first_ior);
21 ACE_OS::fclose (output_file);
23 if (result <= 0
24 || static_cast<size_t> (result) != ACE_OS::strlen (first_ior))
25 ACE_ERROR_RETURN ((LM_ERROR,
26 "ACE_OS::fprintf failed while writing %C to %s\n",
27 first_ior,
28 ior_output_file),
29 -1);
31 return 0;
34 Server_Task::Server_Task (const ACE_TCHAR *output,
35 CORBA::ORB_ptr sorb,
36 ACE_Manual_Event &me,
37 ACE_Thread_Manager *thr_mgr)
38 : ACE_Task_Base (thr_mgr)
39 , output_ (output)
40 , me_ (me)
41 , sorb_ (CORBA::ORB::_duplicate (sorb))
45 int
46 Server_Task::svc (void)
48 try
50 CORBA::Object_var poa_object =
51 this->sorb_->resolve_initial_references("RootPOA");
53 PortableServer::POA_var root_poa =
54 PortableServer::POA::_narrow (poa_object.in ());
56 if (CORBA::is_nil (root_poa.in ()))
57 ACE_ERROR_RETURN ((LM_ERROR,
58 " (%P|%t) Panic: nil RootPOA\n"),
59 1);
61 PortableServer::POAManager_var poa_manager =
62 root_poa->the_POAManager ();
65 CORBA::PolicyList policies (4);
66 policies.length (4);
68 // ID Assignment Policy
69 policies[0] =
70 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
72 // Lifespan Policy
73 policies[1] =
74 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
76 // Request Processing Policy
77 policies[2] =
78 root_poa->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
80 PortableServer::POA_var first_poa;
82 // Servant Retention Policy
83 policies[3] =
84 root_poa->create_servant_retention_policy (PortableServer::RETAIN);
86 ACE_CString name = "firstPOA";
88 // Create firstPOA as the child of RootPOA with the above policies
89 // firstPOA will use SERVANT_ACTIVATOR because of RETAIN policy.
90 first_poa = root_poa->create_POA (name.c_str (),
91 poa_manager.in (),
92 policies);
96 // Destroy the policy objects as they have been passed to
97 // create_POA and no longer needed.
98 for (CORBA::ULong i = 0;
99 i < policies.length ();
100 ++i)
102 CORBA::Policy_ptr policy = policies[i];
103 policy->destroy ();
105 // Allocate the servant activator.
106 ServantActivator activator (this->sorb_.in (), ACE_Thread::self ());
107 // Set ServantActivator object as the servant_manager of
108 // firstPOA.
110 first_poa->set_servant_manager (&activator);
111 // Create a reference with user created ID in firstPOA which uses
112 // the ServantActivator.
114 PortableServer::ObjectId_var first_test_oid =
115 PortableServer::string_to_ObjectId ("first test");
117 CORBA::Object_var first_test =
118 first_poa->create_reference_with_id (first_test_oid.in (), "IDL:test:1.0");
119 // Invoke object_to_string on the references created in firstPOA
121 CORBA::String_var first_test_ior =
122 this->sorb_->object_to_string (first_test.in ());
124 // Print the ior's of first_test.
125 ACE_DEBUG((LM_DEBUG,"<%C>\n",
126 first_test_ior.in ()));
128 int write_result = write_iors_to_file (first_test_ior.in ());
129 if (write_result != 0)
130 return write_result;
132 // Set the poa_manager state to active, ready to process requests.
133 poa_manager->activate ();
134 this->me_.signal ();
135 // Run the ORB.
136 this->sorb_->run ();
138 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
140 catch (const CORBA::Exception& ex)
142 ex._tao_print_exception ("Exception caught:");
143 return 1;
146 return 0;