Examples of BEValue


Examples of com.turn.ttorrent.bcodec.BEValue

   * original byte-encoded form), the peer's IP and the peer's port.
   */
  public BEValue toBEValue() throws UnsupportedEncodingException {
    Map<String, BEValue> peer = new HashMap<String, BEValue>();
    if (this.hasPeerId()) {
      peer.put("peer id", new BEValue(this.getPeerId().array()));
    }
    peer.put("ip", new BEValue(this.getIp(), Torrent.BYTE_ENCODING));
    peer.put("port", new BEValue(this.getPort()));
    return new BEValue(peer);
  }
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

    }

    // Make sure we have the peer IP, fallbacking on the request's source
    // address if the peer didn't provide it.
    if (params.get("ip") == null) {
      params.put("ip", new BEValue(
        request.getClientAddress().getAddress().getHostAddress(),
        TrackedTorrent.BYTE_ENCODING));
    }

View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

    try {
      value = URLDecoder.decode(value, TrackedTorrent.BYTE_ENCODING);

      for (String f : NUMERIC_REQUEST_FIELDS) {
        if (f.equals(key)) {
          params.put(key, new BEValue(Long.valueOf(value)));
          return;
        }
      }

      params.put(key, new BEValue(value, TrackedTorrent.BYTE_ENCODING));
    } catch (UnsupportedEncodingException uee) {
      // Ignore, act like parameter was not there
      return;
    }
  }
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

    }

    Map<String, BEValue> torrent = new HashMap<String, BEValue>();

    if (announce != null) {
      torrent.put("announce", new BEValue(announce.toString()));
    }
    if (announceList != null) {
      List<BEValue> tiers = new LinkedList<BEValue>();
      for (List<URI> trackers : announceList) {
        List<BEValue> tierInfo = new LinkedList<BEValue>();
        for (URI trackerURI : trackers) {
          tierInfo.add(new BEValue(trackerURI.toString()));
        }
        tiers.add(new BEValue(tierInfo));
      }
      torrent.put("announce-list", new BEValue(tiers));
    }
   
    torrent.put("creation date", new BEValue(new Date().getTime() / 1000));
    torrent.put("created by", new BEValue(createdBy));

    Map<String, BEValue> info = new TreeMap<String, BEValue>();
    info.put("name", new BEValue(parent.getName()));
    info.put("piece length", new BEValue(Torrent.PIECE_LENGTH));

    if (files == null || files.isEmpty()) {
      info.put("length", new BEValue(parent.length()));
      info.put("pieces", new BEValue(Torrent.hashFile(parent),
        Torrent.BYTE_ENCODING));
    } else {
      List<BEValue> fileInfo = new LinkedList<BEValue>();
      for (File file : files) {
        Map<String, BEValue> fileMap = new HashMap<String, BEValue>();
        fileMap.put("length", new BEValue(file.length()));

        LinkedList<BEValue> filePath = new LinkedList<BEValue>();
        while (file != null) {
          if (file.equals(parent)) {
            break;
          }

          filePath.addFirst(new BEValue(file.getName()));
          file = file.getParentFile();
        }

        fileMap.put("path", new BEValue(filePath));
        fileInfo.add(new BEValue(fileMap));
      }
      info.put("files", new BEValue(fileInfo));
      info.put("pieces", new BEValue(Torrent.hashFiles(files),
        Torrent.BYTE_ENCODING));
    }
    torrent.put("info", new BEValue(info));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BEncoder.bencode(new BEValue(torrent), baos);
    return new Torrent(baos.toByteArray(), true);
  }
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

    super(type, data);
  }

  public static HTTPTrackerMessage parse(ByteBuffer data)
    throws IOException, MessageValidationException {
    BEValue decoded = BDecoder.bdecode(data);
    if (decoded == null) {
      throw new MessageValidationException(
        "Could not decode tracker message (not B-encoded?)!");
    }

    Map<String, BEValue> params = decoded.getMap();

    if (params.containsKey("info_hash")) {
      return HTTPAnnounceRequestMessage.parse(data);
    } else if (params.containsKey("peers")) {
      return HTTPAnnounceResponseMessage.parse(data);
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

    return this.peers;
  }

  public static HTTPAnnounceResponseMessage parse(ByteBuffer data)
    throws IOException, MessageValidationException {
    BEValue decoded = BDecoder.bdecode(data);
    if (decoded == null) {
      throw new MessageValidationException(
        "Could not decode tracker message (not B-encoded?)!");
    }

    Map<String, BEValue> params = decoded.getMap();

    if (params.get("interval") == null) {
      throw new MessageValidationException(
        "Tracker message missing mandatory field 'interval'!");
    }
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

   */
  public static HTTPAnnounceResponseMessage craft(int interval,
    int minInterval, String trackerId, int complete, int incomplete,
    List<Peer> peers) throws IOException, UnsupportedEncodingException {
    Map<String, BEValue> response = new HashMap<String, BEValue>();
    response.put("interval", new BEValue(interval));
    response.put("complete", new BEValue(complete));
    response.put("incomplete", new BEValue(incomplete));

    ByteBuffer data = ByteBuffer.allocate(peers.size() * 6);
    for (Peer peer : peers) {
      byte[] ip = peer.getRawIp();
      if (ip == null || ip.length != 4) {
        continue;
      }
      data.put(ip);
      data.putShort((short)peer.getPort());
    }
    response.put("peers", new BEValue(data.array()));

    return new HTTPAnnounceResponseMessage(
      BEncoder.bencode(response),
      interval, complete, incomplete, peers);
  }
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

    return this.reason;
  }

  public static HTTPTrackerErrorMessage parse(ByteBuffer data)
    throws IOException, MessageValidationException {
    BEValue decoded = BDecoder.bdecode(data);
    if (decoded == null) {
      throw new MessageValidationException(
        "Could not decode tracker message (not B-encoded?)!");
    }

    Map<String, BEValue> params = decoded.getMap();

    try {
      return new HTTPTrackerErrorMessage(
        data,
        params.get("failure reason")
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

  public static HTTPTrackerErrorMessage craft(String reason)
    throws IOException, MessageValidationException {
    Map<String, BEValue> params = new HashMap<String, BEValue>();
    params.put("failure reason",
      new BEValue(reason, Torrent.BYTE_ENCODING));
    return new HTTPTrackerErrorMessage(
      BEncoder.bencode(params),
      reason);
  }
View Full Code Here

Examples of com.turn.ttorrent.bcodec.BEValue

    return new URL(url.toString());
  }

  public static HTTPAnnounceRequestMessage parse(ByteBuffer data)
    throws IOException, MessageValidationException {
    BEValue decoded = BDecoder.bdecode(data);
    if (decoded == null) {
      throw new MessageValidationException(
        "Could not decode tracker message (not B-encoded?)!");
    }

    Map<String, BEValue> params = decoded.getMap();

    if (!params.containsKey("info_hash")) {
      throw new MessageValidationException(
        ErrorMessage.FailureReason.MISSING_HASH.getMessage());
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.