Roboid Control for C++
Loading...
Searching...
No Matches
ParticipantUDP.h
1#pragma once
2
3#include "Messages/BinaryMsg.h"
4#include "Messages/DestroyMsg.h"
5#include "Messages/InvestigateMsg.h"
6#include "Messages/ModelUrlMsg.h"
7#include "Messages/NameMsg.h"
8#include "Messages/ParticipantMsg.h"
9#include "Messages/PoseMsg.h"
10#include "Messages/NetworkIdMsg.h"
11#include "Messages/ThingMsg.h"
12#include "Participant.h"
13
14#if !defined(NO_STD)
15#include <functional>
16#include <list>
17// #include <unordered_map>
18#endif
19
20#if defined(_WIN32) || defined(_WIN64)
21#include <winsock2.h>
22#elif defined(__unix__) || defined(__APPLE__)
23#include <arpa/inet.h>
24#include <netinet/in.h>
25#include <sys/socket.h>
26#include <unistd.h>
27#endif
28
29namespace RoboidControl {
30
31constexpr int MAX_SENDER_COUNT = 256;
32
46#pragma region Init
47
48 public:
54 ParticipantUDP(int port = 7681);
59 ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681);
60
64 static ParticipantUDP* Isolated();
65
68
69#pragma endregion Init
70
73 bool isIsolated = false;
76
79 long publishInterval = 3000; // 3 seconds
80
82 const char* name = "ParticipantUDP";
83
84 protected:
85 char buffer[1024];
86
87#if !defined(ARDUINO)
88#if defined(__unix__) || defined(__APPLE__)
89 int sock;
90#elif defined(_WIN32) || defined(_WIN64)
91 sockaddr_in remote_addr;
92 sockaddr_in server_addr;
93 sockaddr_in broadcast_addr;
94#endif
95#endif
96 public:
97 void begin();
98 bool connected = false;
99
100#pragma region Update
101
102 public:
103 virtual void Update(unsigned long currentTimeMs = 0) override;
104
105 protected:
106 unsigned long nextPublishMe = 0;
107
108 virtual void UpdateMyThings(unsigned long currentTimeMs);
109 virtual void UpdateOtherThings(unsigned long currentTimeMs);
110
111#pragma endregion Update
112
113#pragma region Send
114
115 void SendThingInfo(Participant* remoteParticipant, Thing* thing);
116 void PublishThingInfo(Thing* thing);
117
118 bool Send(Participant* remoteParticipant, IMessage* msg);
119 bool Publish(IMessage* msg);
120
121#pragma endregion Send
122
123#pragma region Receive
124
125protected:
126 void ReceiveData(unsigned char bufferSize,
127 char* senderIpAddress,
128 unsigned int senderPort);
129 void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant);
130
131 void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort);
132
133 void ReceiveUDP();
134
135 virtual void Process(Participant* sender, ParticipantMsg* msg);
136 virtual void Process(Participant* sender, NetworkIdMsg* msg);
137 virtual void Process(Participant* sender, InvestigateMsg* msg);
138 virtual void Process(Participant* sender, ThingMsg* msg);
139 virtual void Process(Participant* sender, NameMsg* msg);
140 virtual void Process(Participant* sender, ModelUrlMsg* msg);
141 virtual void Process(Participant* sender, PoseMsg* msg);
142 virtual void Process(Participant* sender, BinaryMsg* msg);
143
144#pragma endregion Receive
145
146};
147
148} // namespace RoboidControl
A message containing binary data for custom communication.
Definition BinaryMsg.h:9
Root structure for all communcation messages.
Definition IMessage.h:6
Message to request details for a Thing.
Definition InvestigateMsg.h:9
Message for communicating the URL for a model of the thing.
Definition ModelUrlMsg.h:9
Message for communicating the name of a thing.
Definition NameMsg.h:9
A message communicating the network ID for that participant.
Definition NetworkIdMsg.h:8
A participant is a device which manages things. It can communicate with other participant to synchron...
Definition Participant.h:52
unsigned int port
The port number for UDP communication with the participant.
Definition Participant.h:57
const char * ipAddress
The Ip Address of a participant.
Definition Participant.h:55
A participant messages notifies other participants of its presence When received by another participa...
Definition ParticipantMsg.h:10
A participant using UDP communication A local participant is the local device which can communicate w...
Definition ParticipantUDP.h:45
const char * name
The name of the participant.
Definition ParticipantUDP.h:82
Participant * remoteSite
The remote site when this participant is connected to a site.
Definition ParticipantUDP.h:75
bool isIsolated
True if the participant is running isolated. Isolated participants do not communicate with other part...
Definition ParticipantUDP.h:73
long publishInterval
Definition ParticipantUDP.h:79
static ParticipantUDP * Isolated()
Isolated participant is used when the application is run without networking.
Definition ParticipantUDP.cpp:32
virtual void Update(unsigned long currentTimeMs=0) override
Update all things for this participant.
Definition ParticipantUDP.cpp:69
Message to communicate the pose of the thing The pose is in local space relative to the parent....
Definition PoseMsg.h:10
A thing is the primitive building block.
Definition Thing.h:20
Message providing generic details about a Thing.
Definition ThingMsg.h:7