summaryrefslogtreecommitdiff
path: root/tests/CLASSIX/CLASSIX_Stream_Test.cpp
blob: 4956339f41c3d0d5c352b4431e551dfccfa9407c (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
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
// 
// = FILENAME
//    Stream_Test.cpp
//
// = DESCRIPTION
//     This is a test of the <IPP_CLASSIX_Addr> class.
//
// = AUTHOR
//    Wei Chiang
// 
// ============================================================================

#include "ace/CLASSIX/CLASSIX_Stream.h"

#include "tests/test_config.h"

#define RCV_DELAY 1000        /* We should NOT wait in ipcReceive */

static char sndBody[]  = "The sea is calm, the tide is full ...\n";
static char rcvAnnex[K_CMSGANNEXSIZE];
static char rcvBody[1000];

int
main (int, char *[])
{
  ACE_START_TEST ("Stream_Test");

  /* ================================================================== */
  ACE_CLASSIX_Port_Core remote_port;

  // Sender's socket
  // Use my default port as the sending address
  ACE_CLASSIX_Stream send(remote_port);

  ACE_CLASSIX_Stream rcv(send.local_sap().get_addr(), 
			 ACE_CLASSIX_Port(remote_port));
  // make the rcv's port be one of the multiple receive ports  
  if (rcv.selectable() < 0)
      ACE_DEBUG((LM_DEBUG, "failed to make the port selectable\n"));

  send.open_writer();

  ACE_DEBUG((LM_DEBUG, "send and block on receive....\n"));
  if (send.send_n(&sndBody[0], sizeof(sndBody)) == sizeof(sndBody))
  {
      // Receiver's Socket
      int rslt = rcv.ipcRecv(rcvBody, 1000);
      if (rslt == sizeof (sndBody))
	  ACE_DEBUG((LM_DEBUG, "received %s\n", rcvBody));
      else
	  ACE_DEBUG((LM_ERROR, "???? Error in ipcReceive():%d\n", rslt));
  }
  else
  {
	  ACE_ERROR((LM_ERROR, "(%t)|%p\n", "???? Error in send_n()\n"));
  }

  ACE_DEBUG((LM_DEBUG, "send, peek then block on receive....\n"));
  if (send.send_n(&sndBody[0], sizeof(sndBody)) == sizeof(sndBody))
  {
      // Receiver's Socket
      //  int rslt = rcv.recv(rcvBody, 1000, MSG_PEEK);
      // Equivalent to rcv.peek()
      int rslt = rcv.peek();
      if (rslt < 0)
	  ACE_DEBUG((LM_ERROR, "???? Error while peeking :%d\n", rslt));
      else
      {
	  char *buf = new char(rslt);
	  if (int n = rcv.recv(buf, rslt) == rslt)
	      ACE_DEBUG((LM_DEBUG, "received %s\n", buf));
	  else
	      ACE_DEBUG((LM_ERROR, "???? Error in ipcReceive(): %d\n", n));
	  delete buf;
      }
  }
  else
  {
	  ACE_DEBUG((LM_ERROR, "???? Error in send_n()\n"));
  }

  ACE_DEBUG((LM_DEBUG, "test recv_n()....\n"));
  if (send.send_n(&sndBody[0], sizeof(sndBody)) == sizeof(sndBody) &&
      send.send_n(&sndBody[0], sizeof(sndBody)) == sizeof(sndBody))
  {
      // Receiver's Socket

      int rslt = rcv.ipcRecv_n(rcvBody, 2*sizeof (sndBody) - 10);
      if (rslt == (2 * sizeof (sndBody) -10))
      {
	  rcvBody[rslt] = '\0';	// For %s printout format
	  ACE_DEBUG((LM_DEBUG, "received %d byte: %s + %s\n", rslt,
		     rcvBody,  rcvBody + sizeof(sndBody)));
      }
      else
	  ACE_DEBUG((LM_ERROR, "???(%P|%t) %p\n", "ipcRecv_n()"));
  }
  else
      ACE_DEBUG((LM_ERROR, "???(%P|%t) %p\n", "ipcRecv_n()"));

  ACE_END_TEST;
  return 0;
}