/** * Creates a local peer that is only able to bootstrap to a peer running on the same host. * * @param nodeID the id of the peer to create * @param initialPeer the peer to bootstrap to * @return the network configuration for local peers */ public static NetworkConfiguration createLocalPeer(String nodeID, Peer initialPeer) { return new NetworkConfiguration().setNodeId(nodeID).setPort(AUTO_PORT).setBootstrapLocal(initialPeer); }
/** * Create network configuration for initial peer with given node id. * * @param nodeID defines the location of the peer in the DHT * @return the network configuration */ public static NetworkConfiguration createInitial(String nodeID) { return new NetworkConfiguration().setNodeId(nodeID).setPort(AUTO_PORT); }
protected void execute() { buildNode(); connectNode(NetworkConfiguration.createInitial(askNodeID())); } };
public static void main(String[] args) throws UnknownHostException { IFileConfiguration fileConfiguration = FileConfiguration.createDefault(); node1.connect(NetworkConfiguration.createInitial()); NetworkConfiguration node2Conf = NetworkConfiguration.create(InetAddress.getLocalHost()); node2.connect(node2Conf); NetworkConfiguration node3Conf = NetworkConfiguration.create(InetAddress.getLocalHost()).setPort(4777) .setNodeId("node3"); node3.connect(node3Conf); NetworkConfiguration node4Conf = NetworkConfiguration.create(InetAddress.getLocalHost()).setBootstrapPort(4777); node4.connect(node4Conf);
@Test public void testConnectToOtherPeer() throws UnknownHostException { NetworkManager nodeA = new NetworkManager(new H2HDummyEncryption(), serializer, new TestFileConfiguration()); NetworkManager nodeB = new NetworkManager(new H2HDummyEncryption(), serializer, new TestFileConfiguration()); INetworkConfiguration netConfigA = NetworkConfiguration.createInitial("nodeA"); INetworkConfiguration netConfigB = NetworkConfiguration.create("nodeB", InetAddress.getLocalHost()); try { assertTrue(nodeA.connect(netConfigA)); assertTrue(nodeB.connect(netConfigB)); } finally { nodeA.disconnect(false); nodeB.disconnect(false); } }
/** * Create network configuration for 'normal' peer. The bootstrapping happens to the specified address and * port * * @param nodeID defines the location of the peer in the DHT. Should not be null * @param bootstrapAddress the address to bootstrap to. This can be address of the initial peer or any * other peer connected to the DHT. * @param bootstrapPort the port to bootstrap * @return the network configuration */ public static NetworkConfiguration create(String nodeID, InetAddress bootstrapAddress, int bootstrapPort) { return new NetworkConfiguration().setNodeId(nodeID).setPort(AUTO_PORT).setBootstrap(bootstrapAddress, bootstrapPort); }
/** * Create a local initial peer. Regard that bootstrapping may only work for peers running on the same * host. * * @param nodeID the id of the initial peer * @return the network configuration for local peers (initial) */ public static NetworkConfiguration createInitialLocalPeer(String nodeID) { return new NetworkConfiguration().setNodeId(nodeID).setPort(AUTO_PORT).setLocal(); }
public static void main(String[] args) throws NoPeerConnectionException, InvalidProcessStateException, ProcessExecutionException { // Create a node and open a new overlay network IH2HNode node = H2HNode.createNode(FileConfiguration.createDefault()); node.connect(NetworkConfiguration.createInitial()); // The register functionality is in the user manager API IUserManager userManager = node.getUserManager(); // Create user credentials to register a new user 'Alice' UserCredentials alice = new UserCredentials("Alice", "very-secret-password", "secret-pin"); // Create a new register process and start it (blocking) IProcessComponent<Void> registerAlice = userManager.createRegisterProcess(alice); registerAlice.execute(); // Check if Alice is now registered boolean aliceRegistered = userManager.isRegistered("Alice"); System.out.println("Alice is registered: " + aliceRegistered); // Let's login to Alice's user account (blocking) IProcessComponent<Void> loginAlice = userManager.createLoginProcess(alice, new ExampleFileAgent()); loginAlice.execute(); // Check if Alice is now logged in System.out.println("Alice is logged in: " + userManager.isLoggedIn()); } }
protected void execute() throws UnknownHostException { String nodeID = askNodeID(); print("Specify Bootstrap Address:"); InetAddress bootstrapAddress = InetAddress.getByName(awaitStringParameter()); String bootstrapPort = "default"; if (isExpertMode) { print("Specify Bootstrap Port or enter 'default':"); bootstrapPort = awaitStringParameter(); } buildNode(); NetworkConfiguration config = NetworkConfiguration.create(nodeID, bootstrapAddress); if (!"default".equalsIgnoreCase(bootstrapPort)) { config.setBootstrapPort(Integer.parseInt(bootstrapPort)); } connectNode(config); } };
/** * Create network configuration for 'normal' peer with random node id. The bootstrapping happens at the * default port {@link H2HConstants#H2H_PORT}. * * @param bootstrapAddress the address to bootstrap to. This can be address of the initial peer or any * other peer connected to the DHT. * @return the network configuration */ public static NetworkConfiguration create(InetAddress bootstrapAddress) { return create(UUID.randomUUID().toString(), bootstrapAddress); }
@Test public void getPeer() { // a unconnected node does not provide a peer IFileConfiguration fileConfig = FileConfiguration.createDefault(); IH2HNode node = H2HNode.createNode(fileConfig); Assert.assertNull(node.getPeer()); // connected nodes return a peer for (IH2HNode connectedNode : network) { Assert.assertNotNull(connectedNode.getPeer()); } } }
/** * Creates a default file configuration * * @return the file configuration */ public static IFileConfiguration createDefault() { return new FileConfiguration(H2HConstants.DEFAULT_MAX_FILE_SIZE, H2HConstants.DEFAULT_MAX_NUM_OF_VERSIONS, H2HConstants.DEFAULT_MAX_SIZE_OF_ALL_VERSIONS, H2HConstants.DEFAULT_CHUNK_SIZE); }
/** * @param bootstrapPeer the initial local peer to bootstrap to. Note: this is just for testings * @return this instance */ public NetworkConfiguration setBootstrapLocal(Peer bootstrapPeer) { this.bootstrapPeer = bootstrapPeer; return setLocal(); }
/** * @param bootstrapAddress the address to bootstrap to. If it is <code>null</code>, the peer is 'initial' * and does not bootstrap to any other peer. * @return this instance */ public NetworkConfiguration setBootstrap(InetAddress bootstrapAddress) { return setBootstrap(bootstrapAddress, H2HConstants.H2H_PORT); }
private void buildNode() { IFileConfiguration fileConfig = FileConfiguration.createCustom(maxFileSize, maxNumOfVersions, maxSizeAllVersions, chunkSize); IH2HSerialize serializer; if ("java".equalsIgnoreCase(config.getString("Serializer.mode"))) { serializer = new JavaSerializer(); } else { serializer = new FSTSerializer(config.getBoolean("Serializer.FST.unsafe"), new BCSecurityClassProvider()); } node = H2HNode.createNode(fileConfig, new H2HDefaultEncryption(serializer), serializer); }
@Test public void testConnectDisconnect() throws UnknownHostException { NetworkManager nodeA = new NetworkManager(new H2HDummyEncryption(), serializer, new TestFileConfiguration()); NetworkManager nodeB = new NetworkManager(new H2HDummyEncryption(), serializer, new TestFileConfiguration()); INetworkConfiguration netConfigA = NetworkConfiguration.createInitial("nodeA"); INetworkConfiguration netConfigB = NetworkConfiguration.create("nodeB", InetAddress.getLocalHost()); try { nodeA.connect(netConfigA); nodeB.connect(netConfigB); assertTrue(nodeB.disconnect(false)); assertTrue(nodeB.connect(netConfigB)); } finally { nodeA.disconnect(false); nodeB.disconnect(false); } }
/** * Create network configuration for 'normal' peer. The bootstrapping happens at the default port * {@link H2HConstants#H2H_PORT}. * * @param nodeID defines the location of the peer in the DHT. Should not be null * @param bootstrapAddress the address to bootstrap to. This can be address of the initial peer or any * other peer connected to the DHT. * @return the network configuration */ public static NetworkConfiguration create(String nodeID, InetAddress bootstrapAddress) { return new NetworkConfiguration().setNodeId(nodeID).setPort(AUTO_PORT).setBootstrap(bootstrapAddress, H2HConstants.H2H_PORT); }
/** * Create network configuration for initial peer with random node id * * @return the network configuration */ public static NetworkConfiguration createInitial() { return createInitial(UUID.randomUUID().toString()); }
/** * Create a file configuration with the given parameters * * @param maxFileSize the maximum file size (in bytes) * @param maxNumOfVersions the allowed number of versions * @param maxSizeAllVersions the maximum file size when summing up all versions (in bytes) * @param chunkSize the size of a chunk (in bytes) * @return the created configuration */ public static IFileConfiguration createCustom(BigInteger maxFileSize, int maxNumOfVersions, BigInteger maxSizeAllVersions, int chunkSize) { return new FileConfiguration(maxFileSize, maxNumOfVersions, maxSizeAllVersions, chunkSize); }
@Test public void testConnectAsInitial() { NetworkManager initialNode = new NetworkManager(new H2HDummyEncryption(), serializer, new TestFileConfiguration()); try { INetworkConfiguration netConfig = NetworkConfiguration.createInitial("initial node"); assertTrue(initialNode.connect(netConfig)); } finally { initialNode.disconnect(false); } }