SelectorImplSIM.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-17 22:34:01 -0600 (Mon, 17 Jan 2005) $
00011  * Version:       $Revision: 16635 $
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/SIM/IO/SelectorImplSIM.h>
00045 #include <vpr/md/SIM/IO/Socket/SocketImplSIM.h> /* This kind of sucks, but oh well ... */
00046 
00047 
00048 namespace vpr
00049 {
00050 
00051 bool SelectorImplSIM::addHandle(vpr::IOSys::Handle handle, vpr::Uint16 mask)
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 }
00072 
00073 bool SelectorImplSIM::removeHandle(vpr::IOSys::Handle handle)
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 }
00090 
00091 bool SelectorImplSIM::setIn(vpr::IOSys::Handle handle, vpr::Uint16 mask)
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 }
00108 
00109 vpr::Uint16 SelectorImplSIM::getIn(vpr::IOSys::Handle handle)
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 }
00126 
00127 vpr::Uint16 SelectorImplSIM::getOut(vpr::IOSys::Handle handle)
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 }
00144 
00145 vpr::ReturnStatus SelectorImplSIM::select(vpr::Uint16& numWithEvents,
00146                                           const vpr::Interval timeout)
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 }
00203 
00204 std::vector<SelectorImplSIM::SimPollDesc>::iterator
00205 SelectorImplSIM::getHandle(vpr::IOSys::Handle handle)
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 }
00221 
00222 } // End of vpr namespace

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