00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "TH1.h"
00013 #include "TH2.h"
00014 #include "TProfile.h"
00015 #include "TCanvas.h"
00016 #include "TFrame.h"
00017 #include "TSocket.h"
00018 #include "TServerSocket.h"
00019 #include "TMonitor.h"
00020 #include "TMessage.h"
00021 #include "TRandom.h"
00022 #include "TList.h"
00023 #ifndef __CINT__
00024 #include "TError.h"
00025 #endif
00026
00027
00028 class SpyServ {
00029 private:
00030 TCanvas *fCanvas;
00031 TH1F *fHpx;
00032 TH2F *fHpxpy;
00033 TProfile *fHprof;
00034 TServerSocket *fServ;
00035 TMonitor *fMon;
00036 TList *fSockets;
00037 public:
00038 SpyServ();
00039 ~SpyServ();
00040
00041 void HandleSocket(TSocket *s);
00042 };
00043
00044
00045 void SpyServ::HandleSocket(TSocket *s)
00046 {
00047 if (s->IsA() == TServerSocket::Class()) {
00048
00049 TSocket *sock = ((TServerSocket*)s)->Accept();
00050 fMon->Add(sock);
00051 fSockets->Add(sock);
00052 printf("accepted connection from %s\n", sock->GetInetAddress().GetHostName());
00053 } else {
00054
00055 char request[64];
00056 if (s->Recv(request, sizeof(request)) <= 0) {
00057 fMon->Remove(s);
00058 fSockets->Remove(s);
00059 printf("closed connection from %s\n", s->GetInetAddress().GetHostName());
00060 delete s;
00061 return;
00062 }
00063
00064
00065 TMessage answer(kMESS_OBJECT);
00066 if (!strcmp(request, "get hpx"))
00067 answer.WriteObject(fHpx);
00068 else if (!strcmp(request, "get hpxpy"))
00069 answer.WriteObject(fHpxpy);
00070 else if (!strcmp(request, "get hprof"))
00071 answer.WriteObject(fHprof);
00072 else
00073 Error("SpyServ::HandleSocket", "unexpected message");
00074 s->Send(answer);
00075 }
00076 }
00077
00078 SpyServ::SpyServ()
00079 {
00080
00081
00082
00083
00084
00085
00086
00087 fServ = new TServerSocket(9090, kTRUE);
00088 if (!fServ->IsValid())
00089 gSystem->Exit(1);
00090
00091
00092
00093 fMon = new TMonitor;
00094 fMon->Add(fServ);
00095
00096
00097 fSockets = new TList;
00098
00099
00100 fCanvas = new TCanvas("SpyServ","SpyServ",200,10,700,500);
00101 fCanvas->SetFillColor(42);
00102 fCanvas->GetFrame()->SetFillColor(21);
00103 fCanvas->GetFrame()->SetBorderSize(6);
00104 fCanvas->GetFrame()->SetBorderMode(-1);
00105
00106
00107 fHpx = new TH1F("hpx","This is the px distribution",100,-4,4);
00108 fHpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
00109 fHprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
00110
00111
00112 fHpx->SetFillColor(48);
00113
00114
00115 gRandom->SetSeed();
00116 Float_t px, py, pz;
00117 const Int_t kUPDATE = 1000;
00118 for (Int_t i = 0; ; i++) {
00119 gRandom->Rannor(px,py);
00120 pz = px*px + py*py;
00121 fHpx->Fill(px);
00122 fHpxpy->Fill(px,py);
00123 fHprof->Fill(px,pz);
00124 if (i && (i%kUPDATE) == 0) {
00125 if (i == kUPDATE) fHpx->Draw();
00126 fCanvas->Modified();
00127 fCanvas->Update();
00128
00129
00130
00131 TSocket *s;
00132 if ((s = fMon->Select(20)) != (TSocket*)-1)
00133 HandleSocket(s);
00134 if (!fCanvas->TestBit(TObject::kNotDeleted))
00135 break;
00136 if (gROOT->IsInterrupted())
00137 break;
00138 }
00139 }
00140 }
00141
00142 SpyServ::~SpyServ()
00143 {
00144
00145
00146 fSockets->Delete();
00147 delete fSockets;
00148 delete fServ;
00149 delete fCanvas;
00150 delete fHpx;
00151 delete fHpxpy;
00152 delete fHprof;
00153 }
00154
00155 void spyserv()
00156 {
00157 new SpyServ;
00158 }