2 //=============================================================================
4 * @file simple_test_proactor.cpp
6 * Very simple version of test_proactor.cpp.
8 * @author Alexander Babu Arulanthu (alex@cs.wustl.edu)
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
26 static ACE_TCHAR
*file
= ACE_TEXT("simple_test_proactor.cpp");
27 static ACE_TCHAR
*dump_file
= ACE_TEXT("simple_output");
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
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
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
);
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
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_
);
96 Simple_Tester::open ()
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
,
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)
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"));
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.
170 ACE_DEBUG ((LM_DEBUG
, "%s = %s\n",
172 result
.message_block ().rd_ptr ()));
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"));
188 Simple_Tester::handle_write_file (const ACE_Asynch_Write_File::Result
&result
)
190 ACE_DEBUG ((LM_DEBUG
, "handle_write_File called\n"));
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.
210 ACE_DEBUG ((LM_DEBUG
, "%s = %s\n",
212 result
.message_block ().rd_ptr ()));
214 ACE_Proactor::end_event_loop ();
218 parse_args (int argc
, ACE_TCHAR
*argv
[])
220 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("f:d:"));
223 while ((c
= get_opt ()) != EOF
)
227 file
= get_opt
.opt_arg ();
230 dump_file
= get_opt
.opt_arg ();
233 ACE_ERROR ((LM_ERROR
, "%p.\n",
244 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
246 if (parse_args (argc
, argv
) == -1)
249 Simple_Tester Simple_Tester
;
251 if (Simple_Tester
.open () == -1)
257 success
= !(ACE_Proactor::run_event_loop () == -1);
259 return success
? 0 : 1;
262 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */