Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / C++NPv1 / Logging_Server.cpp
blob13fe4fe228ec678d2d5c25fbc8b6f83eb070906b
1 /*
2 ** Copyright 2001 Addison Wesley. All Rights Reserved.
3 */
5 #include "ace/FILE_Addr.h"
6 #include "ace/FILE_Connector.h"
7 #include "ace/FILE_IO.h"
8 #include "ace/INET_Addr.h"
9 #include "ace/SOCK_Stream.h"
10 #include "Logging_Server.h"
11 #include "ace/OS_NS_string.h"
12 #include "ace/OS_NS_stdlib.h"
13 #include "ace/os_include/os_netdb.h"
15 int Logging_Server::run (int argc, char *argv[])
17 if (this->open (argc > 1 ? ACE_OS::atoi (argv[1]) : 0) == -1)
18 return -1;
20 for (;;) {
21 if (wait_for_multiple_events () == -1)
22 return -1;
23 if (handle_connections () == -1)
24 return -1;
25 if (handle_data () == -1)
26 return -1;
29 ACE_NOTREACHED (return 0;)
33 int Logging_Server::open (u_short logger_port)
35 ACE_INET_Addr server_addr;
36 int result;
38 if (logger_port != 0)
39 result = server_addr.set (logger_port,
40 (ACE_UINT32) INADDR_ANY);
41 else
42 result = server_addr.set ("ace_logger",
43 (ACE_UINT32) INADDR_ANY);
44 if (result == -1) return -1;
46 // Start listening, enable reuse of listen address for quick restarts.
47 return acceptor_.open (server_addr, 1);
51 int Logging_Server::make_log_file (ACE_FILE_IO &logging_file,
52 ACE_SOCK_Stream *logging_peer)
54 char filename[MAXHOSTNAMELEN + sizeof (".log")];
56 if (logging_peer != 0) { // Use client's hostname as log file name.
57 ACE_INET_Addr logging_peer_addr;
58 logging_peer->get_remote_addr (logging_peer_addr);
59 logging_peer_addr.get_host_name (filename, MAXHOSTNAMELEN);
60 ACE_OS::strcat (filename, ".log");
62 else
63 ACE_OS::strcpy (filename, "logging_server.log");
65 ACE_FILE_Connector connector;
66 return connector.connect (logging_file,
67 ACE_FILE_Addr (filename),
68 0, // No timeout.
69 ACE_Addr::sap_any, // Ignored.
70 0, // Don't try to reuse the addr.
71 O_RDWR|O_CREAT|O_APPEND,
72 ACE_DEFAULT_FILE_PERMS);