本文整理汇总了Java中net.tomp2p.utils.Utils类的典型用法代码示例。如果您正苦于以下问题:Java Utils类的具体用法?Java Utils怎么用?Java Utils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Utils类属于net.tomp2p.utils包,在下文中一共展示了Utils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: list
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* Lists friends that are stored in the DHT.
*
* @param nickName
* The nickname as the location key, where the data is stored
* @throws IOException .
* @throws ClassNotFoundException .
*/
public void list(final String nickName) throws IOException, ClassNotFoundException {
Number160 key = Number160.createHash(nickName);
FutureTracker futureTracker = peer.getTracker(key).start();
// now we know which peer has this data, and we also know what other things this peer has
futureTracker.awaitUninterruptibly();
Collection<TrackerData> trackerDatas = futureTracker.getTrackers();
for (TrackerData trackerData : trackerDatas) {
String[] attachement = (String[]) Utils.decodeJavaObject(trackerData.getAttachement(), 0,
trackerData.getAttachement().length);
for (String s1 : attachement) {
System.out.println("this peers' (" + nickName + ") friend:" + s1);
}
}
System.out.println("Tracker reports that " + trackerDatas.size() + " peer(s) are his friends");
}
示例2: otherResponsible
import net.tomp2p.utils.Utils; //导入依赖的package包/类
@Override
public void otherResponsible(final Number160 locationKey, final PeerAddress other) {
// do pex here, but with mesh peers!
LOG.debug("other peer became responsibel and we thought we were responsible, so move the data to this peer");
for (final Number160 domainKey : trackerStorage.responsibleDomains(locationKey)) {
FutureChannelCreator futureChannelCreator = peer.getConnectionBean().reservation().create(1, 0);
futureChannelCreator.addListener(new BaseFutureAdapter<FutureChannelCreator>() {
@Override
public void operationComplete(final FutureChannelCreator future) throws Exception {
if (future.isSuccess()) {
FutureResponse futureResponse = peerExchangeRPC.peerExchange(other, locationKey,
domainKey, true, future.getChannelCreator(),
new DefaultConnectionConfiguration());
Utils.addReleaseListener(future.getChannelCreator(), futureResponse);
peer.notifyAutomaticFutures(futureResponse);
} else {
LOG.error("otherResponsible failed {}", future.getFailedReason());
}
}
});
}
}
示例3: sendDirect
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* If an other peer is responsible, we send this peer our data, so that the other peer can take care of this.
*
* @param other
* The other peer
* @param locationKey
* The location key
* @param domainKey
* The domain key
* @param dataMapConvert
* The data to store
*/
protected void sendDirect(final PeerAddress other, final Number160 locationKey,
final Number160 domainKey, final Map<Number160, Data> dataMapConvert) {
FutureChannelCreator futureChannelCreator = peer.getConnectionBean().reservation().create(0, 1);
futureChannelCreator.addListener(new BaseFutureAdapter<FutureChannelCreator>() {
@Override
public void operationComplete(final FutureChannelCreator future) throws Exception {
if (future.isSuccess()) {
PutBuilder putBuilder = new PutBuilder(peer, locationKey);
putBuilder.setDomainKey(domainKey);
putBuilder.setDataMapContent(dataMapConvert);
FutureResponse futureResponse = storageRPC.put(other, putBuilder,
future.getChannelCreator());
Utils.addReleaseListener(future.getChannelCreator(), futureResponse);
peer.notifyAutomaticFutures(futureResponse);
} else {
if (LOG.isErrorEnabled()) {
LOG.error("otherResponsible failed " + future.getFailedReason());
}
}
}
});
}
示例4: toByteArray
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* Serializes a peer socket address to a byte array. First the ports are serialized: TCP and UDP, then the address.
*
* @param me
* The byte array to store the serialization
* @param offset
* The offset where to start
* @return How many data have been written
*/
public int toByteArray(final byte[] me, final int offset) {
int offset2 = offset;
me[offset2++] = (byte) (tcpPort >>> Utils.BYTE_BITS);
me[offset2++] = (byte) tcpPort;
me[offset2++] = (byte) (udpPort >>> Utils.BYTE_BITS);
me[offset2++] = (byte) udpPort;
if (inetAddress instanceof Inet4Address) {
System.arraycopy(inetAddress.getAddress(), 0, me, offset2, Utils.IPV4_BYTES);
offset2 += Utils.IPV4_BYTES;
} else {
System.arraycopy(inetAddress.getAddress(), 0, me, offset2, Utils.IPV6_BYTES);
offset2 += Utils.IPV6_BYTES;
}
return offset2;
}
示例5: create
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* Converts an byte array into a peer socket address.
*
* @param me
* The byte array
* @param isIPv4
* Indicates if its IPv4 or IPv6
* @param offsetOriginal
* The offset where to start reading in the array
* @return the PeerSocketAddress and the new offset
*/
public static PeerSocketAddress create(final byte[] me, final boolean isIPv4, final int offsetOriginal) {
int offset = offsetOriginal;
final int portTCP = ((me[offset++] & Utils.MASK_FF) << Utils.BYTE_BITS) + (me[offset++] & Utils.MASK_FF);
final int portUDP = ((me[offset++] & Utils.MASK_FF) << Utils.BYTE_BITS) + (me[offset++] & Utils.MASK_FF);
//
final InetAddress address;
if (isIPv4) {
address = Utils.inet4FromBytes(me, offset);
// IPv4 is 32 bit
offset += Utils.IPV4_BYTES;
} else {
address = Utils.inet6FromBytes(me, offset);
// IPv6 is 128 bit
offset += Utils.IPV6_BYTES;
}
return new PeerSocketAddress(address, portTCP, portUDP, offset);
}
示例6: parallelRequests
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* Creates RPCs and executes them parallel.
*
* @param p2pConfiguration
* The configuration that specifies e.g. how many parallel requests there are.
* @param queue
* The sorted set that will be queries. The first RPC takes the first in the queue.
* @param futureDHT
* The future object that tracks the progress
* @param cancleOnFinish
* Set to true if the operation should be canceled (e.g. file transfer) if the future has finished.
* @param operation
* The operation that creates the request
*/
public static <K extends FutureDHT> K parallelRequests(final RequestP2PConfiguration p2pConfiguration,
final NavigableSet<PeerAddress> queue, final boolean cancleOnFinish,
final FutureChannelCreator futureChannelCreator,
final OperationMapper<K> operation, final K futureDHT) {
futureChannelCreator.addListener(new BaseFutureAdapter<FutureChannelCreator>() {
@Override
public void operationComplete(final FutureChannelCreator future) throws Exception {
if (future.isSuccess()) {
parallelRequests(p2pConfiguration, queue, futureDHT, cancleOnFinish,
future.getChannelCreator(), operation);
Utils.addReleaseListener(future.getChannelCreator(), futureDHT);
} else {
futureDHT.setFailed(future);
}
}
});
return futureDHT;
}
示例7: firstPeer
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* The first peer is the initiator. This peer that wants to start the broadcast will send it to all its neighbors.
* Since this peer has an interest in sending, it should also work more than the other peers.
*
* @param messageKey
* The key of the message
* @param dataMap
* The data map to send around
* @param hopCounter
* The number of hops
* @param isUDP
* Flag if message can be sent with UDP
*/
private void firstPeer(final Number160 messageKey, final Map<Number480, Data> dataMap, final int hopCounter,
final boolean isUDP) {
final List<PeerAddress> list = peer.getPeerBean().peerMap().getAll();
for (final PeerAddress peerAddress : list) {
FutureChannelCreator frr = peer.getConnectionBean().reservation().create(isUDP?1:0, isUDP?0:1);
frr.addListener(new BaseFutureAdapter<FutureChannelCreator>() {
@Override
public void operationComplete(final FutureChannelCreator future) throws Exception {
if (future.isSuccess()) {
BroadcastBuilder broadcastBuilder = new BroadcastBuilder(peer, messageKey);
broadcastBuilder.dataMap(dataMap);
broadcastBuilder.hopCounter(hopCounter + 1);
FutureResponse futureResponse = peer.getBroadcastRPC().send(peerAddress, broadcastBuilder,
future.getChannelCreator(), broadcastBuilder);
LOG.debug("1st broadcast to {}", peerAddress);
Utils.addReleaseListener(future.getChannelCreator(), futureResponse);
}
}
});
}
}
示例8: bootstrap
import net.tomp2p.utils.Utils; //导入依赖的package包/类
private FutureBootstrap bootstrap() {
final FutureWrappedBootstrap<FutureWrapper<FutureRouting>> result = new FutureWrappedBootstrap<FutureWrapper<FutureRouting>>();
result.setBootstrapTo(bootstrapTo);
int conn = Math.max(routingConfiguration.getParallel(), requestP2PConfiguration.getParallel());
FutureChannelCreator fcc = peer.getConnectionBean().reservation().create(conn, 0);
fcc.addListener(new BaseFutureAdapter<FutureChannelCreator>() {
@Override
public void operationComplete(final FutureChannelCreator futureChannelCreator) throws Exception {
if (futureChannelCreator.isSuccess()) {
RoutingBuilder routingBuilder = createBuilder(requestP2PConfiguration,
routingConfiguration);
FutureWrapper<FutureRouting> futureBootstrap = peer.getDistributedRouting().bootstrap(
bootstrapTo, routingBuilder, futureChannelCreator.getChannelCreator());
Utils.addReleaseListener(futureChannelCreator.getChannelCreator(), futureBootstrap);
result.waitFor(futureBootstrap);
} else {
result.setFailed(futureChannelCreator);
}
}
});
return result;
}
示例9: Data
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* Creates a data object from an already existing byte buffer.
*
* @param buffer
* The data buffer
* @param version
* The version of a data object, optional
* @param ttlSeconds
* The ttl of a data object, optional
* @param hasHash
* Indication if a hash should also be transmitted
* @param isProtectedEntry
* True if this entry is protected
*/
public Data(final ByteBuf buffer, final int version, final int ttlSeconds, final boolean hasHash,
final boolean isProtectedEntry) {
this.hasVersion = version != -1;
this.hasHash = hasHash;
this.hasTTL = ttlSeconds != -1;
this.isProtectedEntry = isProtectedEntry;
this.buffer = buffer;
this.startReaderIndex = buffer.readerIndex();
this.length = buffer.readableBytes() + (hasVersion ? Utils.INTEGER_BYTE_SIZE : 0)
+ (hasTTL ? Utils.INTEGER_BYTE_SIZE : 0) + (hasHash ? Number160.BYTE_ARRAY_SIZE : 0);
if (length < MAX_BYTE_SIZE) {
this.type = Type.SMALL;
} else if (length < MAX_BYTE_SIZE * MAX_BYTE_SIZE) {
this.type = Type.MEDIUM;
} else {
this.type = Type.LARGE;
}
this.validFromMillis = Timings.currentTimeMillis();
this.ttlSeconds = ttlSeconds;
this.version = version;
}
示例10: compareMessage
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* Checks if two messages are the same.
*
* @param m1
* The first message
* @param m2
* The second message
*/
private void compareMessage(final Message2 m1, final Message2 m2) {
Assert.assertEquals(m1.getMessageId(), m2.getMessageId());
Assert.assertEquals(m1.getVersion(), m2.getVersion());
Assert.assertEquals(m1.getCommand(), m2.getCommand());
compareContentTypes(m1, m2);
Assert.assertEquals(m1.getRecipient(), m2.getRecipient());
Assert.assertEquals(m1.getType(), m2.getType());
Assert.assertEquals(m1.getSender(), m2.getSender());
Assert.assertEquals(true, Utils.isSameSets(m1.getBloomFilterList(), m2.getBloomFilterList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getBufferList(), m2.getBufferList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getDataMapList(), m2.getDataMapList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getIntegerList(), m2.getIntegerList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getKeyList(), m2.getKeyList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getKeysList(), m2.getKeysList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getKeysMapList(), m2.getKeysMapList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getLongList(), m2.getLongList()));
Assert.assertEquals(true, Utils.isSameSets(m1.getNeighborsSetList(), m2.getNeighborsSetList()));
Assert.assertEquals(m1.getSender().isFirewalledTCP(), m2.getSender().isFirewalledTCP());
Assert.assertEquals(m1.getSender().isFirewalledUDP(), m2.getSender().isFirewalledUDP());
}
示例11: testDifference
import net.tomp2p.utils.Utils; //导入依赖的package包/类
@Test
public void testDifference() throws UnknownHostException {
// setup
Collection<PeerAddress> newC = new ArrayList<PeerAddress>();
newC.add(Utils2.createAddress(12));
newC.add(Utils2.createAddress(15));
newC.add(Utils2.createAddress(88));
newC.add(Utils2.createAddress(90));
newC.add(Utils2.createAddress(91));
SortedSet<PeerAddress> result = new TreeSet<PeerAddress>(PeerMap.createComparator(new Number160(88)));
SortedSet<PeerAddress> already = new TreeSet<PeerAddress>(PeerMap.createComparator(new Number160(88)));
already.add(Utils2.createAddress(90));
already.add(Utils2.createAddress(15));
// do testing
Utils.difference(newC, result, already);
// verification
Assert.assertEquals(3, result.size());
Assert.assertEquals(Utils2.createAddress(88), result.first());
}
示例12: putBlindingPubKey
import net.tomp2p.utils.Utils; //导入依赖的package包/类
@Override
public void putBlindingPubKey(FullTradeKey combinedKey,
TradeListingKey individualKey, RSAKeyParameters pubKey)
throws Exception {
Number160 key = Utils.makeSHAHash(getSignedTxKey(combinedKey,
individualKey).toByteArray());
PubKey pk = PubKey
.newBuilder()
.setModulus(
ByteString.copyFrom(pubKey.getModulus().toByteArray()))
.setExponent(
ByteString.copyFrom(pubKey.getExponent().toByteArray()))
.build();
put(key, ((TomP2PTradeListingKey) individualKey).d,
new Data(pk.toByteArray()));
}
示例13: announce
import net.tomp2p.utils.Utils; //导入依赖的package包/类
/**
* Announce my friend list on the DHT.
*
* @throws IOException .
*/
public void announce() throws IOException {
for (Map.Entry<Number160, String> entry : friends.entrySet()) {
Collection<String> tmp = new ArrayList<String>(friends.values());
peer.addTracker(entry.getKey()).setAttachement(Utils.encodeJavaObject(tmp.toArray(new String[0])))
.start().awaitUninterruptibly();
}
}
示例14: exampleAllMaster
import net.tomp2p.utils.Utils; //导入依赖的package包/类
public static void exampleAllMaster()
throws NoSuchAlgorithmException, IOException, ClassNotFoundException
{
KeyPairGenerator gen = KeyPairGenerator.getInstance( "DSA" );
KeyPair pair1 = gen.generateKeyPair();
KeyPair pair2 = gen.generateKeyPair();
KeyPair pair3 = gen.generateKeyPair();
final Number160 peer2Owner = Utils.makeSHAHash( pair2.getPublic().getEncoded() );
Peer peer1 = new PeerMaker( pair1 ).setPorts( 4001 ).makeAndListen();
Peer peer2 = new PeerMaker( pair2 ).setPorts( 4002 ).makeAndListen();
Peer peer3 = new PeerMaker( pair3 ).setPorts( 4003 ).makeAndListen();
Peer[] peers = new Peer[] { peer1, peer2, peer3 };
ExampleUtils.bootstrap( peers );
setProtection( peers, ProtectionEnable.ALL, ProtectionMode.MASTER_PUBLIC_KEY );
// peer 1 stores "test" in the domain key of owner peer 2
FutureDHT futureDHT =
peer1.put( Number160.ONE ).setData( new Data( "test" ) ).setDomainKey( peer2Owner ).setProtectDomain().start();
futureDHT.awaitUninterruptibly();
// peer 2 did not claim this domain, so we stored it
System.out.println( "stored: " + futureDHT.isSuccess() + " -> because no one claimed this domain" );
// peer 3 want to store something
futureDHT =
peer3.put( Number160.ONE ).setData( new Data( "hello" ) ).setDomainKey( peer2Owner ).setProtectDomain().start();
futureDHT.awaitUninterruptibly();
System.out.println( "stored: " + futureDHT.isSuccess() + " -> becaues peer1 already claimed this domain" );
// peer 2 claims this domain
futureDHT =
peer2.put( Number160.ONE ).setData( new Data( "MINE!" ) ).setDomainKey( peer2Owner ).setProtectDomain().start();
futureDHT.awaitUninterruptibly();
System.out.println( "stored: " + futureDHT.isSuccess() + " -> becaues peer2 is the owner" );
// get the data!
futureDHT = peer1.get( Number160.ONE ).setDomainKey( peer2Owner ).start();
futureDHT.awaitUninterruptibly();
System.out.println( "we got " + futureDHT.getData().getObject() );
shutdown( peers );
}
示例15: testRPC1
import net.tomp2p.utils.Utils; //导入依赖的package包/类
@Test
public void testRPC1() throws Exception {
Peer peer1 = null;
Peer peer2 = null;
try {
peer1 = new PeerMaker(new Number160(rnd)).setWorkerThreads(1).setPorts(4001).setEnableTask(true)
.makeAndListen();
peer2 = new PeerMaker(new Number160(rnd)).setWorkerThreads(1).setPorts(4002).setEnableTask(true)
.makeAndListen();
FutureChannelCreator futureChannelCreator = peer1.getConnectionBean().getConnectionReservation().reserve(1);
futureChannelCreator.awaitUninterruptibly();
Number160 taskId = new Number160(11);
Map<Number160, Data> dataMap = new HashMap<Number160, Data>();
dataMap.put(new Number160(22), new Data("testme"));
FutureResponse futureResponse = peer1.getTaskRPC().sendTask(peer1.getPeerAddress(),
futureChannelCreator.getChannelCreator(), taskId, dataMap, new MyWorker1(), null, false, false);
Utils.addReleaseListenerAll(futureResponse, peer1.getConnectionBean().getConnectionReservation(),
futureChannelCreator.getChannelCreator());
futureResponse.awaitUninterruptibly();
Assert.assertEquals(true, futureResponse.isSuccess());
Thread.sleep(1000);
} finally {
if (peer1 != null) {
peer1.halt();
}
if (peer2 != null) {
peer2.halt();
}
}
}