Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

FlReactor.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: FlReactor.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $
00003 
00004 #include "ace/FlReactor.h"
00005 
00006 ACE_RCSID(ace, FlReactor, "$Id: FlReactor.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $")
00007 
00008 #if defined (ACE_HAS_FL)
00009 
00010 #include "ace/Synch_T.h"
00011 #include /**/ <FL/Fl.h>
00012 
00013 ACE_ALLOC_HOOK_DEFINE (ACE_FlReactor)
00014 
00015 // Must be called with lock held
00016 ACE_FlReactor::ACE_FlReactor (size_t size,
00017                               int restart,
00018                               ACE_Sig_Handler *h)
00019   : ACE_Select_Reactor (size, restart, h)
00020 {
00021   // When the ACE_Select_Reactor is constructed it creates the notify
00022   // pipe and registers it with the register_handler_i() method. The
00023   // FlReactor overloads this method BUT because the
00024   // register_handler_i occurs when constructing the base class
00025   // ACE_Select_Reactor, the ACE_Select_Reactor register_handler_i()
00026   // is called not the FlReactor register_handler_i().  This means
00027   // that the notify pipe is registered with the ACE_Select_Reactor
00028   // event handling code not the FlReactor and so notfications don't
00029   // work.  To get around this we simply close and re-opened the
00030   // notification handler in the constructor of the FlReactor.
00031 
00032 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00033   this->notify_handler_->close ();
00034   this->notify_handler_->open (this, 0);
00035 #endif /* ACE_MT_SAFE */
00036 }
00037 
00038 ACE_FlReactor::~ACE_FlReactor (void)
00039 {
00040 }
00041 
00042 // This is just the <wait_for_multiple_events> from ace/Reactor.cpp
00043 // but we use the Fl functions to wait for an event, not <select>
00044 
00045 int
00046 ACE_FlReactor::wait_for_multiple_events (ACE_Select_Reactor_Handle_Set &handle_set,
00047                                          ACE_Time_Value *max_wait_time)
00048 {
00049   ACE_TRACE ("ACE_FlReactor::wait_for_multiple_events");
00050   int nfound;
00051 
00052   do
00053     {
00054       max_wait_time = this->timer_queue_->calculate_timeout (max_wait_time);
00055 
00056       size_t width = this->handler_rep_.max_handlep1 ();
00057       handle_set.rd_mask_ = this->wait_set_.rd_mask_;
00058       handle_set.wr_mask_ = this->wait_set_.wr_mask_;
00059       handle_set.ex_mask_ = this->wait_set_.ex_mask_;
00060 
00061       // Check to make sure our handle's are all usable.
00062       ACE_Select_Reactor_Handle_Set temp_set = handle_set;
00063 
00064       ACE_Time_Value zero = ACE_Time_Value::zero;
00065       if (ACE_OS::select (width,
00066                           temp_set.rd_mask_,
00067                           temp_set.wr_mask_,
00068                           temp_set.ex_mask_,
00069                           &zero) == -1)
00070         return -1; // Bad file arguments...
00071 
00072       // Instead of waiting using <select>, just use the Fl mechanism
00073       // to wait for one or more events...
00074 
00075       // Wait for something to happen.
00076       double t = 0;
00077       if (max_wait_time != 0)
00078         t = max_wait_time->sec () + max_wait_time->usec () / 1000000.0F;
00079 
00080       while (t > 0) {
00081         t = Fl::wait (t);
00082       }
00083 
00084       // Reset the width, in case it changed during the upcalls.
00085       width = this->handler_rep_.max_handlep1 ();
00086 
00087       // Now actually read the result needed by the <Select_Reactor>
00088       // using <select>.
00089       zero = ACE_Time_Value::zero;
00090       nfound = ACE_OS::select (width,
00091                                handle_set.rd_mask_,
00092                                handle_set.wr_mask_,
00093                                handle_set.ex_mask_,
00094                                &zero);
00095 
00096     } while (nfound == -1 && this->handle_error () > 0);
00097 
00098   if (nfound > 0)
00099     {
00100 #if !defined (ACE_WIN32)
00101       handle_set.rd_mask_.sync (this->handler_rep_.max_handlep1 ());
00102       handle_set.wr_mask_.sync (this->handler_rep_.max_handlep1 ());
00103       handle_set.ex_mask_.sync (this->handler_rep_.max_handlep1 ());
00104 #endif /* ACE_WIN32 */
00105     }
00106   return nfound; // Timed out or input available
00107 }
00108 
00109 void
00110 ACE_FlReactor::fl_io_proc (int fd, void* reactor)
00111 {
00112   ACE_FlReactor *self = ACE_static_cast(ACE_FlReactor *, reactor);
00113   ACE_HANDLE handle = (ACE_HANDLE)fd; //ACE_reinterpret_cast(ACE_HANDLE, fd);
00114 
00115   // my copy isn't const.
00116   ACE_Time_Value zero = ACE_Time_Value::zero;
00117 
00118   ACE_Select_Reactor_Handle_Set wait_set;
00119 
00120   // Deal with one file event.
00121 
00122   // - read which kind of event
00123   if (self->wait_set_.rd_mask_.is_set (handle))
00124     wait_set.rd_mask_.set_bit (handle);
00125   if (self->wait_set_.wr_mask_.is_set (handle))
00126     wait_set.wr_mask_.set_bit (handle);
00127   if (self->wait_set_.ex_mask_.is_set (handle))
00128     wait_set.ex_mask_.set_bit (handle);
00129 
00130   int result = ACE_OS::select (fd + 1,
00131                                wait_set.rd_mask_,
00132                                wait_set.wr_mask_,
00133                                wait_set.ex_mask_, &zero);
00134 
00135   ACE_Select_Reactor_Handle_Set dispatch_set;
00136 
00137   // - Use only that one file event (removes events for other files).
00138   if (result > 0)
00139     {
00140       if (wait_set.rd_mask_.is_set (handle))
00141         dispatch_set.rd_mask_.set_bit (handle);
00142       if (wait_set.wr_mask_.is_set (handle))
00143         dispatch_set.wr_mask_.set_bit (handle);
00144       if (wait_set.ex_mask_.is_set (handle))
00145         dispatch_set.ex_mask_.set_bit (handle);
00146 
00147       self->dispatch (1, dispatch_set);
00148     }
00149 }
00150 
00151 void
00152 ACE_FlReactor::fl_timeout_proc (void* reactor)
00153 {
00154   ACE_FlReactor *self = ACE_static_cast (ACE_FlReactor *, reactor);
00155 
00156   // Deal with any timer events
00157   ACE_Select_Reactor_Handle_Set handle_set;
00158   self->dispatch (0, handle_set);
00159   self->reset_timeout ();
00160 }
00161 
00162 
00163 int
00164 ACE_FlReactor::register_handler_i (ACE_HANDLE handle,
00165                                    ACE_Event_Handler *handler,
00166                                    ACE_Reactor_Mask mask)
00167 {
00168   ACE_TRACE ("ACE_FlReactor::register_handler_i");
00169 
00170   int result = ACE_Select_Reactor::register_handler_i (handle,
00171                                                        handler, mask);
00172   if (result == -1)
00173     return -1;
00174 
00175   int condition = 0;
00176 
00177   if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::READ_MASK))
00178     ACE_SET_BITS (condition, FL_READ);
00179   if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::WRITE_MASK))
00180     ACE_SET_BITS (condition, FL_WRITE);
00181   if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::EXCEPT_MASK))
00182     ACE_SET_BITS (condition, FL_EXCEPT);
00183   if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::ACCEPT_MASK))
00184     ACE_SET_BITS (condition, FL_READ);
00185   if (ACE_BIT_ENABLED (mask, ACE_Event_Handler::CONNECT_MASK))
00186     {
00187       ACE_SET_BITS (condition, FL_WRITE); // connected, you may write
00188       ACE_SET_BITS (condition, FL_READ);  // connected, you have data/err
00189     }
00190 
00191   if (condition != 0)
00192     {
00193       Fl::add_fd ((int)handle, // ACE_reinterpret_cast(int,handle),
00194                   ACE_FlReactor::fl_io_proc,
00195                   this);
00196     }
00197   return 0;
00198 }
00199 
00200 int
00201 ACE_FlReactor::register_handler_i (const ACE_Handle_Set &handles,
00202                                    ACE_Event_Handler *handler,
00203                                    ACE_Reactor_Mask mask)
00204 {
00205   return ACE_Select_Reactor::register_handler_i (handles,
00206                                                  handler,
00207                                                  mask);
00208 }
00209 
00210 int
00211 ACE_FlReactor::remove_handler_i (ACE_HANDLE handle,
00212                                  ACE_Reactor_Mask mask)
00213 {
00214   ACE_TRACE ("ACE_FlReactor::remove_handler_i");
00215 
00216   // In the registration phase we registered first with
00217   // ACE_Select_Reactor and then with X.  Now we are now doing things
00218   // in reverse order.
00219 
00220   // First clean up the corresponding X11Input.
00221   Fl::remove_fd ((int)handle); // ACE_reinterpret_cast(int,handle));
00222 
00223   // Now let the reactor do its work.
00224   return ACE_Select_Reactor::remove_handler_i (handle,
00225                                                mask);
00226 }
00227 
00228 int
00229 ACE_FlReactor::remove_handler_i (const ACE_Handle_Set &handles,
00230                                  ACE_Reactor_Mask mask)
00231 {
00232   return ACE_Select_Reactor::remove_handler_i (handles,
00233                                                mask);
00234 }
00235 
00236 // The following function ensures there's an Fl timeout for the first
00237 // timeout in the Reactor's Timer_Queue.
00238 
00239 void
00240 ACE_FlReactor::reset_timeout (void)
00241 {
00242   ACE_Time_Value *max_wait_time =
00243     this->timer_queue_->calculate_timeout (0);
00244 
00245   if (max_wait_time != 0)
00246     {
00247       float t = max_wait_time->sec ()
00248         + max_wait_time->usec () / 1000000.0F;
00249       Fl::add_timeout (t,
00250                        ACE_FlReactor::fl_timeout_proc,
00251                        this);
00252     }
00253 }
00254 
00255 int
00256 ACE_FlReactor::reset_timer_interval
00257   (long timer_id,
00258    const ACE_Time_Value &interval)
00259 {
00260   ACE_TRACE ("ACE_FlReactor::reset_timer_interval");
00261   ACE_MT (ACE_GUARD_RETURN (ACE_Select_Reactor_Token, ace_mon, this->token_, -1));
00262 
00263   int 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_FlReactor::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_FlReactor::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_FlReactor::cancel_timer (ACE_Event_Handler *handler,
00300                              int dont_call_handle_close)
00301 {
00302   ACE_TRACE ("ACE_FlReactor::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_FlReactor::cancel_timer (long timer_id,
00316                              const void **arg,
00317                              int dont_call_handle_close)
00318 {
00319   ACE_TRACE ("ACE_FlReactor::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 
00332 #endif /* ACE_HAS_FL */

Generated on Mon Jun 16 11:19:43 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002