Statistics
| Branch: | Revision:

wcn_emulator / wcn_simulator.py @ ed87a801

History | View | Annotate | Download (4.11 KB)

1
#!/usr/bin/env python
2
import sys
3
import ConfigParser
4
import inspect
5

    
6
sys.path.append('test_code')
7

    
8
from os import path
9
from time import time
10

    
11
from parameters_parser import parameters
12
from network_builder import *
13
from test_code import *
14

    
15

    
16

    
17
class conf(parameters):
18
    def checkCorrectness(self):
19
        self.checkNeededParams()
20
        return True
21

    
22
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
if __name__ == '__main__':
83
    setLogLevel('info')
84
    need = [
85
            ("-f", ["configFile", True, "",
86
                "file with the available configurations", str]),
87
            ("-t", ["testName", True, "",
88
                "base name for test output", str])
89
           ]
90
    opt = [
91
            ("-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
          ]
96

    
97
    P = conf(path.basename(__file__),need, opt)
98
    P.parseArgs()
99
    if P.checkCorrectness() == False:
100
        P.printUsage()
101
        sys.exit(1)
102

    
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
    net.start()
120
    net.enableForwarding()
121
    net.setShortestRoutes()
122
#    CLI(net)
123
    testPath = testName+"_"+str(int(time()))
124
    for i in range(1):
125
        c = "peerstreamer.PSHostsTest"
126
        info("+++++++ Round: "+str(i+1) + '\n')
127
        #test = PSRandomTest(net,duration=6,name=test_name,num_peers=2)
128
        test = C.className(net, testPath, C.confParams)
129
        test.runTest()
130
    net.stop()
131
    test.changePermissions()
132
    info("*** Done with experiment: "+test_name+"\n")