Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / POA / Reference_Counted_Servant / server.cpp
blob36da182f180e496d49744a212b1b94a551995002
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * This example shows how to use reference counted servants to
7 * automatically manage dynamic memory for servants.
8 * Stubs/Skeletons and client code is available in
9 * ../Generic_Servant.
11 * @author Irfan Pyarali
13 //=============================================================================
16 #include "ace/Get_Opt.h"
17 #include "test_i.h"
18 #include "ace/OS_NS_stdio.h"
19 #include "ace/OS_NS_string.h"
21 // This is to remove "inherits via dominance" warnings from MSVC.
22 // MSVC is being a little too paranoid.
23 #if defined (_MSC_VER)
24 # pragma warning (disable : 4250)
25 #endif /* _MSC_VER */
27 class reference_counted_test_i :
28 public virtual test_i
30 public:
31 /// Constructor - takes a POA and a value parameter
32 reference_counted_test_i (CORBA::ORB_ptr orb,
33 PortableServer::POA_ptr poa);
36 reference_counted_test_i::reference_counted_test_i (CORBA::ORB_ptr orb,
37 PortableServer::POA_ptr poa)
38 : test_i (orb,
39 poa)
43 static const ACE_TCHAR *ior_output_file = ACE_TEXT ("ior");
45 static int
46 parse_args (int argc, ACE_TCHAR **argv)
48 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
49 int c;
51 while ((c = get_opts ()) != -1)
52 switch (c)
54 case 'f':
55 ior_output_file = get_opts.opt_arg ();
56 break;
58 case '?':
59 default:
60 ACE_ERROR_RETURN ((LM_ERROR,
61 "usage: %s "
62 "[-f ior_output_file]"
63 "\n",
64 argv [0]),
65 -1);
68 // Indicates successful parsing of command line.
69 return 0;
72 static int
73 write_iors_to_file (const char *ior)
75 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
77 if (output_file == 0)
78 ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output files for writing IORs: %s\n",
79 ior_output_file),
80 -1);
82 u_int result = 0;
84 result = ACE_OS::fprintf (output_file,
85 "%s",
86 ior);
87 ACE_OS::fclose (output_file);
89 if (result != ACE_OS::strlen (ior))
90 ACE_ERROR_RETURN ((LM_ERROR,
91 "ACE_OS::fprintf failed while writing %C to %s\n",
92 ior,
93 ior_output_file),
94 -1);
96 return 0;
99 int
100 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
105 // Initialize the ORB first.
106 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
108 int result = parse_args (argc, argv);
109 if (result != 0)
110 return result;
112 // Obtain the RootPOA.
113 CORBA::Object_var obj =
114 orb->resolve_initial_references ("RootPOA");
116 // Get the POA_var object from Object_var.
117 PortableServer::POA_var root_poa =
118 PortableServer::POA::_narrow (obj.in ());
120 // Get the POAManager of the RootPOA.
121 PortableServer::POAManager_var poa_manager =
122 root_poa->the_POAManager ();
124 // Create a servant.
125 reference_counted_test_i *servant = 0;
126 ACE_NEW_RETURN (servant,
127 reference_counted_test_i (orb.in (),
128 root_poa.in ()),
129 -1);
131 // Get Object Reference for the servant object.
132 test_var test = servant->_this ();
134 // This means that the ownership of <servant> now belongs to
135 // the POA.
136 servant->_remove_ref ();
138 // Stringyfy all the object references and print them out.
139 CORBA::String_var ior = orb->object_to_string (test.in ());
141 ACE_DEBUG ((LM_DEBUG,
142 "%C\n",
143 ior.in ()));
145 int write_result = write_iors_to_file (ior.in ());
146 if (write_result != 0)
147 return write_result;
149 poa_manager->activate ();
151 orb->run ();
153 orb->destroy ();
155 catch (const CORBA::Exception& ex)
157 ex._tao_print_exception ("Exception caught");
158 return -1;
161 return 0;