Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Server.cpp
blobe26ba15d3f8040f9a4557cea9285240a640e1eb4
1 #include "ace/config-lite.h"
2 #include "ace/OS_NS_string.h"
3 #include "ace/Get_Opt.h"
5 #if (ACE_NTRACE != 1)
6 #include "ace/Trace.h"
7 #endif /* (ACE_NTRACE != 1) */
9 #include "JAWS/Server.h"
10 #include "JAWS/Data_Block.h"
11 #include "JAWS/Concurrency.h"
12 #include "JAWS/Jaws_IO.h"
13 #include "JAWS/IO_Handler.h"
14 #include "JAWS/IO_Acceptor.h"
15 #include "JAWS/Pipeline_Tasks.h"
19 JAWS_Server::JAWS_Server (void)
20 : port_ (5432),
21 concurrency_ (0),
22 dispatch_ (0),
23 nthreads_ (5),
24 maxthreads_ (20),
25 flags_ (THR_NEW_LWP)
29 JAWS_Server::JAWS_Server (int argc, char *argv[])
30 : ratio_ (1),
31 port_ (5432),
32 concurrency_ (0),
33 dispatch_ (0),
34 nthreads_ (5),
35 maxthreads_ (20),
36 flags_ (THR_NEW_LWP)
38 this->init (argc, argv);
41 void
42 JAWS_Server::init (int argc, char *argv[])
44 this->parse_args (argc, argv);
46 this->policy_.ratio (this->ratio_);
48 if (this->concurrency_ == 1)
50 JAWS_Thread_Per_Singleton::instance ()->make (this->flags_,
51 this->maxthreads_);
52 this->policy_.concurrency (JAWS_Thread_Per_Singleton::instance ());
54 else
56 JAWS_Thread_Pool_Singleton::instance ()->make (this->flags_,
57 this->nthreads_,
58 this->maxthreads_);
59 this->policy_.concurrency (JAWS_Thread_Pool_Singleton::instance ());
62 #if !(defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS))
63 this->dispatch_ = 0;
64 #endif /* !(ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS) */
66 if (this->dispatch_ == 1)
68 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
69 this->policy_.io (JAWS_Asynch_IO_Singleton::instance ());
70 this->policy_.ioh_factory
71 (JAWS_Asynch_IO_Handler_Factory_Singleton::instance ());
72 this->policy_.acceptor (JAWS_IO_Asynch_Acceptor_Singleton::instance ());
73 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
75 else
77 this->policy_.io (JAWS_Synch_IO_Singleton::instance ());
78 this->policy_.ioh_factory
79 (JAWS_Synch_IO_Handler_Factory_Singleton::instance ());
80 this->policy_.acceptor (JAWS_IO_Synch_Acceptor_Singleton::instance ());
83 //FUZZ: disable check_for_lack_ACE_OS
84 ACE_INET_Addr inet_addr (this->port_);
85 //FUZZ: enable check_for_lack_ACE_OS
87 this->policy_.acceptor ()->open (inet_addr);
90 int
91 JAWS_Server::open (JAWS_Pipeline_Handler *protocol,
92 JAWS_Dispatch_Policy *policy)
94 if (policy == 0)
95 policy = &this->policy_;
97 JAWS_Data_Block *db = new JAWS_Data_Block;
98 if (db == 0)
100 ACE_DEBUG ((LM_DEBUG,
101 "(%t) JAWS_Server::open, could not create Data_Block\n"));
102 return -1;
105 // initialize data block
107 db->task (JAWS_Pipeline_Accept_Task_Singleton::instance ());
108 db->policy (policy);
109 db->io_handler (0);
111 db->task ()->next (protocol);
113 // prime the acceptor if appropriate
114 if (this->dispatch_ == 1)
116 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
118 int n = this->nthreads_;
119 if (this->concurrency_ == 1)
120 n = 1;
122 for (int i = 0; i < n * this->ratio_ - n; i++)
123 db->task ()->put (db);
125 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
128 // The message block should contain an INET_Addr, and call the
129 // io->accept (INET_Addr) method!
131 policy->concurrency ()->put (db);
133 ACE_Thread_Manager::instance ()->wait ();
135 db->release ();
137 return 0;
140 void
141 JAWS_Server::parse_args (int argc, ACE_TCHAR *argv[])
143 int c;
144 int t = 0;
146 //FUZZ: disable check_for_lack_ACE_OS
147 ACE_Get_Opt getopt (argc, argv, ACE_TEXT("t" "p:c:d:n:m:f:r:"));
148 while ((c = getopt ()) != -1)
149 //FUZZ: enable check_for_lack_ACE_OS
150 switch (c)
152 case 't':
153 t = !t;
154 break;
155 case 'p':
156 this->port_ = ACE_OS::atoi (getopt.opt_arg ());
157 break;
158 case 'c':
159 if (ACE_OS::strcmp (getopt.opt_arg (), "PER_REQUEST") == 0)
160 this->concurrency_ = 1;
161 else this->concurrency_ = 0;
162 break;
163 case 'd':
164 if (ACE_OS::strcmp (getopt.opt_arg (), "ASYNCH") == 0)
165 this->dispatch_ = 1;
166 else this->dispatch_ = 0;
167 break;
168 case 'n':
169 this->nthreads_ = ACE_OS::atoi (getopt.opt_arg ());
170 break;
171 case 'm':
172 this->maxthreads_ = ACE_OS::atoi (getopt.opt_arg ());
173 break;
174 case 'f':
175 if (ACE_OS::strcmp (getopt.opt_arg (), "THR_BOUND") == 0)
176 this->flags_ |= THR_BOUND;
177 else if (ACE_OS::strcmp (getopt.opt_arg (), "THR_DAEMON") == 0)
178 this->flags_ |= THR_DAEMON;
179 else if (ACE_OS::strcmp (getopt.opt_arg (), "THR_DETACHED") == 0)
180 this->flags_ |= THR_DETACHED;
181 break;
182 case 'r':
183 this->ratio_ = ACE_OS::atoi (getopt.opt_arg ());
184 break;
187 #if (ACE_NTRACE != 1)
188 if (t)
189 ACE_Trace::start_tracing ();
190 else
191 ACE_Trace::stop_tracing ();
192 #endif /* ACE_NTRACE != 1*/
194 if (this->port_ == 0) this->port_ = 5432;
195 if (this->nthreads_ == 0) this->nthreads_ = 5;
196 if (this->maxthreads_ == 0) this->maxthreads_ = 20;