summaryrefslogtreecommitdiff
path: root/protocols/tests/RMCast/Receiver.cpp
blob: 317dc8e395aa6962d5a800b203ff10524e859c28 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// file      : Receiver.cpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// cvs-id    : $Id$

#include "ace/Vector_T.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_string.h"

#include "ace/RMCast/Socket.h"

#include "Protocol.h"

typedef
ACE_Vector<unsigned char, ACE_VECTOR_DEFAULT_SIZE>
Status_List;

class args {};

int
ACE_TMAIN (int argc, ACE_TCHAR* argv[])
{
  try
  {
    if (argc < 2) throw args ();

    ACE_INET_Addr addr (argv[1]);

    // Turn on message loss and reordering simulation.
    //
    ACE_RMCast::Socket socket (addr, false, true);


    Message expected_msg;
    expected_msg.sn = 0;

    // VC6 does not know about the new rules.
    //
    {
      for (unsigned short i = 0; i < payload_size; i++)
      {
        expected_msg.payload[i] = i;
      }
    }

    Status_List received (message_count);
    Status_List damaged (message_count);
    Status_List duplicate (message_count);

    // VC6 does not know about new rules.
    //
    {
      for (unsigned long i = 0; i < message_count; ++i)
      {
        received.push_back (0);
        damaged.push_back (0);
        duplicate.push_back (0);
      }
    }


    Message msg;

    while (true)
    {
      ssize_t s = socket.size ();

      if (s == -1 && errno == ENOENT)
      {
        ACE_ERROR ((LM_ERROR, "unavailable message detected\n"));

        // Receive it.
        //
        socket.recv (&msg, sizeof (msg));

        continue;
      }

      if (s != sizeof (msg))
      {
        ACE_ERROR ((LM_ERROR, "unexpected message size %d, expected %d\n",
                    s, sizeof (msg)));
        continue;
      }

      if (socket.recv (&msg, sizeof (msg)) != s)
      {
        ACE_ERROR ((LM_ERROR,
                    "recv() reported different size than size()\n"));
        continue;
      }

      if (received[msg.sn] == 1)
      {
        duplicate[msg.sn] = 1;
      }
      else
      {
        received[msg.sn] = 1;

        if (ACE_OS::memcmp (expected_msg.payload,
                            msg.payload,
                            payload_size) != 0)
        {
          damaged[msg.sn] = 1;
        }
      }

      if (msg.sn + 1 == message_count) break;
    }

    unsigned long lost_count (0), damaged_count (0), duplicate_count (0);

    {
      for (Status_List::Iterator i (received); !i.done (); i.advance ())
      {
        unsigned char* e;
        i.next (e);

        if (*e == 0) ++lost_count;
      }
    }


    {
      for (Status_List::Iterator i (damaged); !i.done (); i.advance ())
      {
        unsigned char* e;
        i.next (e);

        if (*e == 1) ++damaged_count;
      }
    }


    {
      for (Status_List::Iterator i (duplicate); !i.done (); i.advance ())
      {
        unsigned char* e;
        i.next (e);

        if (*e == 1) ++duplicate_count;
      }
    }

    ACE_DEBUG ((LM_DEBUG,
                "lost      : %d\n"
                "damaged   : %d\n"
                "duplicate : %d\n",
                lost_count,
                damaged_count,
                duplicate_count));

    if (damaged_count != 0 || duplicate_count != 0)
      return 1;

    if (lost_count != 0)
    {
      // It is ok to lose some number of messages at the beginning.
      //
      unsigned long count (0);

      for (Status_List::Iterator i (received); !i.done (); i.advance ())
      {
        unsigned char* e;
        i.next (e);

        if (*e == 0)
          ++count;
        else
          break; // Seen first ok message.
      }

      if (lost_count != count)
        return 1;
    }

    return 0;
  }
  catch (args const&)
  {
    ACE_ERROR ((LM_ERROR,
                "usage: %s <IPv4 multicast address>:<port>\n", argv[0]));
  }

  return 1;
}