Statistics
| Branch: | Revision:

wcn_emulator / wcn_simulator.py @ ed87a801

History | View | Annotate | Download (4.11 KB)

1 b1b1f173 Leonardo Maccari
#!/usr/bin/env python
2 8a2e0da7 Luca Baldesi
import sys
3 ed87a801 Leonardo Maccari
import ConfigParser
4
import inspect
5 8a2e0da7 Luca Baldesi
6 c09f8d98 Leonardo Maccari
sys.path.append('test_code')
7 8a2e0da7 Luca Baldesi
8 c09f8d98 Leonardo Maccari
from os import path
9
from time import time
10 8a2e0da7 Luca Baldesi
11 c09f8d98 Leonardo Maccari
from parameters_parser import parameters
12 ed87a801 Leonardo Maccari
from network_builder import *
13
from test_code import *
14
15 b672166b Luca Baldesi
16 8a2e0da7 Luca Baldesi
17 b1b1f173 Leonardo Maccari
class conf(parameters):
18
    def checkCorrectness(self):
19
        self.checkNeededParams()
20
        return True
21
22 ed87a801 Leonardo Maccari
class configurationFile():
23
24
    mandatoryOptions = {"testModule":None
25
            , "testClass":None}
26
    confParams = {}
27
    className = None
28
    def __init__(self, fileName, stanza):
29
        # check if filename esists
30
        try:
31
            fd =  open(fileName, "r")
32
        except IOError:
33
            error("Can not open the configuration file: " + fileName\
34
                + "\n")
35
            sys.exit(1)
36
        self.parser = ConfigParser.SafeConfigParser()
37
        self.parser.read(fileName)
38
39
        self.testName = stanza 
40
        if stanza not in self.parser.sections():
41
            error("Can not find configuration " + stanza \
42
                    + " in file " + fileName + "\n")
43
            sys.exit(1)
44
        for o in self.mandatoryOptions:
45
            self.mandatoryOptions[o] = \
46
                self.getConfigurations(o, raiseError=True)
47
48
        moduleName = "test_code." + self.mandatoryOptions['testModule']
49
        if moduleName not in sys.modules:
50
            errorString = "ERROR: no " \
51
                + self.mandatoryOptions['testModule'] \
52
                + " module  has been loaded!\n"
53
            error(errorString)
54
            sys.exit(1)
55
56
        if self.mandatoryOptions['testClass'] not in \
57
                zip(*inspect.getmembers(sys.modules[moduleName]))[0]:
58
            errorString = "ERROR: no " \
59
                + self.mandatoryOptions['testClass'] \
60
                + " simulation class is present in "\
61
                + moduleName + "\n"
62
            error(errorString)
63
            sys.exit(1)
64
65
        self.className = getattr(sys.modules[moduleName],
66
            self.mandatoryOptions['testClass'])
67
68
        for name, value in self.parser.items(self.testName):
69
            self.confParams[name] = value
70
71
    def getConfigurations(self, name, raiseError=False):
72
        try:
73
            r = self.parser.get(self.testName, name)
74
        except ConfigParser.NoOptionError:
75
            if raiseError:
76
                error("No option \'" + name + "\' found!\n")
77
                sys.exit()
78
            else:
79
                return None
80
        return r
81
82 8a2e0da7 Luca Baldesi
if __name__ == '__main__':
83
    setLogLevel('info')
84 8f01e99f Leonardo Maccari
    need = [
85 ed87a801 Leonardo Maccari
            ("-f", ["configFile", True, "",
86
                "file with the available configurations", str]),
87
            ("-t", ["testName", True, "",
88
                "base name for test output", str])
89 8f01e99f Leonardo Maccari
           ]
90
    opt = [
91 ed87a801 Leonardo Maccari
            ("-d", ["drawGraph", False, False,
92
                "draw the graph before you run the test", int]),
93
            ("-g", ["graphFile", True, "",
94
                "file with the topology (overrides configuration)", str])
95 8f01e99f Leonardo Maccari
          ]
96
97 b1b1f173 Leonardo Maccari
    P = conf(path.basename(__file__),need, opt)
98
    P.parseArgs()
99
    if P.checkCorrectness() == False:
100
        P.printUsage()
101
        sys.exit(1)
102 ed87a801 Leonardo Maccari
103
    configFile = P.getParam("configFile")
104
    testName = P.getParam("testName")
105
    C = configurationFile(configFile, testName)
106
    #import code
107
    #code.interact(local=locals())
108
    # parse the conf file
109
    networkGraph = P.getParam("graphFile")
110
    if networkGraph == "":
111
        networkGraph = C.getConfigurations("graphDefinition")
112
        if networkGraph == None:
113
            error("No graph topology specified in config file or command " + \
114
                "line!\n")
115
            sys.exit(1)
116
    drawGraph = P.getParam("drawGraph")
117
118
    net = GraphNet(networkGraph, draw = drawGraph)
119 8a2e0da7 Luca Baldesi
    net.start()
120
    net.enableForwarding()
121
    net.setShortestRoutes()
122 a0657979 Luca Baldesi
#    CLI(net)
123 ed87a801 Leonardo Maccari
    testPath = testName+"_"+str(int(time()))
124 b672166b Luca Baldesi
    for i in range(1):
125 ed87a801 Leonardo Maccari
        c = "peerstreamer.PSHostsTest"
126 8f01e99f Leonardo Maccari
        info("+++++++ Round: "+str(i+1) + '\n')
127 e6debb65 Luca Baldesi
        #test = PSRandomTest(net,duration=6,name=test_name,num_peers=2)
128 ed87a801 Leonardo Maccari
        test = C.className(net, testPath, C.confParams)
129 b672166b Luca Baldesi
        test.runTest()
130 8a2e0da7 Luca Baldesi
    net.stop()
131 29c59d2a Leonardo Maccari
    test.changePermissions()
132 a0657979 Luca Baldesi
    info("*** Done with experiment: "+test_name+"\n")