Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / On_Demand_Act_Direct_Coll / Server_Task.cpp
blob6f3c5e42c9875b0a4fe691d165d6e6856815e22a
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 ()
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);
95 // Destroy the policy objects as they have been passed to
96 // create_POA and no longer needed.
97 for (CORBA::ULong i = 0;
98 i < policies.length ();
99 ++i)
101 CORBA::Policy_ptr policy = policies[i];
102 policy->destroy ();
104 // Allocate the servant activator.
105 ServantActivator activator (this->sorb_.in (), ACE_Thread::self ());
106 // Set ServantActivator object as the servant_manager of
107 // firstPOA.
109 first_poa->set_servant_manager (&activator);
110 // Create a reference with user created ID in firstPOA which uses
111 // the ServantActivator.
113 PortableServer::ObjectId_var first_test_oid =
114 PortableServer::string_to_ObjectId ("first test");
116 CORBA::Object_var first_test =
117 first_poa->create_reference_with_id (first_test_oid.in (), "IDL:test:1.0");
118 // Invoke object_to_string on the references created in firstPOA
120 CORBA::String_var first_test_ior =
121 this->sorb_->object_to_string (first_test.in ());
123 // Print the ior's of first_test.
124 ACE_DEBUG((LM_DEBUG,"<%C>\n",
125 first_test_ior.in ()));
127 int write_result = write_iors_to_file (first_test_ior.in ());
128 if (write_result != 0)
129 return write_result;
131 // Set the poa_manager state to active, ready to process requests.
132 poa_manager->activate ();
133 this->me_.signal ();
134 // Run the ORB.
135 this->sorb_->run ();
137 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
139 catch (const CORBA::Exception& ex)
141 ex._tao_print_exception ("Exception caught:");
142 return 1;
145 return 0;