Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OnlMonComm.cc
Go to the documentation of this file.
1 #include <string>
2 #include <sstream>
3 #include <pthread.h>
4 #include <TMessage.h>
5 #include <TServerSocket.h>
6 #include <TSocket.h>
7 #include <TROOT.h>
8 #include <TH1.h>
9 #include "OnlMonServer.h"
10 #include "OnlMonClient.h"
11 #include "OnlMonComm.h"
12 using namespace std;
13 
14 OnlMonComm* OnlMonComm::m_inst = 0;
15 
17 {
18  if (! m_inst) m_inst = new OnlMonComm();
19  return m_inst;
20 }
21 
23  : m_sp_mode(SP_ALL)
24  , m_sp_num(1)
25  , m_sp_lo(0)
26  , m_sp_hi(0)
27  , m_sp_min(0)
28  , m_sp_max(0)
29  , m_sp_sel(true)
30  , m_n_sp_sel_max(2000)
31 {
32  ;
33 }
34 
36 {
37  ;
38 }
39 
40 void OnlMonComm::SetSpillRangeLow(const int sp)
41 {
42  m_sp_lo = sp;
43 }
44 
46 {
47  m_sp_hi = sp;
48 }
49 
50 void OnlMonComm::SetSpillRange(const int sp_lo, const int sp_hi)
51 {
52  m_sp_lo = sp_lo;
53  m_sp_hi = sp_hi;
54 }
55 
56 void OnlMonComm::GetSpillRange(int& sp_lo, int& sp_hi)
57 {
58  sp_lo = m_sp_lo;
59  sp_hi = m_sp_hi;
60 }
61 
62 void OnlMonComm::AddSpill(const int id)
63 {
64  if (find(m_list_sp.begin(), m_list_sp.end(), id) == m_list_sp.end()) m_list_sp.push_back(id);
65 }
66 
67 void OnlMonComm::FindFullSpillRange(int& id_min, int& id_max)
68 {
69  if (m_list_sp.size() == 0) {
70  id_min = id_max = 0;
71  } else {
72  sort(m_list_sp.begin(), m_list_sp.end());
73  id_min = m_list_sp[0];
74  id_max = m_list_sp[m_list_sp.size()-1];
75  }
76 }
77 
79 {
80  TSocket* sock = ConnectServer();
81  if (! sock) return 1;
82  sock->Send("Spill");
83 
84  TMessage *mess = 0;
85  sock->Recv(mess);
86  int ret = 0;
87  if (mess && mess->What() == kMESS_STRING) {
88  char str[200];
89  mess->ReadString(str, 200);
90  istringstream iss(str);
91  if (! (iss >> m_sp_min >> m_sp_max >> m_sp_sel)) {
92  m_sp_min = m_sp_max = 0;
93  m_sp_sel = false;
94  }
95  delete mess;
96  } else {
97  ret = 1;
98  }
99 
100  sock->Close();
101  delete sock;
102  return ret;
103 }
104 
105 void OnlMonComm::GetFullSpillRange(int& id_min, int& id_max)
106 {
107  id_min = m_sp_min;
108  id_max = m_sp_max;
109 }
110 
112 {
113  string host = OnlMonServer::GetHost();
114  int port = OnlMonServer::GetPort();
115  int port0 = OnlMonServer::GetPort0();
116  int n_port = OnlMonServer::GetNumPorts();
117 
118  TSocket* sock = 0;
119  for (int ip = 0; ip < n_port; ip++) {
120  bool port_ok = false;
121  sock = new TSocket(host.c_str(), port);
122  if (sock->IsValid()) {
123  sock->Send("Ping");
124  if (sock->Select(TSocket::kRead, 2000) > 0) { // 2000 msec
125  TMessage* mess = 0;
126  sock->Recv(mess);
127  if (mess && mess->What() == kMESS_STRING) {
128  char str[200];
129  mess->ReadString(str, 200);
130  if (strcmp(str, "Pong") == 0) port_ok = true;
131  delete mess;
132  }
133  }
134  }
135  if (port_ok) {
136  cout << "OnlMonComm::ConnectServer(): " << host << ":" << port << endl;
137  break;
138  }
139  delete sock;
140  sock = 0;
141  port++;
142  if (port >= port0 + n_port) port -= n_port;
143  }
144  if (sock) OnlMonServer::SetPort(port);
145  return sock;
146 }
void SetSpillRangeHigh(const int sp)
Definition: OnlMonComm.cc:45
void AddSpill(const int id)
Definition: OnlMonComm.cc:62
void GetSpillRange(int &sp_lo, int &sp_hi)
Definition: OnlMonComm.cc:56
static int GetPort()
Definition: OnlMonServer.h:33
TSocket * ConnectServer()
Definition: OnlMonComm.cc:111
static std::string GetHost()
Definition: OnlMonServer.h:32
static int GetNumPorts()
Definition: OnlMonServer.h:35
void SetSpillRange(const int sp_lo, const int sp_hi)
Definition: OnlMonComm.cc:50
void SetSpillRangeLow(const int sp)
Definition: OnlMonComm.cc:40
int ReceiveFullSpillRange()
Definition: OnlMonComm.cc:78
void FindFullSpillRange(int &id_min, int &id_max)
Definition: OnlMonComm.cc:67
static OnlMonComm * instance()
Definition: OnlMonComm.cc:16
static int GetPort0()
Definition: OnlMonServer.h:34
static void SetPort(const int port)
Definition: OnlMonServer.h:28
void GetFullSpillRange(int &id_min, int &id_max)
Definition: OnlMonComm.cc:105
virtual ~OnlMonComm()
Definition: OnlMonComm.cc:35