Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / UPIPE_SAP_Test.cpp
blobb8c420529ebd4f60b2e30ddf0522daec7cdd0b5b
2 //=============================================================================
3 /**
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))
26 // Global pattern
27 static ACE_UPIPE_Addr addr (ACE_TEXT ("pattern"));
29 // connector thread.
31 static void *
32 connector (void *)
34 ACE_UPIPE_Stream c_stream;
36 ACE_OS::sleep (5);
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);
48 mb->copy ("hello");
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.
59 mb->release ();
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)
66 ACE_DEBUG ((LM_DEBUG,
67 ACE_TEXT ("(%t) buffer send from connector failed\n")));
69 char conbuf[BUFSIZ]; // Buffer to receive response.
71 int i = 0;
73 for (char c = ' '; c != '!'; i++)
75 if (c_stream.recv (&c, 1) == -1)
76 ACE_DEBUG ((LM_DEBUG,
77 ACE_TEXT ("(%t) buffer recv from connector failed\n")));
78 else
79 conbuf[i] = c;
82 conbuf[i] = '\0';
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);
86 c_stream.close ();
87 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) exiting thread\n")));
88 return 0;
91 static void *
92 acceptor (void *args)
94 ACE_UPIPE_Acceptor *acceptor = (ACE_UPIPE_Acceptor *) args;
95 ACE_UPIPE_Stream s_stream;
97 if (acceptor->accept (s_stream) == -1)
98 ACE_DEBUG ((LM_DEBUG,
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 ());
109 mb->copy ("thanks");
111 if (s_stream.send (mb) == -1)
112 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) acceptor send failed\n")));
114 char s_buf[BUFSIZ];
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")));
119 else
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")));
129 s_stream.close ();
130 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) exiting thread\n")));
131 return 0;
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),
147 (void *) &acc,
148 THR_NEW_LWP,
149 0) == -1)
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),
154 (void *) 0,
155 THR_NEW_LWP,
156 0) == -1)
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
163 acc.close ();
165 #else
167 #if !defined (ACE_HAS_THREADS)
168 ACE_ERROR ((LM_INFO, ACE_TEXT ("threads not supported on this platform\n")));
169 #else
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) */
174 ACE_END_TEST;
175 return 0;