diff options
| author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-03-29 16:49:34 +0200 | 
|---|---|---|
| committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-03-29 16:49:34 +0200 | 
| commit | 630a8991a90b08d124222f7a853e2c838996deff (patch) | |
| tree | 6de335a548024ba6e53e46d4942f253af0c43d2e | |
| parent | 93e517110ee27a45bfb6827ffacb4ae27fdb64e1 (diff) | |
| download | dabmod-630a8991a90b08d124222f7a853e2c838996deff.tar.gz dabmod-630a8991a90b08d124222f7a853e2c838996deff.tar.bz2 dabmod-630a8991a90b08d124222f7a853e2c838996deff.zip | |
Common: log timestamps, socket changes and frame_timestamp_t
| -rw-r--r-- | lib/Log.cpp | 5 | ||||
| -rw-r--r-- | lib/Socket.cpp | 9 | ||||
| -rw-r--r-- | lib/Socket.h | 3 | ||||
| -rw-r--r-- | lib/edi/common.hpp | 29 | 
4 files changed, 44 insertions, 2 deletions
| diff --git a/lib/Log.cpp b/lib/Log.cpp index abbd69a..089e822 100644 --- a/lib/Log.cpp +++ b/lib/Log.cpp @@ -25,6 +25,7 @@     along with this program.  If not, see <http://www.gnu.org/licenses/>.   */ +#include <iomanip>  #include <list>  #include <cstdarg>  #include <cinttypes> @@ -119,7 +120,9 @@ void Logger::io_process()              }              if (m.level != log_level_t::trace) { -                std::cerr << levels_as_str[m.level] << " " << message << std::endl; +                using namespace std::chrono; +                time_t t = system_clock::to_time_t(system_clock::now()); +                cerr << put_time(std::gmtime(&t), "%Y-%m-%dZ%H:%M:%S") << " " << levels_as_str[m.level] << " " << message << endl;              }          }      } diff --git a/lib/Socket.cpp b/lib/Socket.cpp index d12c970..1ff6418 100644 --- a/lib/Socket.cpp +++ b/lib/Socket.cpp @@ -259,6 +259,15 @@ void UDPSocket::send(const std::vector<uint8_t>& data, InetAddress destination)      }  } +void UDPSocket::send(const std::string& data, InetAddress destination) +{ +    const int ret = sendto(m_sock, data.data(), data.size(), 0, +            destination.as_sockaddr(), sizeof(*destination.as_sockaddr())); +    if (ret == SOCKET_ERROR && errno != ECONNREFUSED) { +        throw runtime_error(string("Can't send UDP packet: ") + strerror(errno)); +    } +} +  void UDPSocket::joinGroup(const char* groupname, const char* if_addr)  {      ip_mreqn group; diff --git a/lib/Socket.h b/lib/Socket.h index 08607a5..f5143a0 100644 --- a/lib/Socket.h +++ b/lib/Socket.h @@ -115,6 +115,7 @@ class UDPSocket          void close(void);          void send(UDPPacket& packet);          void send(const std::vector<uint8_t>& data, InetAddress destination); +        void send(const std::string& data, InetAddress destination);          UDPPacket receive(size_t max_size);          void joinGroup(const char* groupname, const char* if_addr = nullptr);          void setMulticastSource(const char* source_addr); @@ -198,6 +199,8 @@ class TCPSocket {           */          ssize_t recv(void *buffer, size_t length, int flags, int timeout_ms); +        SOCKET get_sockfd() const { return m_sock; } +      private:          explicit TCPSocket(int sockfd);          explicit TCPSocket(int sockfd, InetAddress remote_address); diff --git a/lib/edi/common.hpp b/lib/edi/common.hpp index 5e31984..c3e6c40 100644 --- a/lib/edi/common.hpp +++ b/lib/edi/common.hpp @@ -35,7 +35,7 @@ namespace EdiDecoder {  struct frame_timestamp_t {      uint32_t seconds = 0;      uint32_t utco = 0; -    uint32_t tsta = 0; // According to EN 300 797 Annex B +    uint32_t tsta = 0xFFFFFF; // According to EN 300 797 Annex B      bool valid() const;      std::string to_string() const; @@ -47,6 +47,33 @@ struct frame_timestamp_t {      frame_timestamp_t& operator+=(const std::chrono::milliseconds& ms);      static frame_timestamp_t from_unix_epoch(std::time_t time, uint32_t tai_utc_offset, uint32_t tsta); + +    friend bool operator==(const frame_timestamp_t& l, const frame_timestamp_t& r) { +        return (l.seconds - l.utco) == (r.seconds - r.utco) and l.tsta == r.tsta; +    } + +    friend bool operator!=(const frame_timestamp_t& l, const frame_timestamp_t& r) { +        return not (l == r); +    } + +    friend bool operator< (const frame_timestamp_t& l, const frame_timestamp_t& r) { +        if (l.seconds - l.utco == r.seconds - r.utco) { +            return l.tsta < r.tsta; +        } +        return l.seconds - l.utco < r.seconds - r.utco; +    } + +    friend bool operator<= (const frame_timestamp_t& l, const frame_timestamp_t& r) { +        return l < r or l == r; +    } + +    friend bool operator> (const frame_timestamp_t& l, const frame_timestamp_t& r) { +        return r < l; +    } + +    friend bool operator>= (const frame_timestamp_t& l, const frame_timestamp_t& r) { +        return l > r or l == r; +    }  };  using tag_name_t = std::array<uint8_t, 4>; | 
