Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / ZIOP / server.cpp
blobbe1dbfc6fd5ab24ec2fd01c16a62dcae7b0d654e
1 #include "Hello.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "tao/ZIOP/ZIOP.h"
5 #include "tao/Compression/zlib/ZlibCompressor_Factory.h"
6 #include "tao/Compression/bzip2/Bzip2Compressor_Factory.h"
7 #include "TestCompressor/TestCompressor_Factory.h"
8 #include "tao/Policy_ManagerC.h"
9 #include "tao/Policy_CurrentC.h"
10 #include "tao/Transport.h"
12 #include "common.h"
13 static const ACE_TCHAR *ior_output_file = DEFAULT_IOR_FILENAME;
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:t:"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'o':
25 ior_output_file = get_opts.opt_arg ();
26 break;
27 case 't':
28 test = ACE_OS::atoi (get_opts.opt_arg ());
29 break;
31 case '?':
32 default:
33 ACE_ERROR_RETURN ((LM_ERROR,
34 "usage: %s "
35 "-o <iorfile>"
36 "\n",
37 argv [0]),
38 -1);
40 // Indicates successful parsing of the command line
41 return 0;
44 int
45 register_factories (CORBA::ORB_ptr orb)
47 CORBA::Object_var compression_manager =
48 orb->resolve_initial_references("CompressionManager");
50 ::Compression::CompressionManager_var manager =
51 ::Compression::CompressionManager::_narrow (compression_manager.in ());
53 if (CORBA::is_nil(manager.in ()))
54 ACE_ERROR_RETURN ((LM_ERROR,
55 " (%P|%t) Panic: nil compression manager\n"),
56 1);
57 ::Compression::CompressorFactory_ptr compressor_factory;
58 ::Compression::CompressorFactory_var compr_fact;
60 //register Zlib compressor
61 ACE_NEW_RETURN (compressor_factory, TAO::Zlib_CompressorFactory (), 1);
62 compr_fact = compressor_factory;
63 manager->register_factory(compr_fact.in ());
65 // register bzip2 compressor
66 ACE_NEW_RETURN (compressor_factory, TAO::Bzip2_CompressorFactory (), 1);
67 compr_fact = compressor_factory;
68 manager->register_factory(compr_fact.in ());
70 // register test compressor
71 ACE_NEW_RETURN (compressor_factory, TAO::Test_CompressorFactory (), 1);
72 compr_fact = compressor_factory;
73 manager->register_factory(compr_fact.in ());
75 return 0;
78 CORBA::Policy_ptr
79 create_compressor_id_level_list_policy (CORBA::ORB_ptr orb)
81 ::Compression::CompressorIdLevelList compressor_id_list;
83 if (test == 2)
85 ACE_DEBUG((LM_DEBUG,
86 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
87 compressor_id_list.length(1);
88 compressor_id_list[0].compressor_id = COMPRESSORID_FOR_TESTING;
89 compressor_id_list[0].compression_level = SERVER_COMPRESSION_LEVEL;
91 else
93 compressor_id_list.length(2);
94 compressor_id_list[0].compressor_id = ::Compression::COMPRESSORID_BZIP2;
95 compressor_id_list[0].compression_level = SERVER_COMPRESSION_LEVEL;
96 compressor_id_list[1].compressor_id = ::Compression::COMPRESSORID_ZLIB;
97 compressor_id_list[1].compression_level = SERVER_COMPRESSION_LEVEL;
98 //compressor_id_list[2].compressor_id = COMPRESSORID_FOR_TESTING;
99 //compressor_id_list[2].compression_level = SERVER_COMPRESSION_LEVEL;
102 CORBA::Any compressor_id_any;
103 compressor_id_any <<= compressor_id_list;
105 return orb->create_policy (ZIOP::COMPRESSOR_ID_LEVEL_LIST_POLICY_ID, compressor_id_any);
108 CORBA::Policy_ptr
109 create_low_value_policy (CORBA::ORB_ptr orb)
111 // Setting policy for minimum amount of bytes that needs to be
112 // compressed. If a message is smaller than this, it doesn't get
113 // compressed
114 CORBA::ULong compression_low_value = 100;
115 CORBA::Any low_value_any;
116 low_value_any <<= compression_low_value;
118 return orb->create_policy (ZIOP::COMPRESSION_LOW_VALUE_POLICY_ID, low_value_any);
121 CORBA::Policy_ptr
122 create_compression_enabled_policy (CORBA::ORB_ptr orb)
124 // Setting policy whether compression is used.
125 CORBA::Boolean compression_enabling = true;
126 CORBA::Any compression_enabling_any;
127 compression_enabling_any <<= CORBA::Any::from_boolean(compression_enabling);
129 return orb->create_policy (ZIOP::COMPRESSION_ENABLING_POLICY_ID, compression_enabling_any);
132 CORBA::Policy_ptr
133 create_min_ratio_policy (CORBA::ORB_ptr orb)
135 CORBA::Any min_compression_ratio_any;
136 Compression::CompressionRatio min_compression_ratio = 0.75;
137 min_compression_ratio_any <<= min_compression_ratio;
139 return orb->create_policy (ZIOP::COMPRESSION_MIN_RATIO_POLICY_ID, min_compression_ratio_any);
142 Test::Hello_var
143 prepare_tests (CORBA::ORB_ptr orb, PortableServer::POA_ptr root_poa)
145 register_factories(orb);
147 CORBA::Object_var objectman =
148 orb->resolve_initial_references ("ORBPolicyManager");
150 CORBA::PolicyManager_var policy_manager =
151 CORBA::PolicyManager::_narrow (objectman.in ());
153 PortableServer::POA_var my_compress_poa = 0;
154 CORBA::PolicyList policies(4);
155 policies.length(4);
159 policies[0] = create_compressor_id_level_list_policy (orb);
160 policies[1] = create_low_value_policy (orb);
161 policies[2] = create_compression_enabled_policy (orb);
162 policies[3] = create_min_ratio_policy (orb);
164 my_compress_poa = root_poa->create_POA("My_Compress_Poa", 0, policies);
166 catch(const CORBA::PolicyError&)
168 policies.length(0);
169 my_compress_poa = root_poa->create_POA("My_Compress_Poa", 0, policies);
172 policy_manager->set_policy_overrides (policies, CORBA::ADD_OVERRIDE);
174 CORBA::Object_var pcobject =
175 orb->resolve_initial_references ("PolicyCurrent");
177 CORBA::PolicyCurrent_var policy_current =
178 CORBA::PolicyCurrent::_narrow (pcobject.in ());
180 policy_current->set_policy_overrides (policies, CORBA::ADD_OVERRIDE);
182 PortableServer::POAManager_var poa_manager = my_compress_poa->the_POAManager ();
184 Hello *hello_impl = 0;
185 ACE_NEW_RETURN (hello_impl,
186 Hello (orb),
188 PortableServer::ServantBase_var owner_transfer(hello_impl);
190 PortableServer::ObjectId_var id =
191 my_compress_poa->activate_object (hello_impl);
193 CORBA::Object_var object = my_compress_poa->id_to_reference (id.in ());
195 Test::Hello_var hello = Test::Hello::_narrow (object.in ());
197 poa_manager->activate ();
199 return hello._retn ();
203 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
207 CORBA::ORB_var orb =
208 CORBA::ORB_init (argc, argv);
210 CORBA::Object_var poa_object =
211 orb->resolve_initial_references("RootPOA");
213 PortableServer::POA_var root_poa =
214 PortableServer::POA::_narrow (poa_object.in ());
216 if (CORBA::is_nil (root_poa.in ()))
217 ACE_ERROR_RETURN ((LM_ERROR,
218 " (%P|%t) Panic: nil RootPOA\n"),
221 if (parse_args (argc, argv) != 0)
222 return 1;
224 Test::Hello_var hello = prepare_tests (orb.in (), root_poa.in ());
226 CORBA::String_var ior = orb->object_to_string (hello.in ());
228 // Output the IOR to the <ior_output_file>
229 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
230 if (output_file == 0)
231 ACE_ERROR_RETURN ((LM_ERROR,
232 "Cannot open output file for writing IOR: %s\n",
233 ior_output_file),
235 ACE_OS::fprintf (output_file, "%s", ior.in ());
236 ACE_OS::fclose (output_file);
238 orb->run ();
240 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
242 root_poa->destroy (true, true);
244 orb->destroy ();
246 catch (const CORBA::Exception& ex)
248 ex._tao_print_exception ("Exception caught:");
249 return 1;
252 return 0;