39 class TSocketHandler :
public TFileHandler {
45 TSocketHandler(TMonitor *m, TSocket *s, Int_t interest, Bool_t mainloop = kTRUE);
47 Bool_t ReadNotify() {
return Notify(); }
48 Bool_t WriteNotify() {
return Notify(); }
49 TSocket *GetSocket()
const {
return fSocket; }
52 TSocketHandler::TSocketHandler(TMonitor *m, TSocket *s,
53 Int_t interest, Bool_t mainloop)
54 : TFileHandler(s->GetDescriptor(), interest)
64 Bool_t TSocketHandler::Notify()
67 fMonitor->SetReady(fSocket);
76 class TTimeOutTimer :
public TTimer {
81 TTimeOutTimer(TMonitor *m, Long_t ms);
85 TTimeOutTimer::TTimeOutTimer(TMonitor *m, Long_t ms)
90 gSystem->AddTimer(
this);
93 Bool_t TTimeOutTimer::Notify()
96 fMonitor->SetReady((TSocket *)-1);
109 TMonitor::TMonitor(Bool_t mainloop) : TObject() , TQObject()
114 fDeActive =
new TList;
115 fMainLoop = mainloop;
123 TMonitor::TMonitor(
const TMonitor &m) : TObject() , TQObject()
125 TSocketHandler *sh = 0;
128 TIter nxa(m.fActive);
129 while ((sh = (TSocketHandler *)nxa())) {
131 if (sh->HasReadInterest()) mask |= 0x1;
132 if (sh->HasWriteInterest()) mask |= 0x2;
133 fActive->Add(
new TSocketHandler(
this, sh->GetSocket(), mask, m.fMainLoop));
136 fDeActive =
new TList;
137 TIter nxd(m.fDeActive);
138 while ((sh = (TSocketHandler *)nxd())) {
140 if (sh->HasReadInterest()) mask |= 0x1;
141 if (sh->HasWriteInterest()) mask |= 0x2;
142 fDeActive->Add(
new TSocketHandler(
this, sh->GetSocket(), mask, m.fMainLoop));
145 fMainLoop = m.fMainLoop;
146 fInterrupt = m.fInterrupt;
153 TMonitor::~TMonitor()
159 SafeDelete(fDeActive);
168 void TMonitor::Add(TSocket *sock, Int_t interest)
170 fActive->Add(
new TSocketHandler(
this, sock, interest, fMainLoop));
180 void TMonitor::SetInterest(TSocket *sock, Int_t interest)
182 TSocketHandler *s = 0;
189 while ((s = (TSocketHandler *) next())) {
190 if (sock == s->GetSocket()) {
191 s->SetInterest(interest);
197 TIter next1(fDeActive);
198 while ((s = (TSocketHandler *) next1())) {
199 if (sock == s->GetSocket()) {
200 fDeActive->Remove(s);
202 s->SetInterest(interest);
208 fActive->Add(
new TSocketHandler(
this, sock, interest, fMainLoop));
214 void TMonitor::Remove(TSocket *sock)
219 while ((s = (TSocketHandler *) next())) {
220 if (sock == s->GetSocket()) {
227 TIter next1(fDeActive);
229 while ((s = (TSocketHandler *) next1())) {
230 if (sock == s->GetSocket()) {
231 fDeActive->Remove(s);
241 void TMonitor::RemoveAll()
250 void TMonitor::Activate(TSocket *sock)
252 TIter next(fDeActive);
255 while ((s = (TSocketHandler *) next())) {
256 if (sock == s->GetSocket()) {
257 fDeActive->Remove(s);
268 void TMonitor::ActivateAll()
270 TIter next(fDeActive);
273 while ((s = (TSocketHandler *) next())) {
284 void TMonitor::DeActivate(TSocket *sock)
289 while ((s = (TSocketHandler *) next())) {
290 if (sock == s->GetSocket()) {
302 void TMonitor::DeActivateAll()
307 while ((s = (TSocketHandler *) next())) {
322 TSocket *TMonitor::Select()
326 while (!fReady && !fInterrupt)
327 gSystem->InnerLoop();
333 Info(
"Select",
"*** interrupt occured ***");
348 TSocket *TMonitor::Select(Long_t timeout)
351 return TMonitor::Select();
355 TTimeOutTimer t(
this, timeout);
357 while (!fReady && !fInterrupt)
358 gSystem->InnerLoop();
364 Info(
"Select",
"*** interrupt occured ***");
377 Int_t TMonitor::Select(TList *rdready, TList *wrready, Long_t timeout)
381 TSocketHandler *h = 0;
382 Int_t ns = fActive->GetSize();
385 h = (TSocketHandler *)fActive->First();
386 nr = gSystem->Select((TFileHandler *)h, timeout);
388 nr = gSystem->Select(fActive, timeout);
391 if (nr > 0 && (rdready || wrready)) {
400 while ((h = (TSocketHandler *)next())) {
401 if (rdready && h->IsReadReady())
402 rdready->Add(h->GetSocket());
403 if (wrready && h->IsWriteReady())
404 wrready->Add(h->GetSocket());
407 if (rdready && h->IsReadReady())
408 rdready->Add(h->GetSocket());
409 if (wrready && h->IsWriteReady())
410 wrready->Add(h->GetSocket());
423 void TMonitor::SetReady(TSocket *sock)
438 Int_t TMonitor::GetActive(Long_t timeout)
const
445 while ((s = (TSocketHandler *) next())) {
446 TSocket *xs = s->GetSocket();
447 TTimeStamp ts = xs->GetLastUsage();
448 Long_t dt = (Long_t)(now.GetSec() - ts.GetSec()) * 1000 +
449 (Long_t)(now.GetNanoSec() - ts.GetNanoSec()) / 1000000 ;
451 Info(
"GetActive",
"socket: %p: %s:%d did not show any activity"
452 " during the last %ld millisecs: deactivating",
453 xs, xs->GetInetAddress().GetHostName(),
454 xs->GetInetAddress().GetPort(), timeout);
460 }
else if (timeout == 0) {
462 while ((s = (TSocketHandler *) next())) {
463 s->GetSocket()->Touch();
467 return fActive->GetSize();
473 Int_t TMonitor::GetDeActive()
const
475 return fDeActive->GetSize();
482 Bool_t TMonitor::IsActive(TSocket *sock)
const
485 while (TSocketHandler *h = (TSocketHandler*) next())
486 if (sock == h->GetSocket())
498 TList *TMonitor::GetListOfActives()
const
500 TList *list =
new TList;
504 while (TSocketHandler *h = (TSocketHandler*) next())
505 list->Add(h->GetSocket());
515 TList *TMonitor::GetListOfDeActives()
const
517 TList *list =
new TList;
519 TIter next(fDeActive);
521 while (TSocketHandler *h = (TSocketHandler*) next())
522 list->Add(h->GetSocket());
530 void TMonitor::Ready(TSocket *sock)
532 Emit(
"Ready(TSocket*)", (Long_t)sock);