示例#1
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
示例#2
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
示例#3
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]
示例#4
0
文件: node.py 项目: hj91/cspace
 def __init__(self, rpcSocket, reactor, knownNodes=[]):
     self.rpcSocket = rpcSocket
     self.reactor = reactor
     self.rpcSocket.setRequestCallback(self._onInput)
     self.store = DataStore()
     self.ktable = KTable(idToNum(addrToId(rpcSocket.getAddr())))
     self.ktracker = KTracker(self.ktable)
     for nodeAddr in knownNodes:
         self.ktable.nodeSeen(nodeAddr)
     self.client = DHTClient(rpcSocket, False, self.ktracker)
     self.requestTable = {}
     for x in MESSAGES:
         self.requestTable[x] = getattr(self, 'do%s' % x)
     self.otherOps = set()
     self._initNodeRefresher()
     self._initDataTimer()
示例#5
0
文件: noderunner.py 项目: hj91/cspace
def _testFirewall(tcpListener,
                  rpcSock,
                  nodeAddrList,
                  localIP,
                  reactor,
                  callback=None):
    def doCancel():
        for checkOp in checkOps:
            checkOp.cancel()
        checkOps.clear()
        testServer.close()

    def onCheck(checkOp, err, payload):
        checkOps.remove(checkOp)
        if err >= 0:
            fwResult, fwToken = payload
            if (fwResult >= 0) and (fwToken == token):
                successList.append(1)
        if not checkOps:
            testServer.close()
            # n(success)/n(nodes) >= 1/2
            op.notify(2 * len(successList) >= len(nodeAddrList))

    def doCheck(nodeAddr):
        checkOp = dhtClient.callFirewallCheck(
            localIP, nodeAddr, lambda e, p: onCheck(checkOp, e, p))
        checkOps.add(checkOp)

    assert nodeAddrList
    successList = []
    checkOps = set()
    token = rand_bytes(20)
    dhtClient = DHTClient(rpcSock)
    testServer = FirewallTestServer(tcpListener, token, reactor)
    for nodeAddr in nodeAddrList:
        doCheck(nodeAddr)
    op = AsyncOp(callback, doCancel)
    return op
示例#6
0
class TestClient(object):
    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

    def _initCount(self):
        self.startRequestCount = _requestCount

    def _getCount(self):
        return _requestCount - self.startRequestCount

    def _doneCount(self):
        count = self._getCount()
        print 'request count =', count

    def _onResult(self, err, payload):
        print 'err=%d, payload=%s' % (err, str(payload))
        self._doneCount()
        self.reactor.stop()

    def _initCount(self):
        self.startRequestCount = _requestCount

    def Ping(self):
        self._initCount()
        self.client.callPing(self.nodeAddr, self._onResult)
        self.reactor.run()

    def GetAddr(self):
        self._initCount()
        self.client.callGetAddr(self.nodeAddr, self._onResult)
        self.reactor.run()

    def GetKey(self, publicKey):
        self._initCount()
        self.client.callGetKey(publicKey.toDER_PublicKey(), self.nodeAddr,
                               self._onResult)
        self.reactor.run()

    def PutKey(self, rsaKey, data, updateLevel):
        self._initCount()
        signature = computeSignature(rsaKey, data, updateLevel)
        self.client.callPutKey(rsaKey.toDER_PublicKey(), data, updateLevel,
                               signature, self.nodeAddr, self._onResult)
        self.reactor.run()

    def FindNodes(self, destId):
        self._initCount()
        self.client.callFindNodes(destId, self.nodeAddr, self._onResult)
        self.reactor.run()

    def Lookup(self, destId):
        def onResult(result):
            print 'result = %s' % str(result)
            self._doneCount()
            self.reactor.stop()

        self._initCount()
        self.client.lookup(destId, [self.nodeAddr], onResult)
        self.reactor.run()

    def LookupGetKey(self, publicKey):
        def onResult(result):
            print 'result = %s' % str(result)
            self._doneCount()
            self.reactor.stop()

        self._initCount()
        self.client.lookupGetKey(publicKey, [self.nodeAddr], onResult)
        self.reactor.run()

    def LookupPutKey(self, rsaKey, data, updateLevel):
        def onResult(successCount, latestUpdateLevel):
            print 'successCount=%d, latestUpdateLevel=%d' % (successCount,
                                                             latestUpdateLevel)
            self._doneCount()
            self.reactor.stop()

        self._initCount()
        self.client.lookupPutKey(rsaKey, data, updateLevel, [self.nodeAddr],
                                 onResult)
        self.reactor.run()
示例#7
0
文件: session.py 项目: hj91/cspace
    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
示例#8
0
class TestClient( object ) :
    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

    def _initCount( self ) :
        self.startRequestCount = _requestCount

    def _getCount( self ) :
        return _requestCount - self.startRequestCount

    def _doneCount( self ) :
        count = self._getCount()
        print 'request count =', count

    def _onResult( self, err, payload ) :
        print 'err=%d, payload=%s' % (err,str(payload))
        self._doneCount()
        self.reactor.stop()

    def _initCount( self ) :
        self.startRequestCount = _requestCount

    def Ping( self ) :
        self._initCount()
        self.client.callPing( self.nodeAddr, self._onResult )
        self.reactor.run()

    def GetAddr( self ) :
        self._initCount()
        self.client.callGetAddr( self.nodeAddr, self._onResult )
        self.reactor.run()

    def GetKey( self, publicKey ) :
        self._initCount()
        self.client.callGetKey( publicKey.toDER_PublicKey(),
                self.nodeAddr, self._onResult )
        self.reactor.run()

    def PutKey( self, rsaKey, data, updateLevel ) :
        self._initCount()
        signature = computeSignature( rsaKey, data, updateLevel )
        self.client.callPutKey( rsaKey.toDER_PublicKey(), data,
                updateLevel, signature, self.nodeAddr,
                self._onResult )
        self.reactor.run()

    def FindNodes( self, destId ) :
        self._initCount()
        self.client.callFindNodes( destId, self.nodeAddr, self._onResult )
        self.reactor.run()

    def Lookup( self, destId ) :
        def onResult( result ) :
            print 'result = %s' % str(result)
            self._doneCount()
            self.reactor.stop()
        self._initCount()
        self.client.lookup( destId, [self.nodeAddr], onResult )
        self.reactor.run()

    def LookupGetKey( self, publicKey ) :
        def onResult( result ) :
            print 'result = %s' % str(result)
            self._doneCount()
            self.reactor.stop()
        self._initCount()
        self.client.lookupGetKey( publicKey, [self.nodeAddr], onResult )
        self.reactor.run()

    def LookupPutKey( self, rsaKey, data, updateLevel ) :
        def onResult( successCount, latestUpdateLevel ) :
            print 'successCount=%d, latestUpdateLevel=%d' % (
                    successCount, latestUpdateLevel)
            self._doneCount()
            self.reactor.stop()
        self._initCount()
        self.client.lookupPutKey( rsaKey, data, updateLevel,
                [self.nodeAddr], onResult )
        self.reactor.run()