vpr::SelectorImplBSD Class Reference

BSD Implementation of cross-platform selection interface. More...

#include <vpr/IO/Selector.h>

Inheritance diagram for vpr::SelectorImplBSD:

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

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. Sets the event flags going in to the select to mask.
bool setIn (vpr::IOSys::Handle handle, vpr::Uint16 mask)
 Gets the current in flag mask.
vpr::Uint16 getIn (vpr::IOSys::Handle handle)
 Gets the current out 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)
 Select.
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)
 Test if the selector contain the given handle.

Protected Member Functions

std::vector< BSDPollDesc
>::iterator 
getHandle (int handle)
 Gets the index of the handle given.

Protected Attributes

std::vector< BSDPollDescmPollDescs
 List of Poll Descriptions to pass to select(2).

Classes

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

Detailed Description

BSD Implementation of cross-platform selection interface.

A selector is used to wait on a set of Handles untils any of the events occur that the user is interested in.

Implementation site of the vpr::Selector_t<T> bridge. This class is used as the template parameter to vpr::Selector_t<T> to create the typedef vpr::Selector.

Definition at line 68 of file SelectorImplBSD.h.


Member Function Documentation

bool vpr::SelectorImplBSD::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 is returned otherwise.

Definition at line 66 of file SelectorImplBSD.cpp.

References getHandle(), and mPollDescs.

00067 {
00068    bool status;
00069 
00070    if ( getHandle(handle) == mPollDescs.end() )
00071    {
00072       BSDPollDesc new_desc;
00073       new_desc.fd        = handle;
00074       new_desc.in_flags  = mask;
00075       new_desc.out_flags = 0;
00076 
00077       mPollDescs.push_back(new_desc);
00078       status = true;
00079    }
00080    else
00081    {
00082       status = false;
00083    }
00084 
00085    return status;
00086 }

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

Removes a handle from the selector. Sets the event flags going in to the select to mask.

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 is returned otherwise.

Definition at line 91 of file SelectorImplBSD.cpp.

References getHandle(), and mPollDescs.

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

Gets the current in flag mask.

The flags specify which events should be selected for the given handle.

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 110 of file SelectorImplBSD.cpp.

References getHandle(), and mPollDescs.

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

Gets the current out 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 129 of file SelectorImplBSD.cpp.

References getHandle(), and mPollDescs.

vpr::Uint16 vpr::SelectorImplBSD::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 148 of file SelectorImplBSD.cpp.

References getHandle(), and mPollDescs.

00149 {
00150    vpr::Uint16 flags;
00151    std::vector<BSDPollDesc>::iterator i = getHandle(handle);
00152 
00153    if ( mPollDescs.end() == i )
00154    {
00155       // XXX: This is VERY bad thing to do.  Need to have an error code instead
00156       flags = 0;
00157    }
00158    else
00159    {
00160       flags = (*i).out_flags;
00161    }
00162 
00163    return flags;
00164 }

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

Select.

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::Succeed 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::Fail is returned if the select failed.

Definition at line 169 of file SelectorImplBSD.cpp.

References errno, vpr::SelectorBase::Except, vpr::ReturnStatus::Fail, mPollDescs, vpr::Interval::msec(), vpr::Interval::NoTimeout, vpr::Interval::NoWait, vpr::SelectorBase::Read, vpr::ReturnStatus::setCode(), vpr::ReturnStatus::Timeout, and vpr::SelectorBase::Write.

