SatCat5
chat_printer.h
1 // Copyright 2023-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Callback object that prints incoming "ChatProto" messages
6 
7 #pragma once
8 
9 #include <satcat5/eth_dispatch.h>
10 #include <satcat5/io_core.h>
11 #include <string>
12 
13 namespace satcat5 {
14  namespace eth {
15  // Prototype for the ChatProto class.
16  class ChatProto;
17  }
18 
19  namespace util {
26  class ChatPrinter final
27  : public satcat5::net::Protocol
28  , public satcat5::io::Writeable
29  {
30  public:
34  ChatPrinter(satcat5::eth::ChatProto* chat, bool loopback=true);
35  ~ChatPrinter();
36 
38  void send_message(const std::string& msg);
39 
40  private:
42  void print_message(
43  const satcat5::eth::MacAddr& from,
44  const std::string& msg);
45 
47  void frame_rcvd(satcat5::io::LimitedRead& rd) override;
48 
52  unsigned get_write_space() const override;
53  void write_next(u8 ch);
54  bool write_finalize() override;
56 
57  // Internal state.
58  satcat5::eth::ChatProto* const m_chat;
59  std::string m_line;
60  bool m_loopback;
61  };
62  }
63 }
Protocol handler for a simple text-messaging protocol This class implements a simple as-hoc text-mess...
Definition: eth_chat.h:28
Limited read of next N bytes.
Definition: io_readable.h:255
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Callback object that prints incoming messages.
Definition: chat_printer.h:29
void write_next(u8 ch)
Writeable endpoint: Accumulate characters from input stream.
Definition: chat_printer.cc:72
bool write_finalize() override
Writeable endpoint: Accumulate characters from input stream.
Definition: chat_printer.cc:55
void print_message(const satcat5::eth::MacAddr &from, const std::string &msg)
Pretty formatting for incoming or outgoing messages.
Definition: chat_printer.cc:38
ChatPrinter(satcat5::eth::ChatProto *chat, bool loopback=true)
Attach to the designated ChatProto service.
Definition: chat_printer.cc:17
void frame_rcvd(satcat5::io::LimitedRead &rd) override
Handle incoming data from eth::ChatProto.
Definition: chat_printer.cc:30
void send_message(const std::string &msg)
Send a message string to all other chat clients.
Definition: chat_printer.cc:47
unsigned get_write_space() const override
Writeable endpoint: Accumulate characters from input stream.
Definition: chat_printer.cc:66
I/O interface core definitions.
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29