diff options
Diffstat (limited to 'examples/IOStream')
-rw-r--r-- | examples/IOStream/README | 22 | ||||
-rw-r--r-- | examples/IOStream/client/iostream_client.cpp | 32 | ||||
-rw-r--r-- | examples/IOStream/server/iostream_server.cpp | 185 |
3 files changed, 145 insertions, 94 deletions
diff --git a/examples/IOStream/README b/examples/IOStream/README new file mode 100644 index 00000000000..ab215fc779d --- /dev/null +++ b/examples/IOStream/README @@ -0,0 +1,22 @@ +This example illustrates the use of the ACE_IOStream and +ACE_Streambuf_T templates to create an object based on ACE_*_Stream +classes. These classes provide C++ iostream functionality across a +socket. + +To run the tests simply build the executables in the client and server +directories and then do the following in separate windows: + +# Window 1 +% server/iostream_server +(10049) starting up daemon + +# Window 2 +% client/iostream_client +(10049) starting handler 456d0 +(10049) Client sent: + (1) (2.3) +(10051) Server sent: + (1) (2.3) +(10049) connection closed +(10049) shutting down handler 456d0 +(10049) shutting down server daemon diff --git a/examples/IOStream/client/iostream_client.cpp b/examples/IOStream/client/iostream_client.cpp index 51556b7e34d..9e7004397b6 100644 --- a/examples/IOStream/client/iostream_client.cpp +++ b/examples/IOStream/client/iostream_client.cpp @@ -1,7 +1,14 @@ +// $Id$ + #include "ace/SOCK_Connector.h" #include "ace/IOStream.h" -int main (int argc, char *argv[]) +// This client is a simple example of using the ACE_IOStream and +// ACE_Streambuf_T templates to create an object based on ACE_*_Stream +// classes, which mimic a C++ iostream. + +int +main (int argc, char *argv[]) { #if !defined (ACE_LACKS_ACE_IOSTREAM) const char *server_host = argc > 1 ? argv[1] : ACE_DEFAULT_SERVER_HOST; @@ -9,12 +16,17 @@ int main (int argc, char *argv[]) ACE_IOStream<ACE_SOCK_Stream> server; ACE_SOCK_Connector connector; - ACE_INET_Addr addr (server_port, server_host); + ACE_INET_Addr addr (server_port, + server_host); if (connector.connect (server, addr) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1); + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "open"), + -1); - server << "1 2.3 testing" << endl; // Buffer up some things to send to the server. + // Buffer up some things to send to the server. + server << "1 2.3 testing" << endl; int i; float f; @@ -32,14 +44,16 @@ int main (int argc, char *argv[]) #else server >> i >> f; - cerr << "Server said:\n\t"; - cerr << i << " "; - cerr << f << endl; + cerr << "(" << ACE_OS::getpid () << ") Server sent:\n\t"; + cerr << "(" << i << ") "; + cerr << "(" << f << ")" << endl; #endif /* ACE_HAS_STRING_CLASS */ if (server.close () == -1) - ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "close"), -1); - + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "close"), + -1); #else ACE_ERROR ((LM_ERROR, "ACE_IOSTREAM not supported on this platform\n")); #endif /* !ACE_LACKS_ACE_IOSTREAM */ diff --git a/examples/IOStream/server/iostream_server.cpp b/examples/IOStream/server/iostream_server.cpp index 0e1fb73bfca..2ea2549fb67 100644 --- a/examples/IOStream/server/iostream_server.cpp +++ b/examples/IOStream/server/iostream_server.cpp @@ -1,6 +1,8 @@ +// $Id$ + // This is a simple example of using the ACE_IOStream and // ACE_Streambuf_T templates to create an object based on ACE_*_Stream -// classes which mimic a C++ iostream. +// classes, which mimic a C++ iostream. #include "ace/Acceptor.h" #include "ace/SOCK_Acceptor.h" @@ -10,7 +12,7 @@ #if !defined (ACE_LACKS_ACE_IOSTREAM) // Declare a new type which will case an ACE_SOCK_Stream to behave -// like an iostream. The new type (ACE_SOCK_IOStream) can be used +// like an iostream. The new ACE_SOCK_IOStream type can be used // anywhere an ACE_SOCK_Stream is used. typedef ACE_IOStream<ACE_SOCK_Stream> ACE_SOCK_IOStream; @@ -22,95 +24,97 @@ typedef ACE_IOStream<ACE_SOCK_Stream> ACE_SOCK_IOStream; #define ACE_SOCK_IOSTREAM ACE_SOCK_IOStream, ACE_INET_Addr #endif /* ACE_HAS_TYPENAME_KEYWORD */ -// Create a service handler object based on our new -// iostream/SOCK_Stream hybrid. - -typedef ACE_Svc_Handler<ACE_SOCK_IOSTREAM, ACE_NULL_SYNCH> - Service_Handler; - -class Handler : public Service_Handler +class Handler : public ACE_Svc_Handler<ACE_SOCK_IOSTREAM, ACE_NULL_SYNCH> // = TITLE - // Extend the <Service_Handler> object to do our bidding. All of - // this is fairly standard until we get to the <handle_input> - // where we begin using the iostream characteristics of the - // peer. + // Extend the <ACE_Svc_Handler> template to do our bidding. + // + // = DESCRIPTION + // Create an <ACE_Svc_Handler> object based on our + // iostream/SOCK_Stream hybrid. All of this is fairly standard + // until we get to the <handle_input> where we begin using the + // iostream characteristics of the peer. { public: - Handler (void) {} - - virtual int open (void *) - { - if (this->reactor ()->register_handler - (this, ACE_Event_Handler::READ_MASK) == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "registering connection handler with ACE_Reactor\n"), - -1); - - return 0; - } - - virtual void destroy (void) - { - this->peer ().close (); - delete this ; - } - - virtual int close (u_long) - { - this->destroy (); - return 0 ; - } - - virtual int handle_input (ACE_HANDLE) - { - int i; - float f; + // = Initialization and termination methods. + Handler (void); + ~Handler (void); + + // = <Svc_Handler> hooks. + virtual int open (void *); + + // = <Event_Handler> hooks. + virtual int handle_input (ACE_HANDLE); +}; + +int +Handler::open (void *) +{ + if (this->reactor ()->register_handler (this, + ACE_Event_Handler::READ_MASK) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "registering connection handler with ACE_Reactor\n"), + -1); + return 0; +} + +Handler::Handler (void) +{ + ACE_DEBUG ((LM_DEBUG, "(%P) starting handler %x\n", this)); +} + +Handler::~Handler (void) +{ + ACE_DEBUG ((LM_DEBUG, "(%P) shutting down handler %x\n", this)); + ACE_Reactor::end_event_loop (); +} + +int +Handler::handle_input (ACE_HANDLE) +{ + int i; + float f; + + ACE_INET_Addr addr; + + // Check to see if the socket is closed down. + if (this->peer ().eof ()) + ACE_ERROR_RETURN ((LM_ERROR, "(%P) connection closed\n"), -1); #if defined (ACE_HAS_STRING_CLASS) - ACE_IOStream_String s; + ACE_IOStream_String s; + + if (!(this->peer () >> i >> f >> s)) + ACE_ERROR_RETURN ((LM_ERROR, "(%P) %p\n", "error getting data"), -1); - if (!(this->peer () >> i >> f >> s)) - { - cerr << "Error getting data" << endl ; - return - 1 ; - } + cerr << "(" << ACE_OS::getpid () << ") Client sent:\n\t"; + cerr << "(" << i << ") (" << f << ") (" << s << ")" << endl ; - cerr << "Received (" << i << ") (" << f << ") (" << s << ")" << endl ; + if (!(this->peer () << "Received: " << i << " " << f << " " << s << endl)) + ACE_ERROR_RETURN ((LM_ERROR, "(%P) %p\n", "error sending data"), -1); - if (!(this->peer () << "Received: " << i << " " << f << " " << s << endl)) - { - cerr << __LINE__ << "Error sending data" << endl ; - return - 1 ; - } #else - if (!(this->peer () >> i >> f)) - { - cerr << "Error getting data" << endl ; - return - 1 ; - } - - cerr << "Received (" << i << ") (" << f << ")" << endl; - - if (!(this->peer () << i << " " << f << endl)) - { - cerr << __LINE__ << "Error sending data" << endl ; - return - 1 ; - } + if (!(this->peer () >> i >> f)) + ACE_ERROR_RETURN ((LM_ERROR, "(%P) %p\n", "error getting data"), -1); + + cerr << "(" << ACE_OS::getpid () << ") Client sent:\n\t"; + cerr << "(" << i << ") (" << f << ")" << endl; + + if (!(this->peer () << i << " " << f << endl)) + ACE_ERROR_RETURN ((LM_ERROR, "(%P) %p\n", "error sending data"), -1); #endif /* ACE_HAS_STRING_CLASS */ - // In order to flush the output to the peer, we have to use the - // sync () function. Some iostreams implementations let us use a - // 'flush' function much like the 'endl' function. + // In order to flush the output to the peer, we have to use the sync + // () function. Some iostreams implementations let us use a 'flush' + // function much like the 'endl' function. - // this->peer ().sync (); - return 0; - } -}; + // this->peer ().sync (); + return 0; +} -// Create an object which will accept new connection requests and -// create handler objects for us. +// Create a factory object that will accept new connection requests +// and create handler objects for us. -typedef ACE_Acceptor<Handler, ACE_SOCK_ACCEPTOR> Logging_Acceptor; +typedef ACE_Acceptor<Handler, ACE_SOCK_ACCEPTOR> IOStream_Acceptor; #endif /* !ACE_LACKS_ACE_IOSTREAM */ int @@ -128,23 +132,34 @@ main (int argc, char *argv []) // Register ourselves to receive SIGINT and SIGQUIT so we can shut // down gracefully via signals. - if (ACE_Reactor::instance ()->register_handler (sig_set, &sa) == -1) + if (ACE_Reactor::instance ()->register_handler (sig_set, + &sa) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n"), -1); - Logging_Acceptor peer_acceptor ; + IOStream_Acceptor peer_acceptor; + + ACE_INET_Addr addr (argc > 1 ? atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT); - if (peer_acceptor.open (ACE_INET_Addr (argc > 1 ? atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT)) == -1) - ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), - 1); + if (peer_acceptor.open (addr) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "open"), + -1); else if (ACE_Reactor::instance ()->register_handler - (&peer_acceptor, ACE_Event_Handler::READ_MASK) == - 1) - ACE_ERROR_RETURN ((LM_ERROR, "registering service with ACE_Reactor\n"), - 1); + (&peer_acceptor, + ACE_Event_Handler::READ_MASK) == - 1) + ACE_ERROR_RETURN ((LM_ERROR, + "registering service with ACE_Reactor\n"), + -1); - ACE_DEBUG ((LM_DEBUG, " (%P|%t) starting up daemon\n")); + ACE_DEBUG ((LM_DEBUG, + "(%P) starting up daemon\n")); ACE_Reactor::run_event_loop (); - ACE_DEBUG ((LM_DEBUG, " (%P|%t) shutting down server daemon\n")); + ACE_DEBUG ((LM_DEBUG, + "(%P) shutting down server daemon\n")); #else ACE_ERROR ((LM_ERROR, "ACE_IOSTREAM not supported on this platform\n")); |