Revision d0e53ca8 examples/consoles.py

View differences:

examples/consoles.py
11 11

  
12 12
from mininet.log import setLogLevel
13 13
from mininet.topolib import TreeNet
14
from mininet.term import makeTerms, cleanUpScreens
14 15

  
15 16
class Console( Frame ):
16 17
    "A simple console on a host."
17 18
    
18
    def __init__( self, parent, node, height=16, width=32 ):
19
    def __init__( self, parent, net, node, height=10, width=32 ):
19 20
        Frame.__init__( self, parent )
21
        self.net = net
20 22
        self.node = node
23
        self.prompt = node.name + '# '
21 24
        self.height, self.width = height, width
22 25
        self.text = self.makeWidgets( )
23 26
        self.bindEvents()
27
        self.append( self.prompt )
24 28

  
25 29
    def makeWidgets( self ):
26 30
        "Make a label, a text area, and a scroll bar."
27
        labelStyle = {
31
        buttonStyle = {
28 32
            'font': 'Monaco 7',
29
            'relief': 'sunken'
30 33
        }
31 34
        textStyle = { 
32 35
            'font': 'Monaco 7',
......
34 37
            'fg': 'green',
35 38
            'width': self.width,
36 39
            'height': self.height,
37
            'relief': 'sunken'
40
            'relief': 'sunken',
41
            'insertbackground': 'green',
42
            'highlightcolor': 'green',
43
            'selectforeground': 'black',
44
            'selectbackground': 'green'
38 45
        }
39
        label = Label( self, text=self.node.name, **labelStyle )
46
        def newTerm( net=self.net, node=self.node ):
47
            "Pop up a new terminal window for a node."
48
            net.terms += makeTerms( [ node ] )
49
        label = Button( self, text=self.node.name, command=newTerm, **buttonStyle )
40 50
        label.pack( side='top', fill='x' )
41 51
        text = Text( self, wrap='word', **textStyle )
42 52
        ybar = Scrollbar( self, orient='vertical', command=text.yview )
......
49 59
        "Bind keyboard and file events."
50 60
        self.text.bind( '<Return>', self.handleReturn )
51 61
        self.text.bind( '<Control-c>', self.handleInt )
52
        self.text.bind( '<KeyPress>', self.handleKey )
62
        # self.text.bind( '<KeyPress>', self.handleKey )
53 63
        # This is not well-documented, but it is the correct
54 64
        # way to trigger a file event handler from Tk's
55 65
        # event loop!
......
68 78
    
69 79
    def handleReturn( self, event ):
70 80
        "Handle a carriage return."
71
        print "handleReturn"
72
    
81
        cmd = self.text.get( 'insert linestart', 'insert lineend' )
82
        if cmd.find( self.prompt ) == 0:
83
            cmd = cmd[ len( self.prompt ): ]
84
        self.sendCmd( cmd )
85
        
73 86
    def handleInt( self, event=None ):
74 87
        "Handle control-c."
75 88
        self.node.sendInt()
......
82 95
    def handleReadable( self, file, mask ):
83 96
        data = self.node.monitor()
84 97
        self.append( data )
98
        if not self.node.waiting:
99
            # Print prompt, just for the heck of it
100
            self.append( self.prompt )
85 101

  
86
class Consoles( Frame ):
102
class ConsoleApp( Frame ):
87 103

  
88
    def __init__( self, nodes, parent=None, width=4 ):
104
    def __init__( self, net, nodes, parent=None, width=4 ):
89 105
        Frame.__init__( self, parent )
106
        self.top = self.winfo_toplevel()
107
        self.top.title( 'Mininet' )
108
        self.net = net
90 109
        self.nodes = nodes
91 110
        self.createMenuBar( font='Geneva 7' )
92 111
        self.consoles = self.createConsoles( nodes, width )
93 112
        self.pack( expand=True, fill='both' )
113
        cleanUpScreens()
94 114
        
95 115
    def createConsoles( self, nodes, width ):
96 116
        "Create a grid of consoles in a frame."
......
99 119
        consoles = []
100 120
        index = 0
101 121
        for node in nodes:
102
            console = Console( f, node )
122
            console = Console( f, net, node )
103 123
            consoles.append( console )
104 124
            row = int( index / width )
105 125
            column = index % width
......
128 148
        "Tell each console to ping the next one."
129 149
        consoles = self.consoles
130 150
        count = len( consoles )
131
        i = 1
151
        i = 0
132 152
        for console in consoles:
153
            i = ( i + 1 ) % count
133 154
            ip = consoles[ i ].node.IP()
134 155
            console.sendCmd( 'ping ' + ip )
135
            i = ( i + 1 ) % count
136 156
            
137 157
    def stop( self ):
138 158
        "Interrupt all consoles."
......
142 162
    
143 163
if __name__ == '__main__':
144 164
    setLogLevel( 'info' )
145
    net = TreeNet( depth=2, fanout=2 )
165
    net = TreeNet( depth=2, fanout=4 )
146 166
    net.start()
147
    app = Consoles( net.hosts, width=2 )
167
    app = ConsoleApp( net, net.hosts, width=4 )
148 168
    app.mainloop()
149 169
    net.stop()

Also available in: Unified diff