<s> """<STR_LIT>""" <EOL> import re <EOL> from Tkinter import Frame , Button , Label , Text , Scrollbar , Canvas , Wm , READABLE <EOL> from mininet . log import setLogLevel <EOL> from mininet . topolib import TreeNet <EOL> from mininet . term import makeTerms , cleanUpScreens <EOL> from mininet . util import quietRun <EOL> class Console ( Frame ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , parent , net , node , height = <NUM_LIT:10> , width = <NUM_LIT:32> , title = '<STR_LIT>' ) : <EOL> Frame . __init__ ( self , parent ) <EOL> self . net = net <EOL> self . node = node <EOL> self . prompt = node . name + '<STR_LIT>' <EOL> self . height , self . width , self . title = height , width , title <EOL> self . buttonStyle = { '<STR_LIT>' : '<STR_LIT>' } <EOL> self . textStyle = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:width>' : self . width , <EOL> '<STR_LIT>' : self . height , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . text = self . makeWidgets ( ) <EOL> self . bindEvents ( ) <EOL> self . sendCmd ( '<STR_LIT>' ) <EOL> self . outputHook = None <EOL> def makeWidgets ( self ) : <EOL> "<STR_LIT>" <EOL> def newTerm ( net = self . net , node = self . node , title = self . title ) : <EOL> "<STR_LIT>" <EOL> net . terms += makeTerms ( [ node ] , title ) <EOL> label = Button ( self , text = self . node . name , command = newTerm , <EOL> ** self . buttonStyle ) <EOL> label . pack ( side = '<STR_LIT>' , fill = '<STR_LIT:x>' ) <EOL> text = Text ( self , wrap = '<STR_LIT>' , ** self . textStyle ) <EOL> ybar = Scrollbar ( self , orient = '<STR_LIT>' , width = <NUM_LIT:7> , <EOL> command = text . yview ) <EOL> text . configure ( yscrollcommand = ybar . set ) <EOL> text . pack ( side = '<STR_LIT:left>' , expand = True , fill = '<STR_LIT>' ) <EOL> ybar . pack ( side = '<STR_LIT:right>' , fill = '<STR_LIT:y>' ) <EOL> return text <EOL> def bindEvents ( self ) : <EOL> "<STR_LIT>" <EOL> self . text . bind ( '<STR_LIT>' , self . handleReturn ) <EOL> self . text . bind ( '<STR_LIT>' , self . handleInt ) <EOL> self . text . bind ( '<STR_LIT>' , self . handleKey ) <EOL> self . tk . createfilehandler ( self . node . stdout , READABLE , <EOL> self . handleReadable ) <EOL> ignoreChars = re . compile ( r'<STR_LIT>' ) <EOL> def append ( self , text ) : <EOL> "<STR_LIT>" <EOL> text = self . ignoreChars . sub ( '<STR_LIT>' , text ) <EOL> self . text . insert ( '<STR_LIT:end>' , text ) <EOL> self . text . mark_set ( '<STR_LIT>' , '<STR_LIT:end>' ) <EOL> self . text . see ( '<STR_LIT>' ) <EOL> outputHook = lambda x , y : True <EOL> if self . outputHook : <EOL> outputHook = self . outputHook <EOL> outputHook ( self , text ) <EOL> def handleKey ( self , event ) : <EOL> "<STR_LIT>" <EOL> char = event . char <EOL> if self . node . waiting : <EOL> self . node . write ( char ) <EOL> def handleReturn ( self , event ) : <EOL> "<STR_LIT>" <EOL> cmd = self . text . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . node . waiting : <EOL> self . node . write ( event . char ) <EOL> return <EOL> pos = cmd . find ( self . prompt ) <EOL> if pos >= <NUM_LIT:0> : <EOL> cmd = cmd [ pos + len ( self . prompt ) : ] <EOL> self . sendCmd ( cmd ) <EOL> def handleInt ( self , _event = None ) : <EOL> "<STR_LIT>" <EOL> self . node . sendInt ( ) <EOL> def sendCmd ( self , cmd ) : <EOL> "<STR_LIT>" <EOL> if not self . node . waiting : <EOL> self . node . sendCmd ( cmd ) <EOL> def handleReadable ( self , _fds , timeoutms = None ) : <EOL> "<STR_LIT>" <EOL> data = self . node . monitor ( timeoutms ) <EOL> self . append ( data ) <EOL> if not self . node . waiting : <EOL> self . append ( self . prompt ) <EOL> def waiting ( self ) : <EOL> "<STR_LIT>" <EOL> return self . node . waiting <EOL> def waitOutput ( self ) : <EOL> "<STR_LIT>" <EOL> while self . node . waiting : <EOL> self . handleReadable ( self , timeoutms = <NUM_LIT:1000> ) <EOL> self . update ( ) <EOL> def clear ( self ) : <EOL> "<STR_LIT>" <EOL> self . text . delete ( '<STR_LIT:1.0>' , '<STR_LIT:end>' ) <EOL> class Graph ( Frame ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , parent = None , bg = '<STR_LIT>' , gheight = <NUM_LIT:200> , gwidth = <NUM_LIT> , <EOL> barwidth = <NUM_LIT:10> , ymax = <NUM_LIT> , ) : <EOL> Frame . __init__ ( self , parent ) <EOL> self . bg = bg <EOL> self . gheight = gheight <EOL> self . gwidth = gwidth <EOL> self . barwidth = barwidth <EOL> self . ymax = float ( ymax ) <EOL> self . xpos = <NUM_LIT:0> <EOL> self . title , self . scale , self . graph = self . createWidgets ( ) <EOL> self . updateScrollRegions ( ) <EOL> self . yview ( '<STR_LIT>' , '<STR_LIT:1.0>' ) <EOL> def createScale ( self ) : <EOL> "<STR_LIT>" <EOL> height = float ( self . gheight ) <EOL> width = <NUM_LIT> <EOL> ymax = self . ymax <EOL> scale = Canvas ( self , width = width , height = height , <EOL> background = self . bg ) <EOL> opts = { '<STR_LIT>' : '<STR_LIT>' } <EOL> scale . create_line ( width - <NUM_LIT:1> , height , width - <NUM_LIT:1> , <NUM_LIT:0> , ** opts ) <EOL> for y in range ( <NUM_LIT:0> , int ( ymax + <NUM_LIT:1> ) ) : <EOL> ypos = height * ( <NUM_LIT:1> - float ( y ) / ymax ) <EOL> scale . create_line ( width , ypos , width - <NUM_LIT:10> , ypos , ** opts ) <EOL> scale . create_text ( <NUM_LIT:10> , ypos , text = str ( y ) , ** opts ) <EOL> return scale <EOL> def updateScrollRegions ( self ) : <EOL> "<STR_LIT>" <EOL> ofs = <NUM_LIT:20> <EOL> height = self . gheight + ofs <EOL> self . graph . configure ( scrollregion = ( <NUM_LIT:0> , - ofs , <EOL> self . xpos * self . barwidth , height ) ) <EOL> self . scale . configure ( scrollregion = ( <NUM_LIT:0> , - ofs , <NUM_LIT:0> , height ) ) <EOL> def yview ( self , * args ) : <EOL> "<STR_LIT>" <EOL> self . graph . yview ( * args ) <EOL> self . scale . yview ( * args ) <EOL> def createWidgets ( self ) : <EOL> "<STR_LIT>" <EOL> title = Label ( self , text = '<STR_LIT>' , bg = self . bg ) <EOL> width = self . gwidth <EOL> height = self . gheight <EOL> scale = self . createScale ( ) <EOL> graph = Canvas ( self , width = width , height = height , background = self . bg ) <EOL> xbar = Scrollbar ( self , orient = '<STR_LIT>' , command = graph . xview ) <EOL> ybar = Scrollbar ( self , orient = '<STR_LIT>' , command = self . yview ) <EOL> graph . configure ( xscrollcommand = xbar . set , yscrollcommand = ybar . set , <EOL> scrollregion = ( <NUM_LIT:0> , <NUM_LIT:0> , width , height ) ) <EOL> scale . configure ( yscrollcommand = ybar . set ) <EOL> title . grid ( row = <NUM_LIT:0> , columnspan = <NUM_LIT:3> , sticky = '<STR_LIT>' ) <EOL> scale . grid ( row = <NUM_LIT:1> , column = <NUM_LIT:0> , sticky = '<STR_LIT>' ) <EOL> graph . grid ( row = <NUM_LIT:1> , column = <NUM_LIT:1> , sticky = '<STR_LIT>' ) <EOL> ybar . grid ( row = <NUM_LIT:1> , column = <NUM_LIT:2> , sticky = '<STR_LIT>' ) <EOL> xbar . grid ( row = <NUM_LIT:2> , column = <NUM_LIT:0> , columnspan = <NUM_LIT:2> , sticky = '<STR_LIT>' ) <EOL> self . rowconfigure ( <NUM_LIT:1> , weight = <NUM_LIT:1> ) <EOL> self . columnconfigure ( <NUM_LIT:1> , weight = <NUM_LIT:1> ) <EOL> return title , scale , graph <EOL> def addBar ( self , yval ) : <EOL> "<STR_LIT>" <EOL> percent = yval / self . ymax <EOL> c = self . graph <EOL> x0 = self . xpos * self . barwidth <EOL> x1 = x0 + self . barwidth <EOL> y0 = self . gheight <EOL> y1 = ( <NUM_LIT:1> - percent ) * self . gheight <EOL> c . create_rectangle ( x0 , y0 , x1 , y1 , fill = '<STR_LIT>' ) <EOL> self . xpos += <NUM_LIT:1> <EOL> self . updateScrollRegions ( ) <EOL> self . graph . xview ( '<STR_LIT>' , '<STR_LIT:1.0>' ) <EOL> def clear ( self ) : <EOL> "<STR_LIT>" <EOL> self . graph . delete ( '<STR_LIT:all>' ) <EOL> self . xpos = <NUM_LIT:0> <EOL> def test ( self ) : <EOL> "<STR_LIT>" <EOL> ms = <NUM_LIT:1000> <EOL> if self . xpos < <NUM_LIT:10> : <EOL> self . addBar ( self . xpos / <NUM_LIT:10> * self . ymax ) <EOL> self . after ( ms , self . test ) <EOL> def setTitle ( self , text ) : <EOL> "<STR_LIT>" <EOL> self . title . configure ( text = text , font = '<STR_LIT>' ) <EOL> class ConsoleApp ( Frame ) : <EOL> "<STR_LIT>" <EOL> menuStyle = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def __init__ ( self , net , parent = None , width = <NUM_LIT:4> ) : <EOL> Frame . __init__ ( self , parent ) <EOL> self . top = self . winfo_toplevel ( ) <EOL> self . top . title ( '<STR_LIT>' ) <EOL> self . net = net <EOL> self . menubar = self . createMenuBar ( ) <EOL> cframe = self . cframe = Frame ( self ) <EOL> self . consoles = { } <EOL> titles = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for name in titles : <EOL> nodes = getattr ( net , name ) <EOL> frame , consoles = self . createConsoles ( <EOL> cframe , nodes , width , titles [ name ] ) <EOL> self . consoles [ name ] = Object ( frame = frame , consoles = consoles ) <EOL> self . selected = None <EOL> self . select ( '<STR_LIT>' ) <EOL> self . cframe . pack ( expand = True , fill = '<STR_LIT>' ) <EOL> cleanUpScreens ( ) <EOL> Wm . wm_protocol ( self . top , name = '<STR_LIT>' , func = self . quit ) <EOL> graph = Graph ( cframe ) <EOL> self . consoles [ '<STR_LIT>' ] = Object ( frame = graph , consoles = [ graph ] ) <EOL> self . graph = graph <EOL> self . graphVisible = False <EOL> self . updates = <NUM_LIT:0> <EOL> self . hostCount = len ( self . consoles [ '<STR_LIT>' ] . consoles ) <EOL> self . bw = <NUM_LIT:0> <EOL> self . pack ( expand = True , fill = '<STR_LIT>' ) <EOL> def updateGraph ( self , _console , output ) : <EOL> "<STR_LIT>" <EOL> m = re . search ( r'<STR_LIT>' , output ) <EOL> if not m : <EOL> return <EOL> val , units = float ( m . group ( <NUM_LIT:1> ) ) , m . group ( <NUM_LIT:2> ) <EOL> if units [ <NUM_LIT:0> ] == '<STR_LIT:M>' : <EOL> val *= <NUM_LIT:10> ** - <NUM_LIT:3> <EOL> elif units [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> val *= <NUM_LIT:10> ** - <NUM_LIT:6> <EOL> elif units [ <NUM_LIT:0> ] == '<STR_LIT:b>' : <EOL> val *= <NUM_LIT:10> ** - <NUM_LIT:9> <EOL> self . updates += <NUM_LIT:1> <EOL> self . bw += val <EOL> if self . updates >= self . hostCount : <EOL> self . graph . addBar ( self . bw ) <EOL> self . bw = <NUM_LIT:0> <EOL> self . updates = <NUM_LIT:0> <EOL> def setOutputHook ( self , fn = None , consoles = None ) : <EOL> "<STR_LIT>" <EOL> if consoles is None : <EOL> consoles = self . consoles [ '<STR_LIT>' ] . consoles <EOL> for console in consoles : <EOL> console . outputHook = fn <EOL> def createConsoles ( self , parent , nodes , width , title ) : <EOL> "<STR_LIT>" <EOL> f = Frame ( parent ) <EOL> consoles = [ ] <EOL> index = <NUM_LIT:0> <EOL> for node in nodes : <EOL> console = Console ( f , self . net , node , title = title ) <EOL> consoles . append ( console ) <EOL> row = index / width <EOL> column = index % width <EOL> console . grid ( row = row , column = column , sticky = '<STR_LIT>' ) <EOL> index += <NUM_LIT:1> <EOL> f . rowconfigure ( row , weight = <NUM_LIT:1> ) <EOL> f . columnconfigure ( column , weight = <NUM_LIT:1> ) <EOL> return f , consoles <EOL> def select ( self , groupName ) : <EOL> "<STR_LIT>" <EOL> if self . selected is not None : <EOL> self . selected . frame . pack_forget ( ) <EOL> self . selected = self . consoles [ groupName ] <EOL> self . selected . frame . pack ( expand = True , fill = '<STR_LIT>' ) <EOL> def createMenuBar ( self ) : <EOL> "<STR_LIT>" <EOL> f = Frame ( self ) <EOL> buttons = [ <EOL> ( '<STR_LIT>' , lambda : self . select ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , lambda : self . select ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , lambda : self . select ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , lambda : self . select ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , self . ping ) , <EOL> ( '<STR_LIT>' , self . iperf ) , <EOL> ( '<STR_LIT>' , self . stop ) , <EOL> ( '<STR_LIT>' , self . clear ) , <EOL> ( '<STR_LIT>' , self . quit ) <EOL> ] <EOL> for name , cmd in buttons : <EOL> b = Button ( f , text = name , command = cmd , ** self . menuStyle ) <EOL> b . pack ( side = '<STR_LIT:left>' ) <EOL> f . pack ( padx = <NUM_LIT:4> , pady = <NUM_LIT:4> , fill = '<STR_LIT:x>' ) <EOL> return f <EOL> def clear ( self ) : <EOL> "<STR_LIT>" <EOL> for console in self . selected . consoles : <EOL> console . clear ( ) <EOL> def waiting ( self , consoles = None ) : <EOL> "<STR_LIT>" <EOL> if consoles is None : <EOL> consoles = self . consoles [ '<STR_LIT>' ] . consoles <EOL> for console in consoles : <EOL> if console . waiting ( ) : <EOL> return True <EOL> return False <EOL> def ping ( self ) : <EOL> "<STR_LIT>" <EOL> consoles = self . consoles [ '<STR_LIT>' ] . consoles <EOL> if self . waiting ( consoles ) : <EOL> return <EOL> count = len ( consoles ) <EOL> i = <NUM_LIT:0> <EOL> for console in consoles : <EOL> i = ( i + <NUM_LIT:1> ) % count <EOL> ip = consoles [ i ] . node . IP ( ) <EOL> console . sendCmd ( '<STR_LIT>' + ip ) <EOL> def iperf ( self ) : <EOL> "<STR_LIT>" <EOL> consoles = self . consoles [ '<STR_LIT>' ] . consoles <EOL> if self . waiting ( consoles ) : <EOL> return <EOL> count = len ( consoles ) <EOL> self . setOutputHook ( self . updateGraph ) <EOL> for console in consoles : <EOL> console . node . cmd ( '<STR_LIT>' ) <EOL> i = <NUM_LIT:0> <EOL> for console in consoles : <EOL> i = ( i + <NUM_LIT:1> ) % count <EOL> ip = consoles [ i ] . node . IP ( ) <EOL> console . sendCmd ( '<STR_LIT>' + ip ) <EOL> def stop ( self , wait = True ) : <EOL> "<STR_LIT>" <EOL> consoles = self . consoles [ '<STR_LIT>' ] . consoles <EOL> for console in consoles : <EOL> console . handleInt ( ) <EOL> if wait : <EOL> for console in consoles : <EOL> console . waitOutput ( ) <EOL> self . setOutputHook ( None ) <EOL> quietRun ( '<STR_LIT>' ) <EOL> def quit ( self ) : <EOL> "<STR_LIT>" <EOL> self . stop ( wait = False ) <EOL> Frame . quit ( self ) <EOL> def assign ( obj , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> obj . __dict__ . update ( kwargs ) <EOL> class Object ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , ** kwargs ) : <EOL> assign ( self , ** kwargs ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> setLogLevel ( '<STR_LIT:info>' ) <EOL> network = TreeNet ( depth = <NUM_LIT:2> , fanout = <NUM_LIT:4> ) <EOL> network . start ( ) <EOL> app = ConsoleApp ( network , width = <NUM_LIT:4> ) <EOL> app . mainloop ( ) <EOL> network . stop ( ) </s>
