Statistics
| Branch: | Tag: | Revision:

mininet / examples / xterms.py @ 55dd9368

History | View | Annotate | Download (1.46 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Create a network and run an xterm (connected via screen(1) ) on each
5
host. Requires xterm(1) and GNU screen(1).
6
"""
7

    
8
import os, re
9
from subprocess import Popen
10
from mininet import init, TreeNet, Cli, quietRun
11

    
12
def makeXterm( node, title ):
13
   "Run screen on a node, and hook up an xterm."
14
   node.cmdPrint( 'screen -dmS ' + node.name )
15
   title += ': ' + node.name
16
   if not node.inNamespace: title += ' (root)'
17
   cmd = [ 'xterm', '-title', title ]
18
   cmd += [ '-e', 'screen', '-D', '-RR', '-S', node.name ]
19
   return Popen( cmd )
20

    
21
def cleanUpScreens():
22
   "Remove moldy old screen sessions."      
23
   r = r'(\d+.[hsc]\d+)'
24
   output = quietRun( 'screen -ls' ).split( '\n' )
25
   for line in output:
26
      m = re.search( r, line )
27
      if m is not None:
28
         quietRun( 'screen -S ' + m.group( 1 ) + ' -X kill' )
29
   
30
def makeXterms( nodes, title ):
31
   terms = []
32
   for node in nodes:
33
      if not node.execed:
34
         terms += [ makeXterm( node, title ) ]
35
   return terms
36

    
37
def xterms( controllers, switches, hosts ):
38
   cleanUpScreens()
39
   terms = []
40
   terms += makeXterms( controllers, 'controller' )
41
   terms += makeXterms( switches, 'switch' )
42
   terms += makeXterms( hosts, 'host' )
43
   # Wait for xterms to exit
44
   for term in terms: 
45
      os.waitpid( term.pid, 0 )
46
   cleanUpScreens()
47
   
48
if __name__ == '__main__':
49
   init()
50
   print "Running xterms on", os.environ[ 'DISPLAY' ]
51
   network = TreeNet( depth=2, fanout=2, kernel=True )
52
   network.run( xterms )