from twisted.internet import endpoints, reactor
from txampext.multiplexing import ProxyingFactory


def connectProxy(namespace, identifier, _reactor=reactor):
    """Start listening on some free local port; connections will be
    proxied to the virtual server with the given identifier.

    """
    remote = namespace.get("remote")
    if remote is None:
        raise RuntimeError("You are not connected to the exercise server. "
                           "Call ``connect``.")

    factory = ProxyingFactory(remote, identifier)
    endpoint = endpoints.TCP4ServerEndpoint(_reactor, 0, interface="localhost")
    d = endpoint.listen(factory)
    d.addCallback(_listening, namespace, identifier)
    return None


def _listening(listeningPort, namespace, identifier):
    """Started listening; report success to terminal.

    """
    host = listeningPort.getHost()
    template = "{id} is now listening on {h.host}:{h.port}"
    namespace["manhole"].writeLine(template.format(h=host, id=identifier))

from stanczyk.util import _getRemote
from twisted.internet import endpoints, reactor
from txampext.multiplexing import ProxyingFactory


def connectProxy(namespace, identifier, _reactor=reactor):
    """Start listening on some free local port; connections will be
    proxied to the virtual server with the given identifier.

    """
    endpoint = endpoints.TCP4ServerEndpoint(_reactor, 0, interface="localhost")
    factory = ProxyingFactory(_getRemote(namespace), identifier)
    d = endpoint.listen(factory)
    d.addCallback(_listening, namespace, identifier)
    return None


def _listening(listeningPort, namespace, identifier):
    """Started listening; report success to terminal.

    """
    host = listeningPort.getHost()
    template = "{id} is now listening on {h.host}:{h.port}"
    namespace["manhole"].writeLine(template.format(h=host, id=identifier))

Use the new fancy refactored remote logic
Use the new fancy refactored remote logic

+ from stanczyk.util import _getRemote
  from twisted.internet import endpoints, reactor
  from txampext.multiplexing import ProxyingFactory
  
  
  def connectProxy(namespace, identifier, _reactor=reactor):
      """Start listening on some free local port; connections will be
      proxied to the virtual server with the given identifier.
  
      """
-     remote = namespace.get("remote")
-     if remote is None:
-         raise RuntimeError("You are not connected to the exercise server. "
-                            "Call ``connect``.")
- 
-     factory = ProxyingFactory(remote, identifier)
      endpoint = endpoints.TCP4ServerEndpoint(_reactor, 0, interface="localhost")
+     factory = ProxyingFactory(_getRemote(namespace), identifier)
      d = endpoint.listen(factory)
      d.addCallback(_listening, namespace, identifier)
      return None
  
  
  def _listening(listeningPort, namespace, identifier):
      """Started listening; report success to terminal.
  
      """
      host = listeningPort.getHost()
      template = "{id} is now listening on {h.host}:{h.port}"
      namespace["manhole"].writeLine(template.format(h=host, id=identifier))
  
Use the new fancy refactored remote logic
## Code Before:
from twisted.internet import endpoints, reactor
from txampext.multiplexing import ProxyingFactory


def connectProxy(namespace, identifier, _reactor=reactor):
    """Start listening on some free local port; connections will be
    proxied to the virtual server with the given identifier.

    """
    remote = namespace.get("remote")
    if remote is None:
        raise RuntimeError("You are not connected to the exercise server. "
                           "Call ``connect``.")

    factory = ProxyingFactory(remote, identifier)
    endpoint = endpoints.TCP4ServerEndpoint(_reactor, 0, interface="localhost")
    d = endpoint.listen(factory)
    d.addCallback(_listening, namespace, identifier)
    return None


def _listening(listeningPort, namespace, identifier):
    """Started listening; report success to terminal.

    """
    host = listeningPort.getHost()
    template = "{id} is now listening on {h.host}:{h.port}"
    namespace["manhole"].writeLine(template.format(h=host, id=identifier))

## Instruction:
Use the new fancy refactored remote logic
## Code After:
from stanczyk.util import _getRemote
from twisted.internet import endpoints, reactor
from txampext.multiplexing import ProxyingFactory


def connectProxy(namespace, identifier, _reactor=reactor):
    """Start listening on some free local port; connections will be
    proxied to the virtual server with the given identifier.

    """
    endpoint = endpoints.TCP4ServerEndpoint(_reactor, 0, interface="localhost")
    factory = ProxyingFactory(_getRemote(namespace), identifier)
    d = endpoint.listen(factory)
    d.addCallback(_listening, namespace, identifier)
    return None


def _listening(listeningPort, namespace, identifier):
    """Started listening; report success to terminal.

    """
    host = listeningPort.getHost()
    template = "{id} is now listening on {h.host}:{h.port}"
    namespace["manhole"].writeLine(template.format(h=host, id=identifier))
