diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 1998-11-03 04:05:09 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 1998-11-03 04:05:09 +0000 |
commit | 077bf8c725744cf8aa001af6d8fd53b148dcd9db (patch) | |
tree | 30fc07e7b887835f40f238f88ad8ad8266939ced /examples/Reactor | |
parent | 59a2f9476a407c8f1c347b4fac70aad83a9f9b9a (diff) | |
download | ATCD-077bf8c725744cf8aa001af6d8fd53b148dcd9db.tar.gz |
.
Diffstat (limited to 'examples/Reactor')
-rw-r--r-- | examples/Reactor/Dgram/CODgram.cpp | 118 | ||||
-rw-r--r-- | examples/Reactor/Dgram/Dgram.cpp | 123 |
2 files changed, 168 insertions, 73 deletions
diff --git a/examples/Reactor/Dgram/CODgram.cpp b/examples/Reactor/Dgram/CODgram.cpp index 761adc8d495..9b2c50b3cbc 100644 --- a/examples/Reactor/Dgram/CODgram.cpp +++ b/examples/Reactor/Dgram/CODgram.cpp @@ -1,8 +1,29 @@ // $Id$ -// Exercise the ACE_SOCK_CODgram wrapper along with the ACE_Reactor. -// This test simply ping-pongs datagrams back and forth between the -// parent and child process. +// Exercise the <ACE_SOCK_CODgram> wrapper along with the +// <ACE_Reactor>. This test simply ping-pongs datagrams back and +// forth between the peer1 and peer2 processes. This test can +// be run in two ways: +// +// 1. Stand-alone -- e.g., +// +// % ./CODgram +// +// which will spawn a child process and run peer1 and peer2 +// in different processes on the same machine. +// +// 2. Distributed -- e.g., +// +// # Peer1 +// % ./CODgram 10002 tango.cs.wustl.edu 10003 peer1 +// +// # Peer1 +// % ./CODgram 10003 tango.cs.wustl.edu 10002 peer2 +// +// which will run peer1 and peer2 in different processes +// on the same or different machines. Note that you MUST +// give the name "peer1" as the final argument to one and +// only one of the programs so that the test will work properly. #include "ace/Reactor.h" #include "ace/SOCK_CODgram.h" @@ -18,7 +39,7 @@ class Dgram_Endpoint : public ACE_Event_Handler { public: Dgram_Endpoint (const ACE_INET_Addr &remote_addr, - const ACE_INET_Addr &local_addr); + const ACE_INET_Addr &local_addr); // = Hook methods inherited from the <ACE_Event_Handler>. virtual ACE_HANDLE get_handle (void) const; @@ -70,16 +91,20 @@ Dgram_Endpoint::handle_input (ACE_HANDLE) { char buf[BUFSIZ]; - ACE_DEBUG ((LM_DEBUG, "(%P|%t) activity occurred on handle %d!\n", + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) activity occurred on handle %d!\n", this->endpoint_.get_handle ())); ssize_t n = this->endpoint_.recv (buf, sizeof buf); if (n == -1) - ACE_ERROR ((LM_ERROR, "%p\n", "handle_input")); + ACE_ERROR ((LM_ERROR, + "%p\n", + "handle_input")); else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) buf of size %d = %*s\n", n, n, buf)); - + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) buf of size %d = %*s\n", + n, n, buf)); return 0; } @@ -87,14 +112,16 @@ int Dgram_Endpoint::handle_timeout (const ACE_Time_Value &, const void *) { - ACE_DEBUG ((LM_DEBUG, "(%P|%t) timed out for endpoint\n")); + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) timed out for endpoint\n")); return 0; } static int run_test (u_short localport, const char *remotehost, - u_short remoteport) + u_short remoteport, + const char *peer) { ACE_INET_Addr remote_addr (remoteport, remotehost); @@ -104,31 +131,34 @@ run_test (u_short localport, // Read data from other side. if (ACE_Reactor::instance ()->register_handler - (&endpoint, ACE_Event_Handler::READ_MASK) == -1) + (&endpoint, + ACE_Event_Handler::READ_MASK) == -1) ACE_ERROR_RETURN ((LM_ERROR, "ACE_Reactor::register_handler"), -1); - char buf[BUFSIZ]; - ACE_OS::strcpy (buf, "Data to transmit"); + ACE_OS::strcpy (buf, + "Data to transmit"); size_t len = ACE_OS::strlen (buf); - if (localport == port1) + // "peer1" is the "initiator." + if (ACE_OS::strncmp (peer, "peer1", 5) == 0) { - ACE_DEBUG ((LM_DEBUG, "(%P|%t) sending data\n")); - + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) sending data\n")); for (size_t i = 0; i < 20; i++) { endpoint.send (buf, len); - ACE_DEBUG ((LM_DEBUG, "(%P|%t) .\n")); + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) .\n")); ACE_OS::sleep (1); } } for (int i = 0; i < 40; i++) { - // Wait up to 4 seconds for data. - ACE_Time_Value tv (4, 0); + // Wait up to 10 seconds for data. + ACE_Time_Value tv (10, 0); if (ACE_Reactor::instance ()->handle_events (tv) <= 0) ACE_ERROR_RETURN ((LM_DEBUG, @@ -150,9 +180,8 @@ run_test (u_short localport, ACE_ERROR_RETURN ((LM_ERROR, "ACE_Reactor::remove_handler"), -1); - - ACE_DEBUG ((LM_DEBUG, "(%P|%t) exiting\n")); - + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) exiting\n")); return 0; } @@ -165,40 +194,57 @@ main (int argc, char *argv[]) const char *remotehost = argc > 2 ? argv[2] : ACE_DEFAULT_SERVER_HOST; const u_short port2 = argc > 3 ? ACE_OS::atoi (argv[3]) : port1 + 1; - if (argc > 4) // Providing the fourth command line argument - { // indicate we don't want to spawn a new process. - // On Win32, we use this to exec the new program. - run_test (port1, remotehost, port2); - } + // Providing the fourth command line argument indicates we don't + // want to spawn a new process. On Win32, we use this to exec the + // new program. + if (argc > 4) + run_test (port1, + remotehost, + port2, + argv[4]); else { ACE_DEBUG ((LM_DEBUG, "(%P|%t) local port = %d, remote host = %s, remote port = %d\n", - port1, remotehost, port2)); + port1, + remotehost, + port2)); ACE_Process_Options options; - options.command_line ("%s %d %s %d %c", argv[0], port1, remotehost, port2, 'c'); - options.creation_flags (ACE_Process_Options::NO_EXEC); // This has no effect on - // NT and will spawn a - // process that exec - // the above run_test - // function. + options.command_line ("%s %d %s %d %c", + argv[0], + port1, + remotehost, + port2, + 'c'); + + // This has no effect on NT and will spawn a process that exec + // the above run_test function. + options.creation_flags (ACE_Process_Options::NO_EXEC); ACE_Process new_process; + switch (new_process.spawn (options)) { case -1: return -1; case 0: - run_test (port1, remotehost, port2); + run_test (port1, + remotehost, + port2, + "peer1"); break; default: - run_test (port2, remotehost, port1); + run_test (port2, + remotehost, + port1, + "peer2"); new_process.wait (); break; } } + return 0; } diff --git a/examples/Reactor/Dgram/Dgram.cpp b/examples/Reactor/Dgram/Dgram.cpp index f3058c2e5eb..2263b98880f 100644 --- a/examples/Reactor/Dgram/Dgram.cpp +++ b/examples/Reactor/Dgram/Dgram.cpp @@ -1,8 +1,28 @@ // $Id$ -// Exercise the ACE_SOCK_Dgram wrapper along with the ACE_Reactor. +// Exercise the <ACE_SOCK_Dgram> wrapper along with the <ACE_Reactor>. // This test simply ping-pongs datagrams back and forth between the -// parent and child process. +// peer1 and peer2 processes. This test can be run in two ways: +// +// 1. Stand-alone -- e.g., +// +// % ./Dgram +// +// which will spawn a child process and run peer1 and peer2 +// in different processes on the same machine. +// +// 2. Distributed -- e.g., +// +// # Peer1 +// % ./Dgram 10002 tango.cs.wustl.edu 10003 peer1 +// +// # Peer1 +// % ./Dgram 10003 tango.cs.wustl.edu 10002 peer2 +// +// which will run peer1 and peer2 in different processes +// on the same or different machines. Note that you MUST +// give the name "peer1" as the final argument to one and +// only one of the programs so that the test will work properly. #include "ace/Reactor.h" #include "ace/Process.h" @@ -36,7 +56,9 @@ private: }; int -Dgram_Endpoint::send (const char *buf, size_t len, const ACE_INET_Addr &addr) +Dgram_Endpoint::send (const char *buf, + size_t len, + const ACE_INET_Addr &addr) { return this->endpoint_.send (buf, len, addr); } @@ -59,9 +81,7 @@ Dgram_Endpoint::handle_close (ACE_HANDLE handle, ACE_UNUSED_ARG (handle); this->endpoint_.close (); - delete this; - return 0; } @@ -71,16 +91,24 @@ Dgram_Endpoint::handle_input (ACE_HANDLE) char buf[BUFSIZ]; ACE_INET_Addr from_addr; - ACE_DEBUG ((LM_DEBUG, "(%P|%t) activity occurred on handle %d!\n", + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) activity occurred on handle %d!\n", this->endpoint_.get_handle ())); - ssize_t n = this->endpoint_.recv (buf, sizeof buf, from_addr); + ssize_t n = this->endpoint_.recv (buf, + sizeof buf, + from_addr); if (n == -1) - ACE_ERROR ((LM_ERROR, "%p\n", "handle_input")); + ACE_ERROR ((LM_ERROR, + "%p\n", + "handle_input")); else - ACE_DEBUG ((LM_DEBUG, "(%P|%t) buf of size %d = %*s\n", n, n, buf)); - + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) buf of size %d = %*s\n", + n, + n, + buf)); return 0; } @@ -88,24 +116,31 @@ int Dgram_Endpoint::handle_timeout (const ACE_Time_Value &, const void *) { - ACE_DEBUG ((LM_DEBUG, "(%P|%t) timed out for endpoint\n")); + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) timed out for endpoint\n")); return 0; } static int run_test (u_short localport, const char *remotehost, - u_short remoteport) + u_short remoteport, + const char *peer) { ACE_INET_Addr remote_addr (remoteport, remotehost); ACE_INET_Addr local_addr (localport); - Dgram_Endpoint *endpoint = new Dgram_Endpoint (local_addr); + Dgram_Endpoint *endpoint; + + ACE_NEW_RETURN (endpoint, + Dgram_Endpoint (local_addr), + -1); // Read data from other side. if (ACE_Reactor::instance ()->register_handler - (endpoint, ACE_Event_Handler::READ_MASK) == -1) + (endpoint, + ACE_Event_Handler::READ_MASK) == -1) ACE_ERROR_RETURN ((LM_ERROR, "ACE_Reactor::register_handler"), -1); @@ -114,22 +149,24 @@ run_test (u_short localport, ACE_OS::strcpy (buf, "Data to transmit"); size_t len = ACE_OS::strlen (buf); - if (localport == port1) + if (ACE_OS::strncmp (peer, "peer1", 5) == 0) { - ACE_DEBUG ((LM_DEBUG, "(%P|%t) sending data\n")); + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) sending data\n")); for (size_t i = 0; i < 20; i++) { endpoint->send (buf, len, remote_addr); - ACE_DEBUG ((LM_DEBUG, "(%P|%t) .\n")); + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) .\n")); ACE_OS::sleep (1); } } for (int i = 0; i < 40; i++) { - // Wait up to 4 seconds for data. - ACE_Time_Value tv (4, 0); + // Wait up to 10 seconds for data. + ACE_Time_Value tv (10, 0); if (ACE_Reactor::instance ()->handle_events (tv) <= 0) ACE_ERROR_RETURN ((LM_DEBUG, @@ -147,13 +184,13 @@ run_test (u_short localport, } if (ACE_Reactor::instance ()->remove_handler - (endpoint, ACE_Event_Handler::READ_MASK) == -1) + (endpoint, + ACE_Event_Handler::READ_MASK) == -1) ACE_ERROR_RETURN ((LM_ERROR, "ACE_Reactor::remove_handler"), -1); - - ACE_DEBUG ((LM_DEBUG, "(%P|%t) exiting\n")); - + ACE_DEBUG ((LM_DEBUG, + "(%P|%t) exiting\n")); return 0; } @@ -166,24 +203,30 @@ main (int argc, char *argv[]) const char *remotehost = argc > 2 ? argv[2] : ACE_DEFAULT_SERVER_HOST; const u_short port2 = argc > 3 ? ACE_OS::atoi (argv[3]) : port1 + 1; - if (argc > 4) // Providing the fourth command line argument - { // indicate we don't want to spawn a new process. - // On Win32, we use this to exec the new program. - run_test (port1, remotehost, port2); - } + // Providing the fourth command line argument indicate we don't want + // to spawn a new process. On Win32, we use this to exec the new + // program. + if (argc > 4) + run_test (port1, remotehost, port2, argv[4]); else { ACE_DEBUG ((LM_DEBUG, "(%P|%t) local port = %d, remote host = %s, remote port = %d\n", - port1, remotehost, port2)); + port1, + remotehost, + port2)); ACE_Process_Options options; - options.command_line ("%s %d %s %d %c", argv[0], port1, remotehost, port2, 'c'); - options.creation_flags (ACE_Process_Options::NO_EXEC); // This has no effect on - // NT and will spawn a - // process that exec - // the above run_test - // function. + options.command_line ("%s %d %s %d %c", + argv[0], + port1, + remotehost, + port2, + 'c'); + + // This has no effect on NT and will spawn a process that exec + // the above run_test function. + options.creation_flags (ACE_Process_Options::NO_EXEC); ACE_Process new_process; switch (new_process.spawn (options)) @@ -192,11 +235,17 @@ main (int argc, char *argv[]) return -1; case 0: - run_test (port1, remotehost, port2); + run_test (port1, + remotehost, + port2, + "peer1"); break; default: - run_test (port2, remotehost, port1); + run_test (port2, + remotehost, + port1, + "peer2"); new_process.wait (); break; } |