Changed to not print control chars we don't handle.

This commit is contained in:
Bob Lantz
2010-04-12 18:32:32 -07:00
parent e7ba6b9ebb
commit 3482b4467c
+34 -9
View File
@@ -7,6 +7,7 @@ This demo shows how to monitor a set of nodes by using
Node's monitor() and Tkinter's createfilehandler(). Node's monitor() and Tkinter's createfilehandler().
""" """
import re
from Tkinter import * from Tkinter import *
from mininet.log import setLogLevel from mininet.log import setLogLevel
@@ -43,7 +44,7 @@ class Console( Frame ):
# Set up widgets # Set up widgets
self.text = self.makeWidgets( ) self.text = self.makeWidgets( )
self.bindEvents() self.bindEvents()
self.append( self.prompt ) self.sendCmd( 'export TERM=dumb' )
def makeWidgets( self ): def makeWidgets( self ):
"Make a label, a text area, and a scroll bar." "Make a label, a text area, and a scroll bar."
@@ -67,23 +68,41 @@ class Console( Frame ):
# use special handlers for the following: # use special handlers for the following:
self.text.bind( '<Return>', self.handleReturn ) self.text.bind( '<Return>', self.handleReturn )
self.text.bind( '<Control-c>', self.handleInt ) self.text.bind( '<Control-c>', self.handleInt )
self.text.bind( '<KeyPress>', self.handleKey )
# This is not well-documented, but it is the correct # This is not well-documented, but it is the correct
# way to trigger a file event handler from Tk's # way to trigger a file event handler from Tk's
# event loop! # event loop!
self.tk.createfilehandler( self.node.stdout, READABLE, self.tk.createfilehandler( self.node.stdout, READABLE,
self.handleReadable ) self.handleReadable )
# We're not a terminal (yet?), so we ignore the following
# control characters other than [\b\n\r]
ignoreChars = re.compile( r'[\x00-\x07\x09\x0b\x0c\x0e-\x1f]+' )
def append( self, text ): def append( self, text ):
"Append something to our text frame." "Append something to our text frame."
text = self.ignoreChars.sub( '', text )
self.text.insert( 'end', text ) self.text.insert( 'end', text )
self.text.mark_set( 'insert', 'end' ) self.text.mark_set( 'insert', 'end' )
self.text.see( 'insert' ) self.text.see( 'insert' )
def handleKey( self, event ):
"If it's an interactive command, send it to the node."
char = event.char
if self.node.waiting:
self.node.write( char )
def handleReturn( self, event ): def handleReturn( self, event ):
"Handle a carriage return." "Handle a carriage return."
cmd = self.text.get( 'insert linestart', 'insert lineend' ) cmd = self.text.get( 'insert linestart', 'insert lineend' )
if cmd.find( self.prompt ) == 0: # Send it immediately, if "interactive" command
cmd = cmd[ len( self.prompt ): ] if self.node.waiting:
self.node.write( event.char )
return
# Otherwise send the whole line to the shell
pos = cmd.find( self.prompt )
if pos >= 0:
cmd = cmd[ pos + len( self.prompt ): ]
self.sendCmd( cmd ) self.sendCmd( cmd )
def handleInt( self, event=None ): def handleInt( self, event=None ):
@@ -96,9 +115,9 @@ class Console( Frame ):
if not node.waiting: if not node.waiting:
node.sendCmd( cmd ) node.sendCmd( cmd )
def handleReadable( self, file=None, mask=None ): def handleReadable( self, file=None, mask=None, timeoutms=None ):
"Handle file readable event." "Handle file readable event."
data = self.node.monitor() data = self.node.monitor( timeoutms )
self.append( data ) self.append( data )
if not self.node.waiting: if not self.node.waiting:
# Print prompt # Print prompt
@@ -107,7 +126,9 @@ class Console( Frame ):
def waitOutput( self ): def waitOutput( self ):
"Wait for any remaining output." "Wait for any remaining output."
while self.node.waiting: while self.node.waiting:
self.handleReadable( self ) # A bit of a trade-off here...
self.handleReadable( self, timeoutms=1000)
self.update()
def clear( self ): def clear( self ):
"Clear all of our text." "Clear all of our text."
@@ -213,11 +234,12 @@ class ConsoleApp( Frame ):
ip = consoles[ i ].node.IP() ip = consoles[ i ].node.IP()
console.sendCmd( 'iperf -t 99999 -i 1 -c ' + ip ) console.sendCmd( 'iperf -t 99999 -i 1 -c ' + ip )
def stop( self ): def stop( self, wait=True ):
"Interrupt all hosts." "Interrupt all hosts."
consoles = self.consoles[ 'hosts' ].consoles consoles = self.consoles[ 'hosts' ].consoles
for console in consoles: for console in consoles:
console.handleInt() console.handleInt()
if wait:
for console in consoles: for console in consoles:
console.waitOutput() console.waitOutput()
# Shut down any iperfs that might still be running # Shut down any iperfs that might still be running
@@ -226,9 +248,12 @@ class ConsoleApp( Frame ):
def quit( self ): def quit( self ):
"Stope everything and quit." "Stope everything and quit."
print "Quit" print "Quit"
self.stop() self.stop( wait=False)
Frame.quit( self ) Frame.quit( self )
def reallyQuit( self ):
print "ReallyQuit"
class Object( object ): class Object( object ):
"Generic object you can stuff junk into." "Generic object you can stuff junk into."
def __init__( self, **kwargs ): def __init__( self, **kwargs ):
@@ -237,7 +262,7 @@ class Object( object ):
if __name__ == '__main__': if __name__ == '__main__':
setLogLevel( 'info' ) setLogLevel( 'info' )
net = TreeNet( depth=2, fanout=4 ) net = TreeNet( depth=2, fanout=2 )
net.start() net.start()
app = ConsoleApp( net, width=4 ) app = ConsoleApp( net, width=4 )
app.mainloop() app.mainloop()