2 //=============================================================================
4 * @file UPIPE_SAP_Test.cpp
6 * This is a test that uses <ACE_UPIPE_SAP> and <ACE_Thread> for
7 * intra-process communication.
9 * @author Gerhard Lenzer <Gerhard.Lenzer@med.siemens.de>
10 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
11 * @author and Prashant Jain <pjain@cs.wustl.edu>
13 //=============================================================================
16 #include "test_config.h"
17 #include "ace/Stream.h"
18 #include "ace/UPIPE_Acceptor.h"
19 #include "ace/UPIPE_Connector.h"
20 #include "ace/OS_NS_unistd.h"
23 #if defined (ACE_HAS_THREADS) && \
24 (defined (ACE_HAS_STREAM_PIPES) || defined (ACE_HAS_WIN32_NAMED_PIPES))
27 static ACE_UPIPE_Addr
addr (ACE_TEXT ("pattern"));
34 ACE_UPIPE_Stream c_stream
;
38 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) connector starting connect\n")));
39 ACE_UPIPE_Connector con
;
41 if (con
.connect (c_stream
, addr
) == -1)
42 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) connector ACE_UPIPE_Connector failed\n")));
44 ACE_Message_Block
*mb
= 0;
46 ACE_NEW_RETURN (mb
, ACE_Message_Block (sizeof ("hello thanks") * sizeof (char)), 0);
50 if (c_stream
.send (mb
) == -1)
51 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) error connector send\n")));
53 if (c_stream
.recv (mb
) == -1)
54 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) error connector recv\n")));
56 ACE_TEST_ASSERT (ACE_OS::strcmp (mb
->rd_ptr (), "thanks") == 0);
58 // Free up the memory block.
61 // Now try the send()/recv() interface.
62 char mytext
[] = "This string is sent by connector as a buffer";
64 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) connector sending text\n")));
65 if (c_stream
.send (mytext
, sizeof (mytext
)) == -1)
67 ACE_TEXT ("(%t) buffer send from connector failed\n")));
69 char conbuf
[BUFSIZ
]; // Buffer to receive response.
73 for (char c
= ' '; c
!= '!'; i
++)
75 if (c_stream
.recv (&c
, 1) == -1)
77 ACE_TEXT ("(%t) buffer recv from connector failed\n")));
83 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) conbuf = %s\n"), conbuf
));
84 ACE_TEST_ASSERT (ACE_OS::strcmp (conbuf
, "this is the acceptor response!") == 0);
87 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) exiting thread\n")));
94 ACE_UPIPE_Acceptor
*acceptor
= (ACE_UPIPE_Acceptor
*) args
;
95 ACE_UPIPE_Stream s_stream
;
97 if (acceptor
->accept (s_stream
) == -1)
99 ACE_TEXT ("(%t) ACE_UPIPE_Acceptor.accept failed\n")));
101 ACE_Message_Block
*mb
= 0;
103 if (s_stream
.recv (mb
) == -1)
104 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) acceptor recv failed\n")));
106 ACE_TEST_ASSERT (ACE_OS::strcmp (mb
->rd_ptr (), "hello") == 0);
108 mb
->wr_ptr (mb
->rd_ptr ());
111 if (s_stream
.send (mb
) == -1)
112 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) acceptor send failed\n")));
115 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) acceptor sleeping on recv\n")));
117 if (s_stream
.recv (s_buf
, sizeof (s_buf
)) == -1)
118 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) acceptor recv failed\n")));
120 ACE_TEST_ASSERT (ACE_OS::strcmp (s_buf
,
121 "This string is sent by connector as a buffer") == 0);
123 const char svr_response
[] = "this is the acceptor response!";
124 ACE_OS::strcpy (s_buf
, svr_response
);
126 if (s_stream
.send (s_buf
, sizeof (svr_response
)) == -1)
127 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) acceptor send failed\n")));
130 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) exiting thread\n")));
133 #endif /* ACE_HAS_THREADS && (ACE_HAS_STREAM_PIPES || ACE_HAS_WIN32_NAMED_PIPES) */
136 run_main (int, ACE_TCHAR
*[])
138 ACE_START_TEST (ACE_TEXT ("UPIPE_SAP_Test"));
140 #if defined (ACE_HAS_THREADS) && \
141 (defined (ACE_HAS_STREAM_PIPES) || defined (ACE_HAS_WIN32_NAMED_PIPES))
143 ACE_UPIPE_Acceptor
acc (addr
);
145 // Spawn a acceptor thread.
146 if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (acceptor
),
150 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1);
152 // Spawn a connector thread.
153 if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (connector
),
157 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1);
159 ACE_Thread_Manager::instance ()->wait ();
160 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) joined with acceptor thread\n")));
162 // Close the acceptor
167 #if !defined (ACE_HAS_THREADS)
168 ACE_ERROR ((LM_INFO
, ACE_TEXT ("threads not supported on this platform\n")));
170 ACE_ERROR ((LM_INFO
, ACE_TEXT ("UPIPE is not supported on this platform\n")));
171 #endif /* !defined (ACE_HAS_THREADS) */
172 #endif /* ACE_HAS_THREADS && (ACE_HAS_STREAM_PIPES || ACE_HAS_WIN32_NAMED_PIPES) */