use of org.klomp.snark.bencode.InvalidBEncodingException in project i2p.i2p by i2p.
the class KRPC method receiveMessage.
// /// Reception.....
/**
* @param from dest or null if it didn't come in on signed port
*/
private void receiveMessage(Destination from, int fromPort, byte[] payload) {
try {
InputStream is = new ByteArrayInputStream(payload);
BDecoder dec = new BDecoder(is);
BEValue bev = dec.bdecodeMap();
Map<String, BEValue> map = bev.getMap();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Got KRPC message " + bev.toString());
// Lazy here, just let missing Map entries throw NPEs, caught below
byte[] msgIDBytes = map.get("t").getBytes();
MsgID mID = new MsgID(msgIDBytes);
String type = map.get("y").getString();
if (type.equals("q")) {
// queries must be repliable
String method = map.get("q").getString();
Map<String, BEValue> args = map.get("a").getMap();
receiveQuery(mID, from, fromPort, method, args);
} else if (type.equals("r") || type.equals("e")) {
// get dest from id->dest map
ReplyWaiter waiter = _sentQueries.remove(mID);
if (waiter != null) {
// TODO verify waiter NID and port?
if (type.equals("r")) {
Map<String, BEValue> response = map.get("r").getMap();
receiveResponse(waiter, response);
} else {
List<BEValue> error = map.get("e").getList();
receiveError(waiter, error);
}
} else {
if (_log.shouldLog(Log.WARN))
_log.warn("Rcvd msg with no one waiting: " + bev.toString());
}
} else {
if (_log.shouldLog(Log.WARN))
_log.warn("Unknown msg type rcvd: " + bev.toString());
throw new InvalidBEncodingException("Unknown type: " + type);
}
// success
/**
* } catch (InvalidBEncodingException e) {
* } catch (IOException e) {
* } catch (ArrayIndexOutOfBoundsException e) {
* } catch (IllegalArgumentException e) {
* } catch (ClassCastException e) {
* } catch (NullPointerException e) {
**
*/
} catch (Exception e) {
if (_log.shouldLog(Log.WARN))
_log.warn("Receive error for message", e);
}
}
use of org.klomp.snark.bencode.InvalidBEncodingException in project i2p.i2p by i2p.
the class PeerCoordinator method sendPeers.
/**
* Send a PEX message to the peer, if he supports PEX.
* This just sends everybody we are connected to, we don't
* track new vs. old peers yet.
* @since 0.8.4
*/
void sendPeers(Peer peer) {
if (metainfo != null && metainfo.isPrivate())
return;
Map<String, BEValue> handshake = peer.getHandshakeMap();
if (handshake == null)
return;
BEValue bev = handshake.get("m");
if (bev == null)
return;
try {
if (bev.getMap().get(ExtensionHandler.TYPE_PEX) != null) {
List<Peer> pList = peerList();
pList.remove(peer);
if (!pList.isEmpty())
ExtensionHandler.sendPEX(peer, pList);
}
} catch (InvalidBEncodingException ibee) {
}
}
use of org.klomp.snark.bencode.InvalidBEncodingException in project i2p.i2p by i2p.
the class TrackerClient method getPeersFromDHT.
/**
* @return max peers seen
*/
private int getPeersFromDHT() {
// Get peers from DHT
// FIXME this needs to be in its own thread
int rv = 0;
DHT dht = _util.getDHT();
if (dht != null && (meta == null || !meta.isPrivate()) && (!stop) && (meta == null || _util.getContext().clock().now() > lastDHTAnnounce + MIN_DHT_ANNOUNCE_INTERVAL)) {
int numwant;
if (!coordinator.needOutboundPeers())
numwant = 1;
else
numwant = _util.getMaxConnections();
Collection<Hash> hashes = dht.getPeersAndAnnounce(snark.getInfoHash(), numwant, 5 * 60 * 1000, DHT_ANNOUNCE_PEERS, 3 * 60 * 1000, coordinator.completed(), numwant <= 1);
if (!hashes.isEmpty()) {
runStarted = true;
lastDHTAnnounce = _util.getContext().clock().now();
rv = hashes.size();
} else {
lastDHTAnnounce = 0;
}
if (_log.shouldLog(Log.INFO))
_log.info("Got " + hashes + " from DHT");
// now try these peers
if ((!stop) && !hashes.isEmpty()) {
List<Peer> peers = new ArrayList<Peer>(hashes.size());
for (Hash h : hashes) {
try {
PeerID pID = new PeerID(h.getData(), _util);
peers.add(new Peer(pID, snark.getID(), snark.getInfoHash(), snark.getMetaInfo()));
} catch (InvalidBEncodingException ibe) {
}
}
Random r = _util.getContext().random();
Collections.shuffle(peers, r);
Iterator<Peer> it = peers.iterator();
while ((!stop) && it.hasNext() && coordinator.needOutboundPeers()) {
Peer cur = it.next();
if (coordinator.addPeer(cur) && it.hasNext()) {
int delay = r.nextInt(DELAY_RAND) + DELAY_MIN;
try {
Thread.sleep(delay);
} catch (InterruptedException ie) {
}
}
}
}
} else {
if (_log.shouldLog(Log.INFO))
_log.info("Not getting DHT peers");
}
return rv;
}
use of org.klomp.snark.bencode.InvalidBEncodingException in project i2p.i2p by i2p.
the class TrackerInfo method getPeers.
/**
****
* public static Set<Peer> getPeers(InputStream in, byte[] my_id, MetaInfo metainfo)
* throws IOException
* {
* return getPeers(new BDecoder(in), my_id, metainfo);
* }
*
* public static Set<Peer> getPeers(BDecoder be, byte[] my_id, MetaInfo metainfo)
* throws IOException
* {
* return getPeers(be.bdecodeList().getList(), my_id, metainfo);
* }
*****
*/
/**
* List of Dictionaries or List of Strings
*/
private static Set<Peer> getPeers(List<BEValue> l, byte[] my_id, byte[] infohash, MetaInfo metainfo, I2PSnarkUtil util) throws IOException {
Set<Peer> peers = new HashSet<Peer>(l.size());
for (BEValue bev : l) {
PeerID peerID;
try {
// Case 1 - non-compact - A list of dictionaries (maps)
peerID = new PeerID(bev.getMap());
} catch (InvalidBEncodingException ibe) {
try {
// Case 2 - compact - A list of 32-byte binary strings (hashes)
// This was just for testing and is not the official format
peerID = new PeerID(bev.getBytes(), util);
} catch (InvalidBEncodingException ibe2) {
// Snark.debug("Discarding peer from list: " + ibe, Snark.ERROR);
continue;
}
}
peers.add(new Peer(peerID, my_id, infohash, metainfo));
}
return peers;
}
use of org.klomp.snark.bencode.InvalidBEncodingException in project i2p.i2p by i2p.
the class PeerCoordinator method sendDHT.
/**
* Send a DHT message to the peer, if we both support DHT.
* @since DHT
*/
void sendDHT(Peer peer) {
DHT dht = _util.getDHT();
if (dht == null)
return;
Map<String, BEValue> handshake = peer.getHandshakeMap();
if (handshake == null)
return;
BEValue bev = handshake.get("m");
if (bev == null)
return;
try {
if (bev.getMap().get(ExtensionHandler.TYPE_DHT) != null)
ExtensionHandler.sendDHT(peer, dht.getPort(), dht.getRPort());
} catch (InvalidBEncodingException ibee) {
}
}
Aggregations