Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / protocols / examples / TMCast / Member / member.cpp
blob59c2b0a5c9e3f5f3324d0c4c769ae7802727486c
1 // file : TMCast/Member/member.cpp
2 // author : Boris Kolpackov <boris@dre.vanderbilt.edu>
3 #include "ace/Log_Msg.h"
4 #include "ace/Time_Value.h"
5 #include "ace/OS_NS_stdlib.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_time.h"
9 #include "ace/OS_NS_string.h"
11 #include "ace/TMCast/Group.hpp"
13 class Args {};
15 int
16 ACE_TMAIN (int argc, ACE_TCHAR* argv[])
18 int status = 0;
19 try
21 if (argc < 4) throw Args ();
23 bool receiver (true);
25 if (argv[1][0] == 'r') receiver = true;
26 else if (argv[1][0] == 's') receiver = false;
27 else throw Args ();
29 if (!receiver) ACE_OS::srand ((u_int) ACE_OS::time ());
31 ACE_INET_Addr address (argv[3]);
33 ACE_TMCast::Group group (address, ACE_TEXT_ALWAYS_CHAR (argv[2]));
35 if (receiver)
37 for (char buffer[256];;)
39 group.recv (buffer, sizeof (buffer));
41 ACE_DEBUG ((LM_DEBUG, "%s\n", buffer));
44 else
46 char buffer[256];
48 for (unsigned long i = 0; i < 1000; i++)
50 // Sleep some random time around 1 sec.
52 ACE_UINT64 tmpl = 1000000U;
53 unsigned long t =
54 static_cast<unsigned long> (((tmpl * ACE_OS::rand ()) / RAND_MAX));
56 // ACE_DEBUG ((LM_DEBUG, "sleeping for %u\n", t));
58 ACE_OS::sleep (ACE_Time_Value (0, t));
60 ACE_OS::sprintf (buffer, "message # %lu", i);
62 try
64 group.send (buffer, ACE_OS::strlen (buffer) + 1);
66 catch (ACE_TMCast::Group::Aborted const&)
68 ACE_ERROR ((LM_ERROR, "%s has been aborted\n", buffer));
73 catch (Args const&)
75 ACE_ERROR ((LM_ERROR,
76 "Usage: member {r|s} <id> <IPv4 mcast address>:<port>\n"));
77 status++;
79 catch (ACE_TMCast::Group::Failed const&)
81 ACE_ERROR ((LM_ERROR,
82 "Group failure. Perhaps I am alone in the group.\n"));
83 status++;
85 catch (ACE_TMCast::Group::InsufficienSpace const&)
87 ACE_ERROR ((LM_ERROR, "Insufficient space in receive buffer.\n"));
88 status++;
90 return status;