Exemplo n.º 1
0
def _makeConnection(ip, port, timeout):
    '''Make a new connection.'''
    
    d = defer.Deferred()
    d.addErrback(Utils.getConnErrorback)
    factory = GmuClientFactory(d)   # pb.PBClientFactory()
    factory.setConnectionLostDefered(d)# Fire this deferred when connection lost
    factory.noisy = False
    
    con = reactor.connectTCP(ip, port, factory, timeout)
    
    # Store it in the dict
    key = _makeKey(ip, port, timeout)
    connections[key] = (factory,con)
    
    # Override the conncetion's "disconnect" method so it remains cached.
    con._origDisconnect = con.disconnect
    con.disconnect = types.MethodType(cacheDisconnect, con)
    #con._gmuFinalDisconnect = types.MethodType(_gmuFinalDisconnect, con)
    
    # Some cache specific vars
    con._gmuConnects = 1
    con._gmuDisconnects = 0
    con._gmuLastDisconnectTime = None
    con._gmuKey  = key
    
    return (factory,con)
Exemplo n.º 2
0
def getRemoteConnection(nodeLocation, metricsMessageCounter=None):
    '''Return a factory and connection to a remote node'''
    

    
    if Config.USE_CONNECTION_CACHE:
        (factory, con) = ConnectionCache.connectTCP(nodeLocation.ip, nodeLocation.port, timeout=Config.NETWORK_CONNECTION_TIMEOUT, metricsCounter=metricsMessageCounter)
    else:
        if metricsMessageCounter != None:
            metricsMessageCounter.madeOutgoingConnection() # Counts attempts, because it's easier!
            
        d = defer.Deferred()
        d.addErrback(getConnErrorback)
        factory = GmuClientFactory(d)   # pb.PBClientFactory()
        factory.setConnectionLostDefered(d)# Fire this deferred when connection lost
        factory.noisy = False
        con = reactor.connectTCP(nodeLocation.ip, nodeLocation.port, factory, timeout=Config.NETWORK_CONNECTION_TIMEOUT)
        con.clientLostDefered = d # A little kludgy!
        
    return (factory, con)