Roboid Control for C++
Loading...
Searching...
No Matches
d:/Cpp/RoboidControl-cpp/LocalParticipant.h
1#pragma once
2
3#include "Messages/BinaryMsg.h"
4#include "Messages/InvestigateMsg.h"
5#include "Messages/ModelUrlMsg.h"
6#include "Messages/NameMsg.h"
7#include "Messages/ParticipantMsg.h"
8#include "Messages/PoseMsg.h"
9#include "Messages/SiteMsg.h"
10#include "Messages/ThingMsg.h"
11#include "Participant.h"
12
13#if !defined(NO_STD)
14#include <list>
15#endif
16
17#if defined(_WIN32) || defined(_WIN64)
18#include <winsock2.h>
19#elif defined(__unix__) || defined(__APPLE__)
20#include <arpa/inet.h>
21#include <netinet/in.h>
22#include <sys/socket.h>
23#include <unistd.h>
24#elif defined(ARDUINO)
25// #include <WiFiUdp.h>
26#endif
27
28namespace RoboidControl {
29
30constexpr int MAX_SENDER_COUNT = 256;
31
44 public:
50 LocalParticipant(int port = 7681);
54 LocalParticipant(const char* ipAddress, int port = 7681);
55 // Note to self: one cannot specify the port used by the local participant
56 // now!!
57
61 static LocalParticipant* Isolated();
62
65 bool isIsolated = false;
66
69 long publishInterval = 3000; // 3 seconds
70
72 const char* name = "LocalParticipant";
73
74 // int localPort = 0;
75
78
79#if defined(ARDUINO)
80 // const char* remoteIpAddress = nullptr;
81 // unsigned short remotePort = 0;
82 // char* broadcastIpAddress = nullptr;
83
84 // WiFiUDP udp;
85#else
86
87#if defined(__unix__) || defined(__APPLE__)
88 int sock;
89#endif
90 sockaddr_in remote_addr;
91 sockaddr_in server_addr;
92 sockaddr_in broadcast_addr;
93
94#endif
95
96 void begin();
97 bool connected = false;
98
99 virtual void Update(unsigned long currentTimeMs = 0);
100
101 void SendThingInfo(Participant* remoteParticipant, Thing* thing);
102 void PublishThingInfo(Thing* thing);
103
104 bool Send(Participant* remoteParticipant, IMessage* msg);
105 bool Publish(IMessage* msg);
106
107 void ReceiveData(unsigned char bufferSize,
108 char* senderIpAddress,
109 unsigned int senderPort);
110 void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant);
111
112#if defined(NO_STD)
113 unsigned char senderCount = 0;
114 Participant* senders[MAX_SENDER_COUNT];
115#else
116 std::list<Participant*> senders;
117#endif
118
119 protected:
120 unsigned long nextPublishMe = 0;
121
122 char buffer[1024];
123
124 void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort);
125
126 Participant* GetParticipant(const char* ipAddress, int port);
127 Participant* AddParticipant(const char* ipAddress, int port);
128
129 void ReceiveUDP();
130
131 virtual void Process(Participant* sender, ParticipantMsg* msg);
132 virtual void Process(Participant* sender, SiteMsg* msg);
133 virtual void Process(Participant* sender, InvestigateMsg* msg);
134 virtual void Process(Participant* sender, ThingMsg* msg);
135 virtual void Process(Participant* sender, NameMsg* msg);
136 virtual void Process(Participant* sender, PoseMsg* msg);
137 virtual void Process(Participant* sender, BinaryMsg* msg);
138};
139
140} // namespace RoboidControl
Message to send thing-specific data.
Definition BinaryMsg.h:8
Message to request details for a Thing.
Definition InvestigateMsg.h:6
A local participant is the local device which can communicate with other participants It manages all ...
Definition LocalParticipant.h:43
long publishInterval
Definition LocalParticipant.h:69
Participant * remoteSite
The remote site when this participant is connected to a site.
Definition LocalParticipant.h:77
const char * name
The name of the participant.
Definition LocalParticipant.h:72
bool isIsolated
True if the participant is running isolated. Isolated participants do not communicate with other part...
Definition LocalParticipant.h:65
static LocalParticipant * Isolated()
Isolated participant is used when the application is run without networking.
Definition LocalParticipant.cpp:48
Message for communicating the name of a thing.
Definition NameMsg.h:6
A participant is a device which manages things. It can communicate with other participant to synchron...
Definition Participant.h:14
int port
The port number for UDP communication with the participant. This is 0 for isolated participants.
Definition Participant.h:21
const char * ipAddress
The Ip Address of a participant. When the participant is local, this contains 0.0....
Definition Participant.h:18
A participant messages notifies other participants of its presence When received by another participa...
Definition ParticipantMsg.h:10
Message to communicate the pose of the thing The pose is in local space relative to the parent....
Definition PoseMsg.h:8
A message communicating the network ID for that participant.
Definition SiteMsg.h:6
A thing is the primitive building block.
Definition Thing.h:20
Message providing generic information about a Thing.
Definition ThingMsg.h:6