Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / Bug_2604_Regression / MessengerServer.cpp
blobbc8ddefc3372e25f9189f0029db48461fdbce160
1 #include "Messenger_i.h"
3 #include "tao/ImR_Client/ImR_Client.h"
5 #include "ace/SString.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/OS_NS_string.h"
8 #include "ace/OS_NS_stdio.h"
9 #include "ace/Get_Opt.h"
11 const ACE_TCHAR *ior_output_file = ACE_TEXT ("messenger.ior");
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'o':
23 ior_output_file = get_opts.opt_arg ();
24 break;
26 case '?':
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "usage: %s "
30 "-o <iorfile>"
31 "\n",
32 argv [0]),
33 -1);
35 // Indicates successful parsing of the command line
36 return 0;
39 PortableServer::POA_ptr
40 createPersistentPOA (PortableServer::POA_ptr root_poa, const char *poa_name)
42 CORBA::PolicyList policies;
43 policies.length (2);
45 policies[0] = root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
46 policies[1] =
47 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
49 PortableServer::POAManager_var mgr = root_poa->the_POAManager ();
51 PortableServer::POA_var poa = root_poa->create_POA (poa_name, mgr.in (),
52 policies);
53 policies[0]->destroy ();
54 policies[1]->destroy ();
56 return poa._retn ();
59 void
60 writeIORFile (const char* ior)
62 FILE *out = ACE_OS::fopen (ACE_TEXT_ALWAYS_CHAR(ior_output_file), "w");
63 if (out)
65 ACE_OS::fprintf (out, "%s", ior);
66 ACE_OS::fclose (out);
70 int
71 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
73 try
75 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
77 if (parse_args (argc, argv) != 0)
78 return 1;
80 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Server Process started\n")));
82 CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA");
83 PortableServer::POA_var root_poa =
84 PortableServer::POA::_narrow (obj.in ());
86 PortableServer::POAManager_var mgr = root_poa->the_POAManager ();
88 const char* poa_name = "MessengerService";
90 PortableServer::POA_var poa = createPersistentPOA (root_poa.in (),
91 poa_name);
92 Messenger_i servant (orb.in ());
94 PortableServer::ObjectId_var object_id =
95 PortableServer::string_to_ObjectId ("object");
97 poa->activate_object_with_id (object_id.in (), &servant);
98 obj = poa->id_to_reference (object_id.in ());
99 CORBA::String_var ior = orb->object_to_string (obj.in ());
101 writeIORFile (ior.in ());
103 mgr->activate ();
104 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Messenger server ready.\n")));
106 orb->run ();
108 // the following 1 second of sleep is needed to help
109 // Windows with "server shutdown too quickly so the
110 // client cannot get the reply" issue.
111 ACE_OS::sleep(1);
112 ACE_DEBUG ((LM_DEBUG,
113 ACE_TEXT ("(%P|%t) Messenger server shutting down.\n")));
114 root_poa->destroy (1, 1);
115 orb->destroy ();
116 return 0;
118 catch (const CORBA::Exception &ex)
120 ACE_CString str = ex._info ();
121 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Server main() caught: %C\n"),
122 str.c_str ()));
124 return -1;