*/
for (int i = 0; i < relayPeers; i++) {
Peer peer = createPeer(RELAY_START_PORT + i);
// Note: Does not work if relay does not have a PeerDHT
new PeerBuilderDHT(peer).storageLayer(new LoggingStorageLayer("RELAY", false)).start();
PeerNAT peerNAT = new PeerBuilderNAT(peer).bufferConfiguration(bufferConfig).start();
relays.add(peerNAT);
LOG.debug("Relay peer {} started", i);
}
/**
* Then init the mobile peers
*/
for (int i = 0; i < mobilePeers; i++) {
Peer peer = createPeer(MOBILE_START_PORT + i);
bootstrap(peer);
// start DHT capability
new PeerBuilderDHT(peer).storageLayer(new LoggingStorageLayer("UNREACHABLE", true)).start();
LOG.debug("Connecting to Relay now");
Set<PeerAddress> relayAddresses = new HashSet<PeerAddress>(relayPeers);
for (PeerNAT relay : relays) {
relayAddresses.add(relay.peer().peerAddress());
}
PeerBuilderNAT builder = new PeerBuilderNAT(peer).peerMapUpdateInterval(PEER_MAP_UPDATE_INTERVAL_S)
.relays(relayAddresses).relayType(RELAY_TYPE);
if (RELAY_TYPE == RelayType.ANDROID) {
GCMServerCredentials gcmCredentials = new GCMServerCredentials().senderAuthenticationKey(gcmKey)
.senderId(gcmSenderId).registrationId(GCM_REGISTRATION_ID);
builder.gcmServerCredentials(gcmCredentials);
}
PeerNAT peerNat = builder.start();
FutureRelayNAT futureRelayNAT = peerNat.startRelay(relays.get(0).peer().peerAddress()).awaitUninterruptibly();
if (!futureRelayNAT.isSuccess()) {
LOG.error("Cannot connect to Relay(s). Reason: {}", futureRelayNAT.failedReason());
return;
}
mobiles.add(peerNat);
LOG.debug("Mobile peer {} connected to DHT and Relay(s)", i);
}
/**
* Finally init the query peers
*/
for (int i = 0; i < queryPeers; i++) {
Peer peer = createPeer(QUERY_START_PORT + i);
bootstrap(peer);
PeerDHT peerDHT = new PeerBuilderDHT(peer).storageLayer(new LoggingStorageLayer("QUERY", false)).start();
new PeerBuilderNAT(peer).start();
queries.add(new QueryNode(peerDHT, MEDIUM_SLEEP_TIME_MS, MEDIUM_DATA_SIZE_BYTES));
LOG.debug("Query peer {} started", i);
}