summaryrefslogtreecommitdiff
path: root/TAO/utils/nslist/nslist.cpp
blob: 46acd2d6b4ab9e6df660bed9fdfa31dbc4080322 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

//=============================================================================
/**
 *  @file    nslist.cpp
 *
 *  $Id$
 *
 *  Naming Service listing utility
 *
 *
 *  @author  Thomas Lockhart, NASA/JPL <Thomas.Lockhart@jpl.nasa.gov>
 *  @date 1999-06-03
 */
//=============================================================================


#include "ace/SString.h"
#include "orbsvcs/CosNamingC.h"
#include "tao/Endpoint.h"
#include "tao/Profile.h"
#include "tao/Stub.h"
#include "tao/ORB_Constants.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_stdio.h"

CORBA::ORB_var orb;
int showIOR = 0;
int showNSonly = 0;
int showCtxIOR = 0;

static void list_context (CosNaming::NamingContext_ptr nc,
                          int level
                          ACE_ENV_ARG_DECL);

static void
get_tag_name (CORBA::ULong tag, ACE_CString& tag_string)
{
  if (tag == IOP::TAG_INTERNET_IOP)
      tag_string = "IIOP";
  else if (tag == TAO_TAG_UIOP_PROFILE)
      tag_string = "UIOP";
  else if (tag == TAO_TAG_SHMEM_PROFILE)
      tag_string = "SHMEM";
#ifdef TAO_TAG_UDP_PROFILE
  else if (tag == TAO_TAG_UDP_PROFILE)
      tag_string = "GIOP over UDP";
#endif /* TAO_TAG_UDP_PROFILE */
  else
      tag_string = "Unknown tag: " + tag;
}


static void
display_endpoint_info (CORBA::Object_ptr obj)
{
  if (CORBA::is_nil (obj))
    {
      ACE_DEBUG ((LM_DEBUG, "Nil\n"));
      return;
    }

  TAO_Stub *stub = obj->_stubobj ();
  if (!stub)
    {
      ACE_DEBUG ((LM_DEBUG, "Invalid stub\n"));
      return;
    }

  TAO_Profile* profile = stub->profile_in_use ();
  if (!profile)
    {
      ACE_DEBUG ((LM_DEBUG, "Invalid profile\n"));
      return;
    }


  TAO_Endpoint* endpoint = profile->endpoint ();
  if (!endpoint)
    {
      ACE_DEBUG ((LM_DEBUG, "Invalid profile\n"));
      return;
    }

  CORBA::ULong tag = endpoint->tag ();
  ACE_CString tag_name;
  get_tag_name (tag, tag_name);

  char buf[255];
  if (endpoint->addr_to_string (buf, 255) < 0)
    {
      ACE_DEBUG ((LM_DEBUG, "Could not put endpoint address in string.\n"));
      return;
    }

  ACE_DEBUG ((LM_DEBUG,
              "Protocol: %s,   Endpoint: %s\n",
              tag_name.c_str(),
              buf));
}

// Display NS entries from a finite list.

static void
show_chunk (CosNaming::NamingContext_ptr nc,
            const CosNaming::BindingList &bl,
            int level
            ACE_ENV_ARG_DECL)
{
  for (CORBA::ULong i = 0;
       i < bl.length ();
       i++)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "%*s%s",
                  2 * level,
                  "",
                  bl[i].binding_name[0].id.in ()));

      if (ACE_OS::strlen (bl[i].binding_name[0].kind) > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "(%s)",
                    bl[i].binding_name[0].kind.in ()));

      CosNaming::Name Name;
      Name.length (1);
      Name[0].id =
        CORBA::string_dup (bl[i].binding_name[0].id);
      Name[0].kind =
        CORBA::string_dup (bl[i].binding_name[0].kind);

      CORBA::Object_var obj = nc->resolve (Name ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      // If this is a context node, follow it down to the next
      // level...
      if (bl[i].binding_type == CosNaming::ncontext)
        {
          if (showCtxIOR)
            {
              CORBA::String_var str =
                orb->object_to_string (obj.in ()
                                       ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
              ACE_DEBUG ((LM_DEBUG,
                          ": naming context : <%s>\n",
                          str.in ()));
            }
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                            ": naming context\n"));

            }
          CosNaming::NamingContext_var xc =
            CosNaming::NamingContext::_narrow (obj.in () ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;

          list_context (xc.in (), level + 1 ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;
        }
      // Mark this node as a reference
      else
        {
          if (showIOR)
            {
              CORBA::String_var str =
                orb->object_to_string (obj.in ()
                                       ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
              ACE_DEBUG ((LM_DEBUG,
                          ": <%s>\n",
                          str.in ()));
            }
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                          ": object reference:   "));
              display_endpoint_info (obj.in());
            }
        }
    }
}

static void
list_context (CosNaming::NamingContext_ptr nc,
              int level
              ACE_ENV_ARG_DECL)
{
  CosNaming::BindingIterator_var it;
  CosNaming::BindingList_var bl;
  const CORBA::ULong CHUNK = 100;

  nc->list (CHUNK, bl, it ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  show_chunk (nc, bl.in (), level ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (!CORBA::is_nil (it.in ()))
    {
      CORBA::Boolean more;

      do
        {
          more = it->next_n (CHUNK, bl);
          show_chunk (nc, bl.in (), level ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;
        }
      while (more);

      it->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
    }
}

int
main (int argc, char *argv[])
{
  showIOR = 0;
  showNSonly = 0;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      orb = CORBA::ORB_init (argc, argv, "" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      char *pname = argv[0];

      while (argc > 0)
        {
          if (strcmp(*argv, "--ior") == 0)
            {
              if (showNSonly)
                {
                  ACE_DEBUG ((LM_DEBUG,
                              "Error: --nsior and --ior are "
                              "both specified\n"));
                  return 1;
                }
              showIOR = 1;
            }
          else if (ACE_OS::strcmp (*argv, "--nsior") == 0)
            {
              if (showIOR)
                {
                  ACE_DEBUG ((LM_DEBUG,
                              "Error: --nsior and --ior "
                              "are both specified\n"));
                  return 1;
                }
              showNSonly = 1;
            }
          else if (ACE_OS::strcmp (*argv, "--ctxior") == 0)
            {
              showCtxIOR = 1;
            }
          else if (ACE_OS::strncmp (*argv, "--", 2) == 0)
            {
              ACE_DEBUG ((LM_DEBUG, "Usage: %s [[ --ior ][ --ctxior ] | --nsior ]\n", pname));
              return 1;
            }
          argc--;
          argv++;
        }

      CORBA::Object_var obj =
        orb->resolve_initial_references ("NameService" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CosNaming::NamingContext_var root_nc =
        CosNaming::NamingContext::_narrow (obj.in () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CORBA::String_var str =
        orb->object_to_string (root_nc.in ()
                               ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (obj.in ()) || CORBA::is_nil (root_nc.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Naming Service not found"),
                          -1);

      if (showNSonly)
        {
          // ACE_DEBUG ((LM_DEBUG, "%s", str.in ()));
          ACE_OS::printf ("%s", str.in());
        }
      else
        {
          if (showIOR)
            {
              ACE_DEBUG ((LM_DEBUG,
                          "Naming Service: <%s>\n---------\n",
                          str.in ()));
            }
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                          "Naming Service:\n---------\n"));
            }

          list_context (root_nc.in (), 1 ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in nslist");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}