Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / IOStream / server / iostream_server.cpp
blob1128b002ae558b38cc48571c34b1b0b259648c99
1 // This is a simple example of using the ACE_IOStream and
2 // ACE_Streambuf_T templates to create an object based on ACE_*_Stream
3 // classes, which mimic a C++ iostream.
5 #include "ace/Acceptor.h"
6 #include "ace/SOCK_Acceptor.h"
7 #include "ace/Service_Config.h"
8 #include "ace/Signal.h"
9 #include "ace/Sig_Adapter.h"
11 #include "iostream_server.h"
12 #include "ace/OS_NS_unistd.h"
14 #if !defined (ACE_LACKS_ACE_IOSTREAM)
16 int
17 Handler::open (void *)
19 if (this->reactor ()->register_handler (this,
20 ACE_Event_Handler::READ_MASK) == -1)
21 ACE_ERROR_RETURN ((LM_ERROR,
22 "registering connection handler with ACE_Reactor\n"),
23 -1);
24 return 0;
27 Handler::Handler ()
29 ACE_DEBUG ((LM_DEBUG, "(%P) starting handler %x\n", this));
32 Handler::~Handler ()
34 ACE_DEBUG ((LM_DEBUG, "(%P) shutting down handler %x\n", this));
35 ACE_Reactor::end_event_loop ();
38 int
39 Handler::handle_input (ACE_HANDLE)
41 int i;
42 float f;
44 // Check to see if the socket is closed down.
45 if (this->peer ().eof ())
46 ACE_ERROR_RETURN ((LM_ERROR, "(%P) connection closed\n"), -1);
48 ACE_IOStream_String s;
50 if (!(this->peer () >> i >> f >> s))
51 ACE_ERROR_RETURN ((LM_ERROR, "(%P) %p\n", "error getting data"), -1);
53 cerr << "(" << ACE_OS::getpid () << ") Client sent:\n\t";
54 cerr << "(" << i << ") (" << f << ") (" << s << ")" << endl ;
56 if (!(this->peer () << "Received: " << i << " " << f << " " << s << endl))
57 ACE_ERROR_RETURN ((LM_ERROR, "(%P) %p\n", "error sending data"), -1);
59 // In order to flush the output to the peer, we have to use the sync
60 // () function. Some iostreams implementations let us use a 'flush'
61 // function much like the 'endl' function.
63 // this->peer ().sync ();
64 return 0;
67 // Create a factory object that will accept new connection requests
68 // and create handler objects for us.
70 typedef ACE_Acceptor<Handler, ACE_SOCK_ACCEPTOR> IOStream_Acceptor;
71 #endif /* !ACE_LACKS_ACE_IOSTREAM */
73 int
74 ACE_TMAIN (int argc, ACE_TCHAR *argv [])
76 #if !defined (ACE_LACKS_ACE_IOSTREAM)
77 ACE_Service_Config daemon;
79 // Create an adapter to end the event loop.
80 ACE_Sig_Adapter sa ((ACE_Sig_Handler_Ex) ACE_Reactor::end_event_loop);
82 ACE_Sig_Set sig_set;
83 sig_set.sig_add (SIGINT);
84 sig_set.sig_add (SIGQUIT);
86 // Register ourselves to receive SIGINT and SIGQUIT so we can shut
87 // down gracefully via signals.
88 if (ACE_Reactor::instance ()->register_handler (sig_set,
89 &sa) == -1)
90 ACE_ERROR_RETURN ((LM_ERROR, "%p\n"), -1);
92 IOStream_Acceptor peer_acceptor;
94 ACE_INET_Addr addr (argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT);
96 if (peer_acceptor.open (addr) == -1)
97 ACE_ERROR_RETURN ((LM_ERROR,
98 "%p\n",
99 "open"),
100 -1);
102 ACE_DEBUG ((LM_DEBUG,
103 "(%P) starting up daemon\n"));
105 ACE_Reactor::run_event_loop ();
107 ACE_DEBUG ((LM_DEBUG,
108 "(%P) shutting down server daemon\n"));
110 #else
111 ACE_UNUSED_ARG (argc);
112 ACE_UNUSED_ARG (argv);
113 ACE_ERROR ((LM_ERROR, "ACE_IOSTREAM not supported on this platform\n"));
114 #endif /* !ACE_LACKS_ACE_IOSTREAM */
115 return 0;