FACT++  1.0
void ConnectionSQM::HandleRead ( const boost::system::error_code &  err,
size_t  bytes_received 
)
inlineprivate

Definition at line 40 of file sqmctrl.cc.

References buffer, SQM::Data::counts, data, Error(), SQM::Data::freq, Time::GetAsStr(), HandleRequestTrigger(), SQM::Data::mag, SQM::Data::period, str, and SQM::Data::temp.

Referenced by ConnectionEstablished(), and StartReadReport().

41  {
42  // Do not schedule a new read if the connection failed.
43  if (bytes_received==0 || err)
44  {
45  if (err==ba::error::eof)
46  Warn("Connection closed by remote host.");
47 
48  // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
49  // 125: Operation canceled
50  if (err && err!=ba::error::eof && // Connection closed by remote host
51  err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
52  err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
53  {
54  ostringstream str;
55  str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
56  Error(str);
57  }
58  PostClose(false);//err!=ba::error::basic_errors::operation_aborted);
59  return;
60  }
61 
62  istream is(&fBuffer);
63 
64  string buffer;
65  if (!getline(is, buffer, '\n'))
66  {
67  Fatal("Received message does not contain \\n... closing connection.");
68  PostClose(false);
69  return;
70  }
71 
72  buffer = buffer.substr(0, buffer.size()-1);
73 
74  if (fIsVerbose)
75  {
76  Out() << Time().GetAsStr("%H:%M:%S.%f") << "[" << buffer.size() << "]: " << buffer << "|" << endl;
77  // Out() << Time().GetAsStr("%H:%M:%S.%f") << "[ " << vec.size() << "]: ";
78  // for (auto it=vec.begin(); it!=vec.end(); it++)
79  // Out() << *it << "|";
80  // Out() << endl;
81  }
82 
83  vector<string> vec;
84  boost::split(vec, buffer, boost::is_any_of(","));
85 
86  try
87  {
88  if (vec.size()!=6)
89  throw runtime_error("Unknown number of fields in received data");
90 
91  if (vec[0]!="r")
92  throw runtime_error("Not a proper answer");
93 
95 
96  data.mag = stof(vec[1]);
97  data.freq = stol(vec[2]);
98  data.counts = stol(vec[3]);
99  data.period = stof(vec[4]);
100  data.temp = stof(vec[5]);
101 
102  Update(data);
103 
104  fValid = true;
105  }
106  catch (const exception &e)
107  {
108  if (fFirstMessage)
109  Warn("Parsing first message failed ["+string(e.what())+"]");
110  else
111  {
112  Error("Parsing received message failed ["+string(e.what())+"]");
113  Error("Received: "+buffer);
114  PostClose(false);
115  return;
116  }
117  }
118 
119  // Send next request in fTimeout milliseconds calculated from
120  // the last request onwards.
121  fTrigger.expires_at(fTrigger.expires_at()+boost::posix_time::milliseconds(fTimeout));
122  fTrigger.async_wait(boost::bind(&ConnectionSQM::HandleRequestTrigger,
123  this, dummy::error));
124 
125  fFirstMessage = false;
126  }
uint32_t counts
Definition: HeadersSQM.h:21
virtual void Update(const SQM::Data &)
Definition: sqmctrl.cc:26
bool fIsVerbose
Definition: sqmctrl.cc:31
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
bool fValid
Definition: sqmctrl.cc:33
float period
Definition: HeadersSQM.h:22
boost::asio::deadline_timer fTrigger
Definition: sqmctrl.cc:38
float mag
Definition: HeadersSQM.h:19
boost::asio::streambuf fBuffer
Definition: sqmctrl.cc:36
void HandleRequestTrigger(const bs::error_code &error)
Definition: sqmctrl.cc:172
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
uint16_t fTimeout
Definition: sqmctrl.cc:34
float data[4 *1440]
Error()
Definition: HeadersFTM.h:197
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240
float temp
Definition: HeadersSQM.h:23
uint32_t freq
Definition: HeadersSQM.h:20
bool fFirstMessage
Definition: sqmctrl.cc:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function: