Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Reference_Counted_Servant / server.cpp
blob0746c7199049bb4847c6a4c97c842555093d77db
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[])
104 // Initialize the ORB first.
105 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
107 int result = parse_args (argc, argv);
108 if (result != 0)
109 return result;
111 // Obtain the RootPOA.
112 CORBA::Object_var obj =
113 orb->resolve_initial_references ("RootPOA");
115 // Get the POA_var object from Object_var.
116 PortableServer::POA_var root_poa =
117 PortableServer::POA::_narrow (obj.in ());
119 // Get the POAManager of the RootPOA.
120 PortableServer::POAManager_var poa_manager =
121 root_poa->the_POAManager ();
123 // Create a servant.
124 reference_counted_test_i *servant = 0;
125 ACE_NEW_RETURN (servant,
126 reference_counted_test_i (orb.in (),
127 root_poa.in ()),
128 -1);
130 // Get Object Reference for the servant object.
131 test_var test = servant->_this ();
133 // This means that the ownership of <servant> now belongs to
134 // the POA.
135 servant->_remove_ref ();
137 // Stringyfy all the object references and print them out.
138 CORBA::String_var ior = orb->object_to_string (test.in ());
140 ACE_DEBUG ((LM_DEBUG,
141 "%C\n",
142 ior.in ()));
144 int write_result = write_iors_to_file (ior.in ());
145 if (write_result != 0)
146 return write_result;
148 poa_manager->activate ();
150 orb->run ();
152 orb->destroy ();
154 catch (const CORBA::Exception& ex)
156 ex._tao_print_exception ("Exception caught");
157 return -1;
160 return 0;