Beispiel #1
0
class Node():
    def __init__(self, mylog, myIdentity=None, netAccess=None):
        self.smashlog = mylog
        
        if myIdentity is None:
            self.identity = Identity(mylog)
        else:
            self.identity = myIdentity
            
        if netAccess is None:
            self.net = Network(mylog)
        else:
            self.net = netAccess
        
    def __str__(self):
        res = "\n---Node Information---\n"
        res += str(self.identity) + "\n"
        res += str(self.net) + "\n"
        res += "----------------------"
        return res
        
    def run(self):
        # Booting up network
        self.smashlog.log(1,"Booting up network for node %s" % self.identity.getID())
        if(self.net.startNetwork()):
            self.smashlog.log(3, "Error starting server for node %s" % self.identity.getID())
            return -1
        
        self.smashlog.log(1,"Node %s is up:\n%s" % (self.identity.getID(), str(self.net)))

        try:
            while True:
                self.net.listenForNewConnections()
                time.sleep(0.05)
        except KeyboardInterrupt:
            self.net.cleanup()
            self.smashlog.log(1, "Caught user interrupt! Shutting down node %s" % self.identity.getID())
            self.smashlog.close()