summaryrefslogtreecommitdiff
path: root/TAO/tests/OctetSeq/client.cpp
blob: 757322add9cdbff72a9b052218f94409c9d1c3ca (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
// $Id$

#include "ace/Get_Opt.h"
#include "ace/ACE.h"
#include "testC.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_string.h"

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

const char *ior = "file://test.ior";
int niterations = 5;
ACE_RANDR_TYPE seed = 0;
int verbose = 0;

int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "vk:i:s:");
  int c;

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

      case 'i':
        niterations = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case 's':
        seed = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case 'v':
        verbose = 1;
        break;

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

int
main (int argc, char *argv[])
{
  try
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv, "");

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

      CORBA::Object_var object =
        orb->string_to_object (ior);

      Test::Database_var server =
        Test::Database::_narrow (object.in ());

      if (CORBA::is_nil (server.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Object reference <%s> is nil\n",
                             ior),
                            1);
        }

#if (TAO_HAS_MINIMUM_CORBA == 0)
      CORBA::String_var repository_id =
        server->_repository_id ();

      if (ACE_OS::strcmp (repository_id.in (), "IDL:Test/Database:1.0") != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Repository id is wrong <%s>\n",
                             repository_id.in ()),
                            1);
        }
#endif /* TAO_HAS_MINIMUM_CORBA == 0 */

      if (seed == 0)
        {
          seed = ACE_OS::time (0);
          ACE_DEBUG ((LM_DEBUG, "Seed value is %d\n", seed));
        }

      const int nelements = 64; // @@ TODO
      const int maxsize = 1024;
      Test::OctetSeq elements[nelements];

      for (int i = 0; i != niterations; ++i)
        {
          CORBA::ULong r = ACE_OS::rand_r (seed);
          Test::Index idx = (r % nelements);

          if (i % 100 == 0)
            {
              for (int j = 0; j != nelements; ++j)
                {
                  CORBA::ULong r = ACE_OS::rand_r (seed);
                  CORBA::ULong l = r % maxsize;
                  elements[j].length (l);
                  CORBA::Double token = 0;
                  for (CORBA::ULong k = 0; k != l; ++k)
                    {
                      r = ACE_OS::rand_r (seed);
                      elements[j][k] = (r % 128);
                      token += r;
                    }
                  CORBA::Double returned_token;
                  server->set (Test::Index (j),
                               elements[j],
                               token,
                               returned_token);

                  if (token != returned_token)
                    {
                      ACE_ERROR ((LM_ERROR,
                                  "ERROR - invalid token <%f> returned,"
                                  " expecting %f in (%d,%d)\n",
                                  returned_token, token, i, j));
                    }
                }
            }

          CORBA::ULong crc_remote =
            server->get_crc (idx);

          CORBA::ULong crc_local =
            ACE::crc32 (elements[idx].get_buffer (),
                        elements[idx].length ());

          if (crc_local != crc_remote)
            {
              ACE_DEBUG ((LM_DEBUG,
                          "CRC mismatch for element %d\n", idx));
            }
          else if (verbose == 1)
            {
              ACE_DEBUG ((LM_DEBUG,
                          "CRC <%u> matched for element %d\n",
                          crc_remote, idx));
            }

        }

      server->shutdown ();

      orb->destroy ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Caught exception:");
      return 1;
    }

  return 0;
}