From 99fa368d2f3d5d293b84466652b5cdcf43df0a82 Mon Sep 17 00:00:00 2001
From: adrien-matta <a.matta@surrey.ac.uk>
Date: Sat, 13 Jun 2015 11:19:21 +0100
Subject: [PATCH] *Adding new file for online

---
 NPLib/Core/NPOnline.cxx        | 166 +++++++++++++++++++++++++++++++++
 NPLib/Core/NPOnline.h          |  52 +++++++++++
 NPLib/Core/NPSpectraServer.cxx |  72 ++++++++++++++
 NPLib/Core/NPSpectraServer.h   |  33 +++++++
 NPLib/Utility/nponline.cxx     |  10 ++
 5 files changed, 333 insertions(+)
 create mode 100644 NPLib/Core/NPOnline.cxx
 create mode 100644 NPLib/Core/NPOnline.h
 create mode 100644 NPLib/Core/NPSpectraServer.cxx
 create mode 100644 NPLib/Core/NPSpectraServer.h
 create mode 100644 NPLib/Utility/nponline.cxx

diff --git a/NPLib/Core/NPOnline.cxx b/NPLib/Core/NPOnline.cxx
new file mode 100644
index 000000000..4b1056503
--- /dev/null
+++ b/NPLib/Core/NPOnline.cxx
@@ -0,0 +1,166 @@
+#include "NPOnline.h"
+#include <iostream>
+
+// Root
+#include "TRoot.h"
+#include "TColor.h"
+#include "TSystem.h"
+#include "TString.h"
+#include "TF1.h"
+#include "TCanvas.h"
+#include "TFile.h"
+#include "TASImage.h"
+#include "TMessage.h"
+ClassImp(NPL::NPOnline);
+////////////////////////////////////////////////////////////////////////////////
+NPL::NPOnline::NPOnline(){
+  m_Sock = 0;
+  TString NPLPath = gSystem->Getenv("NPTOOL");
+  gROOT->ProcessLine(Form(".x %s/NPLib/scripts/NPToolLogon.C+", NPLPath.Data()));
+  gROOT->SetStyle("nptool");
+  
+  m_BgColor = gROOT->GetColor(kGray+3)->GetPixel();
+  m_FgColor = gROOT->GetColor(kAzure+7)->GetPixel();
+
+  m_Main = new TGMainFrame(0,0,0);
+  m_Main->SetCleanup(kDeepCleanup);
+  m_Main->SetBackgroundColor(m_BgColor);
+  m_Main->SetForegroundColor(m_FgColor);
+
+
+  m_Tab= new TGTab(m_Main,100,100);
+  m_Tab->SetBackgroundColor(m_BgColor);
+  m_Tab->SetForegroundColor(m_FgColor);
+  m_Tab->ChangeSubframesBackground(m_BgColor);  
+  AddTab();
+  
+  // Create a horizonal frame containing two text buttons
+  m_Lhorz = new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 30);
+  m_Lresize = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 0, 0, 0, 30);
+  m_Lbut = new TGLayoutHints(kLHintsCenterY, 10, 10, 0, 0);
+  m_Lcan = new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,30,30,30,30);
+  m_Horz = new TGHorizontalFrame(m_Main, 100, 100);
+  
+  m_Horz->SetBackgroundColor(m_BgColor);
+  m_Horz->SetForegroundColor(m_FgColor);
+
+  // Create "Connect" and "Quit" buttons
+  // Add to horizontal frame
+  m_Connect = new TGTextButton(m_Horz, "Connect");
+  m_Connect->Connect("Clicked()", "NPL::NPOnline", this, "Connect()");
+  m_Connect->SetBackgroundColor(m_BgColor);
+  m_Connect->SetForegroundColor(m_FgColor);
+
+  m_Quit = new TGTextButton(m_Horz, "Quit");
+  m_Quit->SetCommand("gApplication->Terminate()");
+  m_Quit->SetBackgroundColor(m_BgColor);
+  m_Quit->SetForegroundColor(m_FgColor);
+
+  m_Update = new TGTextButton(m_Horz, "Update");
+  m_Update->Connect("Clicked()", "NPL::NPOnline", this, "Update()");
+  m_Update->SetBackgroundColor(m_BgColor);
+  m_Update->SetForegroundColor(m_FgColor);
+
+  m_Horz->AddFrame(m_Quit, m_Lbut);
+  m_Horz->AddFrame(m_Connect, m_Lbut);
+  m_Horz->AddFrame(m_Update, m_Lbut);
+
+  m_Main->AddFrame(m_Horz, m_Lhorz);
+  m_Main->AddFrame(m_Tab, m_Lresize);
+
+  m_Main->SetWindowName("NPOnline");
+  m_Main->MapSubwindows();
+
+  m_Main->Resize(m_Main->GetDefaultSize());
+  m_Main->MapWindow();
+
+  m_Main->Layout();
+  m_hist=0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+NPL::NPOnline::~NPOnline(){
+  delete m_Main; 
+  delete m_Tab;
+  delete m_Lcan;
+  delete m_Horz;
+  delete m_Lbut;
+  delete m_Lhorz;
+  delete m_Quit;
+  delete m_Connect;
+  delete m_Update;
+  delete m_Sock;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void NPL::NPOnline::Connect(){
+  // Connect to SpectraServer
+  m_Sock = new TSocket("localhost", 9090);
+  if(m_Sock->IsValid()){
+    m_Connect->SetState(kButtonDisabled);
+    Update();
+  }
+}
+////////////////////////////////////////////////////////////////////////////////
+void NPL::NPOnline::Update(){
+  if(!m_Sock || !(m_Sock->IsValid())){
+    cout << "Spectra server not connected" << endl;
+    return;
+  }
+
+  TMessage* message;
+  m_Sock->Send("RequestSpectra");
+
+  if(m_Sock->Recv(message)<=0){
+    cout << "Spectra request failed " << endl;
+    return;
+  }
+  
+  m_CanvasList = (TList*) message->ReadObject(message->GetClass());
+  
+  
+  int current_tab = m_Tab->GetCurrent();
+  int tabs =  m_Tab->GetNumberOfTabs();
+  for(int i = tabs-1 ; i != -1 ; i--){
+    m_Tab->RemoveTab(i,false);
+  }
+ 
+  for(TCanvas* c = (TCanvas*) m_CanvasList->First() ; c !=0 ; c = (TCanvas*) m_CanvasList->After(c)){
+    AddTab(c->GetName(),c);
+  }
+  if( m_Tab->GetNumberOfTabs()==0)
+    AddTab();
+ 
+  m_Main->MapSubwindows();
+  m_Main->MapWindow();
+  m_Main->Layout();
+  m_Tab->SetTab(current_tab);
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
+void NPL::NPOnline::AddTab(std::string name,TCanvas* c){
+  TGCompositeFrame* tf; 
+  TRootEmbeddedCanvas* canvas;
+  if(name=="default"){
+   tf = m_Tab->AddTab("NPOnline");
+  
+   canvas= new TRootEmbeddedCanvas("Canvas",tf,1600,800,!kSunkenFrame); 
+   canvas->GetCanvas()->SetMargin(0,0,0,0);
+   canvas->GetCanvas()->SetFillColor(kGray+3);
+   TImage* logo = TASImage::Open("nptoolLogo.png");
+   logo->SetConstRatio(true);
+   logo->Draw("");
+  }
+  
+  else{
+    tf = m_Tab->AddTab(name.c_str());
+    canvas= new TRootEmbeddedCanvas("Canvas",tf,1600,800,!kSunkenFrame); 
+    if(c)
+      canvas->AdoptCanvas(c);
+  }
+  tf->SetBackgroundColor(m_BgColor);
+  tf->SetForegroundColor(m_FgColor);
+  tf->AddFrame(canvas,m_Lresize);
+
+}
diff --git a/NPLib/Core/NPOnline.h b/NPLib/Core/NPOnline.h
new file mode 100644
index 000000000..55f2ea3b0
--- /dev/null
+++ b/NPLib/Core/NPOnline.h
@@ -0,0 +1,52 @@
+#ifndef NPONLINE_H
+#define NPONLINE_H
+#include "TGLayout.h"
+#include "TGButton.h"
+#include "TGTab.h"
+#include "TRootEmbeddedCanvas.h"
+#include "TH1.h"
+#include "TSocket.h"
+#include "RQ_OBJECT.h"
+using namespace std;
+
+namespace NPL{
+  class NPOnline{
+    RQ_OBJECT("NPOnline")
+
+    public:
+      NPOnline();
+      ~NPOnline();
+
+      void Connect();
+      void Update();
+     
+      // Add a new Tab to the interface
+      void AddTab(std::string name="default",TCanvas* c=0);
+    
+    private: // Server client
+      TSocket* m_Sock;
+      TList* m_CanvasList;
+
+    private: // GUI stuff
+      TGMainFrame* m_Main;
+      TGTab* m_Tab;
+      TGLayoutHints* m_Lcan;
+      TGHorizontalFrame* m_Horz;
+      TGLayoutHints* m_Lbut;
+      TGLayoutHints* m_Lhorz;
+      TGLayoutHints* m_Lresize;
+      TGButton* m_Quit;
+      TGButton* m_Connect;
+      TGButton* m_Update;
+      
+    private: // Style
+      Pixel_t m_BgColor;
+      Pixel_t m_FgColor;
+
+      TH1* m_hist; 
+
+      ClassDef(NPOnline,1);
+  };
+
+}
+#endif
diff --git a/NPLib/Core/NPSpectraServer.cxx b/NPLib/Core/NPSpectraServer.cxx
new file mode 100644
index 000000000..60e6ddf33
--- /dev/null
+++ b/NPLib/Core/NPSpectraServer.cxx
@@ -0,0 +1,72 @@
+#include "NPSpectraServer.h"
+
+NPL::SpectraServer* NPL::SpectraServer::instance = 0 ;
+////////////////////////////////////////////////////////////////////////////////
+NPL::SpectraServer* NPL::SpectraServer::getInstance(){
+  if(!instance) 
+    instance = new NPL::SpectraServer();
+
+  return instance;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+NPL::SpectraServer::SpectraServer(){
+  m_Server= new TServerSocket(9090,true);
+  if(!m_Server->IsValid())
+    exit(1);
+
+  // Add server socket to monitor so we are notified when a client needs to be
+  // accepted
+  m_Monitor  = new TMonitor;
+  m_Monitor->Add(m_Server);
+
+  // Create a list to contain all client connections
+  m_Sockets = new TList;
+
+  // Create the list of Canvas
+  m_Canvas = new TList;
+}
+////////////////////////////////////////////////////////////////////////////////
+void NPL::SpectraServer::AddCanvas(TCanvas* c){
+  m_Canvas->Add(c);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void NPL::SpectraServer::Start(){
+  if(m_Server){
+    while(1){
+      TSocket* s ;
+      if((s=m_Monitor->Select(20))!=(TSocket*)-1)
+        HandleSocket(s);
+    }
+  }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+NPL::SpectraServer::~SpectraServer(){}
+////////////////////////////////////////////////////////////////////////////////
+void NPL::SpectraServer::HandleSocket(TSocket* s){
+  if (s->IsA() == TServerSocket::Class()) {
+    // accept new connection from spy
+    TSocket* socket = ((TServerSocket*)s)->Accept();
+    m_Monitor->Add(socket);
+    m_Sockets->Add(socket);
+  }
+  else {
+    // we only get string based requests from the spy
+    char request[64];
+    if (s->Recv(request, sizeof(request)) <= 0) {
+      m_Monitor->Remove(s);
+      m_Sockets->Remove(s);
+      delete s;
+      return;
+    }
+
+    // send requested object back
+    TMessage answer(kMESS_OBJECT);
+    if (!strcmp(request, "RequestSpectra"))
+      answer.WriteObject(m_Canvas);
+
+    s->Send(answer);
+  }
+}
diff --git a/NPLib/Core/NPSpectraServer.h b/NPLib/Core/NPSpectraServer.h
new file mode 100644
index 000000000..3f8b15fce
--- /dev/null
+++ b/NPLib/Core/NPSpectraServer.h
@@ -0,0 +1,33 @@
+#ifndef NPSPECTRASERVER_H
+#define NPSPECTRASERVER_H
+
+#include "TSocket.h"
+#include "TServerSocket.h"
+#include "TMonitor.h"
+#include "TMessage.h"
+#include "TList.h"
+#include "TCanvas.h"
+namespace NPL{
+  class SpectraServer{
+    public:
+      static SpectraServer* getInstance();
+    
+    private:
+      SpectraServer();
+      ~SpectraServer();
+
+    private:
+      static SpectraServer* instance;
+   
+    public:
+      void HandleSocket(TSocket* s);
+      void AddCanvas(TCanvas* c);
+      void Start();
+   private:
+    TServerSocket* m_Server;     
+    TMonitor* m_Monitor;     
+    TList* m_Sockets;
+    TList* m_Canvas;
+  };
+}
+#endif
diff --git a/NPLib/Utility/nponline.cxx b/NPLib/Utility/nponline.cxx
new file mode 100644
index 000000000..f5525f37e
--- /dev/null
+++ b/NPLib/Utility/nponline.cxx
@@ -0,0 +1,10 @@
+#include"TApplication.h"
+#include"NPOnline.h"
+
+int main(void){
+  TApplication* app = new TApplication("NPOnline",0,0);
+  NPL::NPOnline* instance = new NPL::NPOnline();
+  app->Run();
+  return 0;
+}
+
-- 
GitLab