SelectorImplNSPR.cpp

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile$
00010  * Date modified: $Date: 2005-01-01 11:48:13 -0600 (Sat, 01 Jan 2005) $
00011  * Version:       $Revision: 16520 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
00041 
00042 #include <vpr/vprConfig.h>
00043 
00044 #include <vpr/md/NSPR/NSPRHelpers.h>
00045 #include <vpr/Util/Assert.h>
00046 #include <vpr/md/NSPR/IO/SelectorImplNSPR.h>
00047 #include <vpr/Util/Error.h>
00048 
00049 
00050 namespace vpr
00051 {
00052 
00056 bool SelectorImplNSPR::addHandle(IOSys::Handle handle, vpr::Uint16 mask)
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 }
00073 
00077 bool SelectorImplNSPR::removeHandle(IOSys::Handle handle)
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 }
00091 
00095 bool SelectorImplNSPR::setIn(IOSys::Handle handle, vpr::Uint16 mask)
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 }
00108 
00112 vpr::Uint16 SelectorImplNSPR::getIn(IOSys::Handle handle)
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 }
00124 
00128 vpr::Uint16 SelectorImplNSPR::getOut(IOSys::Handle handle)
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 }
00140 
00144 ReturnStatus SelectorImplNSPR::select(vpr::Uint16& numWithEvents,  const vpr::Interval 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 }
00170 
00174 std::vector<PRPollDesc>::iterator SelectorImplNSPR::getHandle(PRFileDesc const* handle)
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 }
00187 
00188 
00189 PRUint16 SelectorImplNSPR::convertMaskVprToNspr(vpr::Uint16 mask)
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 }
00205 
00206 vpr::Uint16 SelectorImplNSPR::convertMaskNsprToVpr(PRUint16 mask)
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 }
00222 
00223 }  // namespace vpr

Generated on Thu Jan 4 10:52:10 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1