Statistics
| Branch: | Revision:

wcn_emulator / test_generic.py @ c09f8d98

History | View | Annotate | Download (1.61 KB)

1

    
2
import signal
3
from random import sample
4
from psutil import Process
5
from time import sleep
6

    
7
from mininet.log import info, error, debug, output
8

    
9
class MininetTest(object):
10
    def __init__(self,mininet):
11
        self.net = mininet
12
        self.pendingProc = {} 
13
    
14
    def getHostSample(self,num):
15
        hosts = sample(self.net.values(),num)
16
        return hosts[:num]
17

    
18
    def bgCmd(self,host,force_multiple_processes,*args):
19
        # here it's a little workaround for tracing the resulting pid
20
        # it launch the new process using the mininet interface
21
        # but it check the newly created process id using psutil
22
        host_proc = Process(host.pid)
23
        host_ps = set(host_proc.get_children())
24
        debug("Sending cmd: \n\t"+str(args)+"\n")
25
        if force_multiple_processes:
26
            host.waiting = False
27
        host.sendCmd(*(args+("&",)))
28
        sleep(0.5)
29
        try :
30
            pid = (set(host_proc.get_children()).difference(host_ps)).pop().pid
31
            info("BGProcess: "+str(pid)+"; ")
32
            self.pendingProc[pid] = host
33
        except:
34
            info("*** Unable to launch command:\n\t "+str(args))
35
            return None
36
        return pid
37

    
38
    def sendSig(self,pid,sig=signal.SIGTERM):
39
        try:
40
            info("Killing BGProcess: "+str(pid)+"; ")
41
            kill( pid, sig )
42
        except OSError:
43
            error("Error while killing process "+str(pid))
44
            pass
45

    
46
    def killAll(self):
47
        for pid in self.pendingProc.keys():
48
            self.sendSig(pid,signal.SIGKILL)
49
            self.pendingProc[pid].monitor() # wait exiting
50
        self.pendingProc.clear()
51

    
52