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 65 of file WebEVDServer.cxx.

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

Definition at line 71 of file WebEVDServer.cxx.

72 {
73  if(fSock) close(fSock);
74 }
int close(int)
Closes the file descriptor fd.

Member Function Documentation

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

Definition at line 811 of file WebEVDServer.cxx.

812 {
813  if(fSock != 0) return 0;
814 
815  char host[1024];
816  gethostname(host, 1024);
817  char* user = getlogin();
818 
819  std::cout << "\n------------------------------------------------------------\n" << std::endl;
820 
821  // E1071 is DUNE :)
822  int port = 1071;
823 
824  // Search for an open port up-front
825  while(system(TString::Format("ss -an | grep -q %d", port).Data()) == 0) ++port;
826 
827 
828  fSock = socket(AF_INET, SOCK_STREAM, 0);
829  if(fSock == -1) return err("socket");
830 
831  // Reuse port immediately even if a previous instance just aborted.
832  const int one = 1;
833  if(setsockopt(fSock, SOL_SOCKET, SO_REUSEADDR,
834  &one, sizeof(one)) != 0) return err("setsockopt");
835 
836  sockaddr_in addr;
837  addr.sin_family = AF_INET;
838  addr.sin_port = swap_byte_order(port);
839  addr.sin_addr.s_addr = INADDR_ANY;
840 
841  if(bind(fSock, (sockaddr*)&addr, sizeof(addr)) != 0) return err("bind");
842 
843  if(listen(fSock, 128/*backlog*/) != 0) return err("listen");
844 
845 
846  std::cout << "First run" << std::endl;
847  std::cout << "ssh -L "
848  << port << ":localhost:" << port << " "
849  << user << "@" << host << std::endl << std::endl;
850  std::cout << "and then navigate to http://localhost:" << port << "/ in your favorite browser." << std::endl << std::endl;
851  // std::cout << "Press Ctrl-C here when done." << std::endl;
852 
853  return 0;
854 }
Format
Definition: utils.h:7
EResult err(const char *call)
short swap_byte_order(short x)
Fw2dFFT::Data Data
constexpr std::enable_if_t< are_cv_compatible< TO, FROM >::value, std::add_pointer_t< std::remove_pointer_t< TO > > > addr(FROM &from)
Definition: ensurePointer.h:35
QTextStream & endl(QTextStream &s)
template<class T>
Result evd::WebEVDServer< T >::serve ( const T &  evt,
const geo::GeometryCore geom,
const detinfo::DetectorPropertiesData detprop 
)

Definition at line 1011 of file WebEVDServer.cxx.

1014 {
1015  // Don't want a sigpipe signal when the browser hangs up on us. This way we
1016  // will get an error return from the write() call instead.
1017  signal(SIGPIPE, SIG_IGN);
1018 
1019  if(EnsureListen() != 0) return kERROR;
1020 
1021  LazyDigits<T> digs(evt, geom);
1022  LazyWires<T> wires(evt, geom);
1023 
1024  std::list<std::thread> threads;
1025 
1026  while(true){
1027  int sock = accept(fSock, 0, 0);
1028  if(sock == -1) return err("accept");
1029 
1030  std::string req = read_all(sock);
1031 
1032  std::cout << req << std::endl;
1033 
1034  char* ctx;
1035  char* verb = strtok_r(&req.front(), " ", &ctx);
1036 
1037  if(verb && std::string(verb) == "GET"){
1038  char* freq = strtok_r(0, " ", &ctx);
1039  std::string sreq(freq);
1040 
1041  if(sreq == "/NEXT" ||
1042  sreq == "/PREV" ||
1043  sreq == "/NEXT_TRACES" ||
1044  sreq == "/PREV_TRACES" ||
1045  sreq == "/QUIT" ||
1046  sreq.find("/seek/") == 0 ||
1047  sreq.find("/seek_traces/") == 0){
1048  for(std::thread& t: threads) t.join();
1049  return HandleCommand(sreq, sock);
1050  }
1051  else{
1052  threads.emplace_back(_HandleGet<T>, sreq, sock, &evt, &digs, &wires, geom, &detprop);
1053  }
1054  }
1055  else{
1056  write_unimp501(sock);
1057  close(sock);
1058  }
1059  }
1060 
1061  // unreachable
1062 }
std::string string
Definition: nybbler.cc:12
EResult err(const char *call)
std::string read_all(int sock)
int close(int)
Closes the file descriptor fd.
Result HandleCommand(std::string cmd, int sock)
void write_unimp501(int sock)
TCEvent evt
Definition: DataStructs.cxx:7
QTextStream & endl(QTextStream &s)

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: