@Override public String toString() { StringBuilder sb = new StringBuilder("sig["); sb.append(number1.toString()).append('/'); sb.append(number2.toString()).append(']'); return sb.toString(); }
/* (non-Javadoc) * @see net.tomp2p.message.SignatureCodec#encode() */ @Override public byte[] encode() throws IOException { byte me[] = new byte[2 + (2 * (20 + 2))]; me[0] = 0x30; me[1] = 2 * (20 + 2); me[2] = 0x02; me[3] = 20; number1.toByteArray(me, 4); me[24] = 0x02; me[25] = 20; number2.toByteArray(me, 26); return me; }
@Override public void handleResponseMessage(ResponseMessage responseMessage) { String receivedSecret = (String) responseMessage.getContent(); Number160 lKey = Number160.createHash(networkManager.getNodeId()); Number160 cKey = Number160.createHash(contentKey); try { networkManager .getDataManager() .put(lKey, Number160.ZERO, cKey, new H2HTestData(receivedSecret), null) .awaitUninterruptibly(); } catch (NoPeerConnectionException e) { Assert.fail(); } }
public P2PClient() throws Exception { this.localIP = getLocalPeerIP(); this.serviceID = "medical"; this.peerID = Number160.createHash(localIP); this.peer = new Peer(peerID); this.peer.listen(4000, 4000); PeerAddress superAddr = new PeerAddress(Number160.createHash(1), "109.231.77.242", 8888, 8888); FutureDiscover future = this.peer.discover(superAddr); future.awaitUninterruptibly(); FutureBootstrap fb = this.peer.bootstrap(superAddr); fb.awaitUninterruptibly(); peer.discover(fb.getBootstrapTo().iterator().next()).awaitUninterruptibly(); }
private Data deserializeFile(DataInput in) throws IOException, FileNotFoundException { byte[] me = new byte[Number160.BYTE_ARRAY_SIZE]; in.readFully(me); Number160 hash = new Number160(me); RandomAccessFile file = new RandomAccessFile(new File(path, hash.toString()), "r"); FileChannel inChannel = file.getChannel(); MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); buffer.load(); ByteBuf buf = Unpooled.wrappedBuffer(buffer); Data data = Data.decodeHeader(buf, signatureFactory); data.decodeBuffer(buf); data.decodeDone(buf, signatureFactory); file.close(); return data; }
private FutureDHT put(String key, String domain, String content, String data) throws IOException { Number160 locationKey = Number160.createHash(key); Number160 domainKey = Number160.createHash(domain); Number160 contentKey = Number160.createHash(content); MyData myData = new MyData(); myData.setKey(key); myData.setDomain(domain); myData.setContent(content); myData.setData(data); return peer.put(locationKey) .setDomainKey(domainKey) .setData(contentKey, new Data(myData)) .start(); }
private String get(String name) throws ClassNotFoundException, IOException { FutureDHT futureDHT = peer.get(Number160.createHash(name)); futureDHT.awaitUninterruptibly(); if (futureDHT.isSuccess()) { return futureDHT.getData().values().iterator().next().getObject().toString(); } else return "Not Found"; }
@SuppressWarnings("unchecked") private Vector<Number160> getPeerList(String name) throws ClassNotFoundException, IOException { FutureDHT futureDHT = peer.get(Number160.createHash(name)); futureDHT.awaitUninterruptibly(); if (futureDHT.isSuccess()) { return (Vector<Number160>) futureDHT.getData().values().iterator().next().getObject(); } else return null; }
@Override public void run() { String secret = NetworkTestUtil.randomString(); Number160 lKey = Number160.createHash(networkManager.getNodeId()); Number160 cKey = Number160.createHash(contentKey); try { networkManager .getDataManager() .put(lKey, Number160.ZERO, cKey, new H2HTestData(secret), null) .awaitUninterruptibly(); } catch (NoPeerConnectionException e) { Assert.fail(); } sendDirectResponse(createResponse(secret)); }
public static void routing(Number160 key, Peer[] peers, int start) { System.out.println("routing: searching for key " + key); NavigableSet<PeerAddress> pa1 = new TreeSet<PeerAddress>(PeerMap.createComparator(key)); NavigableSet<PeerAddress> queried = new TreeSet<PeerAddress>(PeerMap.createComparator(key)); Number160 result = Number160.ZERO; Number160 resultPeer = new Number160("0xd75d1a3d57841fbc9e2a3d175d6a35dc2e15b9f"); int round = 0; while (!resultPeer.equals(result)) { System.out.println("round " + round); round++; pa1.addAll(peers[start].getPeerBean().peerMap().getAll()); queried.add(peers[start].getPeerAddress()); System.out.println("closest so far: " + queried.first()); PeerAddress next = pa1.pollFirst(); while (queried.contains(next)) { next = pa1.pollFirst(); } result = next.getPeerId(); start = findNr(next.getPeerId().toString(), peers); } }
public static void main(String[] args) throws Exception { P2PClient client = new P2PClient(); System.out.println( client.localIP + " " + client.serviceID + " " + client.peer.getPeerID().toString()); System.out.println("Getting peerIDs for the serviceID \"medical\""); Vector<Number160> vec = client.getPeerList(client.serviceID); if (vec == null) { vec = new Vector<Number160>(); System.out.println("vec was null, now is empty: " + vec.isEmpty()); vec.add(client.peerID); client.store(client.serviceID, vec); } else { for (Number160 number160 : vec) { System.out.println("PeerID = " + number160.toString()); for (String string : client.get(number160)) { System.out.print("\t" + string); } System.out.println(""); } if (!vec.contains(client.peerID)) { vec.add(client.peerID); client.store(client.serviceID, vec); } } /////////////////////////////////////////////////// String[] localVals = {client.localIP, "IPSecrets"}; client.store(client.peerID, localVals); for (String string : client.get(client.peerID)) { System.out.println(string); } /////////////////////////////////////////////////// if (args.length > 1) client.store(args[0], args[1]); if (args.length == 1) System.out.println("Name: " + args[0] + " IP:" + client.get(args[0])); }
private void serializeFile(DataOutput out, Data value) throws IOException, FileNotFoundException { Number160 hash = value.hash(); // store file name out.write(hash.toByteArray()); // store as external file, create path RandomAccessFile file = null; FileChannel rwChannel = null; try { file = new RandomAccessFile(new File(path, hash.toString()), "rw"); rwChannel = file.getChannel(); AlternativeCompositeByteBuf acb = AlternativeCompositeByteBuf.compBuffer(); // store data to disk // header first value.encodeHeader(acb, signatureFactory); rwChannel.write(acb.nioBuffers()); // next data - no need to copy to another buffer, just take the // data from memory rwChannel.write(value.toByteBuffers()); // rest try { value.encodeDone(acb, signatureFactory); rwChannel.write(acb.nioBuffers()); } catch (InvalidKeyException e) { throw new IOException(e); } catch (SignatureException e) { throw new IOException(e); } } finally { if (rwChannel != null) { rwChannel.close(); } if (file != null) { file.close(); } } }
private void store(String key, String val) throws IOException { peer.put(Number160.createHash(key), new Data(val)).awaitUninterruptibly(); }
@Override public int hashCode() { return number1.hashCode() ^ number2.hashCode(); }
private static boolean isMine(Number160 key, PublicKey publicKey) { return key.equals(Utils.makeSHAHash(publicKey.getEncoded())); }
/** * Encodes payload in a big switch statement. Types are: EMPTY, KEY_KEY, PUBLIC_KEY, * KEY_KEY_PUBLIC_KEY, MAP_KEY_DATA, MAP_KEY_DATA_TTL, SET_DATA_TTL, MAP_KEY_KEY, SET_KEYS, * SET_NEIGHBORS, BYTE_ARRAY, INTEGER, USER1, USER2, USER3 * * @param contentType The type of the content to encode * @param input The ProtocolChunkedInput to fill * @param message The message which contains the payload * @throws NoSuchAlgorithmException * @throws SignatureException * @throws InvalidKeyException */ private static int encodePayloadType( final Content content, final ProtocolChunkedInput input, final Message message) { final int size; final byte[] data; int count; switch (content) { case KEY: input.copyToCurrent(message.getKey().toByteArray()); return 20; case KEY_KEY: input.copyToCurrent(message.getKeyKey1().toByteArray()); input.copyToCurrent(message.getKeyKey2().toByteArray()); return 40; case MAP_KEY_DATA: count = 4; if (message.isConvertNumber480to160()) { Map<Number480, Data> dataMap = message.getDataMapConvert(); input.copyToCurrent(dataMap.size()); for (Map.Entry<Number480, Data> entry : dataMap.entrySet()) { input.copyToCurrent(entry.getKey().getContentKey().toByteArray()); count += 20; count += encodeData(input, message, entry.getValue()); } return count; } else { input.copyToCurrent(message.getDataMap().size()); for (Map.Entry<Number160, Data> entry : message.getDataMap().entrySet()) { input.copyToCurrent(entry.getKey().toByteArray()); count += 20; count += encodeData(input, message, entry.getValue()); } return count; } case MAP_KEY_KEY: Map<Number160, Number160> keyMap = message.getKeyMap(); size = keyMap.size(); input.copyToCurrent(size); for (final Map.Entry<Number160, Number160> entry : keyMap.entrySet()) { input.copyToCurrent(entry.getKey().toByteArray()); input.copyToCurrent(entry.getValue().toByteArray()); } return 4 + (size * (20 + 20)); case SET_KEYS: if (message.isConvertNumber480to160()) { Collection<Number480> keys = message.getKeysConvert(); size = keys.size(); input.copyToCurrent(size); for (Number480 key : keys) input.copyToCurrent(key.getContentKey().toByteArray()); return 4 + (size * 20); } else { size = message.getKeys().size(); input.copyToCurrent(size); for (Number160 key : message.getKeys()) input.copyToCurrent(key.toByteArray()); return 4 + (size * 20); } case SET_NEIGHBORS: count = 1; size = Math.min( message.getNeighbors().size(), Math.min(message.getUseAtMostNeighbors(), MAX_BYTE)); input.copyToCurrent((byte) size); final Iterator<PeerAddress> iterator = message.getNeighbors().iterator(); for (int i = 0; iterator.hasNext() && i < size; i++) { byte[] data1 = iterator.next().toByteArray(); input.copyToCurrent(data1); count += data1.length; } return count; case SET_TRACKER_DATA: count = 1; final Collection<TrackerData> trackerDatas = message.getTrackerData(); input.copyToCurrent((byte) trackerDatas.size()); for (TrackerData trackerData : trackerDatas) { byte[] data1 = trackerData.getPeerAddress().toByteArray(); input.copyToCurrent(data1); count += data1.length; if (trackerData.getAttachement() != null) { input.copyToCurrent((byte) 1); input.copyToCurrent(trackerData.getLength()); count += 5; input.copyToCurrent( trackerData.getAttachement(), trackerData.getOffset(), trackerData.getLength()); count += trackerData.getLength(); } else { input.copyToCurrent((byte) 0); count += 1; } } return count; case CHANNEL_BUFFER: ChannelBuffer tmpBuffer = message.getPayload1(); if (tmpBuffer == null) tmpBuffer = message.getPayload2(); else message.setPayload1(null); size = tmpBuffer.writerIndex(); input.copyToCurrent(size); input.copyToCurrent(tmpBuffer.slice()); return 4 + size; case LONG: input.copyToCurrent(message.getLong()); return 8; case INTEGER: input.copyToCurrent(message.getInteger()); return 4; case PUBLIC_KEY: data = message.getPublicKey().getEncoded(); size = data.length; input.copyToCurrent((short) size); input.copyToCurrent(data); return 2 + size; case PUBLIC_KEY_SIGNATURE: // flag to encode public key data = message.getPublicKey().getEncoded(); size = data.length; input.copyToCurrent((short) size); input.copyToCurrent(data); message.setHintSign(true); // count 40 for the signature, which comes later in ProtocolChunkedInput return 40 + 2 + size; case EMPTY: case RESERVED1: case RESERVED2: case RESERVED3: default: return 0; } }
private FutureDHT get(String key, String domain, String content) { Number160 locationKey = Number160.createHash(key); Number160 domainKey = Number160.createHash(domain); Number160 contentKey = Number160.createHash(content); return peer.get(locationKey).setDomainKey(domainKey).setContentKey(contentKey).start(); }
/* (non-Javadoc) * @see net.tomp2p.message.SignatureCodec#write(io.netty.buffer.ByteBuf) */ @Override public SignatureCodec write(ByteBuf buf) { buf.writeBytes(number1.toByteArray()); buf.writeBytes(number2.toByteArray()); return this; }