Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / Reactor / Proactor / test_aiocb_ace.cpp
blob1d9ab41ea23ec2864cbe73a7dfd993b312a5fb1e
2 //=============================================================================
3 /**
4 * @file test_aiocb_ace.cpp
6 * This program helps you to test the <aio_*> calls on a
7 * platform.
9 * Before running this test, make sure the platform can
10 * support POSIX <aio_> calls, using
11 * ACE_ROOT/tests/Aio_Platform_Test.
13 * This program tests the AIOCB (AIO Control Blocks) based
14 * completion approach which uses <aio_suspend> for completion
15 * querying.
17 * If this test is successful, ACE_POSIX_AIOCB_PROACTOR
18 * can be used on this platform.
20 * = COMPILE and RUN
21 * % make
22 * % ./test_aiocb_ace
24 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
26 //=============================================================================
29 #include "ace/ACE.h"
30 #include "ace/Log_Msg.h"
31 #include "ace/os_include/os_aio.h"
32 #include "ace/OS_NS_string.h"
34 class Test_Aio
36 public:
37 /// Default constructor.
38 Test_Aio ();
40 /// Initting the output file and the buffer.
41 int init ();
43 /// Doing the testing stuff.
44 int do_aio ();
46 /// Destructor.
47 ~Test_Aio ();
48 private:
49 /// Output file descriptor.
50 int out_fd_;
52 /// For writing to the file.
53 struct aiocb *aiocb_write_;
55 /// Reading stuff from the file.
56 struct aiocb *aiocb_read_;
58 /// The buffer to be written to the out_fd.
59 char *buffer_write_;
61 /// The buffer to be read back from the file.
62 char *buffer_read_;
65 Test_Aio::Test_Aio ()
66 : aiocb_write_ (0),
67 aiocb_read_ (0),
68 buffer_write_ (0),
69 buffer_read_ (0)
71 ACE_NEW (this->aiocb_write_,
72 struct aiocb);
73 ACE_NEW (this->aiocb_read_,
74 struct aiocb);
77 Test_Aio::~Test_Aio ()
79 delete aiocb_write_;
80 delete aiocb_read_;
81 delete buffer_write_;
82 delete buffer_read_;
85 // Init the output file and init the buffer.
86 int
87 Test_Aio::init ()
89 // Open the output file.
90 this->out_fd_ = ACE_OS::open ("test_aio.log",
91 O_RDWR | O_CREAT | O_TRUNC,
92 0666);
93 if (this->out_fd_ == 0)
94 ACE_ERROR_RETURN ((LM_ERROR,
95 "Error: Opening file\n"),
96 -1);
98 // Init the buffers.
99 this->buffer_write_ = ACE::strnew ("Welcome to the world of AIO... AIO Rules !!!");
100 ACE_DEBUG ((LM_DEBUG,
101 "The buffer : %s\n",
102 this->buffer_write_));
104 // Allocate memory for the read buffer.
105 ACE_NEW_RETURN (this->buffer_read_,
106 char [ACE_OS::strlen (this->buffer_write_)],
107 -1);
109 return 0;
112 // Set the necessary things for the AIO stuff.
113 // Write the buffer asynchly.hmm Disable signals.
114 // Go on aio_suspend. Wait for completion.
115 // Print out the result.
117 Test_Aio::do_aio ()
119 // = Write to the file.
121 // Setup AIOCB.
122 this->aiocb_write_->aio_fildes = this->out_fd_;
123 this->aiocb_write_->aio_offset = 0;
124 this->aiocb_write_->aio_buf = this->buffer_write_;
125 this->aiocb_write_->aio_nbytes = ACE_OS::strlen (this->buffer_write_);
126 this->aiocb_write_->aio_reqprio = 0;
127 this->aiocb_write_->aio_sigevent.sigev_notify = SIGEV_NONE;
128 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
129 this->aiocb_write_->aio_sigevent.sigev_value.sival_ptr =
130 (void *) this->aiocb_write_;
132 // Fire off the aio write.
133 if (aio_write (this->aiocb_write_) != 0)
134 ACE_ERROR_RETURN ((LM_ERROR,
135 "%p\n",
136 "aio_write"),
137 -1);
139 // = Read from that file.
141 // Setup AIOCB.
142 this->aiocb_read_->aio_fildes = this->out_fd_;
143 this->aiocb_read_->aio_offset = 0;
144 this->aiocb_read_->aio_buf = this->buffer_read_;
145 this->aiocb_read_->aio_nbytes = ACE_OS::strlen (this->buffer_write_);
146 this->aiocb_read_->aio_reqprio = 0;
147 this->aiocb_read_->aio_sigevent.sigev_notify = SIGEV_NONE;
148 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
149 this->aiocb_read_->aio_sigevent.sigev_value.sival_ptr =
150 (void *) this->aiocb_read_;
152 // Fire off the aio write. If it doesnt get queued, carry on to get
153 // the completion for the first one.
154 if (aio_read (this->aiocb_read_) < 0)
155 ACE_ERROR_RETURN ((LM_ERROR,
156 "%p\n",
157 "aio_read"),
158 -1);
160 // Wait for the completion on aio_suspend.
161 struct aiocb *list_aiocb[2];
162 list_aiocb [0] = this->aiocb_write_;
163 list_aiocb [1] = this->aiocb_read_;
165 // Do suspend till all the aiocbs in the list are done.
166 int to_finish = 2;
167 int return_val = 0;
168 while (to_finish > 0)
170 return_val = aio_suspend (list_aiocb,
171 to_finish,
173 ACE_DEBUG ((LM_DEBUG,
174 "Result of <aio_suspend> : %d\n",
175 return_val));
177 // Analyze return and error values.
178 if (to_finish > 1)
180 if (aio_error (list_aiocb [1]) != EINPROGRESS)
182 if (aio_return (list_aiocb [1]) == -1)
183 ACE_ERROR_RETURN ((LM_ERROR,
184 "%p\n",
185 "aio_return, item 1"),
186 -1);
187 else
189 // Successful. Remember we have one less thing to finish.
190 --to_finish;
191 list_aiocb [1] = 0;
194 else
195 ACE_DEBUG ((LM_DEBUG,
196 "aio_error says aio 1 is in progress\n"));
199 if (aio_error (list_aiocb [0]) != EINPROGRESS)
201 if (aio_return (list_aiocb [0]) == -1)
202 ACE_ERROR_RETURN ((LM_ERROR,
203 "%p\n",
204 "aio_return, item 0"),
205 -1);
206 else
208 // Successful. Store the pointer somewhere and bump the
209 // read entry up to the front, if it is still not done.
210 --to_finish;
211 list_aiocb [0] = this->aiocb_read_;
214 else
215 ACE_DEBUG ((LM_DEBUG,
216 "aio_error says aio 0 is in progress\n"));
219 ACE_DEBUG ((LM_DEBUG,
220 "Both the AIO operations done.\n"
221 "The buffer is : %s\n",
222 this->buffer_read_));
224 return 0;
228 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
230 ACE_UNUSED_ARG (argc);
231 ACE_UNUSED_ARG (argv);
233 Test_Aio test_aio;
235 if (test_aio.init () != 0)
236 ACE_ERROR_RETURN ((LM_ERROR,
237 "AIOCB test failed:\n"
238 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n"),
239 -1);
241 if (test_aio.do_aio () != 0)
242 ACE_ERROR_RETURN ((LM_ERROR,
243 "AIOCB test failed:\n"
244 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n"),
245 -1);
247 ACE_DEBUG ((LM_DEBUG,
248 "AIOCB test successful:\n"
249 "ACE_POSIX_AIOCB_PROACTOR should work in this platform\n"));
251 return 0;