#include #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 public: ServerRoot(); ~ServerRoot(); virtual void SetSpectra(TH1* histo){fHisto=histo;}; virtual void StartGNetServer(); virtual void StopGNetServer(); 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; fMon =NULL; fHisto = NULL; fStarting=false; } //_________________________________________________________________________________________ ServerRoot::~ServerRoot() { // desctructor StopGNetServer(); } //_________________________________________________________________________________________ void ServerRoot::StopGNetServer () { // Stop the Network server if (fRunning==false){ cout<<"Server already stopped\n"; return; } if (fServSock) fServSock->Close(); if (fSockList) { fSockList->Delete(); delete (fSockList); fSockList=NULL; } if (fServSock) { fMon->Remove(fServSock); delete (fServSock); fServSock=NULL; } if (fMon ) { delete (fMon); fMon =NULL; } if (fServSock) fServSock->Close(); fRunning = false; if(fThreadNet) { TThread::Delete(fThreadNet); delete fThreadNet; fThreadNet=NULL; fRunning = false; cout<<"\n\t*********************\n\t Net Server stopped\n\t*********************\n"; }else{ cout <<" Thread no present\n"; } } //_________________________________________________________________________________________ void ServerRoot::StartGNetServer() { // Start the Network server if ( fRunning==false){ fStarting = true; fServSock = new TServerSocket(9090, kTRUE); fMon = new TMonitor; 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) { // 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***********************\n\t Server is running\n\t***********************\n\n"; iGNet->fStarting = false; while (iGNet->fRunning ){ TSocket *localsock; test = ((localsock= iGNet->fMon->Select(40)) != (TSocket*)-1); if (test) iGNet->TreatmentCommands(localsock); }// end of while } //________________________________________________________________________________________ void ServerRoot::TreatmentCommands(TSocket *localsock) { char recp[256]; char *word; 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"; } } //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 // delete (mess); } //_________________________________________________________________________________________