1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// This example uses the NORM API and demonstrates the use
// of NORM_OBJECT_DATA transmission and reception.
// The "data" sent and received are simple text messages
// of randomly-varying length.
XXX - THIS CODE IS NOT YET A COMPLETE EXAMPLE !!!!!
#include <normApi.h>
// We use some "protolib" functions for
// cross-platform portability
#include <protoDebug.h>
#include <protoTime.h>
#include <stdlib.h> // for rand(), srand()
void Usage()
{
fprintf(stderr, "Usage: normDataExample [send][recv]\n");
}
int main(int argc, char* argv[])
{
bool send = false;
bool recv = false;
// Parse any command-line arguments
int i = 1;
while (i < argc)
{
if (!strcmp("send", argv[i]))
{
send = true;
}
else if (!strcmp("recv", argv[i]))
{
recv = true;
}
else
{
fprintf(stderr, "normDataExample error: invalid command-line option!\n");
Usage();
return -1;
}
return 0;
}
if (!send && !recv)
{
fprintf(stderr, "normDataExample error: not configured as sender or receiver!\n");
Usage();
return -1;
}
// 1) Create a NORM API "NormInstance"
NormInstanceHandle instance = NormCreateInstance();
if (NORM_INSTANCE_INVALID == instance)
{
fprintf(stderr, "normDataExample error: NormCreateInstance() failure!\n");
return -1;
}
// 2) Create a NormSession using default "automatic" local node id
NormSessionHandle session = NormCreateSession(instance,
"224.1.2.3",
6003,
NORM_NODE_ANY);
if (NORM_SESSION_INVALID == instance)
{
fprintf(stderr, "normDataExample error: NormCreateSession() failure!\n");
return -1;
}
// Enable some debugging output here
//SetDebugLevel(2);
// Uncomment to turn on debug NORM message tracing
NormSetMessageTrace(session, true);
// 3) Start receiver operation, if applicable
if (recv)
{
// Start receiver w/ 1MByte buffer per sender
if (!NormStartReceiver(session, 1024*1024))
{
fprintf(stderr, "normDataExample error: NormStartReceiver() failure!\n");
return -1;
}
}
// 4) Start receiver operation, if applicable
if (send)
{
// a) Pick a random sender "sessionId"
// (seed the random generation so we get a new one each time)
struct timeval currentTime;
ProtoSystemTime(currentTime);
srand(currentTime.tv_sec); // seed random number generator
NormSessionId sessionId = (NormSessionId)rand();
// b) Set sender rate or congestion control operation
NormSetTxRate(session, 256.0e+03); // in bits/second
}
// Enter loop waiting for and handling NormEvents ...
// (We illustrate usage of a NormDescriptor for this
// with our WaitForNormEvent() routine.)
// When set to "send" the "interval" is used to
NormDescriptor normDescriptor = NormGetDescriptor(instance);
bool keepGoing = true;
while (keepGoing)
{
boolWaitForNormEvent(normDescriptor, timeDelay)
} // end while (keepGoing)
} // end main()