summaryrefslogtreecommitdiff
path: root/orbsvcs/tests/ImplRepo/ReconnectServer/client.cpp
blob: bc61035a1b90b88a0aa62029ef53c4a937cac3b0 (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
// $Id$
#include "testS.h"
#include "tao/AnyTypeCode/Any.h"
#include "ace/Get_Opt.h"
#include <ace/Task.h>
#include "ace/OS_NS_unistd.h"

const ACE_TCHAR *ior_input_file = ACE_TEXT ("file://serverA.ior");
int test_duration_sec = 15;
bool expect_object_not_exist = false;

class Client_Task : public ACE_Task_Base
{
  public:
    Client_Task (Test::Time_ptr obj)
    : test_ (Test::Time::_duplicate (obj)),
      communication_failed_ (false),
      reconnected_ (false),
      caught_object_not_exist_ (false)
    {}

    virtual int svc (void)
    {
      ACE_Time_Value start = ACE_OS::gettimeofday ();
      ACE_Time_Value elapsed;
      int i = 0;
      while (elapsed < ACE_Time_Value (test_duration_sec))
      {
        try
        {
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t)Request %d\n"), i ));
          test_->current_time();
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t)Done request %d\n"), i ));
          if (communication_failed_)
          {
            communication_failed_ = false;
            reconnected_ = true;
          }
          ACE_OS::sleep (1);
        }
        catch (const CORBA::OBJECT_NOT_EXIST &)
        {
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t)caught OBJECT_NOT_EXIST exception for request %d\n"), i ));
          caught_object_not_exist_ = true;
          ACE_OS::sleep (1);
        }
        catch (const CORBA::Exception & /*ex*/)
        {
          //ex._tao_print_exception ("Exception caught:");
          communication_failed_ = true;
          ACE_OS::sleep (1);
        }
        ++i;
        elapsed = ACE_OS::gettimeofday () - start;
      }

      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t)Client thread exit \n")));
      return 0;
    }

    bool test_passed () const
    {
      return (! communication_failed_ && reconnected_ && ! expect_object_not_exist && ! caught_object_not_exist_)
        || (communication_failed_ && ! reconnected_ && expect_object_not_exist && caught_object_not_exist_);
    }

  private:

    Test::Time_var test_;
    bool communication_failed_;
    bool reconnected_;
    bool caught_object_not_exist_;
};


int
parse_args (int argc, ACE_TCHAR* argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "i:t:e:");
  int c;


  while ((c = get_opts ()) != -1)
    switch (c)
      {
        case 'i':
          ior_input_file = get_opts.opt_arg ();
          break;
        case 't':
          test_duration_sec = ACE_OS::atoi (get_opts.opt_arg ());
          break;
        case 'e':
          expect_object_not_exist = ACE_OS::atoi (get_opts.opt_arg ());
          break;
        case '?':
          default:
          ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-i <iorfile> -t <test_duration>"
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates successful parsing of the command line
  return 0;
}

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
  {
    CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);

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

    CORBA::Object_var object =  orb->resolve_initial_references ("RootPOA");
    PortableServer::POA_var rootPOA =  PortableServer::POA::_narrow (object.in ());

    PortableServer::POAManager_var poa_manager = rootPOA->the_POAManager ();
    poa_manager->activate ();

    object = orb->string_to_object(ior_input_file);

    Test::Time_var test = Test::Time::_narrow(object.in ());

    if (CORBA::is_nil(test.in ()))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
        "(%P|%t) Object reference is nil \n"),
        1);
    }

    Client_Task task (test.in ());
    task.activate (THR_NEW_LWP | THR_JOINABLE, 1, 1);

    ACE_Time_Value tv(test_duration_sec);
    orb->run (&tv);

    task.wait ();

    // Destroy the POA, waiting until the destruction terminates
    rootPOA->destroy (1, 1);
    orb->destroy ();

    if (task.test_passed ())
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t)Client test passed \n")));
    }
    else
    {
      ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t)Client test failed.\n")), 1);
    }
  }
  catch (const CORBA::Exception &ex)
  {
    ex._tao_print_exception ("Exception caught by client:");
    return 1;
  }

  return 0;
}