summaryrefslogtreecommitdiff
path: root/TAO/tests/ZIOP/client.cpp
blob: badddc7e070cbac36f0c23c1e14fc1f6b6bf3070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// $Id$

#include "TestC.h"
#include "ace/Get_Opt.h"
#include "tao/ZIOP/ZIOP.h"
#include "ace/OS.h"
#include "tao/Compression/zlib/ZlibCompressor_Factory.h"
#include "tao/ORB_Constants.h"
#include "tao/TransportCurrent/TCC.h"

ACE_RCSID(Hello, client, "$Id$")

const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");

int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        ior = get_opts.opt_arg ();
        break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-k <ior> "
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful parsing of the command line
  return 0;
}

void log_statistics (ACE_TCHAR* test,
                     ::TAO::CounterT initial_bytes_send,
                     ::TAO::CounterT initial_bytes_recv,
                     ::TAO::CounterT bytes_send_after_test,
                     ::TAO::CounterT bytes_recv_after_test)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("%s statistics:")
              ACE_TEXT (" initial bytes sent:%d")
              ACE_TEXT (" initial bytes recv:%d")
              ACE_TEXT (" bytes sent after test:%d")
              ACE_TEXT (" bytes recv after test:%d\n"),
              test,
              initial_bytes_send,
              initial_bytes_recv,
              bytes_send_after_test,
              bytes_recv_after_test));
}

void start_low_value_test(Test::Hello_ptr hello, ::TAO::Transport::Current_ptr)
{
#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT == 1
//  ::TAO::CounterT initial_bytes_sent = tc->bytes_sent ();
//  ::TAO::CounterT initial_bytes_recv = tc->bytes_received ();
#endif

  ACE_DEBUG((LM_DEBUG,
            ACE_TEXT("Start get_string; large compression ratio\n")));

  CORBA::String_var the_string = hello->get_string ("This is a test string"
                                                    "This is a test string"
                                                    "This is a test string"
                                                    "This is a test string"
                                                    "This is a test string"
                                                    "This is a test string"
                                                    "This is a test string"
                                                    "This is a test string"
                                                    "This is a test string");
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned <%C>\n",
              the_string.in ()));

#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT ==1
//  log_statistics ("low_value_test", initial_bytes_sent, initial_bytes_recv,
//        tc->bytes_sent (), tc->bytes_received ());
#endif
}

void start_min_ratio_test (Test::Hello_ptr hello, ::TAO::Transport::Current_ptr)
{
#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT ==1
//  ::TAO::CounterT initial_bytes_sent = tc->bytes_sent ();
//  ::TAO::CounterT initial_bytes_recv = tc->bytes_received ();
#endif

  ACE_DEBUG((LM_DEBUG,
              ACE_TEXT("Start get_string; small compression ratio\n")));

  //shouldn't compress since compress_ratio < min_ratio
  CORBA::String_var the_string = hello->get_string ("!@#$#%^#@&^%*$@#GFGSd"
                                                    "fgdbdfgwe%^@#$#$%EQRT"
                                                    "sfdgdafs56#$@@#$&((%$"
                                                    "#4&%3#4%^21@!sdfSADHv"
                                                    "dsaAhn~1`2#$#sAFDGHdf");

  ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned <%C>\n",
              the_string.in ()));

#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT ==1
//  log_statistics ("min_ratio_test", initial_bytes_sent, initial_bytes_recv,
//        tc->bytes_sent (), tc->bytes_received ());
#endif
}

void start_big_reply_test (Test::Hello_ptr hello, ::TAO::Transport::Current_ptr)
{
#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT ==1
//  ::TAO::CounterT initial_bytes_sent = tc->bytes_sent ();
//  ::TAO::CounterT initial_bytes_recv = tc->bytes_received ();
#endif

  ACE_DEBUG((LM_DEBUG,
              ACE_TEXT("Start get_big_reply; large compression ratio\n")));

  //Prepare to send a large number of bytes. Should be compressed
  Test::Octet_Seq_var dummy = hello->get_big_reply ();
  if (dummy.ptr ()->length () > 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT("Client side BLOB received\n")));
    }
  else
    {
      ACE_DEBUG ((LM_ERROR,
                  ACE_TEXT("Error recieving BLOB on Client\n")));
    }

#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT ==1
//  log_statistics ("big_reply_test",initial_bytes_sent, initial_bytes_recv,
//        tc->bytes_sent (), tc->bytes_received ());
#endif
}

void start_big_request_test (Test::Hello_ptr hello, ::TAO::Transport::Current_ptr)
{
#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT ==1
//  ::TAO::CounterT initial_bytes_sent = tc->bytes_sent ();
//  ::TAO::CounterT initial_bytes_recv = tc->bytes_received ();
#endif

  ACE_DEBUG((LM_DEBUG,
              ACE_TEXT("Start big_request; large compression ratio\n")));
  //ACE_OS::sleep(1);
  int length = 2000;
  Test::Octet_Seq send_msg(length);
  send_msg.length (length);

  hello->big_request(send_msg);

#if defined (TAO_HAS_TRANSPORT_CURRENT) && TAO_HAS_TRANSPORT_CURRENT ==1
//  log_statistics ("big_request_test", initial_bytes_sent, initial_bytes_recv,
//        tc->bytes_sent (), tc->bytes_received ());
#endif
}

