/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Private Member Functions | Private Attributes
StatsServerAlg Class Reference

#include <StatsServerAlg.h>

Collaboration diagram for StatsServerAlg:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 StatsServerAlg (const std::string &name, ISvcLocator *pSvcLocator)
virtual ~StatsServerAlg ()
virtual StatusCode initialize ()
virtual StatusCode execute ()
virtual StatusCode finalize ()

Private Member Functions

StatusCode startServer ()
StatusCode stopServer ()
StatusCode processRequests ()

Private Attributes

int m_processInterval
int m_portNumber
IStatisticsSvcm_statsSvc
TServerSocket * m_server
TMonitor * m_monitor
TList * m_clients

Detailed Description

Definition at line 23 of file StatsServerAlg.h.


Constructor & Destructor Documentation

StatsServerAlg::StatsServerAlg ( const std::string &  name,
ISvcLocator *  pSvcLocator 
)

Definition at line 13 of file StatsServerAlg.cc.

  : GaudiAlgorithm(name,pSvcLocator),
    m_statsSvc(0),
    m_server(0),
    m_monitor(0),
    m_clients(0)
{
  declareProperty("ProcessInterval",
                  m_processInterval=1,
                  "Interval for processing requests to server");
  declareProperty("PortNumber",
                  m_portNumber=9090,
                  "Port Number for server");
}
StatsServerAlg::~StatsServerAlg ( ) [virtual]

Definition at line 29 of file StatsServerAlg.cc.

{
}

Member Function Documentation

StatusCode StatsServerAlg::initialize ( ) [virtual]

Definition at line 33 of file StatsServerAlg.cc.

{
  // Initialize the necessary services
  StatusCode sc = this->service("StatisticsSvc",m_statsSvc,true);
  if(sc.isFailure()){
    error() << "Failed to get StatisticsSvc" << endreq;
    return sc;
  }

  // Start the server
  sc = startServer();
  return sc;
}
StatusCode StatsServerAlg::execute ( ) [virtual]

Definition at line 47 of file StatsServerAlg.cc.

{
  static int currentCycle = 0;
  currentCycle += 1;
  StatusCode sc = StatusCode::SUCCESS;
  // Process server requests every 'n' event cycles
  if( currentCycle % m_processInterval == 0 ){
    info() << "Processing requests to server" << endreq;
    sc = processRequests();
  }
  return StatusCode::SUCCESS;
}
StatusCode StatsServerAlg::finalize ( ) [virtual]

Definition at line 60 of file StatsServerAlg.cc.

{
  if( m_statsSvc ) m_statsSvc->release();
  StatusCode sc = stopServer();
  return sc;
}
StatusCode StatsServerAlg::startServer ( ) [private]

Definition at line 69 of file StatsServerAlg.cc.

                                      {
  // Start the Server

  m_server = new TServerSocket(m_portNumber, kTRUE);
  if (!m_server->IsValid()){
    error() << "Failed to start server on port " << m_portNumber << endreq;
    return StatusCode::FAILURE;
  }
  m_monitor = new TMonitor;
  m_monitor->Add( m_server );
  m_clients = new TList;
  
  return StatusCode::SUCCESS;
}
StatusCode StatsServerAlg::stopServer ( ) [private]

Definition at line 84 of file StatsServerAlg.cc.

                                     {
  // Stop the Server
  m_clients->Delete();
  delete m_clients;
  delete m_server;
  delete m_monitor;
  return StatusCode::SUCCESS;
}
StatusCode StatsServerAlg::processRequests ( ) [private]

