Beispiel #1
0
 def _onTimer(self):
     self.timerOp = None
     localIP = getLocalIP()
     if localIP is None:
         self._initTimer()
         return
     nodeAddrList = self.nodeTable.getLiveNodes()
     if not nodeAddrList:
         self._initTimer()
         return
     tcpListener = None
     udpSock = None
     for port in xrange(10001, 20000):
         addr = (localIP, port)
         try:
             tcpListener = tcpListen(addr, self.reactor, None)
         except sock_error:
             continue
         udpSock = socket(AF_INET, SOCK_DGRAM)
         try:
             udpSock.bind(addr)
         except sock_error:
             udpSock.close()
             tcpListener.close()
             udpSock = tcpListener = None
             continue
         break
     if tcpListener is None:
         self._initTimer()
         return
     self.rpcSock = RPCSocket(udpSock, self.reactor)
     self.tcpListener = tcpListener
     self.firewallOp = _testFirewall(self.tcpListener, self.rpcSock,
                                     nodeAddrList, localIP, self.reactor,
                                     self._onFirewallCheck)
Beispiel #2
0
def getLocation( publicKey ) :
    reactor = SelectReactor()
    udpSock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
    udpSock.bind( ('',0) )
    rpcSocket = RPCSocket( udpSock, reactor )
    dhtClient = DHTClient( rpcSocket )
    nodeAddr = ('210.210.1.102',10001)
    nodeTable = NodeTable( [nodeAddr] )
    locList = []
    def onLookup( location ) :
        locList.append( location )
        reactor.stop()
    lookupUser( publicKey, dhtClient, nodeTable, onLookup )
    reactor.run()
    rpcSocket.close()
    return locList[0]
Beispiel #3
0
 def _onTimer( self ) :
     self.timerOp = None
     localIP = getLocalIP()
     if localIP is None :
         self._initTimer()
         return
     nodeAddrList = self.nodeTable.getLiveNodes()
     if not nodeAddrList :
         self._initTimer()
         return
     tcpListener = None
     udpSock = None
     for port in xrange(10001,20000) :
         addr = (localIP,port)
         try :
             tcpListener = tcpListen( addr, self.reactor, None )
         except sock_error :
             continue
         udpSock = socket( AF_INET, SOCK_DGRAM )
         try :
             udpSock.bind( addr )
         except sock_error :
             udpSock.close()
             tcpListener.close()
             udpSock = tcpListener = None
             continue
         break
     if tcpListener is None :
         self._initTimer()
         return
     self.rpcSock = RPCSocket( udpSock, self.reactor )
     self.tcpListener = tcpListener
     self.firewallOp = _testFirewall( self.tcpListener,
             self.rpcSock, nodeAddrList, localIP, self.reactor,
             self._onFirewallCheck )
Beispiel #4
0
def startNode( reactor, nodeAddr, knownNodes ) :
    print 'starting node: %s' % str(nodeAddr)
    sock = socket( AF_INET, SOCK_DGRAM )
    sock.bind( nodeAddr )
    rpcSocket = RPCSocket( sock, reactor )
    node = DHTNode( rpcSocket, reactor, knownNodes )
    return node
Beispiel #5
0
 def __init__(self, nodeAddr):
     self.nodeAddr = nodeAddr
     self.sock = socket(AF_INET, SOCK_DGRAM)
     self.sock.bind(('', 0))
     self.reactor = SelectReactor()
     self.rpcSocket = RPCSocket(self.sock, self.reactor)
     self.client = DHTClient(self.rpcSocket)
     self.client.timeout = 1.5
     self.client.retries = 1
     self.client.backoff = 1
Beispiel #6
0
def startNetworkNode(ipAddr, reactor, knownNodes=[]):
    for port in xrange(10001, 20000):
        addr = (ipAddr, port)
        try:
            listener = tcpListen(addr, reactor, None)
        except sock_error:
            continue
        sock = socket(AF_INET, SOCK_DGRAM)
        try:
            sock.bind(addr)
        except sock_error:
            sock.close()
            listener.close()
            continue
        rpcSock = RPCSocket(sock, reactor)
        node = NetworkNode(listener, rpcSock, reactor, knownNodes)
        return node
