00001
00002
00003 #include "ace/FoxReactor/FoxReactor.h"
00004
00005 ACE_RCSID(ace, FoxReactor, "$Id: FoxReactor.cpp 85914 2009-07-07 12:59:36Z olli $")
00006
00007 #include "ace/Synch_T.h"
00008
00009 FXDEFMAP(ACE_FoxReactor) ACE_FoxReactorMap[]={
00010 FXMAPFUNCS(SEL_IO_READ,0,MAXKEY,ACE_FoxReactor::onFileEvents),
00011 FXMAPFUNCS(SEL_IO_WRITE,0,MAXKEY,ACE_FoxReactor::onFileEvents),
00012 FXMAPFUNCS(SEL_IO_EXCEPT,0,MAXKEY,ACE_FoxReactor::onFileEvents),
00013 FXMAPFUNCS(SEL_TIMEOUT,0,MAXKEY,ACE_FoxReactor::onTimerEvents),
00014 };
00015
00016 FXIMPLEMENT(ACE_FoxReactor,FXObject,ACE_FoxReactorMap,ARRAYNUMBER(ACE_FoxReactorMap))
00017
00018 ACE_ALLOC_HOOK_DEFINE (ACE_FoxReactor)
00019
00020
00021 ACE_FoxReactor::ACE_FoxReactor (FXApp* a,
00022 size_t size,
00023 bool restart,
00024 ACE_Sig_Handler *h)
00025 : ACE_Select_Reactor (size, restart, h), fxapp(a)
00026 {
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00039 this->notify_handler_->close ();
00040 this->notify_handler_->open (this, 0);
00041 #endif
00042 }
00043
00044 ACE_FoxReactor::~ACE_FoxReactor (void)
00045 {
00046 }
00047
00048 void ACE_FoxReactor::fxapplication(FXApp *a)
00049 {
00050 fxapp=a;
00051 }
00052
00053
00054
00055
00056 int
00057 ACE_FoxReactor::wait_for_multiple_events (ACE_Select_Reactor_Handle_Set &handle_set,
00058 ACE_Time_Value *max_wait_time)
00059 {
00060 ACE_TRACE( "ACE_FoxReactor::wait_for_multiple_events" );
00061
00062 int nfound = 0;
00063 do
00064 {
00065 max_wait_time = this->timer_queue_->calculate_timeout (max_wait_time);
00066 size_t width = this->handler_rep_.max_handlep1 ();
00067 handle_set.rd_mask_ = this->wait_set_.rd_mask_;
00068 handle_set.wr_mask_ = this->wait_set_.wr_mask_;
00069 handle_set.ex_mask_ = this->wait_set_.ex_mask_;
00070
00071 nfound = FoxWaitForMultipleEvents (width,
00072 handle_set,
00073 max_wait_time);
00074
00075 } while( nfound == -1 && this->handle_error () > 0 );
00076
00077 if (nfound > 0)
00078 {
00079 #if !defined (ACE_WIN32)
00080 handle_set.rd_mask_.sync (this->handler_rep_.max_handlep1 ());
00081 handle_set.wr_mask_.sync (this->handler_rep_.max_handlep1 ());
00082 handle_set.ex_mask_.sync (this->handler_rep_.max_handlep1 ());
00083 #endif
00084 }
00085
00086 return nfound;
00087
00088 }
00089
00090 int
00091 ACE_FoxReactor::FoxWaitForMultipleEvents (int width,
00092 ACE_Select_Reactor_Handle_Set &wait_set,
00093 ACE_Time_Value *)
00094 {
00095
00096 ACE_Select_Reactor_Handle_Set temp_set = wait_set;
00097
00098 if (ACE_OS::select (width,
00099 temp_set.rd_mask_,
00100 temp_set.wr_mask_,
00101 temp_set.ex_mask_,
00102 (ACE_Time_Value *) &ACE_Time_Value::zero ) == -1)
00103 return -1;
00104
00105
00106 this->fxapp->runOneEvent () ;
00107
00108
00109 width = handler_rep_.max_handlep1 ();
00110
00111
00112
00113 return ACE_OS::select(width,
00114 wait_set.rd_mask_,
00115 wait_set.wr_mask_,
00116 wait_set.ex_mask_,
00117 (ACE_Time_Value *) &ACE_Time_Value::zero);
00118 }
00119
00120
00121 long ACE_FoxReactor::onFileEvents(FXObject* , FXSelector se, void* handle){
00122 FXSelector sel=FXSELTYPE(se);
00123 ACE_Select_Reactor_Handle_Set dispatch_set;
00124 bool f=false;
00125 if(sel==SEL_IO_READ){
00126 dispatch_set.rd_mask_.set_bit(ACE_HANDLE(reinterpret_cast<FXival>(handle)));
00127 f=true;
00128 }
00129 else
00130 if(sel==SEL_IO_WRITE){
00131 dispatch_set.wr_mask_.set_bit(ACE_HANDLE(reinterpret_cast<FXival>(handle)));
00132 f=true;
00133 }
00134 else
00135 if(sel==SEL_IO_EXCEPT){
00136 dispatch_set.ex_mask_.set_bit(ACE_HANDLE(reinterpret_cast<FXival>(handle)));
00137 f=true;
00138 };
00139 if(f) dispatch (1, dispatch_set);
00140
00141 return 1;
00142 }
00143
00144 long ACE_FoxReactor::onTimerEvents(FXObject* , FXSelector , void* )
00145 {
00146
00147 ACE_Select_Reactor_Handle_Set handle_set;
00148 this->dispatch (0, handle_set );
00149
00150
00151 this->reset_timeout ();
00152
00153 return 0;
00154 }
00155
00156 int
00157 ACE_FoxReactor::register_handler_i (ACE_HANDLE handle,
00158 ACE_Event_Handler *handler,
00159 ACE_Reactor_Mask mask)
00160 {
00161 ACE_TRACE ("ACE_FoxReactor::register_handler_i");
00162
00163 int result = ACE_Select_Reactor::register_handler_i (handle,
00164 handler, mask);
00165 if (result == -1)
00166 return -1;
00167
00168 unsigned long condition = 0;
00169
00170 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::READ_MASK))
00171 ACE_SET_BITS (condition, FX::INPUT_READ);
00172 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::WRITE_MASK))
00173 ACE_SET_BITS (condition, FX::INPUT_WRITE);
00174 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::EXCEPT_MASK))
00175 ACE_SET_BITS (condition, FX::INPUT_EXCEPT);
00176 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::ACCEPT_MASK))
00177 ACE_SET_BITS (condition, FX::INPUT_READ);
00178 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::CONNECT_MASK))
00179 {
00180 ACE_SET_BITS (condition, FX::INPUT_WRITE);
00181 ACE_SET_BITS (condition, FX::INPUT_READ);
00182 }
00183
00184 if (condition != 0)
00185 {
00186 fxapp->addInput(handle,condition,this,0);
00187 }
00188 return 0;
00189 }
00190
00191 int
00192 ACE_FoxReactor::register_handler_i (const ACE_Handle_Set &handles,
00193 ACE_Event_Handler *handler,
00194 ACE_Reactor_Mask mask)
00195 {
00196 return ACE_Select_Reactor::register_handler_i (handles, handler, mask);
00197 }
00198
00199 int
00200 ACE_FoxReactor::remove_handler_i (ACE_HANDLE handle, ACE_Reactor_Mask mask)
00201 {
00202 ACE_TRACE ("ACE_FoxReactor::remove_handler_i");
00203
00204
00205
00206
00207
00208 int condition = 0;
00209
00210 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::READ_MASK))
00211 ACE_SET_BITS (condition, FX::INPUT_READ);
00212 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::WRITE_MASK))
00213 ACE_SET_BITS (condition, FX::INPUT_WRITE);
00214 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::EXCEPT_MASK))
00215 ACE_SET_BITS (condition, FX::INPUT_EXCEPT);
00216 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::ACCEPT_MASK))
00217 ACE_SET_BITS (condition, FX::INPUT_READ);
00218 if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::CONNECT_MASK))
00219 {
00220 ACE_SET_BITS (condition, FX::INPUT_WRITE);
00221 ACE_SET_BITS (condition, FX::INPUT_READ);
00222 }
00223
00224
00225 fxapp->removeInput ((int)handle,condition);
00226
00227
00228 return ACE_Select_Reactor::remove_handler_i (handle, mask);
00229 }
00230
00231 int
00232 ACE_FoxReactor::remove_handler_i (const ACE_Handle_Set &handles,
00233 ACE_Reactor_Mask mask)
00234 {
00235 return ACE_Select_Reactor::remove_handler_i (handles, mask);
00236 }
00237
00238
00239
00240
00241 void
00242 ACE_FoxReactor::reset_timeout (void)
00243 {
00244 ACE_Time_Value *max_wait_time =
00245 this->timer_queue_->calculate_timeout (0);
00246
00247 if (max_wait_time != 0)
00248 {
00249 float t = max_wait_time->sec () +
00250 max_wait_time->usec () / 1000000.0F;
00251 fxapp->addTimeout (this, 0, t*1000);
00252 }
00253 }
00254
00255 int
00256 ACE_FoxReactor::reset_timer_interval
00257 (long timer_id,
00258 const ACE_Time_Value &interval)
00259 {
00260 ACE_TRACE ("ACE_FoxReactor::reset_timer_interval");
00261 ACE_MT (ACE_GUARD_RETURN (ACE_Select_Reactor_Token, ace_mon, this->token_, -1));
00262
00263 int const result =
00264 ACE_Select_Reactor::reset_timer_interval (timer_id,
00265 interval);
00266
00267 if (result == -1)
00268 return -1;
00269 else
00270 {
00271 this->reset_timeout ();
00272 return result;
00273 }
00274 }
00275
00276 long
00277 ACE_FoxReactor::schedule_timer (ACE_Event_Handler *event_handler,
00278 const void *arg,
00279 const ACE_Time_Value &delay,
00280 const ACE_Time_Value &interval)
00281 {
00282 ACE_TRACE ("ACE_FoxReactor::schedule_timer");
00283 ACE_MT (ACE_GUARD_RETURN (ACE_Select_Reactor_Token, ace_mon, this->token_, -1));
00284
00285 long result = ACE_Select_Reactor::schedule_timer (event_handler,
00286 arg,
00287 delay,
00288 interval);
00289 if (result == -1)
00290 return -1;
00291 else
00292 {
00293 this->reset_timeout ();
00294 return result;
00295 }
00296 }
00297
00298 int
00299 ACE_FoxReactor::cancel_timer (ACE_Event_Handler *handler,
00300 int dont_call_handle_close)
00301 {
00302 ACE_TRACE ("ACE_FoxReactor::cancel_timer");
00303
00304 if (ACE_Select_Reactor::cancel_timer (handler,
00305 dont_call_handle_close) == -1)
00306 return -1;
00307 else
00308 {
00309 this->reset_timeout ();
00310 return 0;
00311 }
00312 }
00313
00314 int
00315 ACE_FoxReactor::cancel_timer (long timer_id,
00316 const void **arg,
00317 int dont_call_handle_close)
00318 {
00319 ACE_TRACE ("ACE_FoxReactor::cancel_timer");
00320
00321 if (ACE_Select_Reactor::cancel_timer (timer_id,
00322 arg,
00323 dont_call_handle_close) == -1)
00324 return -1;
00325 else
00326 {
00327 this->reset_timeout ();
00328 return 0;
00329 }
00330 }
00331