version 1.1.2.7, 2001/08/01 21:07:38
|
version 1.15, 2002/05/29 23:26:34
|
|
|
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#include "Config.h" |
#include <Pegasus/Common/Config.h> |
|
#include <cstring> |
|
#include "Monitor.h" |
|
#include "MessageQueue.h" |
|
#include "Socket.h" |
|
#include <Pegasus/Common/Tracer.h> |
|
#include <Pegasus/Common/HTTPConnection.h> |
| |
#ifdef PEGASUS_OS_TYPE_WINDOWS | #ifdef PEGASUS_OS_TYPE_WINDOWS |
|
# if defined(FD_SETSIZE) && FD_SETSIZE != 1024 |
|
# error "FD_SETSIZE was not set to 1024 prior to the last inclusion \ |
|
of <winsock.h>. It may have been indirectly included (e.g., by including \ |
|
<windows.h>). Find the inclusion of that header which is visible to this \ |
|
compilation unit and #define FD_SETZIE to 1024 prior to that inclusion; \ |
|
otherwise, less than 64 clients (the default) will be able to connect to the \ |
|
CIMOM. PLEASE DO NOT SUPPRESS THIS WARNING; PLEASE FIX THE PROBLEM." |
|
|
|
# endif |
# define FD_SETSIZE 1024 | # define FD_SETSIZE 1024 |
# include <winsock.h> |
# include <windows.h> |
#else | #else |
# include <sys/types.h> | # include <sys/types.h> |
# include <sys/socket.h> | # include <sys/socket.h> |
|
|
# include <unistd.h> | # include <unistd.h> |
#endif | #endif |
| |
#include <cstring> |
|
#include "Monitor.h" |
|
#include "MessageQueue.h" |
|
|
|
PEGASUS_USING_STD; | PEGASUS_USING_STD; |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
// | // |
// Routines for starting and stoping socket interface. |
|
// |
|
//////////////////////////////////////////////////////////////////////////////// |
|
|
|
extern Uint32 _socketInterfaceRefCount; |
|
|
|
static void _OpenSocketInterface() |
|
{ |
|
#ifdef PEGASUS_OS_TYPE_WINDOWS |
|
if (_socketInterfaceRefCount == 0) |
|
{ |
|
WSADATA tmp; |
|
|
|
if (WSAStartup(0x202, &tmp) == SOCKET_ERROR) |
|
WSACleanup(); |
|
} |
|
|
|
_socketInterfaceRefCount++; |
|
#endif |
|
} |
|
|
|
static void _CloseSocketInterface() |
|
{ |
|
#ifdef PEGASUS_OS_TYPE_WINDOWS |
|
_socketInterfaceRefCount--; |
|
|
|
if (_socketInterfaceRefCount == 0) |
|
WSACleanup(); |
|
#endif |
|
} |
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
// |
|
// MonitorRep | // MonitorRep |
// | // |
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
|
|
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
| |
Monitor::Monitor() | Monitor::Monitor() |
|
: _module_handle(0), _controller(0), _async(false) |
{ | { |
_OpenSocketInterface(); |
Socket::initializeInterface(); |
|
|
_rep = new MonitorRep; | _rep = new MonitorRep; |
FD_ZERO(&_rep->rd_fd_set); | FD_ZERO(&_rep->rd_fd_set); |
FD_ZERO(&_rep->wr_fd_set); | FD_ZERO(&_rep->wr_fd_set); |
|
|
| |
Monitor::~Monitor() | Monitor::~Monitor() |
{ | { |
_CloseSocketInterface(); |
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, |
|
"deregistering with module controller"); |
|
|
|
if(_module_handle != NULL) |
|
{ |
|
_controller->deregister_module(PEGASUS_MODULENAME_MONITOR); |
|
_controller = 0; |
|
delete _module_handle; |
|
} |
|
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, "deleting rep"); |
|
|
|
delete _rep; |
|
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, "uninitializing interface"); |
|
Socket::uninitializeInterface(); |
|
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, |
|
"returning from monitor destructor"); |
} | } |
| |
|
|
|
|
|
//<<< Tue May 14 20:38:26 2002 mdd >>> |
|
// register with module controller |
|
// when it is time to enqueue the message, |
|
// use an async_thread_exec call to |
|
// isolate the entire if(events) { enqueue -> fd_clear } block |
|
// let the thread pool grow and shrink according to load. |
|
|
Boolean Monitor::run(Uint32 milliseconds) | Boolean Monitor::run(Uint32 milliseconds) |
{ | { |
|
// register the monitor as a module to gain access to the cimserver's thread pool |
|
// <<< Wed May 15 09:52:16 2002 mdd >>> |
|
while( _module_handle == NULL) |
|
{ |
|
try |
|
{ |
|
_controller = &(ModuleController::register_module(PEGASUS_QUEUENAME_CONTROLSERVICE, |
|
PEGASUS_MODULENAME_MONITOR, |
|
(void *)this, |
|
0, |
|
0, |
|
0, |
|
&_module_handle)); |
|
|
|
} |
|
catch(IncompatibleTypes &) |
|
{ |
|
ModuleController* controlService = |
|
new ModuleController(PEGASUS_QUEUENAME_CONTROLSERVICE); |
|
} |
|
catch( AlreadyExists & ) |
|
{ |
|
break; |
|
} |
|
} |
|
|
#ifdef PEGASUS_OS_TYPE_WINDOWS | #ifdef PEGASUS_OS_TYPE_WINDOWS |
| |
// Windows select() has a strange little bug. It returns immediately if | // Windows select() has a strange little bug. It returns immediately if |
|
|
// Check for events on the selected file descriptors. Only do this if | // Check for events on the selected file descriptors. Only do this if |
// there were no undispatched events from last time. | // there were no undispatched events from last time. |
| |
static int count = 0; |
int count = 0; |
|
|
| |
if (count == 0) |
|
{ |
|
memcpy(&_rep->active_rd_fd_set, &_rep->rd_fd_set, sizeof(fd_set)); | memcpy(&_rep->active_rd_fd_set, &_rep->rd_fd_set, sizeof(fd_set)); |
memcpy(&_rep->active_wr_fd_set, &_rep->wr_fd_set, sizeof(fd_set)); | memcpy(&_rep->active_wr_fd_set, &_rep->wr_fd_set, sizeof(fd_set)); |
memcpy(&_rep->active_ex_fd_set, &_rep->ex_fd_set, sizeof(fd_set)); | memcpy(&_rep->active_ex_fd_set, &_rep->ex_fd_set, sizeof(fd_set)); |
| |
const Uint32 SEC = milliseconds / 1000; |
const Uint32 SECONDS = milliseconds / 1000; |
const Uint32 USEC = (milliseconds % 1000) * 1000; |
const Uint32 MICROSECONDS = (milliseconds % 1000) * 1000; |
struct timeval tv = { SEC, USEC }; |
struct timeval tv = { SECONDS, MICROSECONDS }; |
| |
count = select( | count = select( |
FD_SETSIZE, | FD_SETSIZE, |
|
|
&_rep->active_wr_fd_set, | &_rep->active_wr_fd_set, |
&_rep->active_ex_fd_set, | &_rep->active_ex_fd_set, |
&tv); | &tv); |
|
|
if (count == 0) | if (count == 0) |
|
{ |
return false; | return false; |
|
} |
|
|
#ifdef PEGASUS_OS_TYPE_WINDOWS | #ifdef PEGASUS_OS_TYPE_WINDOWS |
else if (count == SOCKET_ERROR) | else if (count == SOCKET_ERROR) |
#else | #else |
else if (count == -1) | else if (count == -1) |
#endif | #endif |
{ | { |
count = 0; |
|
return false; | return false; |
} | } |
} |
|
| |
for (Uint32 i = 0, n = _entries.size(); i < n; i++) |
Boolean handled_events = false; |
|
for (Uint32 i = 0, n = _entries.size(); i < _entries.size(); i++) |
{ | { |
Sint32 socket = _entries[i].socket; | Sint32 socket = _entries[i].socket; |
Uint32 events = 0; | Uint32 events = 0; |
| |
|
if(_entries[i].dying.value() > 0 ) |
|
{ |
|
if(_entries[i]._type == Monitor::CONNECTION) |
|
{ |
|
|
|
MessageQueue *q = MessageQueue::lookup(_entries[i].queueId); |
|
if(q && static_cast<HTTPConnection *>(q)->is_dying() && |
|
(0 == static_cast<HTTPConnection *>(q)->refcount.value())) |
|
{ |
|
static_cast<HTTPConnection *>(q)->lock_connection(); |
|
static_cast<HTTPConnection *>(q)->unlock_connection(); |
|
|
|
MessageQueue & o = static_cast<HTTPConnection *>(q)->get_owner(); |
|
Message* message= new CloseConnectionMessage(static_cast<HTTPConnection *>(q)->getSocket()); |
|
message->dest = o.getQueueId(); |
|
o.enqueue(message); |
|
i--; |
|
n = _entries.size(); |
|
} |
|
} |
|
} |
|
|
if (FD_ISSET(socket, &_rep->active_rd_fd_set)) | if (FD_ISSET(socket, &_rep->active_rd_fd_set)) |
events |= SocketMessage::READ; | events |= SocketMessage::READ; |
| |
|
|
| |
if (events) | if (events) |
{ | { |
MessageQueue* queue = MessageQueue::lookup(_entries[i].queueId); |
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, |
|
"Monitor::run - Socket Event Detected events = %d", events); |
if (!queue) |
|
unsolicitSocketMessages(_entries[i].queueId); |
|
|
|
|
|
Message* message = new SocketMessage(socket, events); |
|
queue->enqueue(message); |
|
|
|
if (events & SocketMessage::WRITE) | if (events & SocketMessage::WRITE) |
{ | { |
FD_CLR(socket, &_rep->active_wr_fd_set); | FD_CLR(socket, &_rep->active_wr_fd_set); |
|
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, |
|
"Monitor::run FD_CLR WRITE"); |
} | } |
|
|
if (events & SocketMessage::EXCEPTION) | if (events & SocketMessage::EXCEPTION) |
{ | { |
FD_CLR(socket, &_rep->active_ex_fd_set); | FD_CLR(socket, &_rep->active_ex_fd_set); |
|
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, |
|
"Monitor::run FD_CLR EXECEPTION"); |
} | } |
|
|
if (events & SocketMessage::READ) | if (events & SocketMessage::READ) |
{ | { |
FD_CLR(socket, &_rep->active_rd_fd_set); | FD_CLR(socket, &_rep->active_rd_fd_set); |
|
Tracer::trace(TRC_HTTP, Tracer::LEVEL4, |
|
"Monitor::run FD_CLR READ"); |
|
} |
|
MessageQueue* queue = MessageQueue::lookup(_entries[i].queueId); |
|
if( ! queue ) |
|
{ |
|
unsolicitSocketMessages(socket); |
|
break; |
} | } |
| |
|
if(_entries[i]._type == Monitor::CONNECTION) |
|
{ |
|
if( static_cast<HTTPConnection *>(queue)->refcount.value() == 0 ) |
|
{ |
|
|
|
static_cast<HTTPConnection *>(queue)->refcount++; |
|
if( false == static_cast<HTTPConnection *>(queue)->is_dying()) |
|
_controller->async_thread_exec(*_module_handle, _dispatch, (void *)queue); |
|
else |
|
static_cast<HTTPConnection *>(queue)->refcount--; |
|
} |
|
} |
|
else |
|
{ |
|
Message* message = new SocketMessage(socket, events); |
|
queue->enqueue(message); |
|
} |
count--; | count--; |
return true; |
pegasus_yield(); |
} | } |
|
handled_events = true; |
} | } |
| |
return false; |
return(handled_events); |
} | } |
| |
Boolean Monitor::solicitSocketMessages( | Boolean Monitor::solicitSocketMessages( |
Sint32 socket, | Sint32 socket, |
Uint32 events, | Uint32 events, |
Uint32 queueId) |
Uint32 queueId, |
|
int type) |
{ | { |
// See whether a handler is already registered for this one: |
PEG_METHOD_ENTER(TRC_HTTP, "Monitor::solictSocketMessage"); |
| |
|
// See whether a handler is already registered for this one: |
Uint32 pos = _findEntry(socket); | Uint32 pos = _findEntry(socket); |
| |
if (pos != PEGASUS_NOT_FOUND) | if (pos != PEGASUS_NOT_FOUND) |
|
{ |
|
PEG_METHOD_EXIT(); |
return false; | return false; |
|
} |
| |
// Set the events: | // Set the events: |
| |
|
|
FD_SET(socket, &_rep->ex_fd_set); | FD_SET(socket, &_rep->ex_fd_set); |
| |
// Add the entry to the list: | // Add the entry to the list: |
|
_MonitorEntry entry(socket, queueId, type); |
_MonitorEntry entry = { socket, queueId }; |
|
_entries.append(entry); | _entries.append(entry); |
| |
// Success! | // Success! |
| |
|
PEG_METHOD_EXIT(); |
return true; | return true; |
} | } |
| |
Boolean Monitor::unsolicitSocketMessages(Sint32 socket) | Boolean Monitor::unsolicitSocketMessages(Sint32 socket) |
{ | { |
|
PEG_METHOD_ENTER(TRC_HTTP, "Monitor::unsolicitSocketMessage"); |
|
|
// Look for the given entry and remove it: | // Look for the given entry and remove it: |
| |
for (Uint32 i = 0, n = _entries.size(); i < n; i++) | for (Uint32 i = 0, n = _entries.size(); i < n; i++) |
|
|
FD_CLR(socket, &_rep->wr_fd_set); | FD_CLR(socket, &_rep->wr_fd_set); |
FD_CLR(socket, &_rep->ex_fd_set); | FD_CLR(socket, &_rep->ex_fd_set); |
_entries.remove(i); | _entries.remove(i); |
|
// ATTN-RK-P3-20020521: Need "Socket::close(socket);" here? |
|
PEG_METHOD_EXIT(); |
return true; | return true; |
} | } |
} | } |
|
PEG_METHOD_EXIT(); |
return false; | return false; |
} | } |
| |
Uint32 Monitor::_findEntry(Sint32 socket) const |
Uint32 Monitor::_findEntry(Sint32 socket) |
{ | { |
for (Uint32 i = 0, n = _entries.size(); i < n; i++) | for (Uint32 i = 0, n = _entries.size(); i < n; i++) |
{ | { |
|
|
return PEG_NOT_FOUND; | return PEG_NOT_FOUND; |
} | } |
| |
|
|
|
PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL Monitor::_dispatch(void *parm) |
|
{ |
|
HTTPConnection *dst = reinterpret_cast<HTTPConnection *>(parm); |
|
if( true == dst->is_dying()) |
|
{ |
|
dst->refcount--; |
|
return 0; |
|
} |
|
if( false == dst->is_dying()) |
|
{ |
|
if(false == dst->run(1)) |
|
pegasus_sleep(1); |
|
|
|
} |
|
dst->refcount--; |
|
return 0; |
|
} |
|
|
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |