void stopRemoteTunnels(MultiplexedConnection agent, Collection<Tunnel> tunnels)
      throws CoreException {

    CoreException e = null;

    for (Tunnel tunnel : tunnels) {
      try {
        RemoteTunnel rt =
            RemoteTunnelManagerFactory.getInstance().getRemoteTunnel(tunnel.getResourceId());
        if (rt != null) {
          rt.stopListener();
        } else {
          throw new Exception("No active with ID for " + tunnel.getResourceId());
        }
      } catch (Exception ex) {
        throw new TunnelException(TunnelException.INTERNAL_ERROR, ex);
      }
    }
    if (e != null) {
      throw e;
    }
  }
  /**
   * Get a set of the resource ids of all active tunnels (local and remote).
   *
   * @param session
   * @return resource IDs of active tunnels
   */
  public Set<Integer> getActiveTunnels(SessionInfo session) {

    try {
      MultiplexedConnection tunnel = DefaultAgentManager.getInstance().getAgentBySession(session);

      if (tunnel == null) return null;

      HashSet<Integer> activeTunnelIds = new HashSet<Integer>();

      // The agent keeps track of 'local' tunnels

      Request request = new Request(ACTIVE_LOCAL_TUNNELS);
      if (tunnel.sendRequest(request, true) && request.getRequestData() != null) {

        ByteArrayReader reader = new ByteArrayReader(request.getRequestData());
        int count = (int) reader.readInt();
        for (int i = 0; i < count; i++) {
          activeTunnelIds.add(new Integer((int) reader.readInt()));
        }
      }

      // The server keeps track of 'remote' tunnels
      Collection<RemoteTunnel> activeRemoteTunnels =
          RemoteTunnelManagerFactory.getInstance().getRemoteTunnels(session);
      if (activeRemoteTunnels != null) {
        synchronized (activeRemoteTunnels) {
          for (RemoteTunnel r : activeRemoteTunnels) {
            activeTunnelIds.add(r.getTunnel().getResourceId());
          }
        }
      }

      return activeTunnelIds;
    } catch (IOException e) {
      log.error("Failed to get active tunnel list from agent", e);
      return null;
    }
  }
 void startRemoteTunnel(AgentTunnel agent, Tunnel tunnel, LaunchSession launchSession)
     throws TunnelException {
   RemoteTunnel remoteTunnel =
       RemoteTunnelManagerFactory.getInstance().createRemoteTunnel(tunnel, agent, launchSession);
   remoteTunnel.start();
 }