vpr::SelectorImplSIM Class Reference

Implementation of a selector for simulated sockets. More...

#include <vpr/IO/Selector.h>

Inheritance diagram for vpr::SelectorImplSIM:

Inheritance graph
[legend]
Collaboration diagram for vpr::SelectorImplSIM:

Collaboration graph
[legend]
List of all members.

Public Member Functions

bool addHandle (vpr::IOSys::Handle handle, vpr::Uint16 mask=0)
 Adds the given handle to the selector.
bool removeHandle (vpr::IOSys::Handle handle)
 Removes a handle from the selector.
bool setIn (vpr::IOSys::Handle handle, vpr::Uint16 mask)
 Sets the event flags going in to the select to mask.
vpr::Uint16 getIn (vpr::IOSys::Handle handle)
 Gets the current in-flag mask.
vpr::Uint16 getOut (vpr::IOSys::Handle handle)
 Gets the current "out flag" mask after a call to select.
vpr::ReturnStatus select (vpr::Uint16 &numWithEvents, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Poll for any ready events among the registered handles using their in flags.
vpr::Uint16 getNumHandles ()
 For iteration over the registered handles.
vpr::IOSys::Handle getHandle (vpr::Uint16 index)
 Gets the handle at the given index within the collection of registered handles.
bool containsHandle (vpr::IOSys::Handle handle)
 Tests if the selector contain the given handle.

Protected Member Functions

std::vector< SimPollDesc
>::iterator 
getHandle (vpr::IOSys::Handle handle)
 Gets the index of the handle given.

Protected Attributes

std::vector< SimPollDescmPollDescs
 List of poll descriptors to pass to select.

Classes

struct  SimPollDesc
 Simple type used for the vector of poll descriptors. More...

Detailed Description

Implementation of a selector for simulated sockets.

This is typedef'd to vpr::Selector.

Implementation site of the vpr::Selector_t bridge.

Definition at line 65 of file SelectorImplSIM.h.


Member Function Documentation

bool vpr::SelectorImplSIM::addHandle ( vpr::IOSys::Handle  handle,
vpr::Uint16  mask = 0 
)

Adds the given handle to the selector.

Precondition:
handle is a valid handle.

handle is added to the handle set, and its mask is initialized using the given value.

Parameters:
handle The handle to be added to the selector's handle set.
mask The mask used when checking for ready events on the given handle. This argument is optional and defaults to 0 (no events).
Returns:
true is returned if the given handle is added successfully; false otherwise.

Definition at line 51 of file SelectorImplSIM.cpp.

References getHandle(), and mPollDescs.

00052 {
00053    bool status;
00054 
00055    if ( getHandle(handle) == mPollDescs.end() )
00056    {
00057       SimPollDesc new_desc;
00058       new_desc.fd        = handle;
00059       new_desc.in_flags  = mask;
00060       new_desc.out_flags = 0;
00061 
00062       mPollDescs.push_back(new_desc);
00063       status = true;
00064    }
00065    else
00066    {
00067       status = false;
00068    }
00069 
00070    return status;
00071 }

bool vpr::SelectorImplSIM::removeHandle ( vpr::IOSys::Handle  handle  ) 

Removes a handle from the selector.

Precondition:
handle is in the selector.
Postcondition:
handle is removed from the set of valid handles.
Parameters:
handle The handle to be removed from the selector's handle set.
Returns:
true is returned if the given handle is removed successfully; false otherwise.

Definition at line 73 of file SelectorImplSIM.cpp.

References getHandle(), and mPollDescs.

00074 {
00075    bool status;
00076    std::vector<SimPollDesc>::iterator i = getHandle(handle);
00077 
00078    if ( mPollDescs.end() == i )
00079    {
00080       status = false;
00081    }
00082    else
00083    {
00084       mPollDescs.erase(i);
00085       status = true;
00086    }
00087 
00088    return status;
00089 }

bool vpr::SelectorImplSIM::setIn ( vpr::IOSys::Handle  handle,
vpr::Uint16  mask 
)

Sets the event flags going in to the select to mask.

Precondition:
handle has already been registered with this selector.
Postcondition:
handle has its mask updated to use the given value.
Parameters:
handle The handle whose mask will be set.
mask The mask used when checking for ready events on the given handle.

Definition at line 91 of file SelectorImplSIM.cpp.

References getHandle(), and mPollDescs.

00092 {
00093    bool status;
00094    std::vector<SimPollDesc>::iterator i = getHandle(handle);
00095 
00096    if ( mPollDescs.end() == i )
00097    {
00098       status = false;
00099    }
00100    else
00101    {
00102       (*i).in_flags = mask;
00103       status = true;
00104    }
00105 
00106    return status;
00107 }

vpr::Uint16 vpr::SelectorImplSIM::getIn ( vpr::IOSys::Handle  handle  ) 

Gets the current in-flag mask.

Precondition:
handle has already been added to the selector using addHandle.
Parameters:
handle The handle whose "in" event flags will be returned.
Returns:
A bitmask value representing the "in flags" of handle.

Definition at line 109 of file SelectorImplSIM.cpp.

References getHandle(), and mPollDescs.

00110 {
00111    vpr::Uint16 flags;
00112    std::vector<SimPollDesc>::iterator i = getHandle(handle);
00113 
00114    if ( mPollDescs.end() == i )
00115    {
00116       // XXX: This is VERY bad thing to do.  Need to have an error code instead
00117       flags = 0;
00118    }
00119    else
00120    {
00121       flags = (*i).in_flags;
00122    }
00123 
00124    return flags;
00125 }

vpr::Uint16 vpr::SelectorImplSIM::getOut ( vpr::IOSys::Handle  handle  ) 

Gets the current "out flag" mask after a call to select.

The value returned will be the bitwise OR of the "out flags". These state which of the operations named "in flags" were found to be ready.

Precondition:
select() has been called.
Parameters:
handle The handle whose "out" event flags will be returned.
Returns:
A bitmask value representing the "out flags" of handle. These flags state which requested events were detected for handle.

Definition at line 127 of file SelectorImplSIM.cpp.

References getHandle(), and mPollDescs.

00128 {
00129    vpr::Uint16 flags;
00130    std::vector<SimPollDesc>::iterator i = getHandle(handle);
00131 
00132    if ( mPollDescs.end() == i )
00133    {
00134       // XXX: This is VERY bad thing to do.  Need to have an error code instead
00135       flags = 0;
00136    }
00137    else
00138    {
00139       flags = (*i).out_flags;
00140    }
00141 
00142    return flags;
00143 }

vpr::ReturnStatus vpr::SelectorImplSIM::select ( vpr::Uint16 numWithEvents,
const vpr::Interval  timeout = vpr::Interval::NoTimeout 
)

Poll for any ready events among the registered handles using their in flags.

Parameters:
numWithEvents Upon completion, this holds the number of items that have events.
timeout The interval to wait for an event to be raised. This argument is optional and defaults to vpr::Interval::NoTimeout (wait until an event is detected withotu timing out). Passing vpr::Interval::NoWait effects a poll on the registered handles and returns immediately.
Returns:
vpr::ReturnStatus::Success is returned if at least one event was detected within the timeout interval.

vpr::ReturnStatus::Timeout is returned if no events were detected before the timeout expired or if vpr::Interval::NoWait was passed. In this case, numWithEvents should be checked for a value greater than 0.

vpr::ReturnStatus::Failure is returned if the select failed.

Definition at line 145 of file SelectorImplSIM.cpp.

References vpr::SelectorBase::Accept, vpr::SelectorBase::Except, mPollDescs, vpr::SelectorBase::Read, vpr::ReturnStatus::setCode(), vpr::ReturnStatus::Timeout, and vpr::SelectorBase::Write.

00147 {
00148    vpr::ReturnStatus status;
00149    std::vector<SimPollDesc>::iterator i;
00150    bool has_event;
00151 
00152    numWithEvents = 0;
00153 
00154    for ( i = mPollDescs.begin(); i != mPollDescs.end(); ++i )
00155    {
00156       // We have to do this every time to insure that a previous event is not
00157       // "reselected".
00158       (*i).out_flags = 0;
00159 
00160       has_event = false;
00161 
00162       if ( (*i).in_flags & vpr::SelectorBase::Read ||
00163            (*i).in_flags & vpr::SelectorBase::Accept)
00164       {
00165          if ( (*i).fd->isReadReady().success() )
00166          {
00167             has_event = true;
00168             (*i).out_flags |= vpr::SelectorBase::Read;
00169          }
00170       }
00171 
00172       if ( (*i).in_flags & vpr::SelectorBase::Write )
00173       {
00174          if ( (*i).fd->isWriteReady().success() )
00175          {
00176             has_event = true;
00177             (*i).out_flags |= vpr::SelectorBase::Write;
00178          }
00179       }
00180 
00181       if ( (*i).in_flags & vpr::SelectorBase::Except )
00182       {
00183          if ( (*i).fd->inExceptState().success() )
00184          {
00185             has_event = true;
00186             (*i).out_flags |= vpr::SelectorBase::Except;
00187          }
00188       }
00189 
00190       if ( has_event )
00191       {
00192          numWithEvents++;
00193       }
00194    }
00195 
00196    if ( numWithEvents == 0 )
00197    {
00198       status.setCode(vpr::ReturnStatus::Timeout);
00199    }
00200 
00201    return status;
00202 }

vpr::Uint16 vpr::SelectorImplSIM::getNumHandles (  )  [inline]

For iteration over the registered handles.

Returns:
An unsigned value stating how many handles have been registered.

Definition at line 166 of file SelectorImplSIM.h.

00167    {
00168       return mPollDescs.size();
00169    }

vpr::IOSys::Handle vpr::SelectorImplSIM::getHandle ( vpr::Uint16  index  )  [inline]

Gets the handle at the given index within the collection of registered handles.

The index is determined by the order of handle addition using addHandle().

Parameters:
index The index of the desired handle.
Returns:
A vpr::IOSys::Handle object representing the registered handle at the given index.

Definition at line 181 of file SelectorImplSIM.h.

Referenced by addHandle(), getIn(), getOut(), removeHandle(), and setIn().

00182    {
00183       return mPollDescs[index].fd;
00184    }

bool vpr::SelectorImplSIM::containsHandle ( vpr::IOSys::Handle  handle  )  [inline]

Tests if the selector contain the given handle.

Parameters:
handle The handle of interest.
Returns:
true is returned if handle was previously registered with this selector.

false is returned if the handle has not been registered.

Definition at line 195 of file SelectorImplSIM.h.

00196    {
00197       return (getHandle(handle) != mPollDescs.end());
00198    }

std::vector< SelectorImplSIM::SimPollDesc >::iterator vpr::SelectorImplSIM::getHandle ( vpr::IOSys::Handle  handle  )  [protected]

Gets the index of the handle given.

Returns:
.end() is returned if the given index is not found.

Otherwise, the index to the handle in mPollDescs is returned.

Definition at line 205 of file SelectorImplSIM.cpp.

References mPollDescs.

00206 {
00207    // XXX: Should probably be replaced by a map in the future for speed.
00208 
00209    for ( std::vector<SimPollDesc>::iterator i=mPollDescs.begin();
00210          i != mPollDescs.end();
00211          ++i )
00212    {
00213       if ( (*i).fd == handle )
00214       {
00215          return i;
00216       }
00217    }
00218 
00219    return mPollDescs.end();
00220 }


Member Data Documentation

std::vector<SimPollDesc> vpr::SelectorImplSIM::mPollDescs [protected]

List of poll descriptors to pass to select.

Definition at line 218 of file SelectorImplSIM.h.

Referenced by addHandle(), getHandle(), getIn(), getOut(), removeHandle(), select(), and setIn().


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:55:40 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1