Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / DevGuideExamples / AMH_AMI / middle_server.cpp
blob42a638c4fedb169a95493e814e1a124534357b82
1 #include "amh_ami_pch.h"
3 #include "middle_i.h"
4 #include "ace/Get_Opt.h"
5 #include <iostream>
6 #include <fstream>
8 int use_synch = 0;
10 const ACE_TCHAR *ior_output_file = ACE_TEXT ("middle.ior");
11 const ACE_TCHAR *ior_input_file = ACE_TEXT ("file://inner.ior");
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:i:n"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'o':
23 ior_output_file = get_opts.opt_arg ();
24 break;
25 case 'i':
26 ior_input_file = get_opts.opt_arg ();
27 break;
28 case 'n':
29 use_synch = 1;
30 break;
31 case '?':
32 default:
33 ACE_ERROR_RETURN ((LM_ERROR,
34 "usage: %s "
35 "-o <ior_output_file> "
36 "-i <ior_input_file> "
37 "-n"
38 "\n",
39 argv [0]),
40 -1);
42 // Indicates successful parsing of the command line
43 return 0;
47 int
48 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
50 try {
51 // Initialize the ORB.
52 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
54 if (parse_args (argc, argv) != 0)
55 return 1;
57 //Get reference to the RootPOA.
58 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
59 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
61 // Activate the POAManager.
62 PortableServer::POAManager_var mgr = poa->the_POAManager();
63 mgr->activate();
65 obj = orb->string_to_object(ior_input_file);
66 Inner_var peer =
67 Inner::_narrow(obj.in());
68 if (CORBA::is_nil(peer.in()))
70 std::cerr << "Could not initialize peer object reference" << std::endl;
71 ACE_OS::exit (1);
74 // create either a synchronous or AMH_based servant depending on command
75 // line arguement.
76 PortableServer::ServantBase_var servant;
77 if (use_synch)
78 servant = new Middle_i (peer.in());
79 else
80 servant = new Asynch_Middle_i(poa.in(), peer.in());
82 // Register the servant with the RootPOA, obtain its object
83 // reference, stringify it, and write it to a file.
84 PortableServer::ObjectId_var oid = poa->activate_object(servant.in());
85 obj = poa->id_to_reference( oid.in() );
86 CORBA::String_var str = orb->object_to_string( obj.in() );
88 ACE_CString iorname(ACE_TEXT_ALWAYS_CHAR(ior_output_file));
89 std::ofstream iorFile (iorname.c_str());
90 iorFile << str.in() << std::endl;
91 iorFile.close();
92 std::cout << "IOR written to " << iorname << std::endl;
94 // Accept requests from clients.
95 orb->run();
96 orb->destroy();
98 return 0;
100 catch(const CORBA::Exception& ex) {
101 std::cerr << "CORBA exception: " << ex << std::endl;
104 return 1;