void start_tests (Test::Hello_ptr hello, ::TAO::Transport::Current_ptr tc)
{
  //::CORBA::String_var rhost (tc->remote_host ());
  //::CORBA::String_var lhost (tc->local_host ());
  //::CORBA::Long id = tc->id ();
  //::TAO::CounterT bs = tc->bytes_sent ();
  //::TAO::CounterT br = tc->bytes_received ();
  //::TAO::CounterT rs = tc->messages_sent ();
  //::TAO::CounterT rr = tc->messages_received ();


#if !defined (TAO_HAS_TRANSPORT_CURRENT) || TAO_HAS_TRANSPORT_CURRENT == 0
  ACE_DEBUG((LM_DEBUG,
              ACE_TEXT ("No statistical information available since TAO_HAS_TRANSPORT_CURRENT is not set")));
#endif

  start_low_value_test (hello, tc);

  start_min_ratio_test (hello, tc);

  start_big_reply_test (hello, tc);

  start_big_request_test (hello, tc);
}

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  int extra_argc = 2;

  try
    {
      ACE_TCHAR **extra = 0;
      ACE_NEW_RETURN (extra, ACE_TCHAR *[extra_argc], -1);
      extra[0] = ACE::strnew (ACE_TEXT ("-ORBSvcConfDirective"));
      extra[1] = ACE::strnew (ACE_TEXT ("dynamic TAO_Transport_Current_Loader Service_Object")
                              ACE_TEXT (" * TAO_TC:_make_TAO_Transport_Current_Loader() ''"));

      ACE_TCHAR **largv = new ACE_TCHAR *[argc+extra_argc];
      for (int i = 0; i < argc; i++)
        largv[i] = argv[i];

      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("client adding args: ")));
      for (int i = 0; i < extra_argc; i++)
        {
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s "), extra[i]));
          largv[argc+i] = extra[i];
        }
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));

      argc += extra_argc;

      CORBA::ORB_var orb = CORBA::ORB_init (argc, largv);

      if (parse_args (argc, argv) != 0)
        return 1;

      CORBA::Object_var compression_manager =
        orb->resolve_initial_references("CompressionManager");

      Compression::CompressionManager_var manager =
        Compression::CompressionManager::_narrow (compression_manager.in ());

      if (CORBA::is_nil(manager.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Panic: nil compression manager\n"),
                          1);

      Compression::CompressorFactory_ptr compressor_factory;

      ACE_NEW_RETURN (compressor_factory, TAO::Zlib_CompressorFactory (), 1);

      Compression::CompressorFactory_var compr_fact = compressor_factory;
      manager->register_factory(compr_fact.in ());

      Compression::CompressorIdLevelList compressor_id_list(2);
      compressor_id_list.length(2);
      compressor_id_list[0].compressor_id = Compression::COMPRESSORID_ZLIB;
      compressor_id_list[0].compression_level = 5;
      compressor_id_list[1].compressor_id = Compression::COMPRESSORID_BZIP2;
      compressor_id_list[1].compression_level = 5;
      
      //Setting policy whether compression is used.
      CORBA::Boolean compression_enabling = true;
      CORBA::Any compression_enabling_any;
      compression_enabling_any <<= CORBA::Any::from_boolean(compression_enabling);

      //Setting policy for minimum amount of bytes that needs to be 
      //compressed. If a message is smaller than this, it doesn't get
      //compressed
      CORBA::ULong compression_low_value = 100;
      CORBA::Any low_value_any;
      low_value_any <<= compression_low_value;

      CORBA::Any min_compression_ratio_any;
      CORBA::Long min_compression_ratio = 75;
      min_compression_ratio_any <<= min_compression_ratio;
      
      CORBA::Any compressor_id_any;
      compressor_id_any <<= compressor_id_list;

      CORBA::PolicyList policies(4);
      policies.length(4);

      policies[0] = orb->create_policy (ZIOP::COMPRESSION_ENABLING_POLICY_ID, compression_enabling_any);
      policies[1] = orb->create_policy (ZIOP::COMPRESSOR_ID_LEVEL_LIST_POLICY_ID, compressor_id_any);
      policies[2] = orb->create_policy (ZIOP::COMPRESSION_LOW_VALUE_POLICY_ID, low_value_any);
      policies[3] = orb->create_policy (ZIOP::COMPRESSION_MIN_RATIO_POLICY_ID, min_compression_ratio_any);

      CORBA::Object_var tmp = orb->string_to_object(ior);
      CORBA::Object_var tmp2 = tmp->_set_policy_overrides (policies, CORBA::ADD_OVERRIDE);

      Test::Hello_var hello = Test::Hello::_narrow(tmp2.in ());

      if (CORBA::is_nil (hello.in ()))
        {
          ACE_ERROR_RETURN ((LM_DEBUG,
                             "Nil Test::Hello reference <%s>\n",
                             ior),
                            1);
        }

      CORBA::Object_var tcobject =
                orb->resolve_initial_references ("TAO::Transport::Current");
      
      ::TAO::Transport::Current_var tc =
        ::TAO::Transport::Current::_narrow (tcobject.in ());
      if (CORBA::is_nil (tc.in ()))
        throw ::CORBA::INTERNAL ();
      
      start_tests(hello.in (), tc.in ());

      hello->shutdown ();
      
      orb->destroy ();

      for (int i = 0; i < extra_argc; i++)
        ACE::strdelete (extra[i]);
      delete [] extra;
      delete [] largv;
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught:");
      return 1;
    }

  return 0;
}