Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / Reactor / Proactor / test_aiocb.cpp
blobdeb1085a77370a2874cb614a5618eeb465d5b02a
2 //=============================================================================
3 /**
4 * @file test_aiocb.cpp
6 * Checkout $ACE_ROOT/examples/Reactor/Proactor/test_aiocb_ace.cpp,
7 * which is the ACE'ified version of this program.
9 * = COMPILE and RUN
10 * % CC -g -o test_aiocb -lrt test_aiocb.cpp
11 * % ./test_aiocb
13 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
15 //=============================================================================
18 //FUZZ: disable check_for_lack_ACE_OS
19 //FUZZ: disable check_for_improper_main_declaration
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <iostream.h>
31 #include <aio.h>
33 class Test_Aio
35 public:
36 /// Default constructor.
37 Test_Aio ();
39 /// Initting the output file and the buffer.
40 int init ();
42 /// Doing the testing stuff.
43 int do_aio ();
45 /// Destructor.
46 ~Test_Aio ();
47 private:
48 /// Output file descriptor.
49 int out_fd_;
51 /// For writing to the file.
52 struct aiocb *aiocb_write_;
54 /// Reading stuff from the file.
55 struct aiocb *aiocb_read_;
57 /// The buffer to be written to the out_fd.
58 char *buffer_write_;
60 /// The buffer to be read back from the file.
61 char *buffer_read_;
64 Test_Aio::Test_Aio ()
65 : aiocb_write_ (new struct aiocb),
66 aiocb_read_ (new struct aiocb),
67 buffer_write_ (0),
68 buffer_read_ (0)
72 Test_Aio::~Test_Aio ()
74 delete aiocb_write_;
75 delete aiocb_read_;
76 delete buffer_write_;
77 delete [] buffer_read_;
80 // Init the output file and init the buffer.
81 int
82 Test_Aio::init ()
84 // Open the output file.
85 this->out_fd_ = open ("test_aio.log", O_RDWR | O_CREAT | O_TRUNC, 0666);
86 if (this->out_fd_ == 0)
88 cout << "Error : Opening file" << endl;
89 return -1;
92 // Init the buffers.
93 this->buffer_write_ = strdup ("Welcome to the world of AIO... AIO Rules !!!");
94 cout << "The buffer : " << this->buffer_write_ << endl;
95 this->buffer_read_ = new char [strlen (this->buffer_write_) + 1];
96 return 0;
99 // Set the necessary things for the AIO stuff.
100 // Write the buffer asynchly.hmm Disable signals.
101 // Go on aio_suspend. Wait for completion.
102 // Print out the result.
104 Test_Aio::do_aio ()
106 // = Write to the file.
108 // Setup AIOCB.
109 this->aiocb_write_->aio_fildes = this->out_fd_;
110 this->aiocb_write_->aio_offset = 0;
111 this->aiocb_write_->aio_buf = this->buffer_write_;
112 this->aiocb_write_->aio_nbytes = strlen (this->buffer_write_);
113 this->aiocb_write_->aio_reqprio = 0;
114 this->aiocb_write_->aio_sigevent.sigev_notify = SIGEV_NONE;
115 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
116 this->aiocb_write_->aio_sigevent.sigev_value.sival_ptr =
117 (void *) this->aiocb_write_;
119 // Fire off the aio write.
120 if (aio_write (this->aiocb_write_) != 0)
122 perror ("aio_write");
123 return -1;
126 // = Read from that file.
128 // Setup AIOCB.
129 this->aiocb_read_->aio_fildes = this->out_fd_;
130 this->aiocb_read_->aio_offset = 0;
131 this->aiocb_read_->aio_buf = this->buffer_read_;
132 this->aiocb_read_->aio_nbytes = strlen (this->buffer_write_);
133 this->aiocb_read_->aio_reqprio = 0;
134 this->aiocb_read_->aio_sigevent.sigev_notify = SIGEV_NONE;
135 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
136 this->aiocb_read_->aio_sigevent.sigev_value.sival_ptr =
137 (void *) this->aiocb_read_;
139 // Fire off the aio write. If it doesnt get queued, carry on to get
140 // the completion for the first one.
141 if (aio_read (this->aiocb_read_) < 0)
142 perror ("aio_read");
144 // Wait for the completion on aio_suspend.
145 struct aiocb *list_aiocb[2];
146 list_aiocb [0] = this->aiocb_write_;
147 list_aiocb [1] = this->aiocb_read_;
149 // Do suspend till all the aiocbs in the list are done.
150 int done = 0;
151 int return_val = 0;
152 while (!done)
154 return_val = aio_suspend (list_aiocb,
157 cerr << "Return value :" << return_val << endl;
159 // Analyze return and error values.
160 if (list_aiocb[0] != 0)
162 if (aio_error (list_aiocb [0]) != EINPROGRESS)
164 if (aio_return (list_aiocb [0]) == -1)
166 perror ("aio_return");
167 return -1;
169 else
171 // Successful. Store the pointer somewhere and make the
172 // entry NULL in the list.
173 this->aiocb_write_ = list_aiocb [0];
174 list_aiocb [0] = 0;
177 else
178 cout << "AIO write in progress" << endl;
181 if (list_aiocb[1] != 0)
183 if (aio_error (list_aiocb [1]) != EINPROGRESS)
185 int read_return = aio_return (list_aiocb[1]);
186 if (read_return == -1)
188 perror ("aio_return");
189 return -1;
191 else
193 // Successful. Store the pointer somewhere and make the
194 // entry NULL in the list.
195 this->aiocb_read_ = list_aiocb [1];
196 list_aiocb [1] = 0;
197 this->buffer_read_[read_return] = '\0';
200 else
201 cout << "AIO read in progress" << endl;
204 // Is it done?
205 if ((list_aiocb [0] == 0) && (list_aiocb [1] == 0))
206 done = 1;
209 cout << "Both the AIO operations done." << endl;
210 cout << "The buffer is :" << this->buffer_read_ << endl;
212 return 0;
216 main (int argc, char **argv)
218 Test_Aio test_aio;
220 if (test_aio.init () != 0)
222 printf ("AIOCB test failed:\n"
223 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n");
224 return -1;
227 if (test_aio.do_aio () != 0)
229 printf ("AIOCB test failed:\n"
230 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n");
231 return -1;
233 printf ("AIOCB test successful:\n"
234 "ACE_POSIX_AIOCB_PROACTOR should work in this platform\n");
235 return 0;