Update bug_report.md
[ACE_TAO.git] / TAO / orbsvcs / examples / FaultTolerance / RolyPoly / server.cpp
blobf96b1eda01e76edac79f87bba7b5c291a2839ba0
1 // file : RolyPoly/server.cpp
2 // author : Boris Kolpackov <boris@dre.vanderbilt.edu>
3 #include "ace/Get_Opt.h"
4 #include "ace/OS_NS_stdio.h"
6 #include "RolyPoly_i.h"
7 #include "CrashPoint.h"
8 #include "ORB_Initializer.h"
9 #include "tao/ORBInitializer_Registry.h"
11 const ACE_TCHAR *ior_file = 0;
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:c:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'o':
23 ior_file = get_opts.opt_arg ();
24 break;
25 case 'c':
26 crash_point = ACE_OS::atoi (get_opts.opt_arg ());
27 break;
28 default:
29 ACE_ERROR_RETURN ((LM_ERROR,
30 "Usage: %s "
31 "-o <IOR> "
32 "-c <CrashPoint>\n",
33 argv[0]),
34 -1);
37 return 0;
40 int
41 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
43 try
45 if (::parse_args (argc, argv) != 0) return -1;
47 ORB_Initializer *temp_initializer = 0;
48 ACE_NEW_RETURN (temp_initializer,
49 ORB_Initializer,
50 -1); // No exceptions yet!
52 PortableInterceptor::ORBInitializer_var orb_initializer =
53 temp_initializer;
55 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
57 CORBA::ORB_var orb =
58 CORBA::ORB_init (argc, argv, "Server ORB");
60 CORBA::Object_var poa_object =
61 orb->resolve_initial_references ("RootPOA");
63 if (CORBA::is_nil (poa_object.in ()))
64 ACE_ERROR_RETURN ((LM_ERROR,
65 " (%P|%t) Unable to initialize the POA.\n"),
66 1);
68 PortableServer::POA_var root_poa =
69 PortableServer::POA::_narrow (poa_object.in ());
71 PortableServer::POAManager_var poa_manager =
72 root_poa->the_POAManager ();
74 RolyPoly_i* roly_poly_impl;
76 ACE_NEW_RETURN (roly_poly_impl,
77 RolyPoly_i (orb.in ()),
78 1);
80 PortableServer::ServantBase_var owner_transfer (roly_poly_impl);
82 RolyPoly_var t =
83 roly_poly_impl->_this ();
85 CORBA::PolicyList policies; // Empty policy list.
87 CORBA::String_var ior =
88 orb->object_to_string (t.in ());
90 poa_manager->activate ();
92 FILE *output_file= ACE_OS::fopen (ior_file, "w");
93 if (output_file == 0)
95 ACE_ERROR_RETURN ((LM_ERROR,
96 "Cannot open output file <%s> for writing "
97 "IOR: %s",
98 ior.in ()),
99 1);
102 ACE_OS::fprintf (output_file, "%s", ior.in ());
103 ACE_OS::fclose (output_file);
105 ACE_DEBUG ((LM_DEBUG, "Server is ready\n"));
107 // Run the ORB event loop.
108 orb->run ();
110 root_poa->destroy (true, true);
112 orb->destroy ();
114 ACE_DEBUG ((LM_DEBUG, "Event loop finished.\n"));
116 catch (const CORBA::Exception& ex)
118 ex._tao_print_exception ("Caught exception:");
119 return -1;
122 return 0;