Beispiel #7
0
    def goOnline(self, profile):
        assert self.sm.current() == self.OFFLINE
        obj = Dummy()

        def doCleanup():
            if obj.initNodeTableOp is not None:
                obj.initNodeTableOp.cancel()
            if obj.nodeTableRefresher is not None:
                obj.nodeTableRefresher.close()
            if obj.listener is not None:
                obj.listener.close()
            obj.rpcSocket.close()

        def onOffline():
            #self.nodeRunner.close()
            #self.nodeRunner = None
            self.listener.close()
            self.listener = None
            self.permissions = None
            self.locationCache.close()
            self.locationCache = None
            self.nodeTableRefresher.close()
            self.nodeTableRefresher = None
            _saveNodeCache(self.nodeTable)
            self.nodeTable = None
            self.dhtClient = None
            self.rpcSocket.close()
            self.rpcSocket = None
            self.profile = None

        def onListenerStart(result):
            if not result:
                self.sm.removeCallback(obj.callbackId)
                doCleanup()
                self.sm.change(self.OFFLINE)
                return
            obj.listener.setCloseCallback(self._onListenerClose)
            obj.listener.setIncomingCallback(self._onIncoming)
            self.profile = profile
            self.rpcSocket = obj.rpcSocket
            self.dhtClient = obj.dhtClient
            self.nodeTable = obj.nodeTable
            self.nodeTableRefresher = obj.nodeTableRefresher
            self.locationCache = LocationCache(self.dhtClient, self.nodeTable,
                                               self.reactor)
            self.listener = obj.listener
            self.permissions = Permissions(profile, self.services.keys())
            if self.permissions.isModified():
                self.permissions.savePermissions()
            #self.nodeRunner = NodeRunner( self.nodeTable, self.reactor )
            self.sm.removeCallback(obj.callbackId)
            self.sm.appendCallback(onOffline, dest=self.OFFLINE, single=True)
            self.sm.change(self.ONLINE)

        def onNodeTableInit():
            obj.initNodeTableOp = None
            obj.nodeTableRefresher = NodeTableRefresher(
                obj.nodeTable, obj.dhtClient, self.reactor)
            updateLevelStore = UpdateLevelStore(profile)
            obj.listener = UserListener(profile.name, profile.rsaKey,
                                        updateLevelStore, obj.dhtClient,
                                        obj.nodeTable, self.reactor)
            obj.listener.start(onListenerStart)

        self.sm.change(self.CONNECTING)
        obj.callbackId = self.sm.insertCallback(doCleanup,
                                                src=self.CONNECTING,
                                                single=True)
        obj.nodeTable = NodeTable(self.seedNodes)
        #_loadNodeCache( obj.nodeTable )
        udpSock = socket(AF_INET, SOCK_DGRAM)
        udpSock.bind(('', 0))
        obj.rpcSocket = RPCSocket(udpSock, self.reactor)
        obj.dhtClient = DHTClient(obj.rpcSocket, nodeTracker=obj.nodeTable)
        obj.initNodeTableOp = initNodeTable(obj.nodeTable, obj.dhtClient,
                                            self.reactor, onNodeTableInit)
        obj.nodeTableRefresher = None
        obj.listener = None
