summaryrefslogtreecommitdiff
path: root/apps/JAWS/server/PROTOTYPE/JAWS/Server.cpp
blob: 2065a4d5d255b61a5ac8951fa1b2ef0f1a6fcfcb (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
// $Id$

#include "ace/Get_Opt.h"

#include "JAWS/Server.h"
#include "JAWS/Data_Block.h"
#include "JAWS/Concurrency.h"
#include "JAWS/IO_Handler.h"

JAWS_Server::JAWS_Server (void)
  : port_ (5432),
    concurrency_ (0),
    dispatch_ (0),
    nthreads_ (5),
    maxthreads_ (20),
    flags_ (THR_NEW_LWP)
{
}

JAWS_Server::JAWS_Server (int argc, char *argv[])
  : port_ (5432),
    concurrency_ (0),
    dispatch_ (0),
    nthreads_ (5),
    maxthreads_ (20),
    flags_ (THR_NEW_LWP)
{
  this->init (argc, argv);
}

void
JAWS_Server::init (int argc, char *argv[])
{
  this->parse_args (argc, argv);

  JAWS_Thread_Pool_Singleton::instance ()->open (this->flags_,
                                                 this->nthreads_,
                                                 this->maxthreads_);

  JAWS_Thread_Per_Singleton::instance ()->open (this->flags_,
                                                this->maxthreads_);

}

int
JAWS_Server::open (JAWS_Pipeline_Handler *protocol)
{
  JAWS_Synch_IO_Handler_Factory synch_factory;
#if defined (ACE_WIN32)
  JAWS_Asynch_IO_Handler_Factory asynch_factory;
#else
  JAWS_Synch_IO_Handler_Factory &asynch_factory = synch_factory;
#endif /* defined (ACE_WIN32) */

  JAWS_IO_Handler_Factory *factory;
  JAWS_IO_Handler *handler;
  JAWS_Data_Block *db;

  ACE_INET_Addr inet_addr (this->port_);

  // initialize an IO_Handler
  factory = (this->dispatch_ == 0) ? &synch_factory : &asynch_factory;
  handler = factory->create_io_handler ();
  if (handler == 0)
    {
      factory->destroy_io_handler (handler);
      ACE_DEBUG ((LM_DEBUG, "JAWS_Server::open, can't create handler\n"));
      return -1;
    }
  // handler->task (protocol);

  // initialize data block
  db = new JAWS_Data_Block;
  if (db == 0)
    {
      factory->destroy_io_handler (handler);
      ACE_DEBUG ((LM_DEBUG, "JAWS_Server::open, can't create data block\n"));
      return -1;
    }

  // db->addr (&inet_addr);
  db->handler (handler);
  db->task (JAWS_Pipeline_Accept_Task_Singleton::instance ());

  // The message block should contain an INET_Addr, and call the
  // io->accept (INET_Addr) method!

  ACE_Message_Block mb (db);

  JAWS_Concurrency_Base *concurrency;
  concurrency = (this->concurrency_ == 0)
    ? JAWS_Thread_Pool_Singleton::instance ()
    : JAWS_Thread_Per_Singleton::instance ()
    ;

  concurrency->put (&mb);

  while (ACE_OS::thr_join (0, NULL) != -1)
    ;

  return 0;
}

void
JAWS_Server::parse_args (int argc, char *argv[])
{
  int c;

  ACE_Get_Opt getopt (argc, argv, "p:c:d:n:m:f:");
  while ((c = getopt ()) != -1)
    switch (c)
      {
      case 'p':
        this->port_ = ACE_OS::atoi (getopt.optarg);
        break;
      case 'c':
        if (ACE_OS::strcmp (getopt.optarg, "PER_REQUEST") == 0)
          this->concurrency_ = 1;
        else this->concurrency_ = 0;
        break;
      case 'd':
        if (ACE_OS::strcmp (getopt.optarg, "ASYNCH") == 0)
          this->dispatch_ = 1;
        else this->dispatch_ = 0;
        break;
      case 'n':
        this->nthreads_ = ACE_OS::atoi (getopt.optarg);
        break;
      case 'm':
        this->maxthreads_ = ACE_OS::atoi (getopt.optarg);
        break;
      case 'f':
        if (ACE_OS::strcmp (getopt.optarg, "THR_BOUND") == 0)
          this->flags_ |= THR_BOUND;
        else if (ACE_OS::strcmp (getopt.optarg, "THR_DAEMON") == 0)
          this->flags_ |= THR_DAEMON;
        else if (ACE_OS::strcmp (getopt.optarg, "THR_DETACHED") == 0)
          this->flags_ |= THR_DETACHED;
        break;
      }

  if (this->port_ == 0) this->port_ = 5432;
  if (this->nthreads_ == 0) this->nthreads_ = 5;
  if (this->maxthreads_ == 0) this->maxthreads_ = 20;
}