00171 {
00172    vpr::ReturnStatus ret_val;
00173    int result, last_fd;
00174    fd_set read_set, write_set, exception_set;
00175    std::vector<BSDPollDesc>::iterator i;
00176    struct timeval timeout_obj;
00177 
00178    // Zero everything out before doing anything else.
00179    FD_ZERO(&read_set);
00180    FD_ZERO(&write_set);
00181    FD_ZERO(&exception_set);
00182 
00183    last_fd = -1;
00184 
00185    for ( i = mPollDescs.begin(); i != mPollDescs.end(); ++i )
00186    {
00187       (*i).out_flags = 0;
00188 
00189       if ( (*i).in_flags & SelectorBase::Read )
00190       {
00191          FD_SET((*i).fd, &read_set);
00192       }
00193 
00194       if ( (*i).in_flags & SelectorBase::Write )
00195       {
00196          FD_SET((*i).fd, &write_set);
00197       }
00198 
00199       if ( (*i).in_flags & SelectorBase::Except )
00200       {
00201          FD_SET((*i).fd, &exception_set);
00202       }
00203 
00204       // Find the highest-valued file descriptor.
00205       if ( last_fd < (*i).fd )
00206       {
00207          last_fd = (*i).fd;
00208       }
00209    }
00210 
00211    if ( timeout == vpr::Interval::NoWait )
00212    {
00213       timeout_obj.tv_sec  = 0;
00214       timeout_obj.tv_usec = 0;
00215    }
00216    else
00217    {
00218       // Apparently select(2) doesn't like if the microsecond member has a
00219       // time larger than 1 second, so if timeout (given in milliseconds) is
00220       // larger than 1000, we have to split it up between the seconds and
00221       // microseconds members.
00222       if ( timeout.msec() >= 1000 )
00223       {
00224          timeout_obj.tv_sec  = timeout.msec() / 1000;
00225          timeout_obj.tv_usec = (timeout.msec() % 1000) * 1000000;
00226       }
00227       else
00228       {
00229          timeout_obj.tv_sec  = 0;
00230          timeout_obj.tv_usec = timeout.msec() * 1000;
00231       }
00232    }
00233 
00234    // If timeout is 0, this will be the same as polling the descriptors.  To
00235    // get no timeout, NULL must be passed to select(2).
00236    result = ::select(last_fd + 1, &read_set, &write_set, &exception_set,
00237                      (timeout != vpr::Interval::NoTimeout) ? &timeout_obj : NULL);
00238 
00239    // D'oh!
00240    if ( -1 == result )
00241    {
00242       fprintf(stderr, "SelectorImplBSD::select: Error selecting: %s\n",
00243               strerror(errno));
00244       numWithEvents = 0;
00245       ret_val.setCode(ReturnStatus::Fail);
00246    }
00247    // Timeout.
00248    else if ( 0 == result )
00249    {
00250       numWithEvents = 0;
00251       ret_val.setCode(ReturnStatus::Timeout);
00252    }
00253    // We got one!
00254    else
00255    {
00256       for ( i = mPollDescs.begin(); i != mPollDescs.end(); ++i )
00257       {
00258          if ( FD_ISSET((*i).fd, &read_set) )
00259          {
00260             (*i).out_flags |= SelectorBase::Read;
00261          }
00262 
00263          if ( FD_ISSET((*i).fd, &write_set) )
00264          {
00265             (*i).out_flags |= SelectorBase::Write;
00266          }
00267 
00268          if ( FD_ISSET((*i).fd, &exception_set) )
00269          {
00270             (*i).out_flags |= SelectorBase::Except;
00271          }
00272       }
00273 
00274       numWithEvents = result;
00275    }
00276 
00277    return ret_val;
00278 }

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

For iteration over the registered handles.

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

Definition at line 174 of file SelectorImplBSD.h.

References mPollDescs.

00175    {
00176       return mPollDescs.size();
00177    }

vpr::IOSys::Handle vpr::SelectorImplBSD::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 189 of file SelectorImplBSD.h.

References mPollDescs.

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

00190    {
00191       return mPollDescs[index].fd;
00192    }

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

Test 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 203 of file SelectorImplBSD.h.

References getHandle(), and mPollDescs.

00204    {
00205       return (getHandle(handle) != mPollDescs.end());
00206    }

std::vector< SelectorImplBSD::BSDPollDesc >::iterator vpr::SelectorImplBSD::getHandle ( int  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 283 of file SelectorImplBSD.cpp.

References mPollDescs.

00284 {
00285    // XXX: Should probably be replaced by a map in the future for speed
00286 
00287    for ( std::vector<BSDPollDesc>::iterator i=mPollDescs.begin();
00288        i != mPollDescs.end();++i )
00289    {
00290       if ( (*i).fd == handle )
00291          return i;
00292    }
00293 
00294    return mPollDescs.end();
00295 }


Member Data Documentation

std::vector<BSDPollDesc> vpr::SelectorImplBSD::mPollDescs [protected]

List of Poll Descriptions to pass to select(2).

Definition at line 226 of file SelectorImplBSD.h.

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


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