Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

Controller.h

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: Controller.h,v $
00010  * Date modified: $Date: 2003/02/21 23:00:51 $
00011  * Version:       $Revision: 1.22 $
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-2003 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 #ifndef _VPR_SIM_CONTROLLER_H_
00043 #define _VPR_SIM_CONTROLLER_H_
00044 
00045 #include <vpr/vprConfig.h>
00046 
00047 #include <queue>
00048 #include <utility>
00049 #include <map>
00050 #include <vpr/vprTypes.h>
00051 #include <vpr/Sync/Mutex.h>
00052 #include <vpr/Sync/Guard.h>
00053 #include <vpr/Thread/Thread.h>
00054 #include <vpr/Thread/TSObjectProxy.h>
00055 
00056 #include <vpr/md/SIM/Clock.h>
00057 #include <vpr/md/SIM/SocketManager.h>
00058 #include <vpr/md/SIM/Network/NetworkGraph.h>
00059 
00060 
00061 namespace vpr
00062 {
00063 
00064 class Interval;
00065 
00066 namespace sim
00067 {
00068 
00077 class VPR_CLASS_API Controller
00078 {
00079 public:
00089    Controller();
00090 
00097    ~Controller()
00098    {
00099       /* Do nothing. */ ;
00100    }
00101 
00107    static void setInstance (Controller* c)
00108    {
00109       mInstance->setObject(c);
00110    }
00111 
00118    static Controller* instance()
00119    {
00120       // WARNING! race condition possibility, creation of static vars
00121       // are not thread safe.  This is only an issue when creating
00122       // your first thread, since it uses a singleton thread manager,
00123       // the two threads might both try to call instance at the same time
00124       // which then the creation of the following mutex would not be certain.
00125       static vpr::Mutex singleton_lock;
00126 
00127       if ( mInstance->getObject() == NULL )
00128       {
00129          vpr::Guard<vpr::Mutex> guard(singleton_lock);
00130 
00131          if ( mPrimordialInstance == NULL )
00132          {
00133             mPrimordialInstance = new Controller;
00134          }
00135 
00136          if ( mInstance->getObject() == NULL )
00137          {
00138             mInstance->setObject(mPrimordialInstance);
00139          }
00140 
00141          vprASSERT(mInstance->getObject() != NULL && "No instance defined");
00142       }
00143 
00144       return mInstance->getObject();
00145    }
00146 
00147    vpr::ReturnStatus constructNetwork(const std::string& graph_file);
00148 
00149    void destroyNetworkGraph()
00150    {
00151       mGraph.clear();
00152    }
00153 
00161    bool isRunning()
00162    {
00163       vprASSERT(false && "Not supported right now.  If you want it, then implement it.");
00164       //return mGraph.isValid() && mSocketManager.hasActiveSockets();
00165       return false;
00166    }
00167 
00172    void setSimulationPauseTime(const vpr::Uint32 sleep_time)
00173    {
00174       mSleepTime = sleep_time;
00175    }
00176 
00181    void addMessageEvent(const vpr::Interval& event_time,
00182                         const NetworkGraph::net_edge_t edge,
00183                         const NetworkLine::LineDirection dir);
00184 
00185    void addConnectionEvent(const vpr::Interval& event_time,
00186                            vpr::SocketImplSIM* acceptor_sock);
00187 
00188    void addConnectionCompletionEvent(const vpr::Interval& event_time,
00189                                      vpr::SocketImplSIM* connector_sock);
00190 
00191    void addLocalhostDeliveryEvent(const vpr::Interval& event_time,
00192                                   vpr::SocketImplSIM* connector_sock);
00193 
00198    void flushPath(const vpr::SocketImplSIM* sock,
00199                   vpr::sim::NetworkGraph::VertexListPtr path);
00200 
00207    void processNextEvent(vpr::SocketImplSIM** recvSocket = NULL);
00208 
00217    void processEvents(const vpr::Interval& time_step);
00218 
00222    vpr::Uint32 getNumPendingEvents()
00223    {
00224       return mEvents.size();
00225    }
00226 
00227    const vpr::sim::Clock& getClock() const
00228    {
00229       return mClock;
00230    }
00231 
00232    vpr::sim::SocketManager& getSocketManager()
00233    {
00234       return mSocketManager;
00235    }
00236 
00237    vpr::sim::NetworkGraph& getNetworkGraph()
00238    {
00239       return mGraph;
00240    }
00241 
00242 private:
00243    void moveMessage(vpr::sim::MessagePtr, const vpr::Interval& cur_time,
00244                     vpr::SocketImplSIM** recvSocket);
00245 
00246    class ControllerTS
00247    {
00248    public:
00249       ControllerTS() : mObj(NULL)
00250       {
00251          /* Do nothing. */ ;
00252       }
00253 
00254       Controller* getObject() const
00255       {
00256          return mObj;
00257       }
00258 
00259       void setObject(Controller* c)
00260       {
00261          mObj = c;
00262       }
00263 
00264    private:
00265       Controller* mObj;
00266    };
00267 
00268    static Controller* mPrimordialInstance;
00269    static vpr::TSObjectProxy<ControllerTS> mInstance;
00270 
00271 protected:  // --- Data members --- //
00272    // These two have to be here because Visual C++ will try to make them
00273    // exported public symbols.  This causes problems because copying vpr::Mutex
00274    // objects is not allowed.  We do not want to copy sim Controller instances
00275    // anyway, so this should be fine.
00276    Controller(const Controller& o) {;}
00277    void operator=(const Controller& o) {;}
00278 
00279    vpr::sim::Clock         mClock;              
00280    vpr::sim::SocketManager mSocketManager;      
00281    vpr::sim::NetworkGraph  mGraph;              
00283    struct EventData
00284    {
00285       enum EventType
00286       {
00287          MESSAGE,             
00288          CONNECTION_INIT,     
00289          CONNECTION_COMPLETE, 
00290          LOCALHOST_DELIVERY   
00291       };
00292 
00293       EventData(const NetworkGraph::net_edge_t _edge,
00294                 const NetworkLine::LineDirection _dir)
00295          : type(MESSAGE), edge(_edge), direction(_dir)
00296       {
00297          ;
00298       }
00299 
00300       EventData(vpr::SocketImplSIM* sock, const EventType _type)
00301          : type(_type), socket(sock)
00302       {
00303          ;
00304       }
00305 
00306       bool operator==(const EventData& obj) const
00307       {
00308          bool status = false;
00309 
00310          if ( type == obj.type )
00311          {
00312             // This may need to be expanded to a full if/else if/else block
00313             // if the possible message data types expands beyond the above
00314             // four.
00315             status = (type == MESSAGE) ? (edge == obj.edge && direction == obj.direction)
00316                                        : (socket == obj.socket);
00317          }
00318 
00319          return status;
00320       }
00321 
00322       EventType                  type;
00323       NetworkGraph::net_edge_t   edge;
00324       NetworkLine::LineDirection direction;
00325       vpr::SocketImplSIM*        socket;
00326    };
00327 
00328    // This map of intervals to events is always sorted so that we can
00329    // iterate over it in increasing order of event times.
00330    typedef std::multimap<vpr::Interval, EventData> event_map_t;
00331    event_map_t mEvents;
00332 
00333    vpr::Uint32 mSleepTime;
00334 };
00335 
00336 } // End of sim namespace
00337 
00338 } // End of vpr namespace
00339 
00340 
00341 #endif

Generated on Sun May 2 14:43:01 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002