Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Default_Servant2 / server.cpp
blob3504cec4662607f4061cbba0045ac549b8b7aae9
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * A server program for the File IDL module
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "File_i.h"
14 #include "tao/debug.h"
15 #include "ace/streams.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/SString.h"
18 #include "ace/OS_NS_stdio.h"
20 static const ACE_TCHAR *ior_output_file = ACE_TEXT ("ior");
22 static int
23 parse_args (int argc, ACE_TCHAR *argv[])
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:d"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'o':
32 ior_output_file = get_opts.opt_arg ();
33 break;
34 case 'd':
35 ++TAO_debug_level;
36 break;
37 case '?':
38 default:
39 ACE_ERROR_RETURN ((LM_ERROR,
40 "usage: %s "
41 "[-oior_output_file]"
42 "[-d]"
43 "\n",
44 argv [0]),
45 -1);
48 // Indicates successful parsing of command line.
49 return 0;
52 int
53 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
55 try
57 // Initialize the ORB
58 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
60 int result = parse_args (argc, argv);
61 if (result != 0)
62 return result;
64 // Obtain the RootPOA.
65 CORBA::Object_var obj =
66 orb->resolve_initial_references ("RootPOA");
68 // Narrow the object reference to a POA reference
69 PortableServer::POA_var root_poa = PortableServer::POA::_narrow (obj.in ());
71 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
73 CORBA::PolicyList policies (5);
74 policies.length (5);
76 // ID Assignment Policy
77 policies[0] =
78 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
80 // Lifespan Policy
81 policies[1] =
82 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
84 // Request Processing Policy
85 policies[2] =
86 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
88 // Servant Retention Policy
89 policies[3] =
90 root_poa->create_servant_retention_policy (PortableServer::RETAIN);
92 // Id Uniqueness Policy
93 policies[4] =
94 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
96 ACE_CString name = "firstPOA";
97 PortableServer::POA_var first_poa = root_poa->create_POA (name.c_str (),
98 poa_manager.in (),
99 policies);
101 for (CORBA::ULong i = 0;
102 i < policies.length ();
103 ++i)
105 CORBA::Policy_ptr policy = policies[i];
106 policy->destroy ();
109 // Create a File System Implementation object in first_poa
110 FileImpl::System file_system_impl (orb.in (), first_poa.in ());
112 PortableServer::ObjectId_var file_system_oid =
113 PortableServer::string_to_ObjectId ("FileSystem");
115 first_poa->activate_object_with_id (file_system_oid.in (),
116 &file_system_impl);
118 CORBA::Object_var file_system =
119 first_poa->id_to_reference (file_system_oid.in ());
121 // Get the IOR for the "FileSystem" object
122 CORBA::String_var file_system_ior =
123 orb->object_to_string (file_system.in ());
125 ACE_DEBUG ((LM_DEBUG, "%C\n",
126 file_system_ior.in ()));
128 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
129 if (output_file == 0)
130 ACE_ERROR_RETURN ((LM_ERROR,
131 "Cannot open output file %s for writing IOR",
132 ior_output_file),
133 -1);
134 ACE_OS::fprintf (output_file, "%s", file_system_ior.in ());
135 ACE_OS::fclose (output_file);
137 // set the state of the poa_manager to active i.e ready to process requests
138 poa_manager->activate ();
140 // Run the ORB
141 orb->run ();
143 orb->destroy ();
145 catch (const CORBA::Exception& ex)
147 ex._tao_print_exception ("EXCEPTION CAUGHT");
148 return -1;
151 return 0;