summaryrefslogtreecommitdiff
path: root/TAO/examples/AMH/Sink_Server/mt_server.cpp
blob: 48e82f21664e3b3eecc463436565018eaaf2395c (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
// $Id$

#include "ace/Task.h"
#include "ace/Get_Opt.h"

#include "Base_Server.h"
#include "AMH_Servant.h"

void
usage (char *message)
{
  // @@ Mayur, what use is there in placing a space before a newline
  //    character?
  //
  // @@ Mayur, have you considered using string concatenation to make
  //    the following usage message more readable?  For example:
  //
  //      static const char * usage =
  //        "invoke as: mt_server -o <ior_output_file>\n"
  //        "-n <num_threads>\n"
  //        "-s <sleep_time (in microseconds)>\n";

  static const char * usage =
  "invoke as: mt_server -o <ior_output_file> \n -n <num_threads>\n -s <sleep_time (in microseconds)> \n";

  // @@ Mayur, why don't you just place the usage message directly in
  //    the below ACE_ERROR macro?  It's not a big deal.  It's just
  //    something we normally do.
  ACE_ERROR ((LM_ERROR, "%s : %s", message, usage));
}

// @@ Mayur, please do not program like a Java programmer.  Please
//    move the class declaration to a separate header file, and keep
//    the implementation code in another file preferrably separate
//    from this one.
class MT_AMH_Server
  : public Base_Server
  , public ACE_Task_Base
{
public:
  // @@ Mayur, have you considered passing in argc by reference
  //    instead of as a pointer?  It would save you the indirection
  //    code in below, and make the code cleaner.  This is what
  //    ORB_init() does.  Not a big deal in any case.
  MT_AMH_Server (int* argc, char **argv)
    : Base_Server (argc, argv) {}

  // @@ Mayur, empty parameter lists should be denoted with "(void)",
  //    not "()".  Again, this is detailed in the guidelines.
  //
  // @@ Mayur, please put inlined methods in a separate `.inl' file,
  //    as detailed in the ACE/TAO coding/style guidelines, and as per
  //    our conventions.
  ~MT_AMH_Server () {}

  // @@ Mayur, please put inlined methods in a separate `.inl' file,
  //    as detailed in the ACE/TAO coding/style guidelines, and as per
  //    our conventions.

  // We need to parse an extra thread_count paramter for
  // multi-threraded server.
  virtual int parse_args (void)
  {

    // Let the base server parse it's argumrents first
    if (Base_Server::parse_args () != 1)
      {
        usage (ACE_const_cast (char *, ""));
        ACE_OS::exit (1);
      }

    ACE_Get_Opt get_opts (*(this->argc_), this->argv_, "n:");
    int c;
    int count_argv = 0;

    while ((c = get_opts ()) != -1)
      {
        ++count_argv;
        switch (c)
          {
          case 'n':
            {
              this->nthreads_ = ACE_OS::atoi (get_opts.opt_arg ());

              {
                // Added unneeded '{ & }' just to satisfy Win32
                for (int i = count_argv; i <= *(this->argc_); ++i)
                  this->argv_ [i] = this->argv_ [i+2];
              }

              // Decrement the value of this->argc_ to reflect the removal
              // of '-n' option.
              *(this->argc_) = *(this->argc_) - 2;
              return 1;
            }

          case '?':
          default:
            // Don't do anything.
            break;
          }
      }
    return 0;
  }

  // @@ Mayur, please put inlined methods in a separate `.inl' file,
  //    as detailed in the ACE/TAO coding/style guidelines, and as per
  //    our conventions.
  void start_threads (void)
  {
    // Each of this thread runs the event loop
    this->activate (THR_NEW_LWP | THR_JOINABLE, this->nthreads_, 1);
    this->thr_mgr ()->wait ();
  }

  // @@ Mayur, please put inlined methods in a separate `.inl' file,
  //    as detailed in the ACE/TAO coding/style guidelines, and as per
  //    our conventions.

  // the service method
  virtual int svc (void)
  {
    run_event_loop ();
    return 1;
  }

private:
  int nthreads_;
};



int
main (int argc, char *argv[])
{
  MT_AMH_Server amh_server (&argc, argv);
  amh_server.try_RT_scheduling();
  amh_server.start_orb_and_poa ();

  if (amh_server.parse_args () != 1)
    {
        usage (ACE_const_cast (char*, ""));
        ACE_OS::exit (1);
    }

  AMH_Servant servant (amh_server.orb ());

  if (servant.parse_args (&argc, argv) != 1)
    {
      // @@ Mayur, why are you casting away the const-ness?  Just make
      //    your usage() function accept "const char *".
      usage (ACE_const_cast (char*, "sleep time unspecified"));
      ACE_OS::exit (1);
    }

  amh_server.register_servant (&servant);
  amh_server.start_threads ();

  return 1;
}