Revision ec7b211c mininet/node.py

View differences:

mininet/node.py
94 94
        self.defaultMAC = defaultMAC
95 95
        self.lastCmd = None
96 96
        self.lastPid = None
97
        # Grab PID
98
        self.waiting = True
99
        while self.lastPid is None:
100
            self.monitor()
101
        self.pid = self.lastPid
97
        self.readbuf = ''
102 98
        self.waiting = False
103 99

  
104 100
    @classmethod
......
115 111

  
116 112
    # Subshell I/O, commands and control
117 113
    def read( self, bytes ):
118
        """Read from a node.
119
           bytes: maximum number of bytes to read"""
120
        return os.read( self.stdout.fileno(), bytes )
114
        """Buffered read from node, non-blocking.
115
           bytes: maximum number of bytes to return"""
116
        count = len( self.readbuf )
117
        if count < bytes:
118
            data = os.read( self.stdout.fileno(), bytes - count )
119
            self.readbuf += data
120
        if bytes >= len( self.readbuf ):
121
            result = self.readbuf
122
            self.readbuf = ''
123
        else:
124
            result = self.readbuf[ :bytes ]
125
            self.readbuf = self.readbuf[ bytes: ]
126
        return result
127

  
128
    def readline( self ):
129
        """Buffered readline from node, non-blocking.
130
           returns: line (minus newline) or None"""
131
        self.readbuf += self.read( 1024 )
132
        if '\n' not in self.readbuf:
133
            return None
134
        pos = self.readbuf.find( '\n' )
135
        line = self.readbuf[ 0 : pos ]
136
        self.readbuf = self.readbuf[ pos + 1: ]
137
        return line
121 138

  
122 139
    def write( self, data ):
123 140
        """Write data to node.
......
135 152

  
136 153
    def waitReadable( self ):
137 154
        "Wait until node's output is readable."
138
        self.pollOut.poll()
155
        if len( self.readbuf ) == 0:
156
            self.pollOut.poll()
139 157

  
140 158
    def sendCmd( self, cmd, printPid=False ):
141 159
        """Send a command, followed by a command to echo a sentinel,
......
155 173
        self.lastPid = None
156 174
        self.waiting = True
157 175

  
158
    def sendInt( self ):
176
    def sendInt( self, sig=signal.SIGINT ):
159 177
        "Interrupt running command."
160 178
        if self.lastPid:
161
            os.kill( self.lastPid, signal.SIGINT )
179
            os.kill( self.lastPid, sig )
162 180

  
163 181
    def monitor( self ):
164 182
        """Monitor and return the output of a command.
......
315 333
    def setDefaultRoute( self, intf ):
316 334
        """Set the default route to go through intf.
317 335
           intf: string, interface name"""
318
        self.cmd( 'ip route flush' )
336
        self.cmd( 'ip route flush root 0/0' )
319 337
        return self.cmd( 'route add default ' + intf )
320 338

  
321 339
    def IP( self, intf=None ):

Also available in: Unified diff