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

#include "TestC.h"
#include "ace/Get_Opt.h"
#include "ace/Task.h"
#include "ace/OS_NS_unistd.h"

class Pinger : public ACE_Task_Base
{
private:
  const char * ior_;
  CORBA::ORB_var orb_;
  bool do_shutdown_;
  bool stop_;

public:
  Pinger (CORBA::ORB_var &orb, const char *ior)
    : ior_ (ior),
      orb_(orb),
      do_shutdown_ (false),
      stop_ (false)
  {
  }

  int svc (void)
  {
    bool keep_going = true;
    while (keep_going && !this->stop_)
      {
        try
          {
            CORBA::Object_var tmp = this->orb_->string_to_object(this->ior_);

            Test::IORTable_Shutdown_Race_var target =
              Test::IORTable_Shutdown_Race::_narrow(tmp.in ());
            if (CORBA::is_nil (target.in ()))
                ACE_ERROR_RETURN ((LM_DEBUG,
                                   "(%P|%t) Nil target reference <%s>\n",
                                   this->ior_),
                                  1);
            target->ping();
            if (this->do_shutdown_)
              {
                ACE_DEBUG ((LM_DEBUG,"(%P|%t) Calling shutdown\n"));
                this->do_shutdown_ = false;
                target->shutdown ();
              }
          }
        catch (CORBA::Exception &ex)
          {
            ACE_DEBUG ((LM_DEBUG,
                        "(%P|%t) caught an exception - %C\n",ex._name()));
            keep_going = false;
          }
      }
    return 0;
  }

  void killit()
  {
    do_shutdown_ = true;
  }

  void stop ()
  {
    stop_ = true;
  }

};

int port = 0;
ACE_TCHAR const * target_host = ACE_TEXT("localhost");

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

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'p':
        port = ACE_OS::atoi (get_opts.opt_arg ());
        break;
      case 'h':
        target_host = get_opts.opt_arg ();
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-p <server_port> "
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful 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;

      char ior[100];
      ACE_OS::sprintf (ior, "corbaloc::%s:%d/Racer",
                       ACE_TEXT_ALWAYS_CHAR (target_host), port);

      Pinger pinger(orb, ior);

      ACE_OS::sleep (1);

      ACE_DEBUG ((LM_DEBUG,"(%P|%t) client - starting client threads\n"));

      pinger.activate (THR_NEW_LWP | THR_JOINABLE, 5);

      ACE_OS::sleep (1);

      ACE_DEBUG ((LM_DEBUG,"(%P|%t) client - All running, time to shutdown server\n"));
      pinger.killit();

      ACE_OS::sleep (2);

      ACE_DEBUG ((LM_DEBUG,"(%P|%t) client - Stopping client threads\n"));
      pinger.stop ();

      pinger.wait();
      ACE_DEBUG ((LM_DEBUG,"(%P|%t) client done\n"));

    }
  catch (CORBA::Exception &ex)
    {
      ACE_DEBUG ((LM_DEBUG,"Main caught %s\n",ex._name()));
      return 1;
    }

  return 0;
}