LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
LandedSocket_service.cc
Go to the documentation of this file.
1 #include "LandedSocket.h"
2 
10 
11 #include <iostream>
12 #include <sstream>
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include <boost/array.hpp>
17 
18 namespace evd
19 {
22  endpoint_(nullptr),
23  socket_(nullptr),
24  service_()
25  {
26 
27  reg.sPostBeginJob.watch (this, &LandedSocket::postBeginJob);
31 
32  }
33 
34  void LandedSocket::
36  {
37 
38  }
39  void LandedSocket::
41  std::vector<art::Worker*> const& workers)
42  {
43  inputSource_=inputsource;
44  record_=0;
45  }
46  void LandedSocket::
48  {
49 
50  }
51  void LandedSocket::
53  {
54  if (socket_!=nullptr)
55  {
56  art::RootInput* rootinput = dynamic_cast<art::RootInput*>(inputSource_);
57  if (!rootinput)
58  {
59  throw cet::exception("Landed") << "input is not root file, therefore not seekable and therefore unsuitable for EventViewer\n";
60  }
61  boost::array<char, 128> instr;
62  size_t len=socket_->receive(boost::asio::buffer(instr));
63  if (len>0)
64  {
65  long newrec=atol(instr.data());
66  long diff=newrec-record_;
67  diff--;
68  // std::cout << "seek: " << diff << std::endl;
69  if (rootinput->seekToEvent(diff, true))
70  {
71  LOG_DEBUG("Landed") << "seek succeeded\n";
72  socket_->send(boost::asio::buffer("OK\n", 3));
73  record_=newrec;
74  }
75  else
76  {
77  // std::cout << "no such event, reloading\n";
78  if (!rootinput->seekToEvent(event.id(), true))
79  throw cet::exception("Landed") << "problem performing seek on input, lost track of input event\n";
80  socket_->send(boost::asio::buffer("NO\n", 3));
81  // std::cout << "reload performed\n";
82  }
83  //wait for message confirmation
84  boost::array<char, 2> conf;
85  if (socket_->receive(boost::asio::buffer(conf))!=1)
86  throw cet::exception("Landed") << "LANDED app did not confirm instruction result\n";
87 
88  }
89  else
90  throw cet::exception("Landed") << "bad instruction from LANDED app\n";
91  }
92  }
93 
94  bool LandedSocket::
95  connect(void)
96  {
97  //connect to landed app
98  std::ostringstream sockfile;
99  sockfile << getenv("HOME") << "/.landed.sock";
100  endpoint_=new boost::asio::local::stream_protocol::endpoint(sockfile.str());
101  if (!endpoint_)
102  {
103  mf::LogError("Landed") << "failed to create socket for connection to LANDED app\n";
104  return false;
105  }
106  socket_=new boost::asio::local::stream_protocol::socket(service_);
107  if (socket_!=nullptr)
108  socket_->connect(*endpoint_);
109  return socket_!=nullptr;
110  }
111 
112  void LandedSocket::
113  sendGeometry(std::string vrml)
114  {
115  std::ostringstream ss;
116  ss << "GEO:"<< vrml.length() << "\n";
117  if (socket_->send(boost::asio::buffer(ss.str().c_str(), ss.str().length()))!=ss.str().length() ||
118  socket_->send(boost::asio::buffer(vrml.c_str(), vrml.length()))!=vrml.length())
119  {
120  throw cet::exception("Landed") << "failed to send detector geometry to LANDED app\n";
121  }
122  boost::array<char, 2> conf;
123  if (socket_->receive(boost::asio::buffer(conf))!=1)
124  throw cet::exception("Landed") << "LANDED app did not confirm detector geometry\n";
125  else
126  LOG_DEBUG("Landed") << "LANDED app has confirmed detector geometry\n";
127 
128  }
129  void LandedSocket::
130  sendEvent(int nhits, int nvertex, int run, int subrun, int event)
131  {
132  std::ostringstream ss;
133  ss << "EVT:" << record_ << "," << nhits << "," << nvertex << ", " << run << "," << subrun << "," << event << "\n";
134  // std::cout << ss.str();
135  if (socket_->send(boost::asio::buffer(ss.str().c_str(), ss.str().length()))!=ss.str().length())
136  throw cet::exception("Landed") << "failed to send event introduction to LANDED app\n";
137  boost::array<char, 2> conf;
138  if (socket_->receive(boost::asio::buffer(conf))!=1)
139  throw cet::exception("Landed") << "LANDED app did not confirm event\n";
140  }
141  void LandedSocket::
142  sendHit(double x, double y, double z, double e, double ne, int track)
143  {
144  std::ostringstream ss;
145  ss << "HIT:" << x << "," << y << "," << z << "," << e << "," << ne << "," << track << "\n";
146  // std::cout << ss.str();
147  if (socket_->send(boost::asio::buffer(ss.str().c_str(), ss.str().length()))!=ss.str().length())
148  throw cet::exception("Landed") << "failed to send hit to LANDED app\n";
149  boost::array<char, 2> conf;
150  if (socket_->receive(boost::asio::buffer(conf))!=1)
151  throw cet::exception("Landed") << "LANDED app did not confirm hit\n";
152 
153  }
154  void LandedSocket::
155  sendVertex(double x, double y, double z, double e, int id, int pdgcode)
156  {
157  std::ostringstream ss;
158  ss << "VTX:" << x << "," << y << "," << z << "," << e << "," << id << "," << pdgcode << "\n";
159  if (socket_->send(boost::asio::buffer(ss.str().c_str(), ss.str().length()))!=ss.str().length())
160  throw cet::exception("Landed") << "failed to send vertex to LANDED app\n";
161  boost::array<char, 2> conf;
162  if (socket_->receive(boost::asio::buffer(conf))!=1)
163  throw cet::exception("Landed") << "LANDED app did not confirm vertex\n";
164  }
165 }
166 
Float_t x
Definition: compare.C:6
GlobalSignal< detail::SignalResponseType::FIFO, void()> sPostBeginJob
LandedSocket(fhicl::ParameterSet const &, art::ActivityRegistry &reg)
void postBeginJobWorkers(art::InputSource *, std::vector< art::Worker * > const &)
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:93
Float_t y
Definition: compare.C:6
Double_t z
Definition: plot.C:279
Float_t ss
Definition: plot.C:23
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
void sendVertex(double, double, double, double, int, int)
GlobalSignal< detail::SignalResponseType::LIFO, void(InputSource *, std::vector< Worker * > const &)> sPostBeginJobWorkers
void preProcessEvent(art::Event const &)
LArSoft includes.
void sendHit(double, double, double, double, double, int)
void sendGeometry(std::string)
void sendEvent(int, int, int, int, int)
void postProcessEvent(art::Event const &)
art::InputSource * inputSource_
Definition: LandedSocket.h:33
bool seekToEvent(T eventSpec, bool exact=false)
Definition: RootInput.h:145
boost::asio::local::stream_protocol::endpoint * endpoint_
Definition: LandedSocket.h:34
boost::asio::io_service service_
Definition: LandedSocket.h:36
GlobalSignal< detail::SignalResponseType::FIFO, void(Event const &)> sPreProcessEvent
unsigned long record_
Definition: LandedSocket.h:37
#define LOG_DEBUG(id)
GlobalSignal< detail::SignalResponseType::LIFO, void(Event const &)> sPostProcessEvent
Float_t e
Definition: plot.C:34
Float_t track
Definition: plot.C:34
boost::asio::local::stream_protocol::socket * socket_
Definition: LandedSocket.h:35
EventID id() const
Definition: Event.h:56
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Event finding and building.