@RequestMapping(value = VIDEO_SEARCH_PATH, method = RequestMethod.GET)
  public @ResponseBody String[] searchVideo(
      @RequestParam(value = "username") String uName,
      @RequestParam(value = "video") String videoHash,
      HttpServletResponse response) {
    System.out.println("Search from:" + uName);
    if (!user_vidNameMap.containsKey(uName)) {
      response.setStatus(402); // client not connected
      return null;
    }

    Set<String> users = vidName_UserMap.get(videoHash);

    if (users == null) {
      System.out.println("Srearching main server\n");
      try {
        users = masterService.psSearch(hostAdder, videoHash);
      } catch (Exception e) {
        System.err.println(e.getMessage());
        return null;
      }
      if (users == null) return null;
      if (vidName_UserMap.containsKey(videoHash)) {
        vidName_UserMap.get(videoHash).addAll(users);
      } else {
        Set<String> s = new HashSet<String>();
        s.addAll(users);
        vidName_UserMap.put(videoHash, s);
      }
    } else {
      Iterator<String> it = users.iterator();
      while (it.hasNext()) {
        String temp = it.next();
        if (!activeUsers.contains(temp)) {
          it.remove();
        }
      }
    }
    System.out.println("Search result : " + Arrays.asList(users.toArray(new String[0])));
    // String [] a = new String[]
    return users.toArray(new String[0]);
  }
Exemplo n.º 2
0
  /**
   * Unchoke connected peers.
   *
   * <p>This is one of the "clever" places of the BitTorrent client. Every
   * OPTIMISTIC_UNCHOKING_FREQUENCY seconds, we decide which peers should be unchocked and
   * authorized to grab pieces from us.
   *
   * <p>Reciprocation (tit-for-tat) and upload capping is implemented here by carefully choosing
   * which peers we unchoke, and which peers we choke.
   *
   * <p>The four peers with the best download rate and are interested in us get unchoked. This
   * maximizes our download rate as we'll be able to get data from there four "best" peers quickly,
   * while allowing these peers to download from us and thus reciprocate their generosity.
   *
   * <p>Peers that have a better download rate than these four downloaders but are not interested
   * get unchoked too, we want to be able to download from them to get more data more quickly. If
   * one becomes interested, it takes a downloader's place as one of the four top downloaders (i.e.
   * we choke the downloader with the worst upload rate).
   *
   * @param optimistic Whether to perform an optimistic unchoke as well.
   */
  private synchronized void unchokePeers(boolean optimistic) {
    // Build a set of all connected peers, we don't care about peers we're
    // not connected to.
    List<SharingPeer> bound = new ArrayList<SharingPeer>(getConnectedPeers());
    Collections.sort(bound, this.getPeerRateComparator());
    Collections.reverse(bound);

    if (bound.size() == 0) {
      logger.trace("No connected peers, skipping unchoking.");
      return;
    } else {
      logger.trace("Running unchokePeers() on {} connected peers.", bound.size());
    }

    int downloaders = 0;
    Set<SharingPeer> choked = new HashSet<SharingPeer>();

    // We're interested in the top downloaders first, so use a descending
    // set.
    for (SharingPeer peer : bound) {
      if (downloaders < Client.MAX_DOWNLOADERS_UNCHOKE) {
        // Unchoke up to MAX_DOWNLOADERS_UNCHOKE interested peers
        if (peer.isChoking()) {
          if (peer.isInterested()) {
            downloaders++;
          }

          peer.unchoke();
        }
        continue;
      }
      // Choke everybody else
      choked.add(peer);
    }

    // Actually choke all chosen peers (if any), except the eventual
    // optimistic unchoke.
    if (choked.size() > 0) {
      SharingPeer randomPeer =
          choked.toArray(new SharingPeer[0])[this.random.nextInt(choked.size())];

      for (SharingPeer peer : choked) {
        if (optimistic && peer == randomPeer) {
          logger.debug("Optimistic unchoke of {}.", peer);
          continue;
        }

        peer.choke();
      }
    }
  }
  private static String[] findStandardMBeans(URL codeBase) throws Exception {
    Set<String> names;
    if (codeBase.getProtocol().equalsIgnoreCase("file") && codeBase.toString().endsWith("/"))
      names = findStandardMBeansFromDir(codeBase);
    else names = findStandardMBeansFromJar(codeBase);

    Set<String> standardMBeanNames = new TreeSet<String>();
    for (String name : names) {
      if (name.endsWith("MBean")) {
        String prefix = name.substring(0, name.length() - 5);
        if (names.contains(prefix)) standardMBeanNames.add(prefix);
      }
    }
    return standardMBeanNames.toArray(new String[0]);
  }
Exemplo n.º 4
0
 public static ConnectionStatistics[] getPoolStatistics() {
   Set<ConnectionStatistics> stats = new HashSet<ConnectionStatistics>();
   Iterator<TcpConnectionManager> it = poolTable_.values().iterator();
   while (it.hasNext()) {
     TcpConnectionManager cp = it.next();
     ConnectionStatistics cs =
         new ConnectionStatistics(
             cp.getLocalEndPoint(),
             cp.getRemoteEndPoint(),
             cp.getPoolSize(),
             cp.getConnectionsInUse());
     stats.add(cs);
   }
   return stats.toArray(new ConnectionStatistics[0]);
 }