Source

imalse / core / ns3 / experiment / imalse_pure_sim_experiment.py

Full commit
"""

This file contains the imalse pure sim experiment. It inherits from the 
standard ImalseExperiment. In pure sim experiments all nodes are simulated. 
So we don't need the real-time scheduler.

"""
import optparse

import ns.core

from core.ns3.node import ImalseNetnsSimNode
from core.ns3.experiment import ImalseExperiment

class ImalsePureSimExperiment(ImalseExperiment):
    
    """
    
    Pure sim experiments don't depend on netns3 and can be run in simulated time.
    
    """
    
    def initparser(self, parser):
        """Initialize the parser of the parent and extend it with more options."""
        super(ImalsePureSimExperiment, self).initparser(parser)
        
        parser.set_defaults(SimulatorImplementationType='Default')
        parser.add_option("", "--SimulatorImpl", dest = "SimulatorImplementationType",
                help="the simulator implementation Type, available options are['Realtime', 'Default', 'Visual']")


        parser.set_defaults(numnodes = 2, simtime = 30)
        parser.add_option("-n", "--numnodes", dest = "numnodes", type = int,
                          help = "number of nodes; default = %s" %
                          parser.defaults["numnodes"])
        parser.add_option("-t", "--simtime", dest = "simtime", type = float,
                          help = "simulation run time; default = %s" %
                          parser.defaults["simtime"])

    def setup(self):
        """Setup."""
        super(ImalsePureSimExperiment, self).setup()
        ns.core.GlobalValue.Bind("SimulatorImplementationType",
                ns.core.StringValue("ns3::%sSimulatorImpl"%(self.options.SimulatorImplementationType)))

    def run(self):
        """Run the simulation."""
        self.setup()
        print "running simulator for %s sec" % self.options.simtime
        
        ns.core.Simulator.Stop(ns.core.Seconds(self.options.simtime))        
        ns.core.Simulator.Run()
        self.cleanup()
        ns.core.Simulator.Destroy()
        
        print "simulator done"

    @staticmethod
    def event(time, func, *args, **kwds):
        """Schedule an event to the simulator."""
        def run():
            func(*args, **kwds)
        ns.core.Simulator.Schedule(ns.core.Time(str(time)), run)

    @staticmethod
    def NodeCreator():
        """Create a node."""
        return ImalseNetnsSimNode()

    def main(self, args, run = True):
        """Initialize and start the simulator."""
        usage = "%prog [-h] " + self.usagestr() + " ..."
        parser = optparse.OptionParser(usage = usage)
        self.initparser(parser)
        self.options, self.args = parser.parse_args(args)
        if run:
            self.run()

    def usagestr(self):
        """Print usage help string."""
        return "[-n <numnodes>] [-t <simtime>]"

    def print_help(self):
        """Print help."""
        parser = optparse.OptionParser(usage = '')
        self.initparser(parser)
        parser.print_help()

    def cleanup(self):
        """Destroy the simulator and stop all the nodes."""
        ns.core.Simulator.Destroy()
        for i in xrange(self.node_num):
            n = self.get_node(i)
            n.stop()