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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// SPIPE_Test.cpp
//
// = DESCRIPTION
// This is a simple test of Named Pipes that uses
// ACE_SPIPE_Acceptor and ACE_SPIPE_Connector classes. The test
// forks two processes or spawns two threads (depending upon the
// platform) and then executes the client and server allowing
// them to use the named pipe to exchange data. No user input is
// required as far as command line arguments are concerned.
//
// = AUTHOR
// Prashant Jain
//
// ============================================================================
#include "ace/OS.h"
#include "ace/Thread.h"
#include "ace/Thread_Manager.h"
#include "ace/SPIPE_Addr.h"
#include "ace/SPIPE_Connector.h"
#include "ace/SPIPE_Acceptor.h"
#include "test_config.h"
// pipe name to use
static const char *PIPE_NAME = "ace_pipe_name";
static void *
client (void *)
{
#if defined (ACE_WIN32)
ACE_NEW_THREAD;
#endif
const char *rendezvous = PIPE_NAME;
ACE_SPIPE_Stream cli_stream;
ACE_SPIPE_Connector con;
ACE_OS::sleep (10);
if (con.connect (cli_stream, ACE_SPIPE_Addr (ACE_WIDE_STRING (rendezvous))) == -1)
ACE_ERROR ((LM_ERROR, "%p\n", rendezvous));
for (char *c = ACE_ALPHABET; *c != '\0'; c++)
if (cli_stream.send (c, 1) == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "send_n"));
if (cli_stream.close () == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "close"));
#if !defined (ACE_WIN32)
ACE_OS::exit (0);
#endif
return 0;
}
static void *
server (void *)
{
#if defined (ACE_WIN32)
ACE_NEW_THREAD;
#endif
ACE_SPIPE_Acceptor acceptor;
ACE_SPIPE_Stream new_stream;
char buf[BUFSIZ];
char *t = ACE_ALPHABET;
const char *rendezvous = PIPE_NAME;
// Initialize named pipe listener.
if (acceptor.open (ACE_SPIPE_Addr (ACE_WIDE_STRING (rendezvous))) == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "open"));
ACE_DEBUG ((LM_DEBUG, "waiting for connection\n"));
// Accept a client connection
if (acceptor.accept (new_stream, 0) == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "accept"));
ACE_DEBUG ((LM_DEBUG, "Accepted connection\n"));
while (new_stream.recv (buf, 1) > 0)
{
ACE_ASSERT (*t == buf[0]);
t++;
}
ACE_DEBUG ((LM_DEBUG, "End of connection. Closing handle\n"));
new_stream.close ();
return 0;
}
int
main (int, char *[])
{
ACE_START_TEST ("SPIPE_Test");
#if defined (ACE_HAS_STREAM_PIPES) || defined (ACE_WIN32)
#if !defined (ACE_LACKS_EXEC)
switch (ACE_OS::fork ())
{
case -1:
ACE_ERROR ((LM_ERROR, "%p\n%a", "fork failed"));
exit (-1);
case 0:
client (0);
default:
server (0);
}
#elif defined (ACE_HAS_THREADS)
if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (client),
(void *) 0,
THR_NEW_LWP | THR_DETACHED) == -1)
ACE_ERROR ((LM_ERROR, "%p\n%a", "thread create failed"));
if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (server),
(void *) 0,
THR_NEW_LWP | THR_DETACHED) == -1)
ACE_ERROR ((LM_ERROR, "%p\n%a", "thread create failed"));
ACE_Thread_Manager::instance ()->wait ();
#endif /* !ACE_LACKS_EXEC */
#else
ACE_UNUSED_ARG (client);
ACE_UNUSED_ARG (server);
ACE_DEBUG ((LM_DEBUG,
"SPIPE is not supported on this platform\n"));
#endif /* ACE_HAS_STREAM_PIPES || ACE_WIN32 */
ACE_END_TEST;
return 0;
}
|