############################################################## # # Name: monitor.py # # Description: buddycast algorithm # # Usage: monitor buddy cast # act as super node for initial ip registration # # author: Jun Wang j.wang@ewi.tudelft.nl # ############################################################## import sys, time from cmd import * from connectinterface import * from node import node from receiver import * import SocketServer # get socket server, handler objects from BitTornado.bencode import bencode, bdecode from skotvdataread import readSKOData def now(): return time.ctime(time.time()) class CmdInterface(Cmd): # command for access the peer cache def do_addpeer(self,argc): # say hello command superpeer.randCache.add(argc) def help_addpeer(self): # say hello help print """ addpeer add a peer to the peer list """ def do_showpeer(self,argc): for a in superpeer.randCache.getAll(): print a def do_status(self,argc): print 'num. of registered peers:', print superpeer.randCache.getLength() print 'numOPL:', superpeer.status['numOPL'], print 'numBS:', superpeer.status['numBS'] def do_shift(self,argc): superpeer.randCache._shift() def do_print(self,argc): superpeer.randCache._print() def do_length(self,argc): print superpeer.randCache.getLength() def do_removepeer(self,argc): superpeer.randCache.remove() def do_getpeer(self,argc): print superpeer.randCache.getPeer(argc) def do_sort(self,argc): results = superpeer.randCache.sortedby(argc) for a in results: print a # command to contact remote peers def do_getpeercache(argc):# argc is peer id peerid = int(argc) superpeer.randCache.getConnectInfo(peerid) def do_shell(self,argc): # system command import os os.system(argc) # three exit method def do_EOF(self,arc): return 1 def do_exit(self,arc): return 1 def do_quit(self,arc): return 1 def emptyline(self): print "Hey, man! Don't know the command? :) type help" # super node have a user profiles data class supernode(node): status = {} status['numOPL'] = 0 status['numREG'] = 0 status['numBS']= 0 def readUserprofiles(self): self.userprofiles = readSKOData() def showUserprofiles(self): for a in self.userprofiles.keys(): print '\nUsers:', a size = len(self.userprofiles[a]) for b in range( size ): print '(', self.userprofiles[a][b][0], ',' , self.userprofiles[a][b][1],')', superpeer = supernode(cachesize = 2000) # for superpeer randCache store all the peers address print '\nStarting to read userprofiles...\n' superpeer.readUserprofiles() #superpeer.showUserprofiles() handle = monitor_receiver listenPort = 20000 superpeer.startMonitorServer(handle,listenPort) welcome = ''' _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ _/ _/ _/ welcome to the monitor interface for BUDDYCAST! _/ _/ Author: Jun Wang, j.wang@ewi.tudelft.nl _/ _/ Date: June, 2005 _/ _/ _/ _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ ''' x = CmdInterface() x.prompt="BUDDYCAST: " x.cmdloop(welcome)