2 //=============================================================================
6 * Example for using <ACE_UPIPE_SAP> and <ACE_Thread> for
7 * intra-process communication.
9 * @author Gerhard Lenzer and Douglas C. Schmidt
11 //=============================================================================
14 #include "ace/OS_main.h"
15 #include "ace/OS_NS_string.h"
16 #include "ace/Stream.h"
17 #include "ace/UPIPE_Acceptor.h"
18 #include "ace/UPIPE_Connector.h"
21 #if defined (ACE_HAS_THREADS)
24 static ACE_UPIPE_Addr
addr (ACE_TEXT("pattern"));
26 // peer1 thread entry point.
31 ACE_UPIPE_Stream c_stream
;
34 "(%t) peer1 starting connect\n"));
35 ACE_UPIPE_Connector con
;
37 if (con
.connect (c_stream
, addr
) == -1)
39 "(%t) peer1 ACE_UPIPE_Connector failed\n"));
41 ACE_Message_Block
*mb
= 0;
43 ACE_Message_Block (20),
46 mb
->copy ("hello", 6);
48 if (c_stream
.send (mb
) == -1)
50 "(%t) error peer1 send\n"));
52 if (c_stream
.recv (mb
) == -1)
54 "(%t) error peer1 recv\n"));
57 "(%t) peer1 ack is \"%s\"\n",
60 // Free up the memory block.
63 // Now try the send()/recv() interface.
64 char mytext
[] = "This string is sent by peer1 as buffer";
67 "(%t) peer1 sending text\n"));
68 if (c_stream
.send (mytext
, sizeof mytext
) == -1)
70 "(%t) buffer send from peer1 failed\n"));
72 char conbuf
[30]; // Buffer to receive response.
76 for (char c
= ' '; c
!= '!'; i
++)
78 if (c_stream
.recv (&c
, 1) == -1)
80 "(%t) buffer recv from peer1 failed\n"));
87 "(%t) peer1 received buffer with \"%s\"\n",
96 ACE_UPIPE_Acceptor
acc (addr
);
97 ACE_UPIPE_Stream s_stream
;
99 // Spawn a peer1 thread.
100 if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (peer1
),
102 THR_NEW_LWP
| THR_DETACHED
) == -1)
103 ACE_ERROR_RETURN ((LM_ERROR
,
108 ACE_DEBUG ((LM_DEBUG
,
109 "(%t) peer2 starting accept\n"));
111 if (acc
.accept (s_stream
) == -1)
112 ACE_ERROR ((LM_ERROR
,
113 "(%t) ACE_UPIPE_Acceptor.accept failed\n"));
115 ACE_Message_Block
*mb
= 0;
117 if (s_stream
.recv (mb
) == -1)
118 ACE_ERROR ((LM_ERROR
,
119 "(%t) peer2 recv failed\n"));
121 ACE_DEBUG ((LM_DEBUG
, "(%t) peer2 recv is \"%s\"\n",
124 mb
->wr_ptr (mb
->rd_ptr ());
125 mb
->copy ("thanks", 7);
127 if (s_stream
.send (mb
) == -1)
128 ACE_ERROR ((LM_ERROR
,
129 "(%t) peer2 send failed\n"));
132 ACE_DEBUG ((LM_DEBUG
,
133 "(%t) peer2 sleeping on recv\n"));
135 if (s_stream
.recv (s_buf
, sizeof s_buf
) == -1)
136 ACE_ERROR ((LM_ERROR
,
137 "(%t) peer2 recv failed\n"));
139 ACE_DEBUG ((LM_DEBUG
,
140 "(%t) peer2 received buffer with \"%s\"\n",
143 ACE_OS::strcpy (s_buf
,
144 "this is the peer2 response!");
146 if (s_stream
.send (s_buf
, 30) == -1)
147 ACE_ERROR ((LM_ERROR
,
148 "(%t) peer2 send failed\n"));
154 ACE_TMAIN (int, ACE_TCHAR
*[])
156 // Spawn a peer2 thread.
157 if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (peer2
),
159 THR_NEW_LWP
| THR_DETACHED
) == -1)
160 ACE_ERROR_RETURN ((LM_ERROR
,
164 // Wait for peer2 and peer1 threads to exit.
165 ACE_Thread_Manager::instance ()->wait ();
170 ACE_TMAIN (int, ACE_TCHAR
*[])
172 ACE_ERROR_RETURN ((LM_ERROR
,
173 "threads not supported on this platform\n"),
176 #endif /* ACE_HAS_THREADS */