All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
evd::WebEVDServer< T > Class Template Reference

#include <WebEVDServer.h>

Public Member Functions

 WebEVDServer ()
 
 ~WebEVDServer ()
 
Result serve (const T &evt, const geo::GeometryCore *geom, const detinfo::DetectorPropertiesData &detprop)
 

Protected Types

template<class PROD >
using HandleT = typename T::template HandleT< std::vector< PROD >>
 

Protected Member Functions

int EnsureListen ()
 

Protected Attributes

int fSock
 

Detailed Description

template<class T>
class evd::WebEVDServer< T >

Definition at line 24 of file WebEVDServer.h.

Member Typedef Documentation

template<class T>
template<class PROD >
using evd::WebEVDServer< T >::HandleT = typename T::template HandleT<std::vector<PROD>>
protected

Definition at line 35 of file WebEVDServer.h.

Constructor & Destructor Documentation

template<class T >
evd::WebEVDServer< T >::WebEVDServer ( )

Definition at line 67 of file WebEVDServer.cxx.

68  : fSock(0)
69 {
70 }
template<class T >
evd::WebEVDServer< T >::~WebEVDServer ( )

Definition at line 73 of file WebEVDServer.cxx.

74 {
75  if(fSock) close(fSock);
76 }
print OUTPUT<< EOF;< setup name="Default"version="1.0">< worldref="volWorld"/></setup ></gdml > EOF close(OUTPUT)

Member Function Documentation

template<class T >
int evd::WebEVDServer< T >::EnsureListen ( )
protected

Definition at line 830 of file WebEVDServer.cxx.

831 {
832  if(fSock != 0) return 0;
833 
834  char host[1024];
835  gethostname(host, 1024);
836  char* user = getlogin();
837 
838  std::cout << "\n------------------------------------------------------------\n" << std::endl;
839 
840  // E1071 is DUNE :)
841  int port = 1071;
842 
843  // Search for an open port up-front
844  while(system(TString::Format("ss -an | grep -q %d", port).Data()) == 0) ++port;
845 
846 
847  fSock = socket(AF_INET, SOCK_STREAM, 0);
848  if(fSock == -1) return err("socket");
849 
850  // Reuse port immediately even if a previous instance just aborted.
851  const int one = 1;
852  if(setsockopt(fSock, SOL_SOCKET, SO_REUSEADDR,
853  &one, sizeof(one)) != 0) return err("setsockopt");
854 
855  sockaddr_in addr;
856  addr.sin_family = AF_INET;
857  addr.sin_port = swap_byte_order(port);
858  addr.sin_addr.s_addr = INADDR_ANY;
859 
860  if(bind(fSock, (sockaddr*)&addr, sizeof(addr)) != 0) return err("bind");
861 
862  if(listen(fSock, 128/*backlog*/) != 0) return err("listen");
863 
864 
865  std::cout << "First run" << std::endl;
866  std::cout << "ssh -L "
867  << port << ":localhost:" << port << " "
868  << user << "@" << host << std::endl << std::endl;
869  std::cout << "and then navigate to http://localhost:" << port << "/ in your favorite browser." << std::endl << std::endl;
870  // std::cout << "Press Ctrl-C here when done." << std::endl;
871 
872  return 0;
873 }
EResult err(const char *call)
short swap_byte_order(short x)
then echo Cowardly refusing to create a new FHiCL file with the same name as the original one('${SourceName}')." >&2 exit 1 fi echo "'$
BEGIN_PROLOG could also be cout
template<class T>
Result evd::WebEVDServer< T >::serve ( const T &  evt,
const geo::GeometryCore geom,
const detinfo::DetectorPropertiesData detprop 
)

Definition at line 1030 of file WebEVDServer.cxx.

1033 {
1034  // Don't want a sigpipe signal when the browser hangs up on us. This way we
1035  // will get an error return from the write() call instead.
1036  signal(SIGPIPE, SIG_IGN);
1037 
1038  if(EnsureListen() != 0) return kERROR;
1039 
1040  LazyDigits<T> digs(evt, geom);
1041  LazyWires<T> wires(evt, geom);
1042 
1043  std::list<std::thread> threads;
1044 
1045  while(true){
1046  int sock = accept(fSock, 0, 0);
1047  if(sock == -1) return err("accept");
1048 
1049  std::string req = read_all(sock);
1050 
1051  std::cout << req << std::endl;
1052 
1053  char* ctx;
1054  char* verb = strtok_r(&req.front(), " ", &ctx);
1055 
1056  if(verb && std::string(verb) == "GET"){
1057  char* freq = strtok_r(0, " ", &ctx);
1058  std::string sreq(freq);
1059 
1060  if(sreq == "/NEXT" ||
1061  sreq == "/PREV" ||
1062  sreq == "/NEXT_TRACES" ||
1063  sreq == "/PREV_TRACES" ||
1064  sreq == "/QUIT" ||
1065  sreq.find("/seek/") == 0 ||
1066  sreq.find("/seek_traces/") == 0){
1067  for(std::thread& t: threads) t.join();
1068  return HandleCommand(sreq, sock);
1069  }
1070  else{
1071  threads.emplace_back(_HandleGet<T>, sreq, sock, &evt, &digs, &wires, geom, &detprop);
1072  }
1073  }
1074  else{
1075  write_unimp501(sock);
1076  close(sock);
1077  }
1078  }
1079 
1080  // unreachable
1081 }
EResult err(const char *call)
std::string read_all(int sock)
Result HandleCommand(std::string cmd, int sock)
void write_unimp501(int sock)
TCEvent evt
Definition: DataStructs.cxx:8
print OUTPUT<< EOF;< setup name="Default"version="1.0">< worldref="volWorld"/></setup ></gdml > EOF close(OUTPUT)
BEGIN_PROLOG could also be cout

Member Data Documentation

template<class T>
int evd::WebEVDServer< T >::fSock
protected

Definition at line 39 of file WebEVDServer.h.


The documentation for this class was generated from the following files: