summaryrefslogtreecommitdiff
path: root/tests/RMCast/RMCast_UDP_Best_Effort_Test.cpp
blob: 2c8c8e80da72896a56cb80f19bd00319fe0fbbe6 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// $Id$

// ============================================================================
//
// = DESCRIPTION
//    Unit test for the UDP sending module of the RMCast library.
//
// = AUTHORS
//    Carlos O'Ryan <coryan@uci.edu>
//
// ============================================================================

#include "test_config.h"
#include "ace/RMCast/RMCast_IO_UDP.h"
#include "ace/RMCast/RMCast_Fragment.h"

#include "ace/RMCast/RMCast_Module_Factory.h"
#include "ace/RMCast/RMCast_Fragment.h"
#include "ace/RMCast/RMCast_Reassembly.h"

#include "ace/Task.h"

ACE_RCSID(tests, RMCast_UDP_Best_Effort_Test, "$Id$")

const size_t message_size = 8 * 1024;
const int total_message_count = 40;

// ****************************************************************

class Sender_Factory : public ACE_RMCast_Module_Factory
{
public:
  Sender_Factory (void)
  {
  }

  virtual ACE_RMCast_Module *create (void)
  {
    return new ACE_RMCast_Reassembly;
  }

  virtual void destroy (ACE_RMCast_Module *module)
  {
    delete module;
  }
};

// ****************************************************************

class Receiver_Factory : public ACE_RMCast_Module_Factory
{
public:
  Receiver_Factory (ACE_RMCast_Module *module)
    : module_ (module)
  {
  }

  virtual ACE_RMCast_Module *create (void)
  {
    ACE_RMCast_Module *x = new ACE_RMCast_Reassembly;
    x->next (this->module_);
    return x;
  }

  virtual void destroy (ACE_RMCast_Module *module)
  {
    delete module;
  }

private:
  ACE_RMCast_Module *module_;
};

// ****************************************************************

class Sender : public ACE_Task_Base
{
public:
  Sender (const ACE_INET_Addr &mcast_group);

  virtual int svc (void);

private:
  Sender_Factory factory_;
  ACE_RMCast_IO_UDP io_udp_;
  ACE_RMCast_Fragment fragment_;

  ACE_INET_Addr mcast_group_;
};

// ****************************************************************

class Receiver : public ACE_RMCast_Module
{
public:
  Receiver (const ACE_INET_Addr &mcast_group);

  void dump (void);
  // Print the results of the test

  int handle_events (ACE_Time_Value *tv);
  // Invoke the UDP Receiver handle_events function

  virtual int open (void);
  virtual int data (ACE_RMCast::Data &data);

private:
  Receiver_Factory factory_;
  ACE_RMCast_IO_UDP io_udp_;

  ACE_INET_Addr mcast_group_;

  int message_count_;
};

// ****************************************************************

int
main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("RMCast_UDP_Best_Effort_Test"));

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("This is ACE Version %u.%u.%u\n\n"),
              ACE::major_version(),
              ACE::minor_version(),
              ACE::beta_version()));

  ACE_INET_Addr mcast_group;
  mcast_group.set (12345, ACE_TEXT ("224.9.9.1"));

  Receiver receiver (mcast_group);
  if (receiver.open () != 0)
    ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Error in Receiver::open\n")), 1);   

  Sender sender (mcast_group);
  if (sender.activate () != 0)
    ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Error in Sender::activate\n")), 1);

  ACE_Time_Value tv (120, 0);
  for (;;)
    {
      int r = receiver.handle_events (&tv);

      if (r < 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("Error in handle_events()\n")),
                            1);
        }
      if (tv == ACE_Time_Value::zero)
        break;
    }

  if (ACE_Thread_Manager::instance ()->wait () != 0)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       ACE_TEXT ("Error in Thread_Manager::wait\n")), 
                       1);

  receiver.dump ();

  ACE_END_TEST;
  return 0;
}

// ****************************************************************

Receiver::Receiver (const ACE_INET_Addr &mcast_group)
  :  factory_ (this)
  ,  io_udp_ (&factory_)
  ,  mcast_group_ (mcast_group)
  ,  message_count_ (0)
{
}

int
Receiver::handle_events (ACE_Time_Value *tv)
{
  return this->io_udp_.handle_events (tv);
}

int
Receiver::open (void)
{
  if (this->io_udp_.subscribe (this->mcast_group_) != 0)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       ACE_TEXT ("Error in IO_UDP::subscribe\n")), 
                      -1);
  return 0;
}

int
Receiver::data (ACE_RMCast::Data &data)
{
  if (data.total_size != message_size)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Invalid message size (%d,%d,%d)\n"),
                       data.sequence_number,
                       data.total_size,
                       data.fragment_offset), -1);
  if (data.fragment_offset != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Invalid message size (%d,%d,%d)\n"),
                       data.sequence_number,
                       data.total_size,
                       data.fragment_offset), -1);

  char expected = 0;
  for (char *j = data.payload->rd_ptr ();
       j != data.payload->wr_ptr ();
       ++j)
    {
      if (*j != expected++)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("Unexpected byte at pos %d\n"),
                           long(j - data.payload->rd_ptr ())), -1);
    }

  this->message_count_++;

  return 0;
}

void
Receiver::dump (void)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Message count = %d/%d\n"),
              this->message_count_,
              total_message_count));
}

// ****************************************************************

Sender::Sender (const ACE_INET_Addr &mcast_group)
  :  io_udp_ (&factory_)
  ,  mcast_group_ (mcast_group)
{
}

int
Sender::svc ()
{
  if (this->fragment_.next (&this->io_udp_) != 0)
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error in Fragment::next()\n")));

  if (this->io_udp_.subscribe (this->mcast_group_) != 0)
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error in IO_UDP::subscribe()\n")));

  ACE_Message_Block big_blob (message_size);
  big_blob.wr_ptr (message_size);

  char filler = 0;
  for (char* j = big_blob.rd_ptr (); j != big_blob.wr_ptr (); ++j)
    {
      *j = filler++;
    }

  for (int i = 0; i != total_message_count; ++i)
    {
      ACE_RMCast::Data data;
      data.sequence_number = i;
      data.payload = &big_blob;
      this->fragment_.data (data);
    }
  return 0;
}