Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / QtReactor_Test.cpp
blobdbb33561dee99f3aac0e40b7d96d424b6a6092d1
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);
616 return 0;
619 int HandlersRegister::registerDgramHandlers ()
621 int i;
623 // open dgram handlers for all ports
624 for (i = 0; i < HandlersNo; ++i)
625 if (-1 == DgramHandlers_[ i ]->open (
626 ACE_INET_Addr (i + BaseDgramPort,
627 ACE_TEXT ("127.0.0.1"),
628 ACE_PROTOCOL_FAMILY_INET)))
629 ACE_ERROR_RETURN ((LM_ERROR,
630 ACE_TEXT (" (%P) %p\n"),
631 ACE_TEXT ("Cannot open dgram handler")),
632 -1);
634 // register dgram handlers
635 for (i = 0; i < HandlersNo; ++i)
636 if (-1 == reactor_->register_handler (DgramHandlers_[ i ],
637 ACE_Event_Handler::READ_MASK))
638 ACE_ERROR_RETURN ((LM_ERROR,
639 ACE_TEXT (" (%P) %p\n"),
640 ACE_TEXT ("Cannot register dgram handler")),
641 -1);
642 return 0;
645 int HandlersRegister::registerTCPHandlers ()
647 ACE_INET_Addr addr (BaseTCPPort);
649 if (-1 == acceptor_->open (addr, reactor_, 1))
650 ACE_ERROR_RETURN ((LM_ERROR,
651 ACE_TEXT (" (%P) %p\n"),
652 ACE_TEXT ("Cannot open acceptor port")),
653 -1);
655 int i;
656 addr.set (BaseTCPPort, ACE_TEXT ("127.0.0.1"));
658 for (i = 0; i < HandlersNo; ++i)
660 if (-1 == connectors_[ i ]->connect (
661 TCPClients_[ i ],
662 addr,
663 ACE_Synch_Options::asynch))
664 if (errno != EWOULDBLOCK )
665 ACE_ERROR_RETURN ((LM_ERROR,
666 ACE_TEXT (" (%P) %p (%d)\n"),
667 ACE_TEXT ("Cannot connect connector"),
669 -1);
672 return 0;
675 int HandlersRegister::registerTCPServer (TCPConnectionHandler *handler)
677 if (TCPServersNo_ < HandlersNo)
679 TCPServers_[ TCPServersNo_++ ] = handler;
680 return 0;
682 ACE_ERROR ((LM_ERROR,
683 ACE_TEXT ("Too many servers registered (%d). ACE_Reactor or ACE_Acceptor broken?\n"),
684 handler->get_handle ()));
685 return -1;
688 int HandlersRegister::analyze () const
690 int result = 0;
692 if (0 > analyzeTimeouts ())
693 result = -1;
695 if (0 > analyzeDgrams ())
696 result = -1;
698 if (0 > analyzeTCP ())
699 result = -1;
701 return result;
704 int HandlersRegister::analyzeTimeouts () const
706 int i;
707 int result = 0;
709 for (i = 0; i < HandlersNo; ++i)
710 if (DgramHandlers_[ i ]->expectedTriggers () !=
711 DgramHandlers_[ i ]->timeoutsTriggered ())
713 ACE_ERROR ((LM_ERROR,
714 ACE_TEXT ("Dgram_Handlers (%d) expected %d timeouts"
715 " but triggered only %d.\n"),
717 DgramHandlers_[ i ]->expectedTriggers (),
718 DgramHandlers_[ i ]->timeoutsTriggered ()));
719 result = -1;
722 return result;
725 int HandlersRegister::analyzeDgrams () const
727 int result = 0;
728 int i;
730 for (i = 0; i < HandlersNo; ++i)
731 if (DgramHandlers_[ i ]->dgramsSent () !=
732 DgramHandlers_[ i ]->dgramsReceived ())
734 ACE_ERROR ((LM_ERROR,
735 ACE_TEXT ("DgramsHandler (%d) sent %d dgrams but received only %d."
736 "Either reactor failed or system lost local dgrams..\n"),
737 DgramHandlers_[ i ]->dgramsSent (),
738 DgramHandlers_[ i ]->dgramsReceived ()));
739 result = -1 ;
742 return result;
745 int HandlersRegister::analyzeTCP () const
747 int i;
748 int result = 0;
750 for (i = 0; i < HandlersNo; ++i)
752 if (TCPClients_[ i ]->totalSent () != TCPTotalBytesToSend )
754 ACE_ERROR ((LM_ERROR,
755 ACE_TEXT ("TCPClient (%d): wanted to send %d but sent only (%d).\n"),
757 TCPTotalBytesToSend,
758 TCPClients_[ i ]->totalSent ()));
759 result = -1;
762 if (TCPClients_[ i ]->totalReceived () != TCPTotalBytesToSend )
764 ACE_ERROR ((LM_ERROR,
765 ACE_TEXT ("TCPClient (%d): expected %d bytes but received only (%d).\n"),
767 TCPTotalBytesToSend,
768 TCPClients_[ i ]->totalReceived ()));
769 result = -1;
772 if (TCPServers_[ i ])
774 if (TCPServers_[ i ]->totalSent () != TCPTotalBytesToSend )
776 ACE_ERROR ((LM_ERROR,
777 ACE_TEXT ("TCPServer (%d): wanted to send %d bytes "
778 "but sent only (%d).\n"),
780 TCPTotalBytesToSend,
781 TCPServers_[ i ]->totalSent ()));
782 result = -1;
785 if (TCPServers_[ i ]->totalReceived () != TCPTotalBytesToSend )
787 ACE_ERROR ((LM_ERROR,
788 ACE_TEXT ("TCPServer (%d): expected %d bytes but received only (%d).\n"),
790 TCPTotalBytesToSend,
791 TCPServers_[ i ]->totalReceived ()));
792 result = -1;
796 else
798 ACE_ERROR ((LM_ERROR,
799 ACE_TEXT ("TCPServer (%d): not connected.\n"),
800 i));
801 result = -1;
805 return result;
810 TCPAcceptorHandler class implementation
813 TCPAcceptorHandler::TCPAcceptorHandler (HandlersRegister *p_handlersRegister):
814 handlersRegister_ (p_handlersRegister)
816 reference_counting_policy ().value (Reference_Counting_Policy::ENABLED);
819 int TCPAcceptorHandler::make_svc_handler (TCPConnectionHandler *& sh)
821 sh = new TCPConnectionHandler (true);
822 sh->reactor (reactor ());
823 if (handlersRegister_->TCPServersNo () >= HandlersNo)
824 ACE_ERROR ((LM_ERROR,
825 ACE_TEXT ("TCPAcceptorHandler::make_svc_handler called to many times!\n")));
826 else
827 ACE_DEBUG ((LM_DEBUG,
828 ACE_TEXT ("TCPAcceptorHandler::make_svc_handler new TCP server created\n")));
830 return 0;
833 int TCPAcceptorHandler::activate_svc_handler (TCPConnectionHandler * sh)
835 if (0 == inherited::activate_svc_handler (sh) )
837 if (0 != handlersRegister_->registerTCPServer (sh)) // for analysis
839 // release event handler
840 reactor ()->remove_handler (sh, ACE_Event_Handler::ALL_EVENTS_MASK);
841 sh->remove_reference ();
842 // report error
843 ACE_ERROR_RETURN ((LM_ERROR,
844 ACE_TEXT ("Cannot register server TCPConnectionHandler\n")),
845 -1);
849 else
850 ACE_ERROR_RETURN ((LM_ERROR,
851 ACE_TEXT ("Failed to create server TCPConnectionHandler\n")),
852 -1);
854 return 0;
857 TCPAcceptorHandler::~TCPAcceptorHandler ()
859 ACE_TRACE ("TCPAcceptorHandler::~TCPAcceptorHandler");
862 void testNativeReactor (int, ACE_TCHAR *[])
864 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Testing autotest using native reactor\n")));
866 ACE_Reactor reactor;
867 HandlersRegister handlersRegister (&reactor);
868 ACE_Time_Value testTime (TotalTestTime / 1000, (TotalTestTime % 1000) * 1000);
870 if (0 <= handlersRegister.scheduleTimers (testTime) &&
871 0 <= handlersRegister.registerDgramHandlers () &&
872 0 <= handlersRegister.registerTCPHandlers ())
874 reactor.run_reactor_event_loop (testTime);
876 if (0 != handlersRegister.analyze ())
877 ACE_ERROR ((LM_ERROR,
878 ACE_TEXT ("Test failed for native reactor. "
879 "Fix QtReactor_Test or ACE_Reactor.\n")));
880 else
881 ACE_ERROR ((LM_INFO, ACE_TEXT ("Test seems to work with native reactor.\n")));
885 void testQtReactor (int argc, ACE_TCHAR *argv[])
887 // Qt specific code
888 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Testing QtReactor\n")));
890 ACE_Argv_Type_Converter ct (argc, argv);
891 QTestApplication app (argc, ct.get_ASCII_argv ());
893 ACE_QtReactor qtReactor (&app);
894 ACE_Reactor reactor (&qtReactor);
895 HandlersRegister handlersRegister (&reactor);
896 ACE_Time_Value testTime (TotalTestTime / 1000, (TotalTestTime % 1000) * 1000);
898 if (0 <= handlersRegister.scheduleTimers (testTime) &&
899 0 <= handlersRegister.registerDgramHandlers () &&
900 0 <= handlersRegister.registerTCPHandlers ())
902 app.exec (TotalTestTime);
904 if (0 != handlersRegister.analyze ())
905 ACE_ERROR ((LM_ERROR, ACE_TEXT ("QtReactor_Test failed.\n")));
906 else
907 ACE_ERROR ((LM_INFO, ACE_TEXT ("QtReactor_Test passed.\n")));
912 run_main (int argc, ACE_TCHAR *argv[])
914 ACE_START_TEST (ACE_TEXT ("QtReactor_Test"));
916 testNativeReactor (argc, argv);
917 testQtReactor (argc, argv);
919 ACE_END_TEST;
921 return 0;