コード例 #1
0
ファイル: QtReactor_Test.cpp プロジェクト: DOCGroup/ACE_TAO
int DgramHandler::handle_timeout (const ACE_Time_Value &current_time, const void *act)
{
  ACE_UNUSED_ARG (current_time);
  ACE_UNUSED_ARG (act);
  int sendBuffer = 0;

  if (++timeoutsTriggered_ >= expectedTriggers_)
    reactor ()->cancel_timer (this, 1);

  ACE_SOCK_Dgram socket;
  if (-1 == socket.open (ACE_INET_Addr (static_cast< u_short > (0),
                                        static_cast< ACE_UINT32 > (INADDR_ANY)),
                                        ACE_PROTOCOL_FAMILY_INET, 0, 1))
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT (" (%P) %p\n"),
                ACE_TEXT ("Cannot open socket for sending Qt dgrams")));

  ACE_INET_Addr peerAddr;
  peer_.get_local_addr (peerAddr);

  if (sizeof (sendBuffer) != socket.send (&sendBuffer,
                                          sizeof (sendBuffer), peerAddr))
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT (" (%P) %p\n"),
                ACE_TEXT ("Cannot send dgram")));
  else
    ++dgramsSent_;

  socket.close ();

  return 0;
}
コード例 #2
0
ファイル: Multicast_Test.cpp プロジェクト: CCJY/ACE
int send_dgram (ACE_SOCK_Dgram &socket, ACE_INET_Addr addr, int done = 0)
{

  // Send each message twice, once to the right port, and once to the "wrong"
  // port.  This helps generate noise and lets us see if port filtering is
  // working properly.
  const char *address = addr.get_host_addr ();
  int port = addr.get_port_number ();

  for (int i = 0; i < 2; ++i)
    {
      char buf[MAX_STRING_SIZE];
      if (done)
        buf[0] = 0;
      else
        ACE_OS::sprintf (buf, "%s/%d", address, port);

      if (socket.send (buf, ACE_OS::strlen (buf),addr) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Send to %C, %p\n"),
                           address,
                           ACE_TEXT ("send_dgram - error calling send on ")
                           ACE_TEXT ("ACE_SOCK_Dgram.")), -1);
      addr.set_port_number (++port);
    }
  return 0;
}
コード例 #3
0
// Listing 2 code/ch09
void echo_dgram (void)
{
  ACE_INET_Addr my_addr (ACE_static_cast (u_short, 10102));
  ACE_INET_Addr your_addr;
  ACE_SOCK_Dgram udp (my_addr);
  char buff[BUFSIZ];
  size_t buflen = sizeof (buff);
  ssize_t recv_cnt = udp.recv (buff, buflen, your_addr);
  if (recv_cnt > 0)
    udp.send (buff, ACE_static_cast (size_t, buflen), your_addr);
  udp.close ();
  return;
}
コード例 #4
0
int send_unicast (const ACE_INET_Addr &to)
{
  const char *message = "this is the message!\n";
  ACE_INET_Addr my_addr (ACE_static_cast (u_short, 10101));
  ACE_SOCK_Dgram udp (my_addr);
  ssize_t sent = udp.send (message,
                           ACE_OS_String::strlen (message) + 1,
                           to);
  udp.close ();
  if (sent == -1)
    ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
                       ACE_TEXT ("send")), -1);
  return 0;
}
コード例 #5
0
/* Our goal here is to develop a client that can send a datagram to a
   server running on a known host.  We'll use a command-line argument
   to specify the hostname instead of hard-coding it.  */
