autorun: update Commander
authorMarius Sandu-Popa <sandupopamarius@gmail.com>
Fri, 30 Apr 2010 10:14:47 +0000 (13:14 +0300)
committerMarius Sandu-Popa <sandupopamarius@gmail.com>
Fri, 30 Apr 2010 10:15:06 +0000 (13:15 +0300)
autorun/commander/Commander.py

index e06a5bb..f790ffd 100644 (file)
@@ -1,6 +1,7 @@
 import sys, socket
 import os
 import re
+import readline
 from Util import *
 from XMLParser import *
 from threading import Thread
@@ -21,19 +22,21 @@ class Commander(Thread):
                'boot':[self.nodeC,     'Bootstrap server. Options: all | x .'],
                'start': [self.clientC, 'Start clients. Options: all | x | x,y .'],
                'stop': [self.clientC,  'Stop clients. Options: all | x | x,y .'],
-               'status': [self.clientC,        'Status clients. Options: all | x | x,y .'],
+               'status': [self.clientC,'Status clients. Options: all | x | x,y .'],
                'archive': [self.nodeC, 'Archive output information of clients.'+\
                                        'Options: all | x .'],
                'getclients': [self.nodeC,'Get running clients. Options: all | x .'],
                'getoutput': [self.nodeC,'Get clients output. Options: all | x .'],
-               'help': [self.simpleC,  "Show This Message."],
-               'exit': [self.simpleC,  "Exit Commander."]
+               'help': [self.simpleC,  'Show This Message.'],
+               'exit': [self.simpleC,  'Exit Commander.']
                }
        
        def simpleC(self, comm, argv):
                if comm == 'list':
                        for node in self.nodes.getNodes():
                                self.printNodeInfo(node)
+                               si = self.swarm.getSIByNode(node)
+                               self.printClientInfo(si)
                if comm == 'help':
                        print "List of commands:"
                        keys = self.commands.keys()
@@ -137,19 +140,26 @@ class Commander(Thread):
                                                        int(node.listen_port), 
                                                        si.pid)
        
+       
+       #{'download_size': 183509218, 'num_peers': 2, 'eta': 0, 'timestamp': datetime.datetime(2010, 4, 30, 9, 6, 27), 'upload_speed': 0, 'download_speed': 0, 'dht': 0}
        def statusSI(self, node, si):
                ret = self.Commander.status(node.public_address, 
                                                        int(node.listen_port), 
                                                        si.pid)
-               if ret: print ret
+               if ret:
+                       self.printNodeInfo(node)
+                       self.printClientInfo(si)
+                       for k,v in ret.items():
+                               if k=='timestamp':
+                                       print "\t" + k +": " + v.strftime("%H:%M:%S %d/%m/%y")
+                               else:
+                                       print "\t" + k +": " + str(v)
                
        def getNodeStatus(self, node):  
                ret = self.Commander.getClients(node.public_address, 
                                        int(node.listen_port)) 
                if ret:
-                       print 'node ' + node.id +  ' (' + node.public_address +\
-                               ":" +node.public_port  + " -> " + node.private_address +\
-                               ":" +node.listen_port  +' ):'
+                       self.printNodeInfo(node)
                        for l in ret:
                                print " -" + l[0] + " client(pid:" + str(l[1]) +") on \"" + l[2] +"\""
 
@@ -157,9 +167,7 @@ class Commander(Thread):
                ret = self.Commander.getOutput(node.public_address, 
                                        int(node.listen_port)) 
                if ret:
-                       print 'node ' + node.id +  ' (' + node.public_address +\
-                               ":" +node.public_port  + " -> " + node.private_address +\
-                               ":" +node.listen_port  +' ):'
+                       self.printNodeInfo(node)
                        for l in ret:
                                print " -" + l[0] + " client on \"" + l[1] + "\" outputs at:"
                                for ll in l[2:]:
@@ -170,11 +178,9 @@ class Commander(Thread):
                ret = self.Commander.archive(node.public_address, 
                                        int(node.listen_port))
                if ret:
-                       print 'node ' + node.id +  ' (' + node.public_address +\
-                               ":" +node.public_port  + " -> " + node.private_address +\
-                               ":" +node.listen_port  +' ):'
+                       self.printNodeInfo(node)
                        for l in ret:
-                               print "archiving ... " +l
+                               print " archiving ... " +l
                                
        def cleanNode(self, node):
                pass    
@@ -232,17 +238,20 @@ class Commander(Thread):
                                (self.commands[argv[0]][0])(argv[0], argv[1:])
                        except KeyError:
                                print "Unknown command"
+                       except EOFError, KeyboardException:
+                               sys.exit()
        
        def printNodeInfo(self, node):
-               si = self.swarm.getSIByNode(node)
                print 'node ' + node.id +  ' (' + node.public_address +\
                        ":" +node.public_port  + " -> " + node.private_address +\
-                       ":" +node.listen_port  +' ):'
+                       ":" +node.listen_port  +')'
+       
+       def printClientInfo(self, si):
                print ' - client ' + si.id +' (' + si.btclient +  ', Down:' + si.download_limit + \
                        " KB/s" + ", Up:" +si.upload_limit  + " KB/s" + ' )' 
        
        def about(self):
-               print "Commander version 1.1"
+               print "Commander version 1.1.1"
                print "Enter \"help\" for instructions"
        
 if __name__ == "__main__":