Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_2935_Regression / middle.cpp
blob0ca31fd3a57dd73a53953e9a819e22d299b700da
1 #include "middle_i.h"
3 const ACE_TCHAR * ior_output_file = 0;
5 const ACE_TCHAR * ior_input_file = 0;
6 char input_ior[5000];
9 void eat_args (int & argc, ACE_TCHAR *argv[], int argp, int how_many)
11 for (int marg = argp; marg + how_many < argc; ++marg)
13 argv[marg] = argv[marg + how_many];
15 argc -= how_many;
18 bool parse_args (int & argc, ACE_TCHAR *argv[])
20 int argp = 1;
21 while (argp < argc)
23 const ACE_TCHAR * arg = argv[argp];
24 if(arg[0] == '-' && arg[1] == 'o' && argp + 1 < argc)
26 if (ior_output_file != 0)
28 ACE_ERROR ((LM_DEBUG,
29 "Middle (%P|%t) duplicate -o options\n"));
30 return false;
32 // capture output file name
33 // then remove it from arguemnt list
34 ior_output_file = argv[argp + 1];
35 eat_args(argc, argv, argp, 2);
37 else if(arg[0] == '-' && arg[1] == 'f' && argp + 1 < argc)
39 if (ior_input_file != 0)
41 ACE_ERROR ((LM_DEBUG,
42 "Middle (%P|%t) duplicate -f options\n"));
43 return false;
45 // capture input file name
46 // then remove it from arguemnt list
47 ior_input_file = argv[argp + 1];
48 eat_args(argc, argv, argp, 2);
50 else
52 argp += 1;
53 // just ignore unknown arguments
56 if (ior_output_file == 0)
58 ACE_ERROR ((LM_DEBUG,
59 "Middle (%P|%t) missing required -o option\n"));
60 return false;
62 return true;
65 int
66 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
68 ACE_DEBUG ((LM_DEBUG,
69 "Middle (%P|%t) started\n"));
71 try
73 CORBA::ORB_var orb =
74 CORBA::ORB_init (argc,
75 argv);
77 if ( ! parse_args (argc, argv))
79 return -1;
82 ///////////////////////////////
83 // Prepare to be a CORBA server
84 CORBA::Object_var poa_object =
85 orb->resolve_initial_references ("RootPOA");
87 PortableServer::POA_var root_poa =
88 PortableServer::POA::_narrow (poa_object.in ());
90 PortableServer::POAManager_var poa_manager =
91 root_poa->the_POAManager ();
93 ///////////////////////////////
94 // Prepare to be a CORBA client
95 FILE *input_file = ACE_OS::fopen (ior_input_file, "r");
96 if (input_file == 0)
98 ACE_ERROR_RETURN ((LM_ERROR,
99 "Cannot open input IOR file: %s",
100 ior_input_file),
101 -1);
103 ACE_OS::fread (input_ior, 1, sizeof(input_ior), input_file);
104 ACE_OS::fclose (input_file);
106 // Convert the IOR to an object reference.
107 CORBA::Object_var object =
108 orb->string_to_object (input_ior);
110 // narrow the object reference to a ThreeTier reference
111 ThreeTier_var target = ThreeTier::_narrow (object.in ());
113 if (CORBA::is_nil (target.in ()))
115 ACE_ERROR_RETURN ((LM_ERROR,
116 "IOR does not refer to a ThreeTier implementation"),
117 -1);
120 // We should have a good connection now
121 // temporary: check it out
122 //target->tick();
123 //target->tock();
125 Middle_i middle (orb.in(), target.in ());
126 if (middle.parse_args (argc, argv) )
128 /////////////////////////////////
129 // Activate server side mechanism
130 PortableServer::ObjectId_var id =
131 root_poa->activate_object (&middle);
133 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
135 ThreeTier_var server =
136 ThreeTier::_narrow (object.in ());
138 CORBA::String_var ior =
139 orb->object_to_string (server.in ());
141 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
142 if (output_file == 0)
143 ACE_ERROR_RETURN ((LM_ERROR,
144 "Cannot open output file for writing IOR: %s",
145 ior_output_file),
146 -1);
147 ACE_OS::fprintf (output_file, "%s", ior.in ());
148 ACE_OS::fclose (output_file);
150 poa_manager->activate ();
151 orb->run ();
154 catch (const CORBA::UserException& userex)
156 userex._tao_print_exception ("Middle: User Exception in main");
157 return -1;
159 catch (const CORBA::SystemException& sysex)
161 sysex._tao_print_exception ("Middle: System Exception in main");
162 return -1;
165 ACE_DEBUG ((LM_DEBUG,
166 "Middle (%P|%t) exits\n"));
168 return 0;