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

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

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

char *
usage ()
{
  return "invoke as: mt_server -o <ior_output_file> \n -n <num_threads>\n -s <sleep_time (in microseconds)> \n";
}

class MT_AMH_Server
  : public Base_Server
  , public ACE_Task_Base
{
public:
  MT_AMH_Server (int* argc, char **argv)
    : Base_Server (argc, argv) {}

  ~MT_AMH_Server () {}

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

    // Let the base server parse it's argumrents first
    if (Base_Server::parse_args () != 1)
      {
        ACE_ERROR ((LM_ERROR, "%s", usage ()));
        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;
  }

  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 ();
  }

  // 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)
    {
        ACE_ERROR ((LM_ERROR, "%s", usage ()));
        ACE_OS::exit (1);
    }

  AMH_Servant *servant = new AMH_Servant (amh_server.orb ());

  if (servant->parse_args (&argc, argv) != 1)
    {
        ACE_ERROR ((LM_ERROR, "%s : %s", "sleep time unspecified", usage ()));
        ACE_OS::exit (1);
    }

  // Server takes over memory responsibility for servant,
  // no need to deallocate it at end
  amh_server.register_servant (servant);
  amh_server.start_threads ();
}