summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/Scheduling_Service/Scheduling_Service.cpp
blob: 8e9c433e38b27f21a81f7122968a7f986579a2de (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
// $Id$

#include "Scheduling_Service.h"

#include "ace/Get_Opt.h"
#include "ace/Auto_Ptr.h"
#include "ace/Argv_Type_Converter.h"
#include "orbsvcs/CosNamingC.h"
#include "ace/OS_main.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_strings.h"

ACE_RCSID (Scheduling_Service,
           Scheduling_Service,
           "$Id$")

// Default Constructor.

TAO_Scheduling_Service::TAO_Scheduling_Service (void)
  : scheduler_impl_ (0),
    service_name_ ("ScheduleService"),
    scheduler_type_ (CONFIG)
{
}


// Constructor taking the command-line arguments.

TAO_Scheduling_Service::TAO_Scheduling_Service (int argc, ACE_TCHAR* argv[])
  : scheduler_impl_ (0),
    service_name_ ("ScheduleService"),
    scheduler_type_ (CONFIG)
{
  this->init (argc, argv);
}

// Destructor.

TAO_Scheduling_Service::~TAO_Scheduling_Service (void)
{
}


// Initialize the Scheduling Service with the arguments.

int
TAO_Scheduling_Service::init (int argc, ACE_TCHAR* argv[])
{
  int result;
  CORBA::ORB_var orb;
  PortableServer::POAManager_ptr poa_manager;

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

      // Initialize ORB manager.
      this->orb_manager_.init (command_line.get_argc(), command_line.get_ASCII_argv());

      orb = this->orb_manager_.orb ();

      poa_manager = this->orb_manager_.poa_manager ();

      poa_manager->activate ();

      // Check the non-ORB arguments.  this needs to come before we
      // initialize the scheduler implementation so that we know which
      // type of scheduler to use.

      result = this->parse_args (command_line.get_argc(), command_line.get_TCHAR_argv());
      if (result < 0)
        return result;

      // Construct a scheduler implementation of the specified type.
      switch (this->scheduler_type_)
        {
          case RECONFIG:
            ACE_NEW_THROW_EX (scheduler_impl_,
                              RECONFIG_SCHED_TYPE,
                              CORBA::NO_MEMORY ());
            break;

          case CONFIG:
            ACE_NEW_THROW_EX (scheduler_impl_,
                              CONFIG_SCHED_TYPE,
                              CORBA::NO_MEMORY ());
            break;

          default:
            ACE_ERROR_RETURN ((LM_ERROR,
                               "TAO_Scheduling_Service::init: "
                               "unrecognized Scheduler_Type"), -1);
        }

      // Locate the naming service.
      CORBA::Object_var naming_obj =
        orb->resolve_initial_references ("NameService");

      if (CORBA::is_nil (naming_obj.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to locate the Naming Service.\n"),
                          -1);
      CosNaming::NamingContext_var naming_context =
        CosNaming::NamingContext::_narrow (naming_obj.in ());

      RtecScheduler::Scheduler_var scheduler =
        this->scheduler_impl_->_this ();

      CORBA::String_var scheduler_ior_string =
        orb->object_to_string (scheduler.in ());

      ACE_DEBUG ((LM_DEBUG, ACE_TEXT("The scheduler IOR is <%s>\n"),
                            ACE_TEXT_CHAR_TO_TCHAR(scheduler_ior_string.in ())));

      // Register the servant with the Naming Context....
      CosNaming::Name schedule_name (1);
      schedule_name.length (1);
      schedule_name[0].id = CORBA::string_dup (this->service_name_.rep());
      naming_context->rebind (schedule_name, scheduler.in ());

      if (this->ior_file_name_.rep() != 0)
        {
          FILE *iorf = fopen (this->ior_file_name_.rep(), "w");
          if (iorf != 0)
            {
              ACE_OS::fprintf (iorf,
                               ACE_TEXT("%s\n"),
                               ACE_TEXT_CHAR_TO_TCHAR(scheduler_ior_string.in ()));
              ACE_OS::fclose (iorf);
            }
        }

      if (this->pid_file_name_.rep() != 0)
        {
          FILE *pidf = fopen (this->pid_file_name_.rep(), "w");
          if (pidf != 0)
            {
              ACE_OS::fprintf (pidf,
                               ACE_TEXT("%ld\n"),
                               static_cast<long> (ACE_OS::getpid ()));
              ACE_OS::fclose (pidf);
            }
        }
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("TAO_Scheduling_Service::init");
      return -1;
    }

  return 0;
}


// Runs the TAO_Scheduling_Service.

int
TAO_Scheduling_Service::run (void)
{
  // Run the ORB manager.
  return this->orb_manager_.run ();
}


// Parses the command line arguments.

int
TAO_Scheduling_Service::parse_args (int argc, ACE_TCHAR* argv[])
{
  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("n:p:o:s:"));
  int opt;

  while ((opt = get_opt ()) != EOF)
    {
      switch (opt)
        {
        case 'n':
          this->service_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
          break;

        case 'p':
          this->pid_file_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
          break;

        case 'o':
          this->ior_file_name_ = ACE_TEXT_ALWAYS_CHAR(get_opt.opt_arg ());
          break;

        case 's':
          if (ACE_OS::strcasecmp (ACE_TEXT("CONFIG"), get_opt.optarg) == 0)
            {
              this->scheduler_type_ = CONFIG;
            }
          else if (ACE_OS::strcasecmp (ACE_TEXT("RECONFIG"), get_opt.optarg) == 0)
            {
              this->scheduler_type_ = RECONFIG;
            }
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                          "Usage: %s "
                          "[-n service_name] "
                          "[-p pid_file_name] "
                          "[-o ior_file_name] "
                          "[-s <CONFIG | reconfig>]"
                          "\n",
                          argv[0]));

              return -1;
            }
          break;

        case '?':
        default:

          ACE_DEBUG ((LM_DEBUG,
                      "Usage: %s "
                      "[-n service_name] "
                      "[-p pid_file_name] "
                      "[-o ior_file_name] "
                      "[-s <CONFIG | reconfig>]"
                      "\n",
                      argv[0]));

          return -1;
        }
    }

  return 0;
}

int ACE_TMAIN (int argc, ACE_TCHAR* argv[])
{
  try
    {
      TAO_Scheduling_Service scheduling_service;

      ACE_DEBUG ((LM_DEBUG,
                  "%s; initializing scheduling service\n", __FILE__));

      if (scheduling_service.init (argc, argv) < 0)
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "init"), 1);

      ACE_DEBUG ((LM_DEBUG,
                  "%s; running scheduling service\n", __FILE__));

      scheduling_service.run ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("schedule_service");
          return 1;
    }

  return 0;
}