Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / QtReactor_Test.cpp
blobcbc994d43005b0569ad95aab82223ebff8d71faf
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file QtReactor_Test.cpp
7 * Simple test of QtReactor. Test is intended to verify if QtReactor
8 * correctly cooperates with Qt event loop in typical application. Test
9 * creates a number of timers which send datagrams. These datagrams are
10 * expected by datagram_handlers registered in reactor. Test asynchronously
11 * establishes also a number of loopback tcp connections using ACE_Acceptors
12 * and ACE_Connectors. Socket activities are handled asynchronously to ensure
13 * that reactor does not lose events in traffic transmission. Moreover, test
14 * registers and removes handlers frequently to cover register/remove_handler
15 * method of QtReactor which are known (10/07/2004) to be buggy.
17 * Classes:
18 * QTestApplication - main qt application running event loop for a
19 * finite time
20 * Dgram_Handler - responsible for sending and receiving datagrams as
21 * well as handling timeouts. Datagrams are sent in
22 * handle_timeout method.
23 * TCPConnectionHandler - connection handler responsible for sending and
24 * receiving data using tcp streams.
25 * TCPAcceptorHandler - acceptor responsible for acceptance and
26 * registration of connections in HandlersRegister
27 * class.
28 * HandlersRegister - register of event_handlers, responsible also for
29 * the analysis of test results.
31 * @author Marek Brudka <mbrudka@elka.pw.edu.pl>
33 //=============================================================================
36 #include "test_config.h"
38 #include <assert.h>
39 #include <QtCore/qtimer.h>
41 #include "ace/OS_NS_time.h"
42 #include "ace/Time_Value.h"
43 #include "ace/QtReactor/QtReactor.h"
44 #include "ace/Event_Handler.h"
45 #include "ace/Argv_Type_Converter.h"
46 #include "ace/Acceptor.h"
47 #include "ace/Connector.h"
48 #include "ace/SOCK_Acceptor.h"
49 #include "ace/SOCK_Connector.h"
50 #include "ace/SOCK_Dgram.h"
52 // Qt specific code
53 #include "QtReactor_Test.h"
56 QTestApplication class implementation
59 QTestApplication::QTestApplication (int argc, char *argv[]):
60 QApplication (argc, argv, FALSE) /* do not enable GUI */
62 //FUZZ: disable check_for_lack_ACE_OS
63 connect (&finishTimer_, SIGNAL (timeout ()), this, SLOT (finishTest ()));
64 //FUZZ: enable check_for_lack_ACE_OS
67 void QTestApplication::finishTest ()
69 ACE_OS::exit ();
72 void QTestApplication::exec (int msec)
74 finishTimer_.stop ();
75 if (0 < msec )
77 finishTimer_.setSingleShot (TRUE);
78 finishTimer_.start (msec);
80 inherited::exec ();
84 // maximum time for testing QtReactor (msec)
85 const int TotalTestTime = 8000;
87 // how many handlers for each event handler class should be started ?
88 #ifndef __QNXNTO__
89 const int HandlersNo = 8 ;
90 #else /* __QNXNTO__ */
91 // it seems that Qt 3.1 for NTO 6.2 is compiled with rather small FD_SETSIZE
92 // Nevertheless, test works fine with native select reactor
93 const int HandlersNo = 4;
94 #endif
95 // base port for sending datagrams
96 const u_short BaseDgramPort = 5931;
98 // port for TCP connections
99 const u_short BaseTCPPort = 5931;
101 // how many datagrams should be sent for a single handler
102 const int DgramsToSend = 64;
104 // how many bytes to send vie TCP
105 const int TCPBytesToSend = 1024;
107 // how many times to reply tcp recv
108 const int TCPClientPings = 16;
110 // total number of bytes in TCP transmission
111 const int TCPTotalBytesToSend = TCPClientPings * TCPBytesToSend;
114 \class DgramHandler
115 \brief Simple event handler that receives and counts datagrams
116 as well as sends dgrams using timeouts.
118 class DgramHandler: public ACE_Event_Handler
120 public:
121 DgramHandler (ACE_Reactor *p_reactor = 0);
122 virtual ~DgramHandler ();
124 //FUZZ: disable check_for_lack_ACE_OS
125 int open (const ACE_INET_Addr &local,
126 int protocol_family=ACE_PROTOCOL_FAMILY_INET,
127 int protocol=0,
128 int reuse_addr=0);
129 //FUZZ: enable check_for_lack_ACE_OS
131 virtual ACE_HANDLE get_handle () const;
132 virtual int handle_input (ACE_HANDLE handle);
133 virtual int handle_close (ACE_HANDLE handle,ACE_Reactor_Mask close_mask);
134 virtual int handle_timeout (const ACE_Time_Value &current_time, const void *act=0);
135 int dgramsSent () const;
136 int dgramsReceived () const;
137 int timeoutsTriggered () const;
138 int expectedTriggers () const;
139 void expectedTriggers (int);
140 private:
141 int dgramsSent_; //!< the number of sent datagrams
142 int dgramsReceived_; //!< the number of received datagrams
143 int timeoutsTriggered_;
144 int expectedTriggers_;
145 ACE_SOCK_Dgram peer_; //!< datagram socket we listen to
149 \class TCPConnectionHandler
150 \brief TCP stream handler for both sides of connection.
152 class TCPConnectionHandler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
154 public:
155 typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> inherited;
156 public:
157 TCPConnectionHandler (bool p_serverSide = false);
158 virtual ~TCPConnectionHandler ();
160 virtual int handle_output (ACE_HANDLE handle);
161 virtual int handle_input (ACE_HANDLE handle);
162 virtual int handle_close (ACE_HANDLE handle,ACE_Reactor_Mask close_mask);
164 //FUZZ: disable check_for_lack_ACE_OS
165 ///FUZZ: enable check_for_lack_ACE_OS
166 virtual int open (void * = 0);
168 int scheduleSend (ACE_Message_Block *);
169 int sendBuffers ();
170 int totalReceived () const;
171 int totalSent () const;
172 private:
173 ACE_Message_Block *buffers_;
174 int totalReceived_; //!< total number of received bytes
175 int totalSent_; //!< total number of send bytes
176 bool serverSide_; //!< if true, echo received data
177 int pingsNo_; //!< number of pings client should make
180 typedef ACE_Connector< TCPConnectionHandler, ACE_SOCK_CONNECTOR > TCPConnectorHandler;
181 class TCPAcceptorHandler; // forward declaration
183 /* \class HandlersRegister
184 \brief The collection of test tools. Here ACE classes bind to Qt application
186 class HandlersRegister
188 public:
189 HandlersRegister (ACE_Reactor *p_reactor);
190 virtual ~HandlersRegister ();
191 int scheduleTimers (const ACE_Time_Value &p_TestTime); //!< schedule ace timers
192 int registerDgramHandlers (); //!< open dgrams socket and register in reactor
193 int registerTCPHandlers (); //!< creates TCP acceptors and connector
194 int analyze () const; //!< analyze results
195 int analyzeTimeouts () const; //!< analyze triggered timeouts
196 int analyzeDgrams () const; //!< analyze collected dgrams
197 int analyzeTCP () const; //!< analyze TCP transmission
198 int registerTCPServer (TCPConnectionHandler *);
199 int TCPServersNo () const; //!< return the number of accepted connections
201 private:
202 ACE_Reactor *reactor_; //!< reactor for this application
203 DgramHandler *DgramHandlers_[ HandlersNo] ;//!< dgram input handlers
204 int TCPServersNo_; //!< number of accepted connections
205 TCPConnectionHandler *TCPServers_[ HandlersNo ];
206 TCPConnectionHandler *TCPClients_[ HandlersNo ];
207 TCPAcceptorHandler *acceptor_;
208 TCPConnectorHandler *connectors_[ HandlersNo ];
211 class TCPAcceptorHandler : public ACE_Acceptor< TCPConnectionHandler, ACE_SOCK_ACCEPTOR >
213 public:
214 typedef ACE_Acceptor< TCPConnectionHandler, ACE_SOCK_ACCEPTOR > inherited;
215 public:
216 TCPAcceptorHandler (HandlersRegister *p_handlersRegister);
217 virtual ~TCPAcceptorHandler ();
218 virtual int make_svc_handler (TCPConnectionHandler *& sh);
219 virtual int activate_svc_handler (TCPConnectionHandler * sh);
220 private:
221 HandlersRegister *handlersRegister_;
225 DgramHandler class implementation
228 DgramHandler::DgramHandler (ACE_Reactor *p_reactor):
229 ACE_Event_Handler (p_reactor),
230 dgramsSent_ (0),
231 dgramsReceived_ (0),
232 timeoutsTriggered_ (0),
233 expectedTriggers_ (0)
235 reference_counting_policy ().value (Reference_Counting_Policy::ENABLED);
238 DgramHandler::~DgramHandler ()
240 ACE_TRACE ("DgramHandler::~DgramHandler");
243 int DgramHandler::open (const ACE_INET_Addr &local,
244 int protocol_family,
245 int protocol,
246 int reuse_addr)
248 if (0 > peer_.open (local,
249 protocol_family,
250 protocol,
251 reuse_addr))
252 ACE_ERROR_RETURN ((LM_ERROR,
253 ACE_TEXT (" (%P) %p\n"),
254 ACE_TEXT ("Cannot oper dgram socket")),
255 -1);
257 return 0;
260 ACE_HANDLE DgramHandler::get_handle () const
262 return peer_.get_handle ();
265 int DgramHandler::handle_input (ACE_HANDLE)
267 int recvBuffer;
268 ACE_INET_Addr peerAddress;
270 int result = peer_.recv (&recvBuffer, sizeof (recvBuffer) , peerAddress);
272 if (0 >= result)
273 ACE_ERROR_RETURN ((LM_ERROR,
274 ACE_TEXT (" (%P) %p\n"),
275 ACE_TEXT ("While reading datagram from socket"))
276 , -1);
277 else
278 ++dgramsReceived_;
280 return 0;
283 int DgramHandler::handle_timeout (const ACE_Time_Value &current_time, const void *act)
285 ACE_UNUSED_ARG (current_time);
286 ACE_UNUSED_ARG (act);
287 int sendBuffer = 0;
289 if (++timeoutsTriggered_ >= expectedTriggers_)
290 reactor ()->cancel_timer (this, 1);
292 ACE_SOCK_Dgram socket;
293 if (-1 == socket.open (ACE_INET_Addr (static_cast< u_short > (0),
294 static_cast< ACE_UINT32 > (INADDR_ANY)),
295 ACE_PROTOCOL_FAMILY_INET, 0, 1))
296 ACE_ERROR ((LM_ERROR,
297 ACE_TEXT (" (%P) %p\n"),
298 ACE_TEXT ("Cannot open socket for sending Qt dgrams")));
300 ACE_INET_Addr peerAddr;
301 peer_.get_local_addr (peerAddr);
303 if (sizeof (sendBuffer) != socket.send (&sendBuffer,
304 sizeof (sendBuffer), peerAddr))
305 ACE_ERROR ((LM_ERROR,
306 ACE_TEXT (" (%P) %p\n"),
307 ACE_TEXT ("Cannot send dgram")));
308 else
309 ++dgramsSent_;
311 socket.close ();
313 return 0;
316 int DgramHandler::handle_close (ACE_HANDLE handle, ACE_Reactor_Mask close_mask)
318 if (peer_.get_handle () != handle )
319 ACE_ERROR ((LM_ERROR,
320 ACE_TEXT ("Unknown handle %d DgramHandler::handle_close "
321 "with mask %x. My handle is %d\n"),
322 handle,
323 close_mask,
324 peer_.get_handle ()));
325 else
326 peer_.close ();
328 return 0;
331 int DgramHandler::dgramsSent () const
333 return dgramsSent_;
336 int DgramHandler::dgramsReceived () const
338 return dgramsReceived_;
341 int DgramHandler::timeoutsTriggered () const
343 return timeoutsTriggered_;
346 void DgramHandler::expectedTriggers (int triggers)
348 expectedTriggers_ = triggers;
351 int DgramHandler::expectedTriggers () const
353 return expectedTriggers_;
357 TCPConnectionHandler class implementation
360 TCPConnectionHandler::TCPConnectionHandler (bool p_serverSide):
361 buffers_ (0),
362 totalReceived_ (0),
363 totalSent_ (0),
364 serverSide_ (p_serverSide),
365 pingsNo_ (TCPClientPings)
367 reference_counting_policy ().value (Reference_Counting_Policy::ENABLED);
370 TCPConnectionHandler::~TCPConnectionHandler ()
372 ACE_TRACE ("TCPConnectionHandler::~TCPConnectionHandler");
375 int TCPConnectionHandler::handle_input (ACE_HANDLE handle)
377 ACE_UNUSED_ARG (handle);
378 ACE_Message_Block *buffer = new ACE_Message_Block (TCPBytesToSend);
379 int bytesReceived = peer_.recv (buffer->wr_ptr (), buffer->space ());
381 if (bytesReceived > 0)
383 totalReceived_ += bytesReceived;
384 if (serverSide_ || --pingsNo_ > 0) // echo received buffer
386 buffer->wr_ptr (bytesReceived);
387 int result = scheduleSend (buffer);
388 if (0 > result)
389 ACE_ERROR_RETURN ((LM_ERROR,
390 ACE_TEXT (" (%P) %p\n"),
391 ACE_TEXT ("Cannot schedule TCP reply")),
392 -1);
394 else
395 buffer->release ();
397 return 0;
400 if (errno != EWOULDBLOCK)
401 ACE_ERROR_RETURN ((LM_ERROR,
402 ACE_TEXT (" (%P:%p (%d)\n"),
403 ACE_TEXT ("TCPConnectionHandler::handle_input call with no data on handle "), handle),
404 -1);
406 ACE_ERROR ((LM_WARNING,
407 ACE_TEXT (" (%P:%p (%d)\n"),
408 ACE_TEXT ("TCPConnectionHandler::handle_input call with no data on handle "), handle));
410 return 0;
413 int TCPConnectionHandler::handle_output (ACE_HANDLE handle)
415 ACE_UNUSED_ARG (handle);
416 if (!buffers_)
417 ACE_ERROR ((LM_ERROR,
418 ACE_TEXT ("TCPConnectionHandler::handle_output call for empty buffers (%d)\n"), handle));
419 if (0 > sendBuffers ()) // socket broken, kill yourself
420 return -1;
422 if (!buffers_) // everything already send, unregister
424 reactor ()->cancel_wakeup (this,
425 ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::DONT_CALL);
426 reactor ()->remove_handler (this,
427 ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::DONT_CALL);
430 return 0;
433 int TCPConnectionHandler::open (void * )
435 int result = inherited::open ();
437 if (!serverSide_)
439 ACE_Message_Block *buffer = new ACE_Message_Block (TCPBytesToSend);
440 char *bufferData = buffer->wr_ptr ();
441 int i;
443 for (i = buffer->size () - 1; i >= 0; --i)
444 bufferData[ i ] = static_cast< char > (i);
445 buffer->wr_ptr (buffer->size ());
446 if (0 != (scheduleSend (buffer)))
447 ACE_ERROR_RETURN ((LM_ERROR,
448 ACE_TEXT ("Cannot schedule initial send\n")),
449 -1);
452 return result;
455 int TCPConnectionHandler::scheduleSend (ACE_Message_Block * buffer)
457 // link buffer to the end of buffers list
458 if (buffers_)
460 ACE_Message_Block *lastBuffer = buffers_;
461 while (lastBuffer->cont ())
462 lastBuffer = lastBuffer->cont () ;
463 lastBuffer->cont (buffer);
465 else
466 buffers_ = buffer;
468 if (0 > sendBuffers ())
469 ACE_ERROR_RETURN ((LM_ERROR,
470 ACE_TEXT (" (%P) %p\n"),
471 ACE_TEXT ("Cannot schedule TCP send.")),
472 -1);
473 return 0;
476 int TCPConnectionHandler::sendBuffers ()
478 int result = 0;
480 if (buffers_)
481 if (0 < (result = peer_.send_n (buffers_))) // remove sent blocks
483 totalSent_ += result;
484 while (buffers_ &&
485 static_cast< size_t > (result) >= buffers_->length ())
487 ACE_Message_Block *buffer = buffers_;
488 result -= buffers_->length ();
489 buffers_= buffers_->cont ();
490 buffer->cont (0);
491 buffer->release ();
494 if (buffers_) // some buffers were not sent, truncate data
495 buffers_->rd_ptr (result);
498 return result;
501 int TCPConnectionHandler::handle_close (ACE_HANDLE handle,ACE_Reactor_Mask close_mask)
503 if (peer_.get_handle () != handle )
504 ACE_ERROR ((LM_ERROR,
505 ACE_TEXT ("Unknown handle %d TCPConnectionHandler::handle_close "
506 "with mask %x. My handle is %d\n"),
507 handle,
508 close_mask,
509 peer_.get_handle ()));
510 else
511 peer_.close ();
513 return 0;
516 int TCPConnectionHandler::totalReceived () const
518 return totalReceived_;
521 int TCPConnectionHandler::totalSent () const
523 return totalSent_;
528 HandlersRegister class implementation
531 HandlersRegister::HandlersRegister (ACE_Reactor *p_reactor):
532 reactor_ (p_reactor),
533 TCPServersNo_ (0)
535 int i;
537 for (i = 0; i < HandlersNo; ++i)
539 // create dgram input handler
540 DgramHandlers_[ i ] = new DgramHandler (p_reactor);
542 TCPServers_[ i ] = 0;
544 TCPClients_[ i ] = new TCPConnectionHandler (false);
546 connectors_[ i ] =new TCPConnectorHandler (p_reactor, ACE_NONBLOCK);
547 connectors_[ i ]->reference_counting_policy ().value (
548 ACE_Event_Handler::Reference_Counting_Policy::ENABLED) ;
551 acceptor_ = new TCPAcceptorHandler (this);
552 acceptor_->reactor (p_reactor);
555 HandlersRegister::~HandlersRegister ()
557 int i;
558 if (acceptor_)
560 reactor_->remove_handler (acceptor_, ACE_Event_Handler::ALL_EVENTS_MASK);
561 acceptor_->close ();
562 acceptor_->remove_reference ();
565 for (i = 0; i < HandlersNo; ++i)
567 reactor_->cancel_timer (DgramHandlers_[ i ], 1);
568 reactor_->remove_handler (DgramHandlers_[ i ],
569 ACE_Event_Handler::ALL_EVENTS_MASK);
570 DgramHandlers_[ i ]->remove_reference ();
572 if (TCPServers_[ i ])
574 reactor_->remove_handler (TCPServers_[ i ],
575 ACE_Event_Handler::ALL_EVENTS_MASK);
576 TCPServers_[ i ]->remove_reference ();
579 reactor_->remove_handler (connectors_[ i ],
580 ACE_Event_Handler::ALL_EVENTS_MASK);
581 connectors_[ i ]->close ();
582 connectors_[ i ]->remove_reference ();
584 if (TCPClients_[ i ])
586 reactor_->remove_handler (TCPClients_[ i ],
587 ACE_Event_Handler::ALL_EVENTS_MASK);
588 TCPClients_[ i ]->remove_reference ();
593 int HandlersRegister::TCPServersNo () const
595 return TCPServersNo_;
598 int HandlersRegister::scheduleTimers (const ACE_Time_Value &p_TestTime)
600 int i;
602 for (i = 0; i < HandlersNo; ++i)
604 if (-1 == reactor_->schedule_timer (DgramHandlers_[ i ],
605 (const void *) 0,
606 ACE_Time_Value::zero,
607 p_TestTime * (0.5 / DgramsToSend)))
608 ACE_ERROR_RETURN ((LM_ERROR,
609 ACE_TEXT (" (%P) %p\n"),
610 ACE_TEXT ("Cannot schedule ACE timer")),
611 -1);
613 DgramHandlers_[ i ] ->expectedTriggers (DgramsToSend);
617 return 0;
620 int HandlersRegister::registerDgramHandlers ()
622 int i;
624 // open dgram handlers for all ports
625 for (i = 0; i < HandlersNo; ++i)
626 if (-1 == DgramHandlers_[ i ]->open (
627 ACE_INET_Addr (i + BaseDgramPort,
628 ACE_TEXT ("127.0.0.1"),
629 ACE_PROTOCOL_FAMILY_INET)))
630 ACE_ERROR_RETURN ((LM_ERROR,
631 ACE_TEXT (" (%P) %p\n"),
632 ACE_TEXT ("Cannot open dgram handler")),
633 -1);
635 // register dgram handlers
636 for (i = 0; i < HandlersNo; ++i)
637 if (-1 == reactor_->register_handler (DgramHandlers_[ i ],
638 ACE_Event_Handler::READ_MASK))
639 ACE_ERROR_RETURN ((LM_ERROR,
640 ACE_TEXT (" (%P) %p\n"),
641 ACE_TEXT ("Cannot register dgram handler")),
642 -1);
643 return 0;
646 int HandlersRegister::registerTCPHandlers ()
648 ACE_INET_Addr addr (BaseTCPPort);
650 if (-1 == acceptor_->open (addr, reactor_, 1))
651 ACE_ERROR_RETURN ((LM_ERROR,
652 ACE_TEXT (" (%P) %p\n"),
653 ACE_TEXT ("Cannot open acceptor port")),
654 -1);
656 int i;
657 addr.set (BaseTCPPort, ACE_TEXT ("127.0.0.1"));
659 for (i = 0; i < HandlersNo; ++i)
661 if (-1 == connectors_[ i ]->connect (
662 TCPClients_[ i ],
663 addr,
664 ACE_Synch_Options::asynch))
665 if (errno != EWOULDBLOCK )
666 ACE_ERROR_RETURN ((LM_ERROR,
667 ACE_TEXT (" (%P) %p (%d)\n"),
668 ACE_TEXT ("Cannot connect connector"),
670 -1);
673 return 0;
676 int HandlersRegister::registerTCPServer (TCPConnectionHandler *handler)
678 if (TCPServersNo_ < HandlersNo)
680 TCPServers_[ TCPServersNo_++ ] = handler;
681 return 0;
683 ACE_ERROR ((LM_ERROR,
684 ACE_TEXT ("Too many servers registered (%d). ACE_Reactor or ACE_Acceptor broken?\n"),
685 handler->get_handle ()));
686 return -1;
689 int HandlersRegister::analyze () const
691 int result = 0;
693 if (0 > analyzeTimeouts ())
694 result = -1;
696 if (0 > analyzeDgrams ())
697 result = -1;
699 if (0 > analyzeTCP ())
700 result = -1;
702 return result;
705 int HandlersRegister::analyzeTimeouts () const
707 int i;
708 int result = 0;
710 for (i = 0; i < HandlersNo; ++i)
711 if (DgramHandlers_[ i ]->expectedTriggers () !=
712 DgramHandlers_[ i ]->timeoutsTriggered ())
714 ACE_ERROR ((LM_ERROR,
715 ACE_TEXT ("Dgram_Handlers (%d) expected %d timeouts"
716 " but triggered only %d.\n"),
718 DgramHandlers_[ i ]->expectedTriggers (),
719 DgramHandlers_[ i ]->timeoutsTriggered ()));
720 result = -1;
723 return result;
726 int HandlersRegister::analyzeDgrams () const
728 int result = 0;
729 int i;
731 for (i = 0; i < HandlersNo; ++i)
732 if (DgramHandlers_[ i ]->dgramsSent () !=
733 DgramHandlers_[ i ]->dgramsReceived ())
735 ACE_ERROR ((LM_ERROR,
736 ACE_TEXT ("DgramsHandler (%d) sent %d dgrams but received only %d."
737 "Either reactor failed or system lost local dgrams..\n"),
738 DgramHandlers_[ i ]->dgramsSent (),
739 DgramHandlers_[ i ]->dgramsReceived ()));
740 result = -1 ;
743 return result;
746 int HandlersRegister::analyzeTCP () const
748 int i;
749 int result = 0;
751 for (i = 0; i < HandlersNo; ++i)
753 if (TCPClients_[ i ]->totalSent () != TCPTotalBytesToSend )
755 ACE_ERROR ((LM_ERROR,
756 ACE_TEXT ("TCPClient (%d): wanted to send %d but sent only (%d).\n"),
758 TCPTotalBytesToSend,
759 TCPClients_[ i ]->totalSent ()));
760 result = -1;
763 if (TCPClients_[ i ]->totalReceived () != TCPTotalBytesToSend )
765 ACE_ERROR ((LM_ERROR,
766 ACE_TEXT ("TCPClient (%d): expected %d bytes but received only (%d).\n"),
768 TCPTotalBytesToSend,
769 TCPClients_[ i ]->totalReceived ()));
770 result = -1;
773 if (TCPServers_[ i ])
775 if (TCPServers_[ i ]->totalSent () != TCPTotalBytesToSend )
777 ACE_ERROR ((LM_ERROR,
778 ACE_TEXT ("TCPServer (%d): wanted to send %d bytes "
779 "but sent only (%d).\n"),
781 TCPTotalBytesToSend,
782 TCPServers_[ i ]->totalSent ()));
783 result = -1;
786 if (TCPServers_[ i ]->totalReceived () != TCPTotalBytesToSend )
788 ACE_ERROR ((LM_ERROR,
789 ACE_TEXT ("TCPServer (%d): expected %d bytes but received only (%d).\n"),
791 TCPTotalBytesToSend,
792 TCPServers_[ i ]->totalReceived ()));
793 result = -1;
797 else
799 ACE_ERROR ((LM_ERROR,
800 ACE_TEXT ("TCPServer (%d): not connected.\n"),
801 i));
802 result = -1;
806 return result;
811 TCPAcceptorHandler class implementation
814 TCPAcceptorHandler::TCPAcceptorHandler (HandlersRegister *p_handlersRegister):
815 handlersRegister_ (p_handlersRegister)
817 reference_counting_policy ().value (Reference_Counting_Policy::ENABLED);
820 int TCPAcceptorHandler::make_svc_handler (TCPConnectionHandler *& sh)
822 sh = new TCPConnectionHandler (true);
823 sh->reactor (reactor ());
824 if (handlersRegister_->TCPServersNo () >= HandlersNo)
825 ACE_ERROR ((LM_ERROR,
826 ACE_TEXT ("TCPAcceptorHandler::make_svc_handler called to many times!\n")));
827 else
828 ACE_DEBUG ((LM_DEBUG,
829 ACE_TEXT ("TCPAcceptorHandler::make_svc_handler new TCP server created\n")));
831 return 0;
834 int TCPAcceptorHandler::activate_svc_handler (TCPConnectionHandler * sh)
836 if (0 == inherited::activate_svc_handler (sh) )
838 if (0 != handlersRegister_->registerTCPServer (sh)) // for analysis
840 // release event handler
841 reactor ()->remove_handler (sh, ACE_Event_Handler::ALL_EVENTS_MASK);
842 sh->remove_reference ();
843 // report error
844 ACE_ERROR_RETURN ((LM_ERROR,
845 ACE_TEXT ("Cannot register server TCPConnectionHandler\n")),
846 -1);
850 else
851 ACE_ERROR_RETURN ((LM_ERROR,
852 ACE_TEXT ("Failed to create server TCPConnectionHandler\n")),
853 -1);
855 return 0;
858 TCPAcceptorHandler::~TCPAcceptorHandler ()
860 ACE_TRACE ("TCPAcceptorHandler::~TCPAcceptorHandler");
863 void testNativeReactor (int, ACE_TCHAR *[])
865 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Testing autotest using native reactor\n")));
867 ACE_Reactor reactor;
868 HandlersRegister handlersRegister (&reactor);
869 ACE_Time_Value testTime (TotalTestTime / 1000, (TotalTestTime % 1000) * 1000);
871 if (0 <= handlersRegister.scheduleTimers (testTime) &&
872 0 <= handlersRegister.registerDgramHandlers () &&
873 0 <= handlersRegister.registerTCPHandlers ())
875 reactor.run_reactor_event_loop (testTime);
877 if (0 != handlersRegister.analyze ())
878 ACE_ERROR ((LM_ERROR,
879 ACE_TEXT ("Test failed for native reactor. "
880 "Fix QtReactor_Test or ACE_Reactor.\n")));
881 else
882 ACE_ERROR ((LM_INFO, ACE_TEXT ("Test seems to work with native reactor.\n")));
886 void testQtReactor (int argc, ACE_TCHAR *argv[])
888 // Qt specific code
889 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Testing QtReactor\n")));
891 ACE_Argv_Type_Converter ct (argc, argv);
892 QTestApplication app (argc, ct.get_ASCII_argv ());
894 ACE_QtReactor qtReactor (&app);
895 ACE_Reactor reactor (&qtReactor);
896 HandlersRegister handlersRegister (&reactor);
897 ACE_Time_Value testTime (TotalTestTime / 1000, (TotalTestTime % 1000) * 1000);
899 if (0 <= handlersRegister.scheduleTimers (testTime) &&
900 0 <= handlersRegister.registerDgramHandlers () &&
901 0 <= handlersRegister.registerTCPHandlers ())
904 app.exec (TotalTestTime);
906 if (0 != handlersRegister.analyze ())
907 ACE_ERROR ((LM_ERROR, ACE_TEXT ("QtReactor_Test failed.\n")));
908 else
909 ACE_ERROR ((LM_INFO, ACE_TEXT ("QtReactor_Test passed.\n")));
914 run_main (int argc, ACE_TCHAR *argv[])
916 ACE_START_TEST (ACE_TEXT ("QtReactor_Test"));
918 testNativeReactor (argc, argv);
919 testQtReactor (argc, argv);
921 ACE_END_TEST;
923 return 0;