SatCat5
chat_printer.cc
1 // Copyright 2023 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Console application for viewing Chat/Log messages
6 //
7 // The application prompts the user to select an interface, then prints each
8 // received Chat/Log message until the user hits Ctrl+C.
9 
10 #include <hal_posix/chat_printer.h>
11 #include <hal_posix/posix_utils.h>
12 #include <satcat5/eth_chat.h>
13 #include <iostream>
14 
16 
17 ChatPrinter::ChatPrinter(satcat5::eth::ChatProto* chat, bool loopback)
18  : satcat5::net::Protocol(satcat5::net::TYPE_NONE)
19  , m_chat(chat)
20  , m_loopback(loopback)
21 {
22  m_chat->set_callback(this);
23 }
24 
25 ChatPrinter::~ChatPrinter()
26 {
27  m_chat->set_callback(0);
28 }
29 
31 {
32  std::string msg;
33  while (rd.get_read_ready())
34  msg.push_back((char)rd.read_u8());
35  print_message(m_chat->reply_mac(), msg);
36 }
37 
39  const satcat5::eth::MacAddr& from, const std::string& msg)
40 {
41  // Cross-platform: printf(...) works in cases where cout fails
42  // silently, even after calling cout.flush(). May be unicode-related?
43  std::string from_str = satcat5::log::format(from);
44  printf("From: %s\n%s\n\n", from_str.c_str(), msg.c_str());
45 }
46 
47 void ChatPrinter::send_message(const std::string& msg)
48 {
49  if (m_loopback)
50  print_message(m_chat->local_mac(), msg);
51  m_chat->send_text(satcat5::eth::MACADDR_BROADCAST,
52  msg.length(), msg.c_str());
53 }
54 
56 {
57  if (m_line.length() > 0) {
58  send_message(m_line);
59  m_line.clear();
60  return true;
61  } else {
62  return false;
63  }
64 }
65 
67 {
68  // Always ready to accept new data.
69  return 1000;
70 }
71 
73 {
74  m_line.push_back(ch);
75 }
Protocol handler for a simple text-messaging protocol This class implements a simple as-hoc text-mess...
Definition: eth_chat.h:28
void send_text(const satcat5::eth::MacAddr &dst, unsigned nbytes, const char *msg)
Send a human-readable text message.
Definition: eth_chat.cc:58
void set_callback(satcat5::net::Protocol *callback)
Set callback for processing incoming messages.
Definition: eth_chat.h:38
satcat5::eth::MacAddr local_mac() const
Get the local device's source MAC address.
Definition: eth_chat.cc:92
satcat5::eth::MacAddr reply_mac() const
Source MAC address of the most recent received message.
Definition: eth_chat.cc:97
Limited read of next N bytes.
Definition: io_readable.h:255
unsigned get_read_ready() const override
How many bytes can be read without blocking?
Definition: io_readable.cc:293
u8 read_u8()
One of many functions for reading integer/floating point values, see details.
Definition: io_readable.cc:44
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
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
Miscellaneous POSIX wrappers (e.g., heap allocation, log to console...)
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29