gondola_core/tof/
panic.rs

1//! Reporting catastrophic errors throughout the TOF system 
2//! related to software crashes 
3//!
4// The following file is part of gaps-online-software and published 
5// under the GPLv3 license
6
7use crate::prelude::*;
8
9/// To be sent right before a panic occurs
10#[derive(Debug, Clone, PartialEq)]
11#[cfg_attr(feature="pybindings", pyclass)]
12pub struct PanicPacket {
13  sender  : u8,
14  message : String,
15}
16
17impl PanicPacket {
18
19  pub fn new(sender : u8, message : String) -> Self {
20    Self { 
21      sender  : sender,
22      message : message
23    } 
24  }
25
26  /// Sent this over a newly created zmq socket.
27  /// (If any data publisher thread is active, this 
28  /// might itself cause a panic, so that this works
29  /// all data publsher threads have to cease first 
30  pub fn sent(&self) {
31    let _bs = self.to_bytestream();
32  }
33}
34
35impl Default for PanicPacket {
36
37  fn default() -> Self {
38    Self::new(0,String::from("Unknonwn panic!"))
39  }
40}
41
42impl fmt::Display for PanicPacket {
43  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44    let mut repr = format!("<PanicPacket from {}\n  ", self.sender);
45    repr += &self.message;
46    repr += ">";
47    write!(f, "{}", repr)
48  }
49}
50
51impl TofPackable for PanicPacket {
52  const TOF_PACKET_TYPE        : TofPacketType = TofPacketType::PanicPacket; 
53}
54
55impl Serialization for PanicPacket {
56  const HEAD               : u16   = 43690; //0xAAAA
57  const TAIL               : u16   = 21845; //0x5555
58  
59  fn to_bytestream(&self) -> Vec<u8> {
60    let mut stream = Vec::<u8>::new();
61    stream.extend_from_slice(&Self::HEAD.to_le_bytes());
62    stream.push(self.sender);
63    let mut payload = CRFrame::string_to_bytes(self.message.clone());
64    let pl_len  = payload.len() as u16;
65    stream.extend(&pl_len.to_le_bytes()); 
66    stream.append(&mut payload);
67    stream.extend_from_slice(&Self::TAIL.to_le_bytes());
68    stream
69  }
70  
71  fn from_bytestream(stream    : &Vec<u8>, 
72                     pos       : &mut usize) 
73    -> Result<Self, SerializationError>{
74    
75        
76    let head      = parse_u16(stream, pos);
77    if head != Self::HEAD {
78      error!("Decoding of HEAD failed! Got {} instead!", head);
79      return Err(SerializationError::HeadInvalid);
80    }
81    let sender    = parse_u8(stream, pos);
82    let msg       = parse_string(stream, pos);
83    let tail      = parse_u16(stream, pos);
84    if tail != Self::HEAD {
85      error!("Decoding of HEAD failed! Got {} instead!", head);
86      return Err(SerializationError::HeadInvalid);
87    }
88    let panic = Self::new(sender, msg);
89    Ok(panic)
90  }
91}