Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / tests / HTIOP / AMI / server.cpp
blob94a895e81513e5dddfdc1a38899fe4288c73d3e0
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * Implementation of the server.
8 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
9 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
11 //=============================================================================
14 #include "ami_test_i.h"
16 #include "tao/debug.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/OS_NS_sys_socket.h"
21 const ACE_TCHAR *ior_output_file = 0;
23 int
24 parse_args (int argc, ACE_TCHAR *argv[])
26 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:d"));
27 int c;
29 while ((c = get_opts ()) != -1)
30 switch (c)
32 case 'o':
33 ior_output_file = get_opts.opt_arg ();
34 break;
35 case 'd':
36 TAO_debug_level++;
37 break;
38 case '?':
39 default:
40 ACE_ERROR_RETURN ((LM_ERROR,
41 "usage: %s "
42 "-o <iorfile>"
43 "\n",
44 argv [0]),
45 -1);
47 // Indicates successful parsing of the command line
48 return 0;
51 int
52 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
54 try
56 CORBA::ORB_var orb =
57 CORBA::ORB_init (argc, argv);
59 CORBA::Object_var poa_object =
60 orb->resolve_initial_references("RootPOA");
62 if (CORBA::is_nil (poa_object.in ()))
63 ACE_ERROR_RETURN ((LM_ERROR,
64 " (%P|%t) Unable to initialize the POA.\n"),
65 1);
67 PortableServer::POA_var root_poa =
68 PortableServer::POA::_narrow (poa_object.in ());
70 PortableServer::POAManager_var poa_manager =
71 root_poa->the_POAManager ();
73 if (parse_args (argc, argv) != 0)
74 return 1;
76 ACE_OS::socket_init ();
78 AMI_Test_i ami_test_i (orb.in ());
80 A::AMI_Test_var ami_test_var =
81 ami_test_i._this ();
83 CORBA::String_var ior =
84 orb->object_to_string (ami_test_var.in ());
86 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
88 // If the ior_output_file exists, output the ior to it
89 if (ior_output_file != 0)
91 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
92 if (output_file == 0)
93 ACE_ERROR_RETURN ((LM_ERROR,
94 "Cannot open output file for writing IOR: %s",
95 ior_output_file),
96 1);
97 ACE_OS::fprintf (output_file, "%s", ior.in ());
98 ACE_OS::fclose (output_file);
101 poa_manager->activate ();
103 orb->run ();
105 root_poa->destroy (true, // ethernalize objects
106 false); // wait for completion
108 orb->destroy ();
110 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
112 catch (const CORBA::Exception& ex)
114 ex._tao_print_exception ("Caught exception:");
115 return 1;
118 return 0;