sim/Simulator.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2004-2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #include <queue>
       
    18 #include <oasys/debug/DebugUtils.h>
       
    19 #include <oasys/debug/Log.h>
       
    20 #include <oasys/util/Singleton.h>
       
    21 
       
    22 #include "SimEvent.h"
       
    23 #include "SimEventHandler.h"
       
    24 
       
    25 namespace dtnsim {
       
    26 
       
    27 /**
       
    28  * The main simulator class. This defines the main event loop
       
    29  */
       
    30 class Simulator : public oasys::Singleton<Simulator, false>,
       
    31                   public SimEventHandler,
       
    32                   public oasys::Logger
       
    33 {
       
    34 public:
       
    35     /**
       
    36      * Return the current simulator time.
       
    37      */
       
    38     static double time() { return time_; }
       
    39 
       
    40     /**
       
    41      * Constructor.
       
    42      */
       
    43     Simulator();
       
    44         
       
    45     /**
       
    46      * Destructor.
       
    47      */
       
    48     virtual ~Simulator() {}
       
    49 
       
    50     /**
       
    51      * Add an event to the main event queue.
       
    52      */
       
    53     static void post(SimEvent *e);
       
    54     
       
    55     /**
       
    56      * Stops simulation and exits the loop
       
    57      */
       
    58     void exit();
       
    59 
       
    60     /**
       
    61      * Main run loop.
       
    62      */
       
    63     void run();
       
    64 
       
    65     /**
       
    66      * Handle all bundle events at nodes returning the amount of time
       
    67      * (in ms) until the next timer is due.
       
    68      */
       
    69     int run_node_events();
       
    70 
       
    71     /**
       
    72      * Pause execution of the simulator, running a console loop until
       
    73      * it exits.
       
    74      */
       
    75     void pause();
       
    76 
       
    77     /**
       
    78      * Run the command loop.
       
    79      */
       
    80     void run_console(bool complete);
       
    81 
       
    82     /**
       
    83      * Register a command to run at exit.
       
    84      */
       
    85     void set_exit_event(SimAtEvent* event);
       
    86 
       
    87     static double runtill_;             ///< time to end the simulation
       
    88     
       
    89 private:
       
    90     /**
       
    91      * Virtual from SimEventHandler
       
    92      */
       
    93     void process(SimEvent* e);
       
    94 
       
    95     void log_inqueue_stats();
       
    96 
       
    97     static double time_;                ///< current time (static to avoid object)
       
    98 
       
    99     std::priority_queue<SimEvent*,
       
   100                         std::vector<SimEvent*>,
       
   101                         SimEventCompare> eventq_;
       
   102 
       
   103     SimAtEvent* exit_event_;
       
   104 
       
   105     void run_at_event(SimAtEvent* evt);
       
   106 
       
   107     /*
       
   108      * Handlers for SIGINT to pause the simulation while it's running.
       
   109      */
       
   110     static void handle_interrupt(int sig);
       
   111     void check_interrupt();
       
   112     static bool interrupted_;
       
   113 };
       
   114 
       
   115 } // namespace dtnsim
       
   116