summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/LifeCycle_Service/LifeCycle_Service.cpp
blob: 0bd674008d490bb901a026dea916aa46bb10cd2d (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

//=============================================================================
/**
 *  @file    LifeCycle_Service.cpp
 *
 *  The server for the LifeCycleService of the quoter example.
 *
 *  @author Michael Kircher (mk1@cs.wustl.edu)
 */
//=============================================================================


#include "orbsvcs/Log_Macros.h"
#include "LifeCycle_Service.h"

#include "ace/Argv_Type_Converter.h"
#include "ace/OS_main.h"

Life_Cycle_Service_Server::Life_Cycle_Service_Server ()
:   debug_level_ (1)
{
}

Life_Cycle_Service_Server::~Life_Cycle_Service_Server ()
{
  try
    {
      // Unbind the Factory Finder.
      CosNaming::Name generic_Factory_Name (2);
      generic_Factory_Name.length (2);
      generic_Factory_Name[0].id = CORBA::string_dup ("LifeCycle_Service");
      this->namingContext_var_->unbind (generic_Factory_Name);
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("User Exception");
    }
}

int
Life_Cycle_Service_Server::init (int argc,
                                 ACE_TCHAR *argv[])
{
  int retval = 0;

  // Copy command line parameter.
  ACE_Argv_Type_Converter command(argc, argv);

  retval = this->orb_manager_.init (command.get_argc(),
                                    command.get_TCHAR_argv());

  if (retval == -1)
    ORBSVCS_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT("%p\n"),
                       ACE_TEXT("init")),
                      -1);

  // Activate the POA manager
  retval = this->orb_manager_.activate_poa_manager ();

  if (retval == -1)
    ORBSVCS_ERROR_RETURN ((LM_ERROR, "%p\n", "activate_poa_manager"), -1);


  this->parse_args (command.get_argc(), command.get_TCHAR_argv());

  ACE_NEW_RETURN (this->life_Cycle_Service_i_ptr_,
                  Life_Cycle_Service_i(this->debug_level_),
                  -1);

  // Activate the object.
  CORBA::String_var str  =
    this->orb_manager_.activate (this->life_Cycle_Service_i_ptr_);

  if (this->debug_level_ >= 2)
    ORBSVCS_DEBUG ((LM_DEBUG, "LifeCycle_Service: IOR is: <%C>\n", str.in ()));

  // Register the LifeCycle Service with the Naming Service.
  try
    {
      if (this->debug_level_ >= 2)
        ORBSVCS_DEBUG ((LM_DEBUG,
                    ACE_TEXT("LifeCycle_Service: Trying to get a reference to the Naming Service.\n")));

      // Get the Naming Service object reference.
      CORBA::Object_var namingObj_var =
        orb_manager_.orb()->resolve_initial_references ("NameService");

      if (CORBA::is_nil (namingObj_var.in ()))
        ORBSVCS_ERROR ((LM_ERROR,
                   " LifeCycle_Service: Unable get the Naming Service.\n"));

      // Narrow the object reference to a Naming Context.
      namingContext_var_ = CosNaming::NamingContext::_narrow (namingObj_var.in ());


      if (CORBA::is_nil (namingContext_var_.in ()))
        ORBSVCS_ERROR ((LM_ERROR,
                   "LifeCycle_Service: Unable get the Naming Service.\n"));

      if (this->debug_level_ >= 2)
        ORBSVCS_DEBUG ((LM_DEBUG,
                    ACE_TEXT("LifeCycle_Service: Have a proper reference to the Naming Service.\n")));

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

      CORBA::Object_ptr tmp = this->life_Cycle_Service_i_ptr_->_this();

      namingContext_var_->bind (life_Cycle_Service_Name,
                                tmp);

      if (this->debug_level_ >= 2)
        ORBSVCS_DEBUG ((LM_DEBUG,
                    ACE_TEXT("LifeCycle_Service: Bound the LifeCycle Service to the Naming Context.\n")));
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception (
        "Life_Cycle_Service_Server::init");
    }

  return 0;
}


int
Life_Cycle_Service_Server::run ()
{
  if (this->debug_level_ >= 1)
    ORBSVCS_DEBUG ((LM_DEBUG,
                ACE_TEXT("\nLifeCycle Service: Life_Cycle_Service_Server is running\n")));

  orb_manager_.orb()->run ();

  return 0;
}


// Function get_options.

u_int
Life_Cycle_Service_Server::parse_args (int argc,
                                       ACE_TCHAR* argv[])
{
  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("?d:"));
  int opt;
  int exit_code = 0;

  while ((opt = get_opt ()) != EOF)
    switch (opt)
      {
      case 'd':  // debug flag.
        this->debug_level_ = ACE_OS::atoi (get_opt.opt_arg ());
        break;
      default:
        exit_code = 1;
        ORBSVCS_ERROR ((LM_ERROR,
                    "%s: unknown arg, -%c\n",
                    argv[0], char(opt)));
        ACE_FALLTHROUGH;
      case '?':
        ORBSVCS_DEBUG ((LM_DEBUG,
                    ACE_TEXT("usage:  %s")
                    ACE_TEXT(" [-d] <debug level> - Set the debug level\n")
                    ACE_TEXT(" [-?]               - Prints this message\n")
                    ACE_TEXT("\n"),
                    argv[0]));
        ACE_OS::exit (exit_code);
        break;
      }
  return 0;
}

// function main

int
ACE_TMAIN (int argc, ACE_TCHAR* argv [])
{
  Life_Cycle_Service_Server life_Cycle_Service_Server;

  try
    {
      int check = life_Cycle_Service_Server.init (argc,
                                                  argv);

      if (check)
        return 1;
      else
        {
          life_Cycle_Service_Server.run ();
        }
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("LifeCycleService::main");
      return -1;
    }
  return 0;
}