def runProtocolsWithReactor(reactorBuilder, serverProtocol, clientProtocol, endpointCreator):
 reactor = reactorBuilder.buildReactor()
 serverProtocol._setAttributes(reactor, Deferred())
 clientProtocol._setAttributes(reactor, Deferred())
 serverFactory = _SingleProtocolFactory(serverProtocol)
 clientFactory = _SingleProtocolFactory(clientProtocol)
 serverEndpoint = endpointCreator.server(reactor)
 d = serverEndpoint.listen(serverFactory)
 def gotPort(p):
  clientEndpoint = endpointCreator.client(reactor, p.getHost())
  return clientEndpoint.connect(clientFactory)
 d.addCallback(gotPort)
 def failed(result):
  log.err(result, 'Connection setup failed.')
 disconnected = gatherResults([serverProtocol._done, clientProtocol._done])
 d.addCallback((lambda _: disconnected))
 d.addErrback(failed)
 d.addCallback((lambda _: needsRunningReactor(reactor, reactor.stop)))
 reactorBuilder.runReactor(reactor)
 return reactor

def runProtocolsWithReactor ( reactorBuilder , serverProtocol , clientProtocol , endpointCreator ) : reactor = reactorBuilder . buildReactor ( ) serverProtocol . _setAttributes ( reactor , Deferred ( ) ) clientProtocol . _setAttributes ( reactor , Deferred ( ) ) serverFactory = _SingleProtocolFactory ( serverProtocol ) clientFactory = _SingleProtocolFactory ( clientProtocol ) serverEndpoint = endpointCreator . server ( reactor ) d = serverEndpoint . listen ( serverFactory ) def gotPort ( p ) : clientEndpoint = endpointCreator . client ( reactor , p . getHost ( ) ) return clientEndpoint . connect ( clientFactory ) d . addCallback ( gotPort ) def failed ( result ) : log . err ( result , 'Connection setup failed.' ) disconnected = gatherResults ( [ serverProtocol . _done , clientProtocol . _done ] ) d . addCallback ( ( lambda _ : disconnected ) ) d . addErrback ( failed ) d . addCallback ( ( lambda _ : needsRunningReactor ( reactor , reactor . stop ) ) ) reactorBuilder . runReactor ( reactor ) return reactor
connect two protocols using endpoints and a new reactor instance .