示例#1
0
  /**
   * 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;
    }
  }
示例#2
0
  void stopLocalTunnels(MultiplexedConnection agent, Collection<Tunnel> tunnels)
      throws CoreException {

    CoreException e = null;

    for (Tunnel tunnel : tunnels) {
      try {
        ByteArrayWriter msg = new ByteArrayWriter();
        msg.writeInt(tunnel.getResourceId());
        if (!agent.sendRequest(new Request(STOP_LOCAL_TUNNEL, msg.toByteArray()), false)
            && e == null) {
          e =
              new TunnelException(
                  TunnelException.AGENT_REFUSED_LOCAL_TUNNEL_STOP, (Throwable) null);
        }
      } catch (IOException ex) {
        throw new TunnelException(TunnelException.INTERNAL_ERROR, ex);
      }
    }
    if (e != null) {
      throw e;
    }
  }