Source

imalse / core / ns3 / experiment / imalse_pure_sim_experiment.py

"""

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
import ns3

from core.ns3.node import ImalseNetnsSimNode
from core.ns3.experiment import ImalseExperiment
from core.ns3.util.netanim import AnimationInterface

import settings

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(ExportXML=None)
        parser.add_option("", "--ExportXML", dest = "ExportXML", 
                help ="Define filename of XML used to animate network in NetAanim.")        
        
        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)) 
        if self.options.ExportXML != None:
            anim = AnimationInterface(settings.ROOT + '/' + self.options.ExportXML)                    
        
        ns.core.Simulator.Run()
        
        if self.options.ExportXML != None:
            anim.pcapToXML()
        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()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.