Revision ec7b211c examples/udpbwtest.py

View differences:

examples/udpbwtest.py
18 18

  
19 19
import os
20 20
import re
21
import select
22 21
import sys
23 22
from time import time
24 23

  
......
30 29
from mininet.topolib import TreeTopo
31 30
from mininet.util import quietRun
32 31

  
33
# Some useful stuff: buffered readline and host monitoring
34

  
35
def readline( host, buf ):
36
    "Read a line from a host, buffering with buffer."
37
    buf += host.read( 1024 )
38
    if '\n' not in buf:
39
        return None, buf
40
    pos = buf.find( '\n' )
41
    line = buf[ 0 : pos ]
42
    rest = buf[ pos + 1: ]
43
    return line, rest
44

  
45
def monitor( hosts, seconds ):
46
    "Monitor a set of hosts and yield their output."
47
    poller = select.poll()
48
    Node = hosts[ 0 ] # so we can call class method fdToNode
49
    buffers = {}
50
    for host in hosts:
51
        poller.register( host.stdout )
52
        buffers[ host ] = ''
53
    quitTime = time() + seconds
54
    while time() < quitTime:
55
        ready = poller.poll()
56
        for fd, event in ready:
57
            host = Node.fdToNode( fd )
58
            if event & select.POLLIN:
59
                line, buffers[ host ] = readline( host, buffers[ host ] )
60
                if line:
61
                    yield host, line
62
    yield None, ''
63

  
64 32
# bwtest support
65 33

  
66 34
def parsebwtest( line,
......
111 79
    print
112 80
    results = {}
113 81
    print "*** Monitoring hosts"
114
    output = monitor( hosts, seconds )
115
    while True:
82
    output = net.monitor( hosts )
83
    quitTime = time() + seconds
84
    while time() < quitTime:
116 85
        host, line = output.next()
117 86
        if host is None:
118 87
            break

Also available in: Unified diff