summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Bug_3673_Regression/nsmain.cpp
blob: f8b50ad46b56859a1040716204e4beaa1466c2f9 (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
// $Id$

#include "NamingTask.h"
#include "Hello.h"
#include "NsShutdown.h"
#include "orbsvcs/CosNamingC.h"
#include "ace/Get_Opt.h"

const ACE_TCHAR *ior_output_file = ACE_TEXT("ns.ior");
const ACE_TCHAR *ior_shutdown_file = ACE_TEXT("shutdown.ior");

class TestTask : public ACE_Task_Base
{
public:
  TestTask (int argc, ACE_TCHAR **argv);
  virtual int svc ();

  int parse_args (int argc, ACE_TCHAR **argv);

  void end();
private:
  NamingTask namingServiceA_;
  NamingTask namingServiceB_;
  CORBA::ORB_var orb_;
  CORBA::Boolean shutdown_ns_;
};

TestTask::TestTask (int argc, ACE_TCHAR **argv)
 : namingServiceA_("NamingORBA", argc, argv, 9931),
   namingServiceB_("NamingORBB", argc, argv, 9932)
{
  orb_ = CORBA::ORB_init (argc, argv, "ServerORB");
  shutdown_ns_ = false;
  parse_args (argc, argv);
}

void TestTask::end()
{
  orb_->shutdown(0);
  this->wait();
}

int
TestTask::parse_args (int argc, ACE_TCHAR **argv)
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:s:"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'o':
        ior_output_file = get_opts.opt_arg ();
        break;
      case 's':
        ior_shutdown_file = get_opts.opt_arg ();
        shutdown_ns_ = true;
        break;
      }
  // Indicates sucessful parsing of the command line
  return 0;
}

int TestTask::svc()
{

  try {
    // Start the Naming Service tasks
    namingServiceA_.activate();
    // Wait for the Naming Service initialized.
    namingServiceA_.waitInit();

    namingServiceB_.activate();
    // Wait for the Naming Service initialized.
    namingServiceB_.waitInit();

    FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
    if (output_file == 0)
      ACE_ERROR_RETURN ((LM_ERROR,
                          "Cannot open output file for writing IOR: ns.ior\n"),
                          1);
    ACE_OS::fprintf (output_file, "%s", ACE_TEXT_CHAR_TO_TCHAR (namingServiceA_.ior ()));
    ACE_OS::fclose (output_file);

    // Get reference to Root POA
    CORBA::Object_var obj = orb_->resolve_initial_references("RootPOA");
    PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());

    // Activate POA Manager
    PortableServer::POAManager_var mgr = poa->the_POAManager();
    mgr->activate();

    // Find the Naming Service
    obj = orb_->string_to_object (namingServiceB_.ior ());
    CosNaming::NamingContext_var root =
      CosNaming::NamingContext::_narrow(obj.in());

    if (CORBA::is_nil(root.in())) {
      ACE_ERROR ((LM_ERROR, "Error, Nil Naming Context reference\n"));
      return 1;
    }
    // Bind the example Naming Context, if necessary
    CosNaming::NamingContext_var example_nc;
    CosNaming::Name name;
    name.length(1);
    name[0].id = CORBA::string_dup("example");
    try
    {
      obj = root->resolve(name);
      example_nc = CosNaming::NamingContext::_narrow(obj.in());
    }
    catch (const CosNaming::NamingContext::NotFound&)
    {
      example_nc = root->bind_new_context(name);
    }

    // Bind the Test object
    name.length(2);
    name[1].id = CORBA::string_dup("Hello");

    // Create an object
    Hello servant(orb_.in ());
    PortableServer::ObjectId_var oid = poa->activate_object(&servant);
    obj = poa->id_to_reference(oid.in());
    root->rebind(name, obj.in());

    ACE_DEBUG ((LM_INFO, "Hello object bound in Naming Service B\n"));

    name.length(1);
    obj = orb_->string_to_object (namingServiceA_.ior ());
    root = CosNaming::NamingContext::_narrow(obj.in());
    root->bind_context (name, example_nc.in ());

    ACE_DEBUG ((LM_INFO, "'example' context of NS B bound in Naming Service A\n"));

    if (shutdown_ns_)
    {
      namingServiceB_.end();

      ACE_DEBUG ((LM_INFO, "Naming Service B shut down\n"));
    }

    // Create shutdown server
    NsShutdown shutdown_servant(orb_.in ());
    PortableServer::ObjectId_var shutdown_oid = poa->activate_object(&shutdown_servant);
    obj = poa->id_to_reference(shutdown_oid.in());
    CORBA::String_var ior = orb_->object_to_string (obj.in ());

    output_file= ACE_OS::fopen (ior_shutdown_file, "w");
    if (output_file == 0)
      ACE_ERROR_RETURN ((LM_ERROR,
                          "Cannot open output file %s for writing IOR: %C\n",
                          ior_shutdown_file,
                          ior.in ()),
                          1);
    ACE_OS::fprintf (output_file, "%s", ior.in ());
    ACE_OS::fclose (output_file);

    // Normally we run the orb and the orb is shutdown by
    // calling TestTask::end().
    // Accept requests
    orb_->run();
    orb_->destroy();

    // Shutdown the Naming Services.
    namingServiceA_.end();
    if (!shutdown_ns_)
      {
        namingServiceB_.end();
      }

    return 0;
  }
  catch (CORBA::Exception& ex)
  {
    ex._tao_print_exception ("CORBA exception: ");
  }

  return -1;
}

int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  // Start the Test task
  TestTask test_ (argc, argv);
  if (test_.activate() == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR, "Unable to start test task.\n"), -1);
    }

  // Wait tasks finish.
  test_.thr_mgr ()->wait();

  return 0;
}