OpenCOVER
rel_mcast.h
Go to the documentation of this file.
1/* This file is part of COVISE.
2
3 You can use it under the terms of the GNU Lesser General Public License
4 version 2.1 or later, see lgpl-2.1.txt.
5
6 * License: LGPL 2+ */
7
144// Default debug mode (0=errors, 1=info, 2=status, 3=all(trace) )
145#define DEBUG_LVL 0
146
147// Default address/port and interface to be used for multicast
148#define MCAST_ADDR "224.223.222.221"
149#define MCAST_PORT 23232
150#define MCAST_IFACE "eth0"
151
152// Default values of various NORM settings
153#define SND_BUFFER_SPACE 1000000
154#define RCV_BUFFER_SPACE 1000000
155#define MTU_SIZE 1500
156#define BLOCK_SIZE 4
157#define NUM_PARITY 0
158
159// Default TTL (for communication on a LAN, this can be 1)
160#define NORM_TTL 1
161
162// Default cache bounds
163#define CACHE_MAX_SIZE 100000000
164#define CACHE_MIN_NUM 1
165#define CACHE_MAX_NUM 128
166
167// Default transmit rate in Mbps (1000 = 1Gbps)
168#define TX_MBPS 1000
169
170// Default backoff factor (low value yields low latency)
171#define BACKOFF_FACTOR 0.0
172
173// Default buffer size (bytes) of the sending+receiving UDP sockets (increase to prevent socket bottleneck due to high Tx rate)
174#define SOCK_BUFFER_SIZE 512000
175
176// Default maximum amount of time in sec to wait for select()
177#define READ_TIMEOUT_SEC 30
178#define WRITE_TIMEOUT_MSEC 500
179#define RETRY_TIMEOUT 100
180
181// Define max object length for read/write
182#define MAX_LENGTH 1000000
183
184// Include the NORM API
185#include "normApi.h"
186#include "protoDefs.h"
187#include "protoDebug.h"
188// Include necessary header files
189#include <stdio.h> // for printf(), stderr, etc.
190#include <string.h> // for strcmp()
191#include <stdlib.h> // for rand()
192#include <unistd.h> // for usleep()
193#include <stdarg.h> // for variable argument list
194#include <util/coTypes.h> // for COVISE
195
196using namespace std;
197
198namespace opencover
199{
200// Class for multicast
201class Rel_Mcast
202{
203public:
204 // Error types
206 {
207 RM_OK, // No error
208 RM_INIT_ERROR, // Error during init() phase
209 RM_SETTING_ERROR, // Error when changing a setting (i.e. parameter out of bounds)
210 RM_WRITE_ERROR, // Error while writing a multicast message
211 RM_READ_ERROR // Error while reading a multicast message
212 };
213
214 // Constructor for server
215 Rel_Mcast(bool server, int numClients, const char *addr = MCAST_ADDR, int port = MCAST_PORT, const char *interface = MCAST_IFACE);
216 // Constructor for client
217 Rel_Mcast(int clientNum, const char *addr = MCAST_ADDR, int port = MCAST_PORT, const char *interface = MCAST_IFACE);
218 // Destructor
220 // Initialize Rel_Mcast instance
222 // Send a multicast message to all clients
223 RM_Error_Type write_mcast(const void *data, int length);
224 // Read a multicast message sent from the server
225 RM_Error_Type read_mcast(void *dest, int length);
226 // Change settings anytime
233 RM_Error_Type setTxCacheBounds(unsigned int bytes, int min, int max);
235 // Change settings before init()
237 RM_Error_Type setInterface(const char *iface);
239 RM_Error_Type setBufferSpace(unsigned int bytes);
243
244private:
245 // Set up server node upon a call to init()
246 RM_Error_Type init_server();
247 // Set up client node upon a call to init()
248 RM_Error_Type init_client();
249 // Event handlers for write_mcast()
250 void waitForVacancy();
251 void waitForSend();
252 // Event handler for read_mcast()
253 void waitForRead(int sec, int msec, void *dest, int length);
254 // Function to print error/debug messages: lvl=0 -> error, lvl>=1 -> info_msg
255 void printMsg(int lvl, const char *msg, ...);
256
257 // Instance variables
258 int debugLevel; // (0=errors, 1=basic info, 2=detailed info,
259 // 3=per-message info [warning: many print statements], 4=all)
260 const char *mcastAddr; // Multicast address, i.e. Class-D IP address
261 // in range 224.0.0.0 - 239.255.255.255
262 int mcastPort; // Port that communication will take place on
263 const char *mcastIface; // Interface to use for multicast (e.g. eth0)
264 int nodeId; // Unique identifier for each node taking part in the communication
265 // (i.e. server and each client)
266 bool isServer; // Should be 'true' for one node-- the master
267 bool lback; // If 'true', multicast packets will loopback to the sender
268 int ttl; // Max number of routing hops a packet can make before being discarded
269 NormInstanceHandle normInstance;
270 NormSessionHandle normSession;
271 NormSessionId normSessionId;
272 NormDescriptor normFD; // Norm file descriptor for pselect() call
273 fd_set fdSet; // File descriptor set for pselect() call
274 struct timespec tv; // timeout for pselect() call
275 int retval; // Return value of pselect() call
276 bool isRunning; // Whether the node is currently running
277 bool quitting; // Whether the node is due to quit
278 int numObjPending; // Sender's count of the number of pending transmissions
279 // in the transmit queue (keep low to avoid flooding receivers)
280 int numPurged; // Count of the number of purged items
281 unsigned int sndBufferSpace; // How much history (bytes) is kept on sender-side
282 unsigned int rcvBufferSpace; // How much history (bytes) is kept on receiver-side
283 int mtu; // Size of NORM segments (includes protocol headers)
284 int blockSize; // Size of NORM blocks (used for FEC error-correction)
285 int numParity; // Number of parity symbol segments sender will calculate per FEC block
286 int txRate; // Sender's max transmission rate in Mbps (e.g. 1000=>1Gbps)
287 NormSize txCacheSize; // Size of server's cache containing pending data items
288 int txCacheMin; // Minumum number of data items cached by server (overrides txCacheSize)
289 int txCacheMax; // Maximum number of data items cached by server (overrides txCacheSize)
290 int sockBufferSize; // Size of the UDP socket's buffer (should be increased for fast communication)
291 double backoffFactor; // Time (sec) to scale NACK-related repairs (0.0 for lowest latency)
292 int groupSize; // Expected number of clients, does not need to be exact
293 // (within an order of magnitude)
294 bool dataSent; // For server to keep track the "send" status of each multicast
295 long sentCounter; // Counter for the number of sent multicasts
296 bool gotData; // True if a multicast has been previously received
297 long readCounter; // Counter for the number of received multicasts
298 int readTimeoutSec; // Max #seconds for a client to wait for a read
299 int retryTimeout; // Microseconds to wait before retrying a failed data enqueue (due to full queue)
300 int writeTimeoutSec; // Max #seconds to wait for a write (in addition to writeTimeoutMsec)
301 int writeTimeoutMsec; // Max #msecs to wait for a write (in addition to writeTimeoutSec)
302 int maxLength; // Max length (bytes) of a multicast
303};
304}
#define MCAST_ADDR
Definition: rel_mcast.h:148
#define MCAST_IFACE
Definition: rel_mcast.h:150
#define MCAST_PORT
Definition: rel_mcast.h:149
Definition: ARToolKit.h:33
RM_Error_Type setMaxLength(int m)
RM_Error_Type setInterface(const char *iface)
Rel_Mcast(bool server, int numClients, const char *addr=MCAST_ADDR, int port=MCAST_PORT, const char *interface=MCAST_IFACE)
RM_Error_Type read_mcast(void *dest, int length)
RM_Error_Type setBlocksAndParity(int b, int p)
RM_Error_Type setLoopback(bool lb)
RM_Error_Type setMTU(int bytes)
RM_Error_Type setTimeout(int t)
RM_Error_Type setTTL(int t)
RM_Error_Type setRetryTimeout(int u)
RM_Error_Type setBufferSpace(unsigned int bytes)
Rel_Mcast(int clientNum, const char *addr=MCAST_ADDR, int port=MCAST_PORT, const char *interface=MCAST_IFACE)
RM_Error_Type init()
RM_Error_Type setTxRate(int Mbps)
RM_Error_Type setDebugLevel(int lvl)
RM_Error_Type
Error Codes.
Definition: rel_mcast-old.h:202
@ RM_READ_ERROR
Definition: rel_mcast-old.h:207
@ RM_SETTING_ERROR
Definition: rel_mcast.h:209
@ RM_OK
no error
Definition: rel_mcast-old.h:203
@ RM_INIT_ERROR
Definition: rel_mcast.h:208
@ RM_WRITE_ERROR
Definition: rel_mcast-old.h:206
RM_Error_Type setTxCacheBounds(unsigned int bytes, int min, int max)
RM_Error_Type write_mcast(const void *data, int length)
RM_Error_Type setSockBufferSize(int bytes)
RM_Error_Type setBackoffFactor(double factor)
RM_Error_Type setNumClients(int n)