summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Logger/clnt.cpp
blob: 8fbac9b997bd64852ba5b8d12f8ede2d08460a0c (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/orbsvcs/bin/Logger
//
// = FILENAME
//    clnt.cpp
//
// = DESCRIPTION 
//    This program tests an implementation of a logger service.  It uses the
//    Logger_Factory server to create a number of logger objects.  It then
//    uses their object references to test functions supported by the
//    logger server. 
//
// = AUTHORS
//      Sergio Flores-Gaitan
//
// ============================================================================

#include "ace/streams.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Dgram_Mcast.h"
#include "orbsvcs/CosNamingC.h"
#include "orbsvcs/LoggerC.h"
#include "clnt.h"

// constructor

Logger_Client::Logger_Client (void)
  : test_nesting_ (0)
{
}

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

int
Logger_Client::parse_args (void)
{
  ACE_Get_Opt get_opts (argc_, argv_, "dn");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':  // debug flag
        TAO_debug_level++;
        break;
      case 'n':
        this->test_nesting_++;
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           " [-d]"
                           " [-n]"
                           "\n"
			   "    -d: increase debug level\n"
			   "    -n: test nesting in Naming Service\n",
                           this->argv_ [0]),
                          -1);
      }

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

// Execute client example code.

int
Logger_Client::run (void)
{
  TAO_TRY
    {
      CORBA::String_var msg1 =
	CORBA::string_copy ("Logging at logger 1");
      this->logger_1_->log (msg1.in (), TAO_TRY_ENV);
      TAO_CHECK_ENV;

      CORBA::String_var msg2 =
	CORBA::string_copy ("Logging at logger 2");
      this->logger_2_->log (msg2.in (), TAO_TRY_ENV);
      TAO_CHECK_ENV;

    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("run");
      return -1;
    }
  TAO_ENDTRY;
  return 0;
}

Logger_Client::~Logger_Client (void)
{
}

int
Logger_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;

      CORBA::Object_var naming_obj =
	this->orb_->resolve_initial_references ("NameService");
      if (CORBA::is_nil (naming_obj.in ()))
	ACE_ERROR_RETURN ((LM_ERROR,
			   " (%P|%t) Unable to initialize the POA.\n"),
			  -1);

      CosNaming::NamingContext_var naming_context = 
        CosNaming::NamingContext::_narrow (naming_obj.in (), TAO_TRY_ENV);
      TAO_CHECK_ENV;
      
      ACE_DEBUG ((LM_DEBUG, "Obtained naming_context\n"));

      // Parse command line and verify parameters.
      if (this->parse_args () == -1)
	return -1;

      CosNaming::Name factory_name(1);
      factory_name.length (1);
      factory_name[0].id = CORBA::string_dup ("logger_factory");  

      CORBA::Object_var factory_ref = 
	naming_context->resolve (factory_name, TAO_TRY_ENV);
      TAO_CHECK_ENV;

      /// The following resolve() must fail with an exception.

      CosNaming::Name factory_name2(1);
      factory_name2.length (1);
      factory_name2[0].id = CORBA::string_dup ("logger_factory2");  

      CORBA::Object_var factory_ref2 = 
	naming_context->resolve (factory_name2, TAO_TRY_ENV);

      if (TAO_TRY_ENV.exception ())
	{
	  CosNaming::NamingContext::NotFound_ptr ex;
	  ex = CosNaming::NamingContext::NotFound::_narrow (TAO_TRY_ENV.exception ());
	  if (ex != 0)
	    TAO_TRY_ENV.print_exception ("Negative test case for name not found, succeeded\n");
	  else
	    TAO_TRY_ENV.print_exception ("Negative test case for name not found, FAILED!\n");
	}

      if (CORBA::is_nil (factory_ref.in ()))
	ACE_ERROR_RETURN ((LM_ERROR, "resolved to nil object"), -1);

      ACE_DEBUG ((LM_DEBUG, "Logger_Factory resolved\n"));
      
      Logger_Factory_var factory = 
	Logger_Factory::_narrow (factory_ref.in (), TAO_TRY_ENV);
      TAO_CHECK_ENV;

      if (CORBA::is_nil (factory.in ()))
	ACE_ERROR_RETURN ((LM_ERROR, "narrow returned nil"), -1);

      ACE_DEBUG ((LM_DEBUG, "Logger_Factory narrowed\n"));

      CORBA::String_var str =
	this->orb_->object_to_string (factory.in (), TAO_TRY_ENV);
      TAO_CHECK_ENV;

      ACE_DEBUG ((LM_DEBUG, "The factory IOR is <%s>\n", str.in ()));

      // Now retrieve the Logger obj ref corresponding to key1 and key2
      this->logger_1_ = factory->make_logger ("key1", TAO_TRY_ENV);
      TAO_CHECK_ENV;
      this->logger_2_ = factory->make_logger ("key2", TAO_TRY_ENV);
      TAO_CHECK_ENV;

      ACE_DEBUG ((LM_DEBUG, "Created two loggers"));

      if (CORBA::is_nil (this->logger_1_.in ()))
	ACE_ERROR_RETURN ((LM_ERROR, "nil logger1"), -1);

      if (CORBA::is_nil (this->logger_2_.in ()))
	ACE_ERROR_RETURN ((LM_ERROR, "nil logger2"), -1);

      ACE_DEBUG ((LM_DEBUG, "Created two loggers\n"));

      if (this->test_nesting_)
	{
	  CosNaming::Name nested(1);
	  nested.length (1);
	  nested[0].id = CORBA::string_dup ("my_naming_context_1");

	  CosNaming::NamingContext_var nc1 = 
	    naming_context->bind_new_context (nested, TAO_TRY_ENV);
	  TAO_CHECK_ENV;

	  if (CORBA::is_nil (nc1.in ()))
	    ACE_ERROR_RETURN ((LM_ERROR, "nil nested context"), -1);
 
	  ACE_DEBUG ((LM_DEBUG, "Nested naming context created\n"));

	  CosNaming::Name logger1_name (1);
	  logger1_name.length (1);
	  logger1_name[0].id = CORBA::string_dup ("logger_1_");
	  nc1->bind (logger1_name, this->logger_1_.in (), TAO_TRY_ENV);
	  TAO_CHECK_ENV;

	  ACE_DEBUG ((LM_DEBUG, "Bind in nested context succeded\n"));

	  nc1->unbind (logger1_name, TAO_TRY_ENV);
	  TAO_CHECK_ENV;

	  ACE_DEBUG ((LM_DEBUG, "Unbind in nested context succeded\n"));

	  // destroy the naming context created above.
	  nc1->destroy (TAO_TRY_ENV);
	  TAO_CHECK_ENV;

	  ACE_DEBUG ((LM_DEBUG, "destroy of nested context succeded\n"));
	}
    }
  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)
{
  Logger_Client logger_client;
 
  if (logger_client.init (argc, argv) != 0)
    return 1;

  return logger_client.run ();
}