summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Concurrency/NS_client.cpp
blob: 16dea85c540d6d0488ccafbd29803ba62ce1c28c (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/orbsvcs/bin/Naming_Service/TAO
//
// = FILENAME
//    clnt.cpp
//
// = DESCRIPTION
//      This class implements a simple CORBA client for the CosNaming
//      example using stubs generated by the TAO ORB IDL compiler.
//
// = AUTHORS
//      Sergio Flores-Gaitan <sergio@cs.wustl.edu>
//      Torben Worm <tworm@cs.wustl.edu>
//
// ============================================================================

#include "NS_client.h"
#include "tao/debug.h"
#include <stdio.h>

ACE_RCSID(Concurrency, NS_client, "$Id$")

// constructor

CosNaming_Client::CosNaming_Client (void)
  : list_contents_ (0),
    resolve_name_ (0),
    name_to_resolve_ (0),
    context_to_resolve_ (0),
    argc_ (0),
    argv_ (0),
    exit_later_ (0)
{
}

// Parses the command line arguments and returns an error status.

int
CosNaming_Client::parse_args (void)
{
  ACE_Get_Opt get_opts (argc_, argv_, "dxn:c:l");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':  // debug flag
        TAO_debug_level++;
        break;
      case 'l':
        this->list_contents_ = 1;
        break;
      case 'x':
        this->exit_later_++;
        break;
      case 'n':
        this->resolve_name_ = 1;
        this->name_to_resolve_ = get_opts.opt_arg ();
        break;
      case 'c':
        this->resolve_name_ = 1;
        this->context_to_resolve_ = get_opts.opt_arg ();
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           " [-d]"
                           " [-x]"
                           "\n",
                           this->argv_ [0]),
                          -1);
      }

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

// Execute client example code.

int
CosNaming_Client::run (void)
{
  // @@ TODO, add some interesting test here, maybe creating some
  // nested naming contexts and registering a number of objreferences
  // in there.  We could even use the iterators.

  if (this->resolve_name_)
    resolve_name (this->context_to_resolve_,
                  this->name_to_resolve_);

  if (this->list_contents_)
    list_contents ();

  return 0;
}

CosNaming_Client::~CosNaming_Client (void)
{
}

int
CosNaming_Client::resolve_name (char *c, char *n)
{
  TAO_TRY
    {
      CosNaming::Name name (2);
      name.length (2);
      name[0].id = CORBA::string_dup (c);
      name[1].id = CORBA::string_dup (n);
      CORBA::Object_var obj = this->my_name_client_->resolve (name,
                                                              TAO_TRY_ENV);
      TAO_CHECK_ENV;

      if (CORBA::is_nil (obj.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Could not resolve name in Naming service <%s>\n"),
                          -1);
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("init");
      return -1;
    }
  TAO_ENDTRY;
  return 0;
}

void
CosNaming_Client::list_contents (void)
{
  CosNaming::BindingIterator_var bi;
  CosNaming::BindingList_var li;
  CORBA::ULong how_many = 0;
  CosNaming::Binding_var b;
  CosNaming::Name n;
  CORBA::ULong names = 0;

  TAO_TRY
    {
      this->my_name_client_->list (how_many, li, bi, TAO_TRY_ENV);

      while (bi->next_one (b, TAO_TRY_ENV))
        {
          n = b->binding_name;
          names = n.length ();
          ACE_DEBUG ((LM_DEBUG,
                      " (%i) Name: ",
                      names));

          for (CORBA::ULong i = 0; i < names; i++)
            ACE_DEBUG ((LM_DEBUG,
                        "%s ",
                        n[i].id._retn ()));

          ACE_DEBUG ((LM_DEBUG, "type: %s\n",
                      b->binding_type == CosNaming::ncontext ? "C" : "O"));
        }
    }
  TAO_CATCHANY
    {
    }
  TAO_ENDTRY;
}

int
CosNaming_Client::init (int argc, char *argv[])
{
  this->argc_ = argc;
  this->argv_ = argv;

  TAO_TRY
    {
      // Initialize ORB.
      this->orb_ = CORBA::ORB_init (argc, argv, "internet", TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // Initialize the naming services
      if (my_name_client_.init (orb_) != 0)
	ACE_ERROR_RETURN ((LM_ERROR,
			   " (%P|%t) Unable to initialize "
			   "the TAO_Naming_Client. \n"),
			  -1);

      // Parse command line and verify parameters.
      if (this->parse_args () == -1)
        return -1;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("init");
      return -1;
    }
  TAO_ENDTRY;

  return 0;
}

// This function runs the test.

int
main (int argc, char **argv)
{
  CosNaming_Client cosnaming_client;

  if (cosnaming_client.init (argc, argv) == -1)
    return 1;

  return cosnaming_client.run ();
}