#include using std::ostream; #include #include #include #include #include #include #include #include #include #include #include //_________________________________________________________________________________________ class ServerRoot : public TObject { private: TH1* fHisto; //! TServerSocket *fServSock; //! Server TList *fSockList; //! List of open socket with client TMonitor *fMon; //! socket monitor (if we want to drive few client connection) TThread *fThreadNet; //! bool fRunning; // informe about running of server bool fStarting; // informe about starting of server Int_t fPort; // port number for TCPIP communication bool fQuiet; // bolean to allow print or not public: ServerRoot(); ~ServerRoot(); virtual void SetSpectra(TH1* histo) { fHisto=histo; } ; virtual void StartGNetServer(bool quiet=false); virtual void StopGNetServer(bool quiet=false); virtual void SetPort(int port) { fPort = port; } virtual int GetPort() { return fPort; } virtual void TreatmentCommands(TSocket *localsock); private: static void ThreadWaitCommandsFromClient(void* arg); void GiveSpectrum(TSocket *localsock); ClassDef (ServerRoot, 1); // Nerwork ROOT Server to send spectra }; ClassImp(ServerRoot); //_________________________________________________________________________________________ ServerRoot::ServerRoot() { fRunning=false; fSockList=NULL; fThreadNet=NULL; fServSock=NULL; fHisto = NULL; fStarting=false; fMon = new TMonitor; fPort = 9090;// default port for Root communication } //_________________________________________________________________________________________ ServerRoot::~ServerRoot() { // desctructor StopGNetServer(); if (fMon) delete fMon; if (fMon) { delete (fMon); fMon =NULL; } } //_________________________________________________________________________________________ void ServerRoot::StopGNetServer(bool quiet) { // Stop the Network server // if quiet is true no message is screened , default value is false fQuiet = quiet; if (fRunning==false) { if (!quiet) cout << "Server already stopped\n"; return; } if (fThreadNet) { fRunning = false; cout<<"\n\t Net Server stopped\n "; } else { cout <<" Thread no present\n"; } fMon->RemoveAll(); if (fServSock) { delete (fServSock); fServSock=NULL; } if (fSockList) { fSockList->Delete(); delete (fSockList); fSockList=NULL; } sleep(1); if (fThreadNet) { TThread::Delete(fThreadNet); fThreadNet=NULL; } } //_________________________________________________________________________________________ void ServerRoot::StartGNetServer(bool quiet) { // Start the Network server if (fRunning==false) { if (!quiet)cout<<"\n\t Server is starting\n"; fStarting = true; fServSock = new TServerSocket(fPort, kTRUE); fMon->Add(fServSock); // fSockList = new TList; // Creation of list of all client connections fSockList->SetOwner(); // // si le thread n'existe pas, on le cree if (!fThreadNet) { fThreadNet =new TThread("MESSAGE_Net_Server", (void (*)(void *))&ServerRoot::ThreadWaitCommandsFromClient, (void*) this); fThreadNet->Run(); fRunning=true; fStarting = false; } } else { cout <<" Server already running\n"; } } //_________________________________________________________________________________________ void ServerRoot::ThreadWaitCommandsFromClient(void* arg) { // Thread for waiting command // All command are: // MESSAGE HISTO -> ask to receive a histogram with name NAME_OF_HISTOGRAM // MESSAGE TEST -> test // ServerRoot* iGNet = (ServerRoot*) arg; // instance of ServerRoot given in parameter bool test; cout<<"\n\t Server is running\n"; while (iGNet->fRunning) { TSocket *localsock; test = ((localsock= iGNet->fMon->Select(40)) != (TSocket*)-1); if (test) iGNet->TreatmentCommands(localsock); else { usleep(100); } } // end of while } //________________________________________________________________________________________ void ServerRoot::TreatmentCommands(TSocket *localsock) { char recp[256]; char *word; TString tempo; if (localsock->IsA() == TServerSocket::Class()) { // accept new connection from client TSocket *sock = ((TServerSocket*)localsock)->Accept(); fMon->Add(sock); fSockList->Add(sock); cout<< "accepted connection from "<< sock->GetInetAddress().GetHostName()<<"\n"; } else { // we only get string based requests from the client // receive command if (localsock->Recv(recp, sizeof(recp)) <= 0) { fMon->Remove(localsock); fSockList->Remove(localsock); cout << "closed connection from "<< localsock->GetInetAddress().GetHostName()<<"\n"; delete localsock; return; } cout<< "Received Command : "<< recp<<"\n"; //Get GROUP command word word = strtok(recp, " "); if (strcmp(word, "MESSAGE")!=0) { cout<< " It is not a MESSAGE Net commmand! : "<Send("MESSAGE_TESTOK"); cout<< "send " " MESSAGE_TESTOK" " \n"; return; } } //end of else } //_________________________________________________________________________________________ void ServerRoot::GiveSpectrum(TSocket *localsock) { TMessage mess(kMESS_OBJECT); if (fHisto == NULL) { cout << " No spectrum \n"; localsock->Send("MESSAGE_NO_SPECTRUM"); return; } mess.Reset(); // re-use TMessage object mess.WriteObject(fHisto); // write object in message buffer localsock->Send(mess); // send message } //_________________________________________________________________________________________