Skip to content
Snippets Groups Projects
Commit d0e53ca8 authored by Bob Lantz's avatar Bob Lantz
Browse files

Added semi-interactive command line support.

Now you can type commands into each mini-console!
parent f8901597
No related branches found
No related tags found
No related merge requests found
......@@ -11,22 +11,25 @@
from mininet.log import setLogLevel
from mininet.topolib import TreeNet
from mininet.term import makeTerms, cleanUpScreens
class Console( Frame ):
"A simple console on a host."
def __init__( self, parent, node, height=16, width=32 ):
def __init__( self, parent, net, node, height=10, width=32 ):
Frame.__init__( self, parent )
self.net = net
self.node = node
self.prompt = node.name + '# '
self.height, self.width = height, width
self.text = self.makeWidgets( )
self.bindEvents()
self.append( self.prompt )
def makeWidgets( self ):
"Make a label, a text area, and a scroll bar."
labelStyle = {
buttonStyle = {
'font': 'Monaco 7',
'relief': 'sunken'
}
textStyle = {
'font': 'Monaco 7',
......@@ -34,9 +37,16 @@ def makeWidgets( self ):
'fg': 'green',
'width': self.width,
'height': self.height,
'relief': 'sunken'
'relief': 'sunken',
'insertbackground': 'green',
'highlightcolor': 'green',
'selectforeground': 'black',
'selectbackground': 'green'
}
label = Label( self, text=self.node.name, **labelStyle )
def newTerm( net=self.net, node=self.node ):
"Pop up a new terminal window for a node."
net.terms += makeTerms( [ node ] )
label = Button( self, text=self.node.name, command=newTerm, **buttonStyle )
label.pack( side='top', fill='x' )
text = Text( self, wrap='word', **textStyle )
ybar = Scrollbar( self, orient='vertical', command=text.yview )
......@@ -49,7 +59,7 @@ def bindEvents( self ):
"Bind keyboard and file events."
self.text.bind( '<Return>', self.handleReturn )
self.text.bind( '<Control-c>', self.handleInt )
self.text.bind( '<KeyPress>', self.handleKey )
# self.text.bind( '<KeyPress>', self.handleKey )
# This is not well-documented, but it is the correct
# way to trigger a file event handler from Tk's
# event loop!
......@@ -68,8 +78,11 @@ def handleKey( self, event ):
def handleReturn( self, event ):
"Handle a carriage return."
print "handleReturn"
cmd = self.text.get( 'insert linestart', 'insert lineend' )
if cmd.find( self.prompt ) == 0:
cmd = cmd[ len( self.prompt ): ]
self.sendCmd( cmd )
def handleInt( self, event=None ):
"Handle control-c."
self.node.sendInt()
......@@ -82,15 +95,22 @@ def sendCmd( self, cmd ):
def handleReadable( self, file, mask ):
data = self.node.monitor()
self.append( data )
if not self.node.waiting:
# Print prompt, just for the heck of it
self.append( self.prompt )
class Consoles( Frame ):
class ConsoleApp( Frame ):
def __init__( self, nodes, parent=None, width=4 ):
def __init__( self, net, nodes, parent=None, width=4 ):
Frame.__init__( self, parent )
self.top = self.winfo_toplevel()
self.top.title( 'Mininet' )
self.net = net
self.nodes = nodes
self.createMenuBar( font='Geneva 7' )
self.consoles = self.createConsoles( nodes, width )
self.pack( expand=True, fill='both' )
cleanUpScreens()
def createConsoles( self, nodes, width ):
"Create a grid of consoles in a frame."
......@@ -99,7 +119,7 @@ def createConsoles( self, nodes, width ):
consoles = []
index = 0
for node in nodes:
console = Console( f, node )
console = Console( f, net, node )
consoles.append( console )
row = int( index / width )
column = index % width
......@@ -128,11 +148,11 @@ def ping( self ):
"Tell each console to ping the next one."
consoles = self.consoles
count = len( consoles )
i = 1
i = 0
for console in consoles:
i = ( i + 1 ) % count
ip = consoles[ i ].node.IP()
console.sendCmd( 'ping ' + ip )
i = ( i + 1 ) % count
def stop( self ):
"Interrupt all consoles."
......@@ -142,8 +162,8 @@ def stop( self ):
if __name__ == '__main__':
setLogLevel( 'info' )
net = TreeNet( depth=2, fanout=2 )
net = TreeNet( depth=2, fanout=4 )
net.start()
app = Consoles( net.hosts, width=2 )
app = ConsoleApp( net, net.hosts, width=4 )
app.mainloop()
net.stop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment