1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
** $Id: RT_Thread_Per_Connection_Logging_Server.cpp 80826 2008-03-04 14:51:23Z wotte $
**
** Copyright 2001 Addison Wesley. All Rights Reserved.
*/
#include "ace/Auto_Ptr.h"
#include "ace/FILE_IO.h"
#include "ace/Log_Msg.h"
#include "ace/Sched_Params.h"
#include "ace/Signal.h"
#include "ace/Thread_Manager.h"
#include "RT_Thread_Per_Connection_Logging_Server.h"
#include "Logging_Handler.h"
#include <errno.h>
namespace {
extern "C" void sigterm_handler (int /* signum */) { /* No-op. */ }
}
int
RT_Thread_Per_Connection_Logging_Server::open (u_short port)
{
ACE_Sched_Params fifo_sched_params
(ACE_SCHED_FIFO,
ACE_Sched_Params::priority_min (ACE_SCHED_FIFO),
ACE_SCOPE_PROCESS);
if (ACE_OS::sched_params (fifo_sched_params) != 0) {
if (errno == EPERM || errno == ENOTSUP)
ACE_DEBUG ((LM_DEBUG,
"Warning: user's not superuser, so "
"we're running in time-shared class\n"));
else
ACE_ERROR_RETURN ((LM_ERROR,
"%p\n", "ACE_OS::sched_params()"), -1);
}
// Initialize the parent classes.
return Thread_Per_Connection_Logging_Server::open (port);
}
int
RT_Thread_Per_Connection_Logging_Server::handle_data (ACE_SOCK_Stream *client)
{
int prio =
ACE_Sched_Params::next_priority
(ACE_SCHED_FIFO,
ACE_Sched_Params::priority_min (ACE_SCHED_FIFO),
ACE_SCOPE_THREAD);
ACE_OS::thr_setprio (prio);
return Thread_Per_Connection_Logging_Server::handle_data (client);
}
// For simplicity, the Thread_Per_Connection_Logging_Server methods
// are duplicated here.
ACE_THR_FUNC_RETURN Thread_Per_Connection_Logging_Server::run_svc (void *arg)
{
auto_ptr<Thread_Args> thread_args (static_cast<Thread_Args *> (arg));
thread_args->this_->handle_data (&thread_args->logging_peer_);
thread_args->logging_peer_.close ();
return 0; // Return value is ignored
}
int
Thread_Per_Connection_Logging_Server::handle_connections ()
{
auto_ptr<Thread_Args> thread_args (new Thread_Args (this));
if (acceptor ().accept (thread_args->logging_peer_) == -1)
return -1;
if (ACE_Thread_Manager::instance ()->spawn (
// Pointer to function entry point.
Thread_Per_Connection_Logging_Server::run_svc,
// <run_svc> parameter.
static_cast<void *> (thread_args.get ()),
THR_DETACHED | THR_SCOPE_SYSTEM) == -1)
return -1;
thread_args.release (); // Spawned thread now owns memory
return 0;
}
int
Thread_Per_Connection_Logging_Server::handle_data (ACE_SOCK_Stream *client)
{
ACE_FILE_IO log_file;
// Client's hostname is logfile name.
make_log_file (log_file, client);
// Place the connection into blocking mode since this
// thread isn't doing anything except handling this client.
client->disable (ACE_NONBLOCK);
Logging_Handler logging_handler (log_file, *client);
// Keep handling log records until client closes connection
// or this thread is asked to cancel itself.
ACE_Thread_Manager *mgr = ACE_Thread_Manager::instance ();
ACE_thread_t me = ACE_Thread::self ();
while (!mgr->testcancel (me) &&
logging_handler.log_record () != -1)
continue;
log_file.close ();
return 0;
}
int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
// Register to receive the <SIGTERM> signal.
ACE_Sig_Action sa ((ACE_SignalHandler) sigterm_handler,
SIGTERM);
RT_Thread_Per_Connection_Logging_Server server;
if (server.run (argc, argv) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "server.run()"), 1);
// Cooperative thread cancellation and barrier synchronization.
ACE_Thread_Manager::instance ()->cancel_all ();
return ACE_Thread_Manager::instance ()->wait ();
}
|