RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
Receiver.h
Go to the documentation of this file.
1#pragma once
2#include "BaseComponent.h"
3#include "../Common.h"
4#include <thread>
5#include <mutex>
6#include <condition_variable>
7#include <atomic>
8#include <functional>
9#include <string>
10#include <string.h>
11#include <errno.h>
12#include <unistd.h>
13#include <sys/types.h>
14#include <sys/socket.h>
15#include <netinet/in.h>
16
17namespace RumPi
18{
26 class Receiver : public BaseComponent
27 {
28 private:
29 std::atomic<bool> myIsConnected;
30 std::atomic<bool> myIsSendMessageReady;
31 std::string mySendMessageBuffer;
33 unsigned int myPortNumber;
34 std::atomic<unsigned int> myNumCharactersRead;
35 std::atomic<unsigned int> myNumCharactersWritten;
36 std::atomic<int> mySocketFileDescriptor;
37 std::atomic<int> myNewSocketFileDescriptor;
38 std::thread myConnectionThread;
39 std::thread mySendThread;
40 std::thread myReceiveThread;
41 mutable std::mutex mySendLock;
42 mutable std::mutex myReceiveLock;
43 std::condition_variable mySendCondition;
44
56 void ConnectToSocket();
57
69 void SendViaThread();
70
82 void ReceiveViaThread();
83
84 public:
86 typedef std::function<void(const char* theReceivedMessage)> OnMessageReceived;
88
103 Receiver(unsigned int portNum, OnMessageReceived messageReceivedHandler);
104
116 virtual ~Receiver();
117
129 virtual void TurnOn() override;
130
142 virtual void TurnOff() override;
143
154 virtual void ProcessRawValues() override;
155
169 virtual std::string ToString() const override;
170
181 void ClearSendMessage();
182
194
207 void SendMessage(std::string message);
208
221 bool GetIsConnected() const;
222
235 bool GetIsSendMessageReady() const;
236
249 unsigned int GetPortNumber() const;
250
263 unsigned int GetNumCharactersRead() const;
264
277 unsigned int GetNumCharactersWritten() const;
278
291 int GetSocketFileDescriptor() const;
292
305 int GetNewSocketFileDescriptor() const;
306
319 void SetPortNumber(unsigned int portNum);
320 };
321} // namespace RumPi
Abstract base class for every component (sensor, actuator, etc.) the library manages.
Definition: BaseComponent.h:60
Implements TCP client/server socket communication, running its accept, send, and receive loops on the...
Definition: Receiver.h:27
bool GetIsSendMessageReady() const
Gets whether a message is queued and waiting to be sent.
Definition: Receiver.cpp:494
virtual std::string ToString() const override
Serializes the receiver's connection state, buffers, port, byte counts, and socket descriptors to a s...
Definition: Receiver.cpp:402
std::atomic< int > mySocketFileDescriptor
The listening socket file descriptor.
Definition: Receiver.h:36
std::function< void(const char *theReceivedMessage)> OnMessageReceived
A callback invoked with each message received from the connected client.
Definition: Receiver.h:86
unsigned int GetNumCharactersWritten() const
Gets the number of characters written in the last send.
Definition: Receiver.cpp:545
void ClearReceiveMessageBuffer()
Clears the received-message buffer, under the receive lock.
Definition: Receiver.cpp:459
std::thread myReceiveThread
The thread that reads incoming messages.
Definition: Receiver.h:40
void SendMessage(std::string message)
Queues a message to send (rejecting it if one is already pending) and wakes the send thread.
Definition: Receiver.cpp:242
std::atomic< unsigned int > myNumCharactersRead
The number of characters read in the last receive.
Definition: Receiver.h:34
std::atomic< int > myNewSocketFileDescriptor
The connected client's socket file descriptor.
Definition: Receiver.h:37
std::mutex myReceiveLock
Guards the receive buffer.
Definition: Receiver.h:42
void SendViaThread()
Send worker: waits for a queued message (or shutdown), then writes it to the client with MSG_NOSIGNAL...
Definition: Receiver.cpp:274
std::mutex mySendLock
Guards the send buffer and ready flag.
Definition: Receiver.h:41
void ReceiveViaThread()
Receive worker: reads incoming messages in a loop, forwards each to the registered callback,...
Definition: Receiver.cpp:335
virtual void ProcessRawValues() override
No-op: the receiver delivers messages via its callback, so there are no raw values to process.
Definition: Receiver.cpp:189
int GetSocketFileDescriptor() const
Gets the listening socket file descriptor.
Definition: Receiver.cpp:562
Receiver(unsigned int portNum, OnMessageReceived messageReceivedHandler)
Builds a receiver bound to the given port with the given message-received callback (sockets/threads s...
Definition: Receiver.cpp:20
std::atomic< bool > myIsSendMessageReady
True when a message is queued and waiting to be sent.
Definition: Receiver.h:30
unsigned int GetPortNumber() const
Gets the port the server listens on.
Definition: Receiver.cpp:511
void ConnectToSocket()
Accepts a client connection (blocking until one arrives or the socket is shut down),...
Definition: Receiver.cpp:205
std::thread myConnectionThread
The thread that accepts a client connection.
Definition: Receiver.h:38
unsigned int myPortNumber
The port the server listens on.
Definition: Receiver.h:33
std::condition_variable mySendCondition
Signals the send thread that a message is ready (or shutdown).
Definition: Receiver.h:43
virtual ~Receiver()
Destroys the receiver, stopping its threads and closing its sockets via TurnOff.
Definition: Receiver.cpp:45
void SetPortNumber(unsigned int portNum)
Sets the port the server listens on.
Definition: Receiver.cpp:596
std::string mySendMessageBuffer
The message currently queued to send.
Definition: Receiver.h:31
OnMessageReceived OnMessageReceivedCallback
The registered message-received callback.
Definition: Receiver.h:87
std::string myReceiveMessageBuffer
The most recently received message.
Definition: Receiver.h:32
std::atomic< unsigned int > myNumCharactersWritten
The number of characters written in the last send.
Definition: Receiver.h:35
virtual void TurnOn() override
Creates, binds, and listens on the server socket, then accepts a client connection on a background th...
Definition: Receiver.cpp:63
std::thread mySendThread
The thread that sends queued messages.
Definition: Receiver.h:39
std::atomic< bool > myIsConnected
True while a client is connected and the loops should run.
Definition: Receiver.h:29
void ClearSendMessage()
Clears the queued send message and marks no message as ready, under the send lock.
Definition: Receiver.cpp:442
bool GetIsConnected() const
Gets whether a client is currently connected.
Definition: Receiver.cpp:477
int GetNewSocketFileDescriptor() const
Gets the connected client's socket file descriptor.
Definition: Receiver.cpp:579
unsigned int GetNumCharactersRead() const
Gets the number of characters read in the last receive.
Definition: Receiver.cpp:528
virtual void TurnOff() override
Stops all loops, unblocks any threads parked in accept/read/wait, joins the connection,...
Definition: Receiver.cpp:123
RumPi is the library's top-level facade.
Definition: AlertManager.h:6