Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / DIOP / server.cpp
blobed556905685be5d1d9749d21fd235ecab55f2ce2
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * Implementation of the server running the UDP object.
8 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
9 */
10 //=============================================================================
13 #include "UDP_i.h"
15 #include "tao/debug.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "ace/Get_Opt.h"
19 // The following include file forces DIOP to be linked into the
20 // executable and initialized for static builds.
21 #include "tao/Strategies/advanced_resource.h"
23 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
25 int
26 parse_args (int argc, ACE_TCHAR *argv[])
28 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:d"));
29 int c;
31 while ((c = get_opts ()) != -1)
32 switch (c)
34 case 'o':
35 ior_output_file = get_opts.opt_arg ();
36 break;
37 case 'd':
38 TAO_debug_level++;
39 break;
40 case '?':
41 default:
42 ACE_ERROR_RETURN ((LM_ERROR,
43 "usage: %s "
44 "-o <iorfile>"
45 "\n",
46 argv [0]),
47 -1);
49 // Indicates successful parsing of the command line
50 return 0;
53 int
54 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
56 try
58 CORBA::ORB_var orb =
59 CORBA::ORB_init (argc, argv);
61 CORBA::Object_var poa_object =
62 orb->resolve_initial_references("RootPOA");
64 if (CORBA::is_nil (poa_object.in ()))
65 ACE_ERROR_RETURN ((LM_ERROR,
66 " (%P|%t) Unable to initialize the POA.\n"),
67 1);
69 PortableServer::POA_var root_poa =
70 PortableServer::POA::_narrow (poa_object.in ());
72 PortableServer::POAManager_var poa_manager =
73 root_poa->the_POAManager ();
75 // Install a persistent POA in order to achieve a persistent IOR
76 // for our object.
77 CORBA::PolicyList policies;
78 policies.length (2);
79 policies[0] =
80 root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
81 policies[1] =
82 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
85 PortableServer::POA_var persistent_poa =
86 root_poa->create_POA("persistent",
87 poa_manager.in (),
88 policies);
90 policies[0]->destroy ();
92 policies[1]->destroy ();
94 if (parse_args (argc, argv) != 0)
95 return 1;
97 UDP_i udp_i (orb.in ());
99 PortableServer::ObjectId_var id =
100 PortableServer::string_to_ObjectId ("UDP_Object");
102 persistent_poa->activate_object_with_id (id.in (),
103 &udp_i);
105 CORBA::Object_var obj =
106 persistent_poa->id_to_reference (id.in ());
109 UDP_var udp_var = UDP::_narrow (obj.in ());
111 // UDP_var udp_var = udp_i._this ();
112 if (CORBA::is_nil (udp_var.in ()))
113 ACE_DEBUG ((LM_DEBUG,
114 "Failed to narrow correct object reference.\n"));
116 CORBA::String_var ior =
117 orb->object_to_string (udp_var.in ());
119 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
121 // If the ior_output_file exists, output the ior to it
122 if (ior_output_file != 0)
124 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
125 if (output_file == 0)
126 ACE_ERROR_RETURN ((LM_ERROR,
127 "Cannot open output file for writing IOR: %s",
128 ior_output_file),
130 ACE_OS::fprintf (output_file, "%s", ior.in ());
131 ACE_OS::fclose (output_file);
134 poa_manager->activate ();
136 orb->run ();
138 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
140 root_poa->destroy (true, // ethernalize objects
141 false); // wait for completion
143 orb->destroy ();
145 catch (const CORBA::Exception& ex)
147 ex._tao_print_exception ("Caught exception:");
148 return 1;
151 return 0;