Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / CodeSets / simple / server.cpp
blob0076baf6e29c2d129ab8af8471bf5144b55ced92
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file server.cpp
7 * A simple server to demonstrate the use of codeset translation
9 * @author Phil Mesnier <mesnier_p@ociweb.com>
11 //=============================================================================
13 // IDL generated header
14 #include "simpleS.h"
16 // FUZZ: disable check_for_streams_include
17 #include "ace/streams.h"
19 #include "ace/OS_NS_stdio.h"
20 #include "ace/Get_Opt.h"
22 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
24 int
25 parse_args (int argc, ACE_TCHAR *argv[])
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'o':
34 ior_output_file = get_opts.opt_arg ();
35 break;
37 case '?':
38 default:
39 ACE_ERROR_RETURN ((LM_ERROR,
40 "usage: %s "
41 "-o <iorfile>"
42 "\n",
43 argv [0]),
44 -1);
46 // Indicates successful parsing of the command line
47 return 0;
50 // ------------------------------------------------------------
51 // Servant for associated CORBA object
52 // ------------------------------------------------------------
53 class SimpleImpl : public POA_simple
55 public:
56 SimpleImpl (CORBA::ORB_ptr orb)
57 : orb_ (CORBA::ORB::_duplicate (orb))
60 // implementation of corba interface
61 char * op1 (const char * name,
62 const CORBA::Any & inany,
63 CORBA::Any_out outany)
65 ACE_DEBUG ((LM_DEBUG,
66 "Server: bare string: %C\n", name));
67 const char *any_str;
68 inany >>= any_str;
69 ACE_DEBUG ((LM_DEBUG,
70 "Server: inserted string: %C\n\n", any_str));
72 CORBA::Any *out_ptr = 0;
73 ACE_NEW_RETURN (out_ptr,
74 CORBA::Any,
75 0);
76 (*out_ptr) <<= any_str;
77 outany = out_ptr;
79 return CORBA::string_dup (name);
82 ACE_CDR::WChar * op2 (const ACE_CDR::WChar *s1)
84 return CORBA::wstring_dup (s1);
87 //FUZZ: disable check_for_lack_ACE_OS
88 void shutdown (void)
90 this->orb_->shutdown (0);
92 //FUZZ: enable check_for_lack_ACE_OS
94 private:
95 /// Use an ORB reference to shutdown
96 /// the application.
97 CORBA::ORB_var orb_;
100 // ------------------------------------------------------------
101 // Main routine
102 // ------------------------------------------------------------
103 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
107 // Init the orb
108 CORBA::ORB_var orb= CORBA::ORB_init (argc, argv);
110 // Initialize POA
111 CORBA::Object_var poa_object=
112 orb->resolve_initial_references ("RootPOA");
114 // Check POA
115 if (CORBA::is_nil (poa_object.in ()))
117 ACE_DEBUG ((LM_DEBUG,
118 "Couldn't initialize POA\n"));
119 return 1;
122 // Get the ROOT POA
123 PortableServer::POA_var root_poa =
124 PortableServer::POA::_narrow (poa_object.in ());
126 // Get the POA manager
127 PortableServer::POAManager_var poa_manager =
128 root_poa->the_POAManager ();
130 if (parse_args (argc, argv) != 0)
131 return 1;
133 // Create a C++ implementation of CORBA object
134 SimpleImpl* my_impl = 0;
135 ACE_NEW_RETURN (my_impl,
136 SimpleImpl (orb.in ()),
137 -1);
138 PortableServer::ServantBase_var safe (my_impl);
140 // Create CORBA object for servant and REGISTER with POA
141 PortableServer::ObjectId_var id =
142 root_poa->activate_object (my_impl);
144 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
146 simple_var server = simple::_narrow (object_act.in ());
148 // Get the IOR for our object
149 CORBA::String_var ior = orb->object_to_string (server.in ());
151 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
152 if (output_file == 0)
153 ACE_ERROR_RETURN ((LM_ERROR,
154 "Cannot open output file for writing IOR: server.ior"),
156 ACE_OS::fprintf (output_file, "%s", ior.in ());
157 ACE_OS::fclose (output_file);
159 // Activate POA manager
160 poa_manager->activate ();
162 // Wait for calls
163 orb->run ();
165 root_poa->destroy (1, 1);
167 orb->destroy ();
169 catch (const CORBA::Exception& ex)
171 ex._tao_print_exception ("Exception caught in server:");
172 return 1;
175 return 0;