summaryrefslogtreecommitdiff
path: root/TAO/docs/tutorials/Quoter/RTCORBA/Admin.cpp
blob: c6de33c1e8449bace57542e4763af23b398cc4ad (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
// $Id$

// ACE header files
#include "ace/Get_Opt.h"
#include "ace/OS_NS_stdlib.h"
#include "orbsvcs/CosNamingC.h"
// STL strings
#include <string>

// local header files
#include "DistributorC.h"

enum Option_Types
{
  START,
  STOP,
  RATE
};

// Name of the file that stores the StockDistributor IOR.
static std::string ior = "file://StockDistributor.ior";

// Name of the StockDistributor registered with the Naming Service.
static std::string distributor_name = "StockDistributor";

// The default command, which starts the StockDistributor.
static Option_Types option = START;

// The default rate, which sends data out once per second.
static std::string rate = "1";

// A flag that indicates use of the Naming Service.
static bool use_naming = false;

// Parse the command-line arguments and set the global options.

static int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:r:bsc"));
  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'o':
          ior = get_opts.opt_arg ();
          break;
        case 'r':
          option = RATE;
          rate = get_opts.opt_arg ();
          break;
        case 'b':
          option = START;
          break;
        case 's':
          option = STOP;
          break;
        case 'c':
          use_naming = get_opts.opt_arg ();
          break;

        case '?':
        default:
          ACE_ERROR_RETURN ((LM_ERROR,
                             "usage: %s\n"
                             "-o <Distributor IOR> (the ior file of stock distributor (default is file://StockDistributor.ior))\n"
                             "-r <rate> (set the distribution rate to 'n' seconds (default is 1))\n"
                             "-b (start the stock distributor)\n"
                             "-s (stop the stock distributor)\n"
                             "\n",
                             argv [0]),
                            -1);
        }
    }

  return 0;
}

static CORBA::Object_ptr
get_distributor_reference (CORBA::ORB_ptr orb)
{
  if (use_naming)
    {
      CORBA::Object_var tmp =
        orb->resolve_initial_references ("NameService");

      CosNaming::NamingContext_var pns =
        CosNaming::NamingContext::_narrow (tmp.in ());

      CosNaming::Name name (1);
      name.length (1);
      name[0].id = distributor_name.c_str ();

      return pns->resolve (name);
    }
  else
    // Read and destringify the Stock_Distributor object's IOR.
    return orb->string_to_object (ior.c_str ());
}

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
    {
      // Initialiaze the ORB.
      CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);

      // This call MUST come after ORB_init(), which may need to
      // extract -ORB options first.
      if (parse_args (argc, argv) != 0)
        return 1;

      CORBA::Object_var obj = get_distributor_reference (orb);

      // Narrow the IOR to a Stock_Distributor object reference.
      Stock::StockDistributor_var stock_distributor =
        Stock::StockDistributor::_narrow(obj);

      if (CORBA::is_nil (stock_distributor.in ()))
        ACE_ERROR_RETURN ((LM_DEBUG,
                           "Nil StockDistributor object reference <%s>\n",
                           ior.c_str ()),
                          1);

      if (option == STOP)
        {
          ACE_DEBUG ((LM_DEBUG, "Stop the stock distributor.\n"));
          stock_distributor->stop ();
        }
      else if (option == RATE)
        {
          CORBA::Long notify_rate = ACE_OS::atoi (rate.c_str ());
          ACE_DEBUG ((LM_DEBUG,
                      "Set the distribution notification rate to %d seconds.\n",
                      notify_rate));
          stock_distributor->notification_rate (notify_rate);
        }
      else // option == START
        {
          ACE_DEBUG ((LM_DEBUG, "Start the stock distributor.\n"));
          stock_distributor->start ();
        }

      // Since we are a "pure client" we don't need to run the ORB's event loop!
      // Cleanup the ORB.
      orb->destroy ();
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("Exception caught:");
      return 1;
    }

  return 0;
}