FACT++  1.0
void ConnectionFTM::HandleReceivedData ( const bs::error_code &  err,
size_t  bytes_received,
int   
)
inlineprivate

Definition at line 189 of file ftmctrl.cc.

References buffer, data, Error(), FTM::Header::fDataSize, FTM::Header::fDelimiter, FTM::Header::fType, Connection::HandleReadTimeout(), Header(), kBold, FTM::kDelimiterEnd, FTM::kDelimiterStart, FTM::kDynamicData, FTM::kErrorList, FTM::kFtuList, FTM::kHeader, FTM::kRegister, FTM::kStaticData, str, and FTM::StaticData::valid().

190  {
191  // Do not schedule a new read if the connection failed.
192  if (bytes_received==0 || err)
193  {
194  if (err==ba::error::eof)
195  Warn("Connection closed by remote host (FTM).");
196 
197  // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
198  // 125: Operation canceled
199  if (err && err!=ba::error::eof && // Connection closed by remote host
200  err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
201  err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
202  {
203  ostringstream str;
204  str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
205  Error(str);
206  }
207  PostClose(err!=ba::error::basic_errors::operation_aborted);
208  return;
209  }
210 
211  // If we have not yet received a header we expect one now
212  // This could be moved to a HandleReceivedHeader function
213  if (!fHasHeader)
214  {
215  if (bytes_received!=sizeof(FTM::Header))
216  {
217  ostringstream str;
218  str << "Excepted " << sizeof(FTM::Header) << " bytes (FTM::Header) but received " << bytes_received << ".";
219  Error(str);
220  PostClose(false);
221  return;
222  }
223 
224  fHeader = fBuffer;
225 
226  // Check the data integrity
228  {
229  ostringstream str;
230  str << "Invalid header received: start delimiter wrong, received ";
231  str << hex << fHeader.fDelimiter << ", expected " << FTM::kDelimiterStart << ".";
232  Error(str);
233  PostClose(false);
234  return;
235  }
236 
237  fHasHeader = true;
238 
239  // Convert FTM state into FtmCtrl state
240  if (++fCounter[FTM::kHeader]==1)
242 
243  UpdateCounter();
244  UpdateHeader();
245 
246  // Start reading of data
247  switch (fHeader.fType)
248  {
249  case FTM::kStaticData:
250  case FTM::kDynamicData:
251  case FTM::kFtuList:
252  case FTM::kRegister:
253  case FTM::kErrorList:
254  // This is not very efficient because the space is reallocated
255  // maybe we can check if the capacity of the std::vector
256  // is ever decreased. If not, everythign is fine.
257  fBuffer.resize(fHeader.fDataSize);
258  AsyncRead(ba::buffer(fBuffer));
259  AsyncWait(fInTimeout, 1000, &Connection::HandleReadTimeout);
260  return;
261 
262  default:
263  ostringstream str;
264  str << "Unknonw type " << fHeader.fType << " in received header." << endl;
265  Error(str);
266  PostClose(false);
267  return;
268  }
269 
270  return;
271  }
272 
273  // Check the data integrity (check end delimiter)
274  if (ntohs(fBuffer.back())!=FTM::kDelimiterEnd)
275  {
276  ostringstream str;
277  str << "Invalid data received: end delimiter wrong, received ";
278  str << hex << ntohs(fBuffer.back()) << ", expected " << FTM::kDelimiterEnd << ".";
279  Error(str);
280  PostClose(false);
281  return;
282  }
283 
284  // Remove end delimiter
285  fBuffer.pop_back();
286 
287  try
288  {
289  // If we have already received a header this is the data now
290  // This could be moved to a HandleReceivedData function
291 
293  UpdateCounter();
294 
295  switch (fHeader.fType)
296  {
297  case FTM::kFtuList:
298  fFtuList = fBuffer;
299  UpdateFtuList();
300  break;
301 
302  case FTM::kStaticData:
303  if (fCounter[FTM::kStaticData]==1)
304  {
305  // This check is only done at startup
307  if (!CheckConsistency(data))
308  {
310  CmdPing(); // FIXME: Only needed in case of warn3
311  break;
312  }
313  }
314 
316 
317  // is this the first received static data block?
318  if (!fBufStaticData.valid())
320 
322  break;
323 
324  case FTM::kDynamicData:
327  break;
328 
329  case FTM::kRegister:
330  if (fIsVerbose)
331  {
332  Out() << endl << kBold << "Register received: " << endl;
333  Out() << "Addr: " << ntohs(fBuffer[0]) << endl;
334  Out() << "Value: " << ntohs(fBuffer[1]) << endl;
335  }
336  break;
337 
338  case FTM::kErrorList:
339  fError = fBuffer;
340  UpdateError();
341  break;
342 
343  default:
344  ostringstream str;
345  str << "Unknonw type " << fHeader.fType << " in header." << endl;
346  Error(str);
347  PostClose(false);
348  return;
349  }
350  }
351  catch (const logic_error &e)
352  {
353  ostringstream str;
354  str << "Exception converting buffer into data structure: " << e.what();
355  Error(str);
356  PostClose(false);
357  return;
358  }
359 
360  fInTimeout.cancel();
361 
362  //fHeader.clear();
363  fHasHeader = false;
364  fBuffer.resize(sizeof(FTM::Header)/2);
365  AsyncRead(ba::buffer(fBuffer));
366  }
bool fIsVerbose
Definition: ftmctrl.cc:44
vector< uint16_t > fBuffer
Definition: ftmctrl.cc:40
FTM::DynamicData fDynamicData
Definition: ftmctrl.cc:54
FTM::Error fError
Definition: ftmctrl.cc:55
Dynamic data (rates)
Definition: HeadersFTM.h:90
char str[80]
Definition: test_client.c:7
FTM::StaticData fBufStaticData
Definition: ftmctrl.cc:57
Header()
Definition: HeadersFTM.h:195
Static (configuration) data.
Definition: HeadersFTM.h:89
bool CheckConsistency(FTM::StaticData &data)
Definition: ftmctrl.cc:144
uint16_t fDataSize
Size in words to be received after the header (incl end delim.)
Definition: HeadersFTM.h:108
virtual void UpdateFirstHeader()
Definition: ftmctrl.cc:59
virtual void UpdateCounter()
Definition: ftmctrl.cc:127
virtual void UpdateFtuList()
Definition: ftmctrl.cc:83
FTM::StaticData fStaticData
Definition: ftmctrl.cc:53
uint16_t fType
Type of the data to be received after the header.
Definition: HeadersFTM.h:107
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
virtual void UpdateError()
Definition: ftmctrl.cc:116
map< uint16_t, uint32_t > fCounter
Definition: ftmctrl.cc:49
Local extension to identify a header in fCounter.
Definition: HeadersFTM.h:88
FTU list (answer of ping)
Definition: HeadersFTM.h:91
float data[4 *1440]
Start delimiter send before each header.
Definition: HeadersFTM.h:100
bool fHasHeader
Definition: ftmctrl.cc:42
bool valid() const
Definition: HeadersFTM.h:266
virtual void HandleReadTimeout(const boost::system::error_code &)
Definition: Connection.h:138
A requested register value.
Definition: HeadersFTM.h:93
virtual void UpdateHeader()
Definition: ftmctrl.cc:68
FTM::FtuList fFtuList
Definition: ftmctrl.cc:52
virtual void UpdateStaticData()
Definition: ftmctrl.cc:94
Error()
Definition: HeadersFTM.h:197
void CmdPing()
Definition: ftmctrl.cc:495
End delimiter send after each data block.
Definition: HeadersFTM.h:101
void CmdSendStatDat(const FTM::StaticData &data)
Definition: ftmctrl.cc:510
uint16_t fDelimiter
Start delimiter.
Definition: HeadersFTM.h:106
virtual void UpdateDynamicData()
Definition: ftmctrl.cc:105
FTM::Header fHeader
Definition: ftmctrl.cc:51
Set attribute Bold.
Definition: WindowLog.h:36
Error list (error when FTU communication failed)
Definition: HeadersFTM.h:92

+ Here is the call graph for this function: