5 #include <TServerSocket.h>
29 std::string OnlMonServer::m_mon_host =
"localhost";
30 int OnlMonServer::m_mon_port = 9081;
31 int OnlMonServer::m_mon_port_0 = 9081;
32 int OnlMonServer::m_mon_n_port = 5;
50 pthread_mutexattr_t mutex_attr;
51 pthread_mutexattr_init(&mutex_attr);
52 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
54 int ret = pthread_mutex_init(&
mutex, &mutex_attr);
56 cout <<
"WARNING: pthread_mutex_init() returned " << ret <<
"." << endl;
68 cout <<
"OnlMonServer::StartServer(): Start." << endl;
73 #if defined(SERVER) || defined(ROOTTHREAD)
75 pthread_t ThreadId = 0;
78 if (
Verbosity() > 2) cout <<
" Creating server thread." << endl;
92 cout <<
" Started." << endl;
100 for (
int ii = 0; ii < 30; ii++) {
101 if (m_svr_ready)
break;
129 TSocket sock(m_mon_host.c_str(), port);
130 if (sock.IsValid()) {
131 cout <<
" Close the existing onlmon server at " << port <<
"." << endl;
132 sock.Send(
"Suicide");
146 if (se->
Verbosity() >= 0) cout <<
"OnlMonServer::FuncServer(): start." << endl;
149 TServerSocket* ss = 0;
151 for (port = m_mon_port_0; port < m_mon_port_0 + m_mon_n_port; port++) {
153 ss =
new TServerSocket(port, kTRUE);
154 if (ss->IsValid())
break;
159 cout <<
"Too many online-monitor servers are running. Start none." << endl;
163 cout <<
" Port = " << port << endl;
170 if (se->
Verbosity() >= 0) cout <<
"OnlMonServer::RemoveSockets():" << endl;
171 int isock = gROOT->GetListOfSockets()->IndexOf(ss);
172 gROOT->GetListOfSockets()->RemoveAt(isock);
177 if (se->
Verbosity() >= 0) cout <<
"OnlMonServer::WaitForConnection():" << endl;
178 TSocket *s0 = ss->Accept();
180 cout <<
"Server socket " << port <<
" in use, either go to a different node or change the port and recompile server and client. Abort." << endl;
186 TInetAddress adr = s0->GetInetAddress();
188 cout <<
"Connection from " << adr.GetHostName() <<
"/" << adr.GetHostAddress() <<
":" << adr.GetPort() << endl;
190 UInt_t ip0 = adr.GetAddress();
191 if ((ip0 >> 16) == (192 << 8) + 168 || ip0 == (127 << 24) + 1) {
194 cout <<
"OnlMonServer::FuncServer(): Ignore a connection from WAN.\n ";
201 cout <<
"OnlMonServer::FuncServer(): End." << endl;
216 TMessage *mess =
NULL;
218 if (
Verbosity() > 2) cout <<
"OnlMonServer::HandleConnection(): while loop." << endl;
221 if (
Verbosity() > 2) cout <<
" Broken Connection, closing socket." << endl;
225 if (
Verbosity() > 2) cout <<
" Already going to end, closing socket." << endl;
229 if (mess->What() == kMESS_STRING) {
231 mess->ReadString(msg_str_c, 64);
232 string msg_str = msg_str_c;
235 if (
Verbosity() > 2) cout <<
" Received message: " << msg_str << endl;
237 if (msg_str ==
"Finished") {
239 }
else if (msg_str ==
"Suicide") {
240 cout <<
"OnlMonServer::HandleConnection(): Suicide." << endl;
244 }
else if (msg_str ==
"Ping") {
245 if (
Verbosity() > 2) cout <<
" Ping." << endl;
247 }
else if (msg_str ==
"Spill") {
248 if (
Verbosity() > 2) cout <<
" Spill." << endl;
254 sock->Send(oss.str().c_str());
255 }
else if (msg_str.substr(0, 7) ==
"SUBSYS:") {
256 istringstream iss(msg_str.substr(7));
259 iss >> name_subsys >> sp_min >> sp_max;
260 cout <<
" Subsystem " << name_subsys << endl;
263 cout <<
" ... Not available." << endl;
264 sock->Send(
"NotReady");
267 cli->
SendHist(sock, sp_min, sp_max);
271 cout <<
" Unexpected string message (" << msg_str <<
"). Ignore it." << endl;
275 cerr <<
"OnlMonServer::HandleConnection(): Unexpected message ("
276 << mess->What() <<
"). Ignore it." << endl;
bool GetSpillSelectability()
OnlMonServer(const std::string &name="OnlMonServer")
void HandleConnection(TSocket *sock)
int SendHist(TSocket *sock, int sp_min, int sp_max)
void SetThreadId(pthread_t &id)
void SetServerReady(const bool val)
virtual int Verbosity() const
Gets the verbosity of this module.
bool CloseExistingServer(const int port)
Close an existing server process if such exists.
void FindFullSpillRange(int &id_min, int &id_max)
static OnlMonComm * instance()
SubsysReco * getSubsysReco(const std::string &name)
virtual void Verbosity(const int ival)
Sets the verbosity of this module (0 by default=quiet).
static OnlMonServer * instance()
static void * FuncServer(void *arg)
Base class for the OnlMon subsystem module.