int
main (int argc,char *argv[])
{
  /* All datagrams must have a point of origin.  Since we intend to
    transmit instead of receive, we initialize an address with zero
    and let the OS choose a port for us.  We could have chosen our own
    value between 1025 and 65535 as long as it isn't already in use.

    The biggest difference between client and server when datagrams
    are used is the fact that servers tend to have a known/fixed
    address at which they listen and clients tend to have arbitrary
    addresses assigned by the OS.  */
  ACE_INET_Addr local((u_short) 0);

  /* And here is our datagram object.  */
  ACE_SOCK_Dgram dgram;

  /* Notice that this looks a lot like the server application.
    There's no difference in creating server datagrams an client
    datagrams.  You can even use a zero-constructed address for your
    server datagram as long as you tell the client where you're
    listening (eg -- by writting into a file or some such).  */
  if (dgram.open (local) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "datagram open"),
                      -1);

  /* Yep.  We've seen this before too...  */
  char buf[BUFSIZ];

  /* Ok, now we're doing something different.  */
  sprintf (buf, "Hello World!");

  /* Just like sending a telegram, we have to address our datagram.
    Here, we create an address object at the desired port on the
    chosen host.  To keep us from crashing, we'll provide a default
    host name if we aren't given one.  */
  ACE_INET_Addr remote (PORT,
                        argc > 1 ? argv[1] : "localhost");

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Sending (%s) to the server.\n",
              buf));
  /* Now we send our buffer of stuff to the remote address.  This is
    just exactly what the server did after receiving a client message.
    Datagrams are rather orthogonal that way: they don't generally
    make much of a fuss about being either client or server.  */
  if (dgram.send (buf,
                  ACE_OS::strlen (buf) + 1,
                  remote) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "send"),
                      -1);

  /* Now we've turned around and put ourselves into "server mode" by
    invoking the recv() method.  We know our server is going to send
    us something, so we hang out here and wait for it.  Because we
    know datagrams are unreliable, there is a chance that the server
    will respond but we won't hear.  You might consider providing a
    timeout on the recv() in that case.  If recv() fails due to
    timeout it will return -1 and you can then resend your query and
    attempt the recv() again.

    Like the server application, we have to give the recv() an
    uninitialized addr object so that we can find out who is talking
    back to us.  */
  if (dgram.recv (buf,
                  sizeof (buf),
                  remote) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "recv"),
                      -1);

  /* Find out what the server had to say.  */
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) The server said:  %s\n",
              buf));

  /* Using the "remote" object instance, find out where the server
    lives.  We could then save this address and use directed datagrams
    to chat with the server for a while.  */
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) The server can be found at:  (%s:%d)\n",
              remote.get_host_name(),
              PORT));

  return 0;
}
コード例 #6
0
ファイル: RW_Process_Mutex_Test.cpp プロジェクト: jiaoyk/ATCD
static void
writer (void)
{
    ACE_RW_Process_Mutex mutex (mutex_name.c_str ());

    // Make sure the constructor succeeded
    if (ACE_LOG_MSG->op_status () != 0)
    {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer, mutex %s %p\n"),
                    mutex_name.c_str (),
                    ACE_TEXT ("ctor")));
        return;
    }

    ACE_SOCK_Dgram sock;
    ACE_INET_Addr parent;
    parent.set (reporting_port, ACE_LOCALHOST, 1, AF_INET);
    ACE_TCHAR me_str[80];
    parent.addr_to_string (me_str, 80);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sending reports to %s\n"), me_str));
    if (sock.open (ACE_Addr::sap_any, PF_INET) == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("UDP open")));

    Range_Report report;
    report.child_ = 0;   // We're the writer
    ACE_Time_Value start (ACE_Time_Value::zero), stop (ACE_Time_Value::zero);

    // Grab the lock
    if (-1 == mutex.acquire_write ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer first %p\n"),
                    ACE_TEXT ("acquire_write")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer acquired first time\n")));
    }

    // Now sleep, making the readers wait for the lock. Then release the lock,
    // sleep, and reacquire the lock.
    ACE_OS::sleep (2);
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer %p\n"),
                    ACE_TEXT ("first release")));
    else
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Writer released first time\n")));

    report.range_.set (start, stop);
    ssize_t bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer sent %b byte report\n"), bytes));

    ACE_OS::sleep (1);   // Ensure we don't immediately grab the lock back

    start = stop = ACE_Time_Value::zero;

    if (-1 == mutex.acquire_write ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer second %p\n"),
                    ACE_TEXT ("acquire_write")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer acquired second time\n")));
    }

    ACE_OS::sleep (2);
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer %p\n"),
                    ACE_TEXT ("second release")));
    else
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Writer released second time\n")));
    report.range_.set (start, stop);
    bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer sent %b byte report\n"), bytes));
    sock.close ();
    return;
}
コード例 #7
0
ファイル: RW_Process_Mutex_Test.cpp プロジェクト: jiaoyk/ATCD
/*
 * The set of readers and the writer will operate in a staggered sequence
 * of acquiring and releasing the lock. The sequence is designed to exercise
 * waiting behavior of both readers and writer, as well as allowing multiple
 * readers in, without getting tripped up by any differences in ordering
 * on different platforms which may favor writers, or vice-versa.
 * In this timeline, time on seconds is on the left, time holding the lock
 * is solid, time waiting is dots, acquire/release point is a dash, and
 * time without the lock is blank.
 *
 *   TIME        WRITER    READER1    READER2    READER3
 *     0            |
 *                  |
 *     1            |         .
 *                  |         .
 *     2            -         -          -
 *                            |          |
 *     3                      |          |          -
 *                            |          |          |
 *     4                      -          |          |
 *                                       |          |
 *     5                                 -          |
 *                                                  |
 *     6            -                               -
 *                  |
 *     7            |         .          .          .
 *                  |         .          .          .
 *     8            -         -          -          -
 *                            |          |          |
 *     9                      |          |          |
 *
 * A file is used to test the sequencing. When the writer first gets the
 * lock, it will ensure the file is not present. At the end of its time
 * holding the lock the first time, it will write a "writer 1" string to
 * the file. When it gets the lock the second time, it will write a
 * different string to the file, and just before releasing the second time
 * write a "writer 2" string to the file. The readers all check to be sure
 * that the file is present and says "writer 1" at the start and end of
 * their periods of holding the reader lock and, similarly, check for
 * "writer 2" the second time they hold the lock.
 */
