vpr::SelectorImplNSPR Class Reference

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

#include <vpr/IO/Selector.h>

Inheritance diagram for vpr::SelectorImplNSPR:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

bool addHandle (IOSys::Handle handle, vpr::Uint16 mask=0)
 Adds the given handle to the selector.
bool removeHandle (IOSys::Handle handle)
 Removes a handle from the selector.
bool setIn (IOSys::Handle handle, vpr::Uint16 mask)
 Sets the event flags going in to the select to mask.
vpr::Uint16 getIn (IOSys::Handle handle)
 Gets the current in flag mask.
vpr::Uint16 getOut (IOSys::Handle handle)
 Gets the current out flag mask.
vpr::ReturnStatus select (vpr::Uint16 &numWithEvents, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Select.
vpr::Uint16 getNumHandles ()
 Gets the number of handles.
IOSys::Handle getHandle (vpr::Uint16 index)
bool containsHandle (IOSys::Handle handle)

Protected Member Functions

std::vector< PRPollDesc
>::iterator 
getHandle (PRFileDesc const *handle)
 Gets the index of the handle given.
PRUint16 convertMaskVprToNspr (vpr::Uint16 mask)
vpr::Uint16 convertMaskNsprToVpr (PRUint16 mask)

Protected Attributes

std::vector< PRPollDesc > mPollDescs
 List of Poll Descriptions to pass to PR_Poll().

Detailed Description

NSPR 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.

This is an implementation site of the vpr::Selector_t bridge.

Definition at line 69 of file SelectorImplNSPR.h.


Member Function Documentation

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

Adds the given handle to the selector.

Precondition:
handle is a valid handle.
Postcondition:
handle is added to the handle set and initialized to a mask of no-events.

Definition at line 56 of file SelectorImplNSPR.cpp.

References convertMaskVprToNspr(), getHandle(), and mPollDescs.

00057 {
00058    if(getHandle(handle) != mPollDescs.end())
00059    {
00060       return false;
00061    }
00062 
00063    PRPollDesc new_desc;
00064    new_desc.fd = handle;
00065    new_desc.in_flags = convertMaskVprToNspr(mask);
00066    //new_desc.in_flags = 0;
00067    new_desc.out_flags = 0;
00068 
00069    mPollDescs.push_back(new_desc);
00070 
00071    return true;
00072 }

bool vpr::SelectorImplNSPR::removeHandle ( 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.

Definition at line 77 of file SelectorImplNSPR.cpp.

References getHandle(), and mPollDescs.

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

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

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

Definition at line 95 of file SelectorImplNSPR.cpp.

References convertMaskVprToNspr(), getHandle(), and mPollDescs.

00096 {
00097    std::vector<PRPollDesc>::iterator i = getHandle(handle);
00098 
00099    if(mPollDescs.end() == i)
00100    {
00101       return false;
00102    }
00103 
00104    (*i).in_flags = convertMaskVprToNspr(mask);
00105 
00106    return true;
00107 }

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

Gets the current in flag mask.

Definition at line 112 of file SelectorImplNSPR.cpp.

References convertMaskNsprToVpr(), getHandle(), and mPollDescs.

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

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

Gets the current out flag mask.

Definition at line 128 of file SelectorImplNSPR.cpp.

References convertMaskNsprToVpr(), getHandle(), and mPollDescs.

00129 {
00130    std::vector<PRPollDesc>::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       return 0;
00136    }
00137 
00138    return convertMaskNsprToVpr((*i).out_flags);
00139 }

ReturnStatus vpr::SelectorImplNSPR::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 number of milliseconds to select for (passing vpr::Interval::NoWait means don't wait).

Definition at line 144 of file SelectorImplNSPR.cpp.

References vpr::ReturnStatus::Fail, mPollDescs, vpr::NSPR_getInterval(), vpr::ErrorImplPosix::outputCurrentError(), vpr::ReturnStatus::setCode(), and vpr::ReturnStatus::Timeout.

00145 {
00146    vpr::ReturnStatus ret_val;
00147 
00148    PRInt32 result;
00149 
00150    // Call poll - If timeout == 0, then make sure we pass 0
00151    result = PR_Poll(&(mPollDescs[0]), mPollDescs.size(), NSPR_getInterval(timeout) );
00152 
00153    if( -1 == result)
00154    {
00155       //NSPR_PrintError("SelectorImplNSPR::select: Error selecting. ");
00156       vpr::Error::outputCurrentError(std::cerr, "SelectorImplNSPR::select: Error selecting.");
00157       numWithEvents = 0;
00158       ret_val.setCode(ReturnStatus::Fail);
00159    }
00160    else if(0 == result)    // Timeout
00161    {
00162       numWithEvents = 0;
00163       ret_val.setCode(ReturnStatus::Timeout);
00164    }
00165    //else                    // Got some
00166 
00167    numWithEvents = result;
00168    return ret_val;
00169 }

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

Gets the number of handles.

Definition at line 123 of file SelectorImplNSPR.h.

00124    {
00125       return mPollDescs.size();
00126    }

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

Definition at line 128 of file SelectorImplNSPR.h.

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

00129    {
00130       return mPollDescs[index].fd;
00131    }

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

Definition at line 133 of file SelectorImplNSPR.h.

00134    {
00135       return (getHandle(handle) != mPollDescs.end());
00136    }

std::vector< PRPollDesc >::iterator vpr::SelectorImplNSPR::getHandle ( PRFileDesc const *  handle  )  [protected]

Gets the index of the handle given.

Returns:
.end() if not found, else the index to the handle in mPollDescs.

Definition at line 174 of file SelectorImplNSPR.cpp.

References mPollDescs.

00175 {
00176    // XXX: Should probably be replaced by a map in the future for speed
00177 
00178    for(std::vector<PRPollDesc>::iterator i=mPollDescs.begin();
00179           i != mPollDescs.end();++i)
00180    {
00181       if((*i).fd == handle)
00182          return i;
00183    }
00184 
00185    return mPollDescs.end();
00186 }

PRUint16 vpr::SelectorImplNSPR::convertMaskVprToNspr ( vpr::Uint16  mask  )  [protected]

Definition at line 189 of file SelectorImplNSPR.cpp.

References vpr::SelectorBase::Except, vpr::SelectorBase::Invalid, vpr::SelectorBase::Read, and vpr::SelectorBase::Write.

Referenced by addHandle(), and setIn().

00190 {
00191    PRUint16 ret_mask(0);
00192    if(mask & Read)
00193       ret_mask |= PR_POLL_READ;
00194    if(mask & Write)
00195       ret_mask |= PR_POLL_WRITE;
00196    if(mask & Except)
00197       ret_mask |= PR_POLL_EXCEPT;
00198    if(mask & Error)
00199       ret_mask |= PR_POLL_ERR;
00200    if(mask & Invalid)
00201       ret_mask |= PR_POLL_NVAL;
00202 
00203    return ret_mask;
00204 }

vpr::Uint16 vpr::SelectorImplNSPR::convertMaskNsprToVpr ( PRUint16  mask  )  [protected]

Definition at line 206 of file SelectorImplNSPR.cpp.

References vpr::SelectorBase::Except, vpr::SelectorBase::Invalid, vpr::SelectorBase::Read, and vpr::SelectorBase::Write.

Referenced by getIn(), and getOut().

00207 {
00208    vpr::Uint16 ret_mask(0);
00209    if(mask & PR_POLL_READ)
00210       ret_mask |= Read;
00211    if(mask & PR_POLL_WRITE)
00212       ret_mask |= Write;
00213    if(mask & PR_POLL_EXCEPT)
00214       ret_mask |= Except;
00215    if(mask & PR_POLL_ERR)
00216       ret_mask |= Error;
00217    if(mask & PR_POLL_NVAL)
00218       ret_mask |= Invalid;
00219 
00220    return ret_mask;
00221 }


Member Data Documentation

std::vector<PRPollDesc> vpr::SelectorImplNSPR::mPollDescs [protected]

List of Poll Descriptions to pass to PR_Poll().

Definition at line 152 of file SelectorImplNSPR.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:24 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1