Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Service_Configurator / IPC-tests / server / Handle_Thr_Stream.cpp
blob172fefacc57d1fd76cc1e88ce6273fa3483eb7ff
1 #if !defined (ACE_HANDLE_THR_STREAM_C)
2 #define ACE_HANDLE_THR_STREAM_C
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/INET_Addr.h"
9 #include "ace/TLI_Acceptor.h"
10 #include "Handle_Thr_Stream.h"
12 #if defined (ACE_HAS_THREADS)
14 #include "ace/OS_NS_time.h"
16 // Shorthand names.
17 #define SVH SVC_HANDLER
18 #define PR_AC_1 ACE_PEER_ACCEPTOR_1
19 #define PR_AC_2 ACE_PEER_ACCEPTOR_2
20 #define PR_ST_1 ACE_PEER_STREAM_1
21 #define PR_ST_2 ACE_PEER_STREAM_2
23 template <class SVH, PR_AC_1>
24 Handle_Thr_Acceptor<SVH, PR_AC_2>::~Handle_Thr_Acceptor ()
28 template <class SVH, PR_AC_1>
29 Handle_Thr_Acceptor<SVH, PR_AC_2>::Handle_Thr_Acceptor ()
30 : thr_flags_ (THR_DETACHED | THR_NEW_LWP)
34 template <class SVH, PR_AC_1> int
35 Handle_Thr_Acceptor<SVH, PR_AC_2>::info (ACE_TCHAR **strp,
36 size_t length) const
38 ACE_TCHAR buf[BUFSIZ];
39 ACE_INET_Addr sa;
41 if (this->acceptor ().get_local_addr (sa) == -1)
43 return -1;
46 ACE_OS::sprintf (buf, ACE_TEXT("%d/"), sa.get_port_number ());
47 ACE_OS::strcat (buf, ACE_TEXT("tcp # tests threaded remote stream\n"));
49 if (*strp == 0 && (*strp = ACE_OS::strdup (buf)) == 0)
51 return -1;
53 else
55 ACE_OS::strncpy (*strp, buf, length);
58 return ACE_Utils::truncate_cast<int> (ACE_OS::strlen (buf));
61 template <class SVH, PR_AC_1> int
62 Handle_Thr_Acceptor<SVH, PR_AC_2>::init (int argc, ACE_TCHAR *argv[])
64 ACE_INET_Addr local_addr (ACE_DEFAULT_THR_PORT);
65 int n_threads = ACE_DEFAULT_THREADS;
67 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("p:t:"), 0);
69 for (int c; (c = get_opt ()) != -1; )
70 switch (c)
72 case 'p':
73 local_addr.set (ACE_OS::atoi (get_opt.opt_arg ()));
74 break;
75 case 't':
76 n_threads = ACE_OS::atoi (get_opt.opt_arg ());
77 break;
78 default:
79 break;
82 // Initialize the threading strategy.
83 if (this->thr_strategy_.open (&this->thr_mgr_,
84 this->thr_flags_,
85 n_threads) == -1)
86 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("open")), -1);
88 // Initialize the Acceptor base class, passing in the desired
89 // concurrency strategy.
90 else if (this->open (local_addr,
91 ACE_Reactor::instance (),
94 &this->thr_strategy_) == -1)
95 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("open")), -1);
96 else
97 return 0;
100 template <class SVH, PR_AC_1> int
101 Handle_Thr_Acceptor<SVH, PR_AC_2>::fini ()
103 return ACE_Reactor::instance ()->remove_handler
104 (this, ACE_Event_Handler::ACCEPT_MASK);
107 template <PR_ST_1>
108 CLI_Stream<PR_ST_2>::CLI_Stream (ACE_Thread_Manager *thr_mgr)
109 : inherited (thr_mgr)
113 template <PR_ST_1> int
114 CLI_Stream<PR_ST_2>::close (u_long)
116 ACE_DEBUG ((LM_DEBUG,
117 ACE_TEXT ("(%t) client stream object closing down\n")));
118 this->peer ().close ();
120 // Must be allocated dynamically!
121 delete this;
122 return 0;
125 template <PR_ST_1> int
126 CLI_Stream<PR_ST_2>::open (void *)
128 ACE_INET_Addr sa;
130 ACE_DEBUG ((LM_DEBUG,
131 ACE_TEXT ("(%t) client handle = %d\n"),
132 this->peer ().get_handle ()));
134 if (this->peer ().get_remote_addr (sa) == -1)
135 return -1;
137 ACE_DEBUG ((LM_DEBUG,
138 ACE_TEXT ("(%t) accepted at port %d\n"),
139 sa.get_port_number ()));
140 return 0;
143 template <PR_ST_1> int
144 CLI_Stream<PR_ST_2>::svc ()
146 char buf[BUFSIZ];
147 char login_name[ACE_MAX_USERID];
148 int bytes;
150 ACE_OS::puts ("----------------------------------------");
152 while ((bytes = this->peer ().recv (buf, sizeof buf)) > 0)
153 ACE_OS::write (ACE_STDOUT, buf, bytes);
155 ACE_OS::puts ("----------------------------------------");
156 ACE_OS::fflush (stdout);
158 time_t t = ACE_OS::time (0L);
159 #if !defined(ACE_LACKS_CUSERID)
160 ACE_OS::cuserid (login_name);
161 #else
162 login_name[0] = '.';
163 login_name[1] = '\0';
164 #endif
165 ACE_OS::sprintf (buf,
166 "user %s %s",
167 login_name,
168 ACE_TEXT_ALWAYS_CHAR (ACE_OS::ctime ((const time_t *) &t)));
170 if (this->peer ().send_n (buf, ACE_OS::strlen (buf) + 1) == -1)
171 return -1;
173 ACE_DEBUG ((LM_DEBUG, "(%t) sent reply %s", buf));
174 return 0;
177 #undef SVH
178 #undef PR_AC_1
179 #undef PR_AC_2
180 #undef PR_ST_1
181 #undef PR_ST_2
183 //----------------------------------------
185 #if defined (ACE_HAS_TLI)
186 #include "ace/TLI_Stream.h"
187 #include "ace/TLI_Acceptor.h"
188 #define THR_STREAM ACE_TLI_STREAM
189 #define THR_ACCEPTOR ACE_TLI_ACCEPTOR
190 #else
191 #include "ace/SOCK_Stream.h"
192 #include "ace/SOCK_Acceptor.h"
193 #define THR_STREAM ACE_SOCK_STREAM
194 #define THR_ACCEPTOR ACE_SOCK_ACCEPTOR
195 #endif /* ACE_HAS_TLI */
196 #include "ace/INET_Addr.h"
198 typedef CLI_Stream <THR_STREAM> CLI_STREAM;
199 typedef Handle_Thr_Acceptor<CLI_STREAM, THR_ACCEPTOR> HANDLE_THR_ACCEPTOR;
201 // Service object.
202 HANDLE_THR_ACCEPTOR remote_thr_stream;
203 ACE_Service_Object_Type rts (&remote_thr_stream, ACE_TEXT("Remote_Thr_Stream"));
205 #endif /* ACE_HAS_THREADS */
206 #endif /* ACE_HANDLE_THR_STREAM_C */