static void
reader (int num)
{
    // Let the writer get there first.
    ACE_OS::sleep (1);

    ACE_SOCK_Dgram sock;
    ACE_INET_Addr parent;
    parent.set (reporting_port, ACE_LOCALHOST, 1, AF_INET);
    ACE_TCHAR me_str[80];
    parent.addr_to_string (me_str, 80);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sending reports to %s\n"), me_str));

    if (sock.open (ACE_Addr::sap_any, PF_INET) == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("UDP open")));

    Range_Report report;
    report.child_ = num;
    ACE_Time_Value start (ACE_Time_Value::zero), stop (ACE_Time_Value::zero);

    ACE_RW_Process_Mutex mutex (mutex_name.c_str ());

    // Make sure the constructor succeeded
    if (ACE_LOG_MSG->op_status () != 0)
    {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d, mutex %s %p\n"),
                    num,
                    mutex_name.c_str (),
                    ACE_TEXT ("ctor")));
        return;
    }

    ACE_OS::sleep (num);
    // Grab the lock
    if (-1 == mutex.acquire_read ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("first acquire_read")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Reader %d acquired first time\n"),
                    num));
    }

    // Wait a bit, then release and report the range held.
    ACE_OS::sleep (num);

    // Release the lock then wait; in the interim, the writer should change
    // the file.
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("first release")));
    else
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reader %d released first time\n"), num));
    report.range_.set (start, stop);
    ssize_t bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("Reader %d sent %b byte report\n"),
                num,
                bytes));

    ACE_OS::sleep (4 - num);
    start = stop = ACE_Time_Value::zero;
    if (-1 == mutex.acquire_read ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("second acquire_read")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Reader %d acquired second time\n"),
                    num));
    }

    // Done; small delay, release, report, and return.
    ACE_OS::sleep (1);
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("second release")));
    else
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Reader %d released second time; done\n"),
                    num));
    report.range_.set (start, stop);
    bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("Reader %d sent %b byte report\n"),
                num,
                bytes));
    sock.close ();
    return;
}