Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / Reactor / Proactor / simple_test_proactor.cpp
blob4a1e4d415062a1c8e0bc1b6e0b0e74655d767b61
2 //=============================================================================
3 /**
4 * @file simple_test_proactor.cpp
6 * Very simple version of test_proactor.cpp.
8 * @author Alexander Babu Arulanthu (alex@cs.wustl.edu)
9 */
10 //=============================================================================
13 #include "ace/Service_Config.h"
14 #include "ace/Proactor.h"
15 #include "ace/Asynch_IO.h"
16 #include "ace/Asynch_IO_Impl.h"
17 #include "ace/Message_Block.h"
18 #include "ace/Get_Opt.h"
19 #include "ace/OS_main.h"
22 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
23 // This only works on Win32 platforms and on Unix platforms supporting
24 // POSIX aio calls.
26 static ACE_TCHAR *file = ACE_TEXT("simple_test_proactor.cpp");
27 static ACE_TCHAR *dump_file = ACE_TEXT("simple_output");
29 /**
30 * @class Simple_Tester
32 * @brief Simple_Tester
34 * The class will be created by main(). This class reads a block
35 * from the file and write that to the dump file.
37 class Simple_Tester : public ACE_Handler
39 public:
40 /// Constructor.
41 Simple_Tester ();
43 ~Simple_Tester ();
45 //FUZZ: disable check_for_lack_ACE_OS
46 /// Open the operations and initiate read from the file.
47 ///FUZZ: enble check_for_lack_ACE_OS
48 int open ();
50 protected:
51 // = These methods are called by the freamwork.
53 /// This is called when asynchronous reads from the socket complete.
54 virtual void handle_read_file (const ACE_Asynch_Read_File::Result &result);
56 /// This is called when asynchronous writes from the socket complete.
57 virtual void handle_write_file (const ACE_Asynch_Write_File::Result &result);
59 private:
60 int initiate_read_file ();
62 /// rf (read file): for writing from the file.
63 ACE_Asynch_Read_File rf_;
65 /// ws (write File): for writing to the file.
66 ACE_Asynch_Write_File wf_;
68 /// File to read from.
69 ACE_HANDLE input_file_;
71 /// File for dumping data.
72 ACE_HANDLE dump_file_;
74 // u_long file_offset_;
75 // Current file offset
77 // u_long file_size_;
78 // File size
82 Simple_Tester::Simple_Tester ()
83 : input_file_ (ACE_INVALID_HANDLE),
84 dump_file_ (ACE_INVALID_HANDLE)
88 Simple_Tester::~Simple_Tester ()
90 ACE_OS::close (this->input_file_);
91 ACE_OS::close (this->dump_file_);
95 int
96 Simple_Tester::open ()
98 // Initialize stuff
100 // Open input file (in OVERLAPPED mode)
101 this->input_file_ = ACE_OS::open (file,
102 GENERIC_READ | FILE_FLAG_OVERLAPPED);
103 if (this->input_file_ == ACE_INVALID_HANDLE)
104 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_OS::open"), -1);
106 // Open dump file (in OVERLAPPED mode)
107 this->dump_file_ = ACE_OS::open (dump_file,
108 O_CREAT | O_RDWR | O_TRUNC | FILE_FLAG_OVERLAPPED,
109 0644);
110 if (this->dump_file_ == ACE_INVALID_HANDLE)
111 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_OS::open"), -1);
113 // Open ACE_Asynch_Read_File
114 if (this->rf_.open (*this, this->input_file_) == -1)
115 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Asynch_Read_File::open"), -1);
117 // Open ACE_Asynch_Write_File
118 if (this->wf_.open (*this, this->dump_file_) == -1)
119 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Asynch_Write_File::open"), -1);
121 ACE_DEBUG ((LM_DEBUG,
122 "Simple_Tester::open: Files and Asynch Operations opened successfully\n"));
125 // Start an asynchronous read file
126 if (this->initiate_read_file () == -1)
127 return -1;
129 return 0;
134 Simple_Tester::initiate_read_file ()
136 // Create Message_Block
137 ACE_Message_Block *mb = 0;
138 ACE_NEW_RETURN (mb, ACE_Message_Block (BUFSIZ + 1), -1);
140 // Inititiate an asynchronous read from the file
141 if (this->rf_.read (*mb, mb->size () - 1) == -1)
142 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Asynch_Read_File::read"), -1);
144 ACE_DEBUG ((LM_DEBUG,
145 "Simple_Tester:initiate_read_file: Asynch Read File issued successfully\n"));
147 return 0;
150 void
151 Simple_Tester::handle_read_file (const ACE_Asynch_Read_File::Result &result)
153 ACE_DEBUG ((LM_DEBUG, "handle_read_file called\n"));
155 result.message_block ().rd_ptr ()[result.bytes_transferred ()] = '\0';
157 ACE_DEBUG ((LM_DEBUG, "********************\n"));
158 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_to_read", result.bytes_to_read ()));
159 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "handle", result.handle ()));
160 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_transfered", result.bytes_transferred ()));
161 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "act", (u_long) result.act ()));
162 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "success", result.success ()));
163 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "completion_key", (u_long) result.completion_key ()));
164 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "error", result.error ()));
165 ACE_DEBUG ((LM_DEBUG, "********************\n"));
166 // Watch out if you need to enable this... the ACE_Log_Record::MAXLOGMSGLEN
167 // value controls to max length of a log record, and a large output
168 // buffer may smash it.
169 #if 0
170 ACE_DEBUG ((LM_DEBUG, "%s = %s\n",
171 "message_block",
172 result.message_block ().rd_ptr ()));
173 #endif /* 0 */
175 if (result.success ())
177 // Read successful: write this to the file.
178 if (this->wf_.write (result.message_block (),
179 result.bytes_transferred ()) == -1)
181 ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Asynch_Write_File::write"));
182 return;
187 void
188 Simple_Tester::handle_write_file (const ACE_Asynch_Write_File::Result &result)
190 ACE_DEBUG ((LM_DEBUG, "handle_write_File called\n"));
192 // Reset pointers
193 result.message_block ().rd_ptr (result.message_block ().rd_ptr () - result.bytes_transferred ());
195 result.message_block ().rd_ptr ()[result.bytes_transferred ()] = '\0';
197 ACE_DEBUG ((LM_DEBUG, "********************\n"));
198 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_to_write", result.bytes_to_write ()));
199 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "handle", result.handle ()));
200 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_transfered", result.bytes_transferred ()));
201 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "act", (u_long) result.act ()));
202 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "success", result.success ()));
203 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "completion_key", (u_long) result.completion_key ()));
204 ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "error", result.error ()));
205 ACE_DEBUG ((LM_DEBUG, "********************\n"));
206 // Watch out if you need to enable this... the ACE_Log_Record::MAXLOGMSGLEN
207 // value controls to max length of a log record, and a large output
208 // buffer may smash it.
209 #if 0
210 ACE_DEBUG ((LM_DEBUG, "%s = %s\n",
211 "message_block",
212 result.message_block ().rd_ptr ()));
213 #endif /* 0 */
214 ACE_Proactor::end_event_loop ();
217 static int
218 parse_args (int argc, ACE_TCHAR *argv[])
220 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("f:d:"));
221 int c;
223 while ((c = get_opt ()) != EOF)
224 switch (c)
226 case 'f':
227 file = get_opt.opt_arg ();
228 break;
229 case 'd':
230 dump_file = get_opt.opt_arg ();
231 break;
232 default:
233 ACE_ERROR ((LM_ERROR, "%p.\n",
234 "usage :\n"
235 "-d <dumpfile>\n"
236 "-f <file>\n"));
237 return -1;
240 return 0;
244 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
246 if (parse_args (argc, argv) == -1)
247 return -1;
249 Simple_Tester Simple_Tester;
251 if (Simple_Tester.open () == -1)
252 return -1;
254 int success = 1;
256 // dispatch events
257 success = !(ACE_Proactor::run_event_loop () == -1);
259 return success ? 0 : 1;
262 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */