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

// ============================================================================
//
// = LIBRARY
//    TAO/tests
//
// = FILENAME
//    client.cpp
//
// = AUTHOR
//    Chris Cleeland
//
// ============================================================================

#include "ace/Auto_Ptr.h"
#include "ace/Get_Opt.h"

#include "tao/corba.h"

#include "eh_i.h"

CORBA::String_var remote_reactor_key = (char *) 0;

// Completely arbitrary constant that ought to be big enough.
#define MAX_IOR_SIZE 4096

int
parse_args (int argc_, char* argv_[])
{
  ACE_Get_Opt get_opts (argc_, argv_, "df:k:");
  int c;
  char temp_buf[MAX_IOR_SIZE];
  char *result = 0;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':  // debug flag
        TAO_debug_level++;
        break;
      case 'f':
        {
          FILE *ior_file_ =
            ACE_OS::fopen (get_opts.optarg,"r");

          if (ior_file_ == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "Unable to open %s for writing: %p\n",
                               get_opts.optarg, "open"), -1);

          result = ACE_OS::fgets (temp_buf, MAX_IOR_SIZE, ior_file_);
          if (result == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "Unable to read ior from file %s: %p\n",
                               get_opts.optarg,
                               "fgets"),
                              -1);

          remote_reactor_key =
            CORBA::string_copy (temp_buf);
          ACE_OS::fclose (ior_file_);
        }
	break;
      case 'k':
        remote_reactor_key =
          CORBA::string_copy (get_opts.optarg);
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           " [-d]"
                           " [-f ior-key-file]"
                           " [-k ior-as-string]"
                           "\n",
                           argv_ [0]),
                          -1);
      }

  // Indicates successful parsing of command line.
  return 0;
}

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

      // Initialize the object adapter.
      CORBA::Object_var poa_object =
	orb->resolve_initial_references ("RootPOA");
      if (CORBA::is_nil (poa_object.in ()))
	ACE_ERROR_RETURN ((LM_ERROR,
			   " (%P|%t) Unable to initialize the POA.\n"),
			  1);

      PortableServer::POA_var root_poa =
	PortableServer::POA::_narrow (poa_object.in (),
                                      TAO_TRY_ENV);
      TAO_CHECK_ENV;

      parse_args (argc, argv);

      if (remote_reactor_key.in () == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%s: no remote Reactor key specified\n"),
                          -1);

      CORBA::Object_var reactor_object =
        orb->string_to_object (ACE_const_cast(char*, remote_reactor_key.in ()),
                               TAO_TRY_ENV);
      TAO_CHECK_ENV;

      Reactor_var remote_reactor = Reactor::_narrow (reactor_object.in (),
                                                     TAO_TRY_ENV);
      TAO_CHECK_ENV;

      if (CORBA::is_nil (remote_reactor.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%s: invalid Reactor key <%s>\n",
                           argv[0], remote_reactor_key.in ()), -1);

      // Create an EventHandler servant to hand to the other side...
      auto_ptr<EventHandler_i> eh_impl (new EventHandler_i);
      EventHandler_var eh = eh_impl->_this (TAO_TRY_ENV);

      // Get into the event loop briefly...just to make sure that the
      // ORB gets a chance to set things up for us to be a server.
      // What this really means is that there's a listening port.
      //
      // The bad thing is that we have to do something non-standard
      // such as call orb->run() with a zero timeout.  It would be
      // nice if the spec gave us a standard way to do this.
      if (orb->run (ACE_Time_Value::zero) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%s: %p\n",
                           argv[0], "unable to get the ORB Core to listen"),
                          -1);

      // Now, after all that, we can invoke an operation on the remote
      // side.
      CORBA::Long r = remote_reactor->register_handler (eh.in (), TAO_TRY_ENV);

      // We ought to have a result!
      ACE_DEBUG ((LM_DEBUG,
                  "%s: received %d as return from register_handler ()\n",
                  argv[0], r));

      remote_reactor->set_value (TAO_TRY_ENV);
      TAO_CHECK_ENV;

      remote_reactor->decrement (eh.in (), 5, TAO_TRY_ENV);
      TAO_CHECK_ENV;

      remote_reactor->stop (TAO_TRY_ENV);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("main");
      return 1;
    }
  TAO_ENDTRY;

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class auto_ptr<EventHandler_i>;
template class ACE_Auto_Basic_Ptr<EventHandler_i>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate auto_ptr<EventHandler_i>
#pragma instantiate ACE_Auto_Basic_Ptr<EventHandler_i>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */