Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / POA / DSI / server.cpp
blobbf94db7344f4bcc2d12c2e6af7104bcc9cb40285
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * A server program for the File IDL module
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "Database_i.h"
14 #include "ace/Get_Opt.h"
15 #include "ace/SString.h"
17 static const ACE_TCHAR *ior_output_file = ACE_TEXT ("ior");
19 static int
20 parse_args (int argc, ACE_TCHAR **argv)
22 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
23 int c;
25 while ((c = get_opts ()) != -1)
26 switch (c)
28 case 'f':
29 ior_output_file = get_opts.opt_arg ();
30 break;
31 case '?':
32 default:
33 ACE_ERROR_RETURN ((LM_ERROR,
34 "usage: %s "
35 "[-f ior_output_file] "
36 "\n",
37 argv [0]),
38 -1);
41 // Indicates successful parsing of command line.
42 return 0;
45 static int
46 write_iors_to_file (const char *first_ior)
48 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
50 if (output_file == 0)
51 ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output files for writing IOR: %s\n",
52 ior_output_file),
53 -1);
55 int result = 0;
57 result = ACE_OS::fprintf (output_file,
58 "%s",
59 first_ior);
61 ACE_OS::fclose (output_file);
63 if (result < 0 ||
64 static_cast<size_t> (result) != ACE_OS::strlen (first_ior))
65 ACE_ERROR_RETURN ((LM_ERROR,
66 "ACE_OS::fprintf failed while writing %C to %s\n",
67 first_ior,
68 ior_output_file),
69 -1);
71 return 0;
74 int
75 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
77 try
79 // Initialize the ORB
80 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
82 int result = parse_args (argc, argv);
83 if (result != 0)
84 return result;
86 // Obtain the RootPOA.
87 CORBA::Object_var obj =
88 orb->resolve_initial_references ("RootPOA");
90 // Narrow the object reference to a POA reference
91 PortableServer::POA_var root_poa = PortableServer::POA::_narrow (obj.in ());
93 PortableServer::POAManager_var poa_manager =
94 root_poa->the_POAManager ();
96 CORBA::PolicyList policies (5);
97 policies.length (5);
99 // ID Assignment Policy
100 policies[0] =
101 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
103 // Lifespan Policy
104 policies[1] =
105 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
107 // Request Processing Policy
108 policies[2] =
109 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
111 // Servant Retention Policy
112 policies[3] =
113 root_poa->create_servant_retention_policy (PortableServer::RETAIN);
115 // Id Uniqueness Policy
116 policies[4] =
117 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
120 ACE_CString name = "firstPOA";
121 PortableServer::POA_var first_poa = root_poa->create_POA (name.c_str (),
122 poa_manager.in (),
123 policies);
125 for (CORBA::ULong i = 0;
126 i < policies.length ();
127 ++i)
129 CORBA::Policy_ptr policy = policies[i];
130 policy->destroy ();
133 // Create a Database Agent Implementation object in first_poa
134 DatabaseImpl::Agent database_agent_impl (orb.in (),
135 first_poa.in ());
137 PortableServer::ObjectId_var database_agent_oid =
138 PortableServer::string_to_ObjectId ("DatabaseAgent");
140 first_poa->activate_object_with_id (database_agent_oid.in (),
141 &database_agent_impl);
143 CORBA::Object_var database_agent =
144 first_poa->id_to_reference (database_agent_oid.in ());
146 // Get the IOR for the "DatabaseAgent" object
147 CORBA::String_var database_agent_ior =
148 orb->object_to_string (database_agent.in ());
150 ACE_DEBUG ((LM_DEBUG,"%C\n",
151 database_agent_ior.in ()));
153 int write_result = write_iors_to_file (database_agent_ior.in ());
154 if (write_result != 0)
155 return write_result;
157 // set the state of the poa_manager to active i.e ready to process requests
158 poa_manager->activate ();
160 // Run the ORB
161 orb->run ();
163 orb->destroy ();
165 catch (const CORBA::Exception& ex)
167 ex._tao_print_exception ("Exception caught");
168 return -1;
171 return 0;