Beispiel #8
0
class NodeRunner( object ) :
    def __init__( self, nodeTable, reactor ) :
        self.nodeTable = nodeTable
        self.reactor = reactor
        self.timerOp = None
        self.firewallOp = None
        self.rpcSock = None
        self.tcpListener = None
        self.router = None
        self.dhtNode = None
        self._initTimer()

    def close( self ) :
        if self.timerOp :
            self.timerOp.cancel()
            self.timerOp = None
        if self.firewallOp :
            self.firewallOp.cancel()
            self.firewallOp = None
        if self.rpcSock :
            self.rpcSock.close()
            self.rpcSock = None
        if self.tcpListener :
            self.tcpListener.close()
            self.tcpListener = None
        if self.router is not None :
            self.router.close()
            self.router = None
        if self.dhtNode is not None :
            self.dhtNode.close()
            self.dhtNode = None

    def _initTimer( self ) :
        timeout = NODE_CHECK_TIME_DELAY
        self.timerOp = self.reactor.callLater( timeout, self._onTimer )

    def _onTimer( self ) :
        self.timerOp = None
        localIP = getLocalIP()
        if localIP is None :
            self._initTimer()
            return
        nodeAddrList = self.nodeTable.getLiveNodes()
        if not nodeAddrList :
            self._initTimer()
            return
        tcpListener = None
        udpSock = None
        for port in xrange(10001,20000) :
            addr = (localIP,port)
            try :
                tcpListener = tcpListen( addr, self.reactor, None )
            except sock_error :
                continue
            udpSock = socket( AF_INET, SOCK_DGRAM )
            try :
                udpSock.bind( addr )
            except sock_error :
                udpSock.close()
                tcpListener.close()
                udpSock = tcpListener = None
                continue
            break
        if tcpListener is None :
            self._initTimer()
            return
        self.rpcSock = RPCSocket( udpSock, self.reactor )
        self.tcpListener = tcpListener
        self.firewallOp = _testFirewall( self.tcpListener,
                self.rpcSock, nodeAddrList, localIP, self.reactor,
                self._onFirewallCheck )
    
    def _onFirewallCheck( self, result ) :
        self.firewallOp = None
        if not result :
            self.rpcSock.close()
            self.rpcSock = None
            self.tcpListener.close()
            self.tcpListener = None
            self._initTimer()
            return
        self.router = Router( self.tcpListener, self.reactor )
        self.dhtNode = DHTNode( self.rpcSock, self.reactor,
                self.nodeTable.getLiveNodes() )
        self.tcpListener = None
        self.rpcSock = None
Beispiel #9
0
class NodeRunner(object):
    def __init__(self, nodeTable, reactor):
        self.nodeTable = nodeTable
        self.reactor = reactor
        self.timerOp = None
        self.firewallOp = None
        self.rpcSock = None
        self.tcpListener = None
        self.router = None
        self.dhtNode = None
        self._initTimer()

    def close(self):
        if self.timerOp:
            self.timerOp.cancel()
            self.timerOp = None
        if self.firewallOp:
            self.firewallOp.cancel()
            self.firewallOp = None
        if self.rpcSock:
            self.rpcSock.close()
            self.rpcSock = None
        if self.tcpListener:
            self.tcpListener.close()
            self.tcpListener = None
        if self.router is not None:
            self.router.close()
            self.router = None
        if self.dhtNode is not None:
            self.dhtNode.close()
            self.dhtNode = None

    def _initTimer(self):
        timeout = NODE_CHECK_TIME_DELAY
        self.timerOp = self.reactor.callLater(timeout, self._onTimer)

    def _onTimer(self):
        self.timerOp = None
        localIP = getLocalIP()
        if localIP is None:
            self._initTimer()
            return
        nodeAddrList = self.nodeTable.getLiveNodes()
        if not nodeAddrList:
            self._initTimer()
            return
        tcpListener = None
        udpSock = None
        for port in xrange(10001, 20000):
            addr = (localIP, port)
            try:
                tcpListener = tcpListen(addr, self.reactor, None)
            except sock_error:
                continue
            udpSock = socket(AF_INET, SOCK_DGRAM)
            try:
                udpSock.bind(addr)
            except sock_error:
                udpSock.close()
                tcpListener.close()
                udpSock = tcpListener = None
                continue
            break
        if tcpListener is None:
            self._initTimer()
            return
        self.rpcSock = RPCSocket(udpSock, self.reactor)
        self.tcpListener = tcpListener
        self.firewallOp = _testFirewall(self.tcpListener, self.rpcSock,
                                        nodeAddrList, localIP, self.reactor,
                                        self._onFirewallCheck)

    def _onFirewallCheck(self, result):
        self.firewallOp = None
        if not result:
            self.rpcSock.close()
            self.rpcSock = None
            self.tcpListener.close()
            self.tcpListener = None
            self._initTimer()
            return
        self.router = Router(self.tcpListener, self.reactor)
        self.dhtNode = DHTNode(self.rpcSock, self.reactor,
                               self.nodeTable.getLiveNodes())
        self.tcpListener = None
        self.rpcSock = None