Definition at line 93 of file StatsServerAlg.cc.

                                          {
  // Process any requests from clients
  int timeout = 20; // timeout if no requests after 20ms 
  TSocket* s = m_monitor->Select(timeout);
  if( !s ){
    error() << "Failure in client socket monitor." << endreq;
    return StatusCode::FAILURE;
  }
  if( s == (TSocket*)-1 ){
    // No pending requests before timeout
    return StatusCode::SUCCESS;
  }

  // Found a request
  if( s->IsA() == TServerSocket::Class() ){
    // New connection
    TSocket* sock = ((TServerSocket*)s)->Accept();
    m_monitor->Add(sock);
    m_clients->Add(sock);
    info() << "Accepted connection from client: " 
           << sock->GetInetAddress().GetHostName() << endreq;
    return StatusCode::SUCCESS;
  }

  // Request for statistics service
  char request[1024];
  if (s->Recv(request, sizeof(request)) <= 0) {
    // Closing connection
    m_monitor->Remove(s);
    m_clients->Remove(s);
    info() << "Closed connection from client: " 
           << s->GetInetAddress().GetHostName() << endreq;
    delete s;
    return StatusCode::SUCCESS;
  }

  info() << "Received request: " << request << endreq;

  // Respond to actual request (white-space separated list of paths)
  // Return a TMap of path/result pairs
  
  TMap itemMap;
  TList cleanupList;
  cleanupList.SetOwner();
  TObjString* nullObject = new TObjString("NULL");
  cleanupList.Add( nullObject );

  TString reqStr( request );
  TObjArray* paths = reqStr.Tokenize( " " );
  info() << "Number of requested paths: " << paths->GetEntriesFast() << endreq;
  for(int pathIdx=0; pathIdx<paths->GetEntriesFast(); pathIdx++){

    TObjString* path = new TObjString( *(dynamic_cast<TObjString*>(paths->At(pathIdx))));
    cleanupList.Add( path );
    std::string pathStr( path->GetName() );
    info() << "Retrieving path: \"" << pathStr << "\"" << endreq;

    // Check if path exists
    if(! m_statsSvc->exists( pathStr ) ){
      warning() << "Request for invalid path: " << pathStr << endreq;
      itemMap.Add( path, nullObject );
      continue;
    }

    // Check if path has an object
    TObject* object = m_statsSvc->get( pathStr );
    if( object ){
      info() << "Found object at path: " << pathStr << endreq;
      itemMap.Add( path, object );
      continue;
    }

    // Path is not an object, must be a directory
    std::vector<std::string> subFolds = m_statsSvc->getSubFolders( pathStr );
    TList* subFolders = new TList();
    subFolders->SetName("subFolders");
    subFolders->SetOwner();
    for(size_t idx=0; idx<subFolds.size(); idx++){
      subFolders->Add( new TObjString( subFolds[idx].c_str() ) );
    }

    std::vector<std::string> conts = m_statsSvc->getContents( pathStr );
    TList* contents = new TList();
    contents->SetName("contents");
    contents->SetOwner();
    for(size_t idx=0; idx<conts.size(); idx++){
      contents->Add( new TObjString( conts[idx].c_str() ) );
    }

    TMap* directory = new TMap();
    directory->SetName("directory");
    directory->SetOwner();
    directory->Add(new TObjString("subFolders"),subFolders);
    directory->Add(new TObjString("contents"),contents);
    itemMap.Add( path, directory );
    cleanupList.Add( directory );
  }
  delete paths;

  // Send requested items to client
  TMessage answer(kMESS_OBJECT);
  answer.WriteObject( &itemMap );
  s->Send(answer);

  cleanupList.Delete();

  return StatusCode::SUCCESS;
}

Member Data Documentation

Definition at line 43 of file StatsServerAlg.h.

Definition at line 46 of file StatsServerAlg.h.

Definition at line 49 of file StatsServerAlg.h.

TServerSocket* StatsServerAlg::m_server [private]

Definition at line 52 of file StatsServerAlg.h.

TMonitor* StatsServerAlg::m_monitor [private]

Definition at line 55 of file StatsServerAlg.h.

TList* StatsServerAlg::m_clients [private]

Definition at line 58 of file StatsServerAlg.h.


The documentation for this class was generated from the following files:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:54:00 for StatisticsSvc by doxygen 1.7.4