/** * 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); }
protected void execute() { buildNode(); connectNode(NetworkConfiguration.createInitial(askNodeID())); } };
/** * 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); }
/** * 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); }
/** * 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(); }
netConfig = NetworkConfiguration.createInitial(); logger.info("Start as initial peer"); netConfig = NetworkConfiguration.create(nodeId, address); logger.info("Connect to existing peer. Address: {}", address.toString());
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);
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); } };
netConfig = NetworkConfiguration.createInitial(); String nodeId = UUID.randomUUID().toString(); Peer initialPeer = initialNode.getPeer().peer(); netConfig = NetworkConfiguration.createLocalPeer(nodeId, initialPeer);
/** * Creates a network with the given number of nodes. First node in the list is the * initial node where all other nodes bootstrapped to him.<br> * <b>Important:</b> After usage please shutdown the network. See {@link NetworkTestUtil#shutdownNetwork} * * @param numberOfNodes * size of the network (has to be larger than one) * @return list containing all nodes where the first one is the bootstrapping node (initial) */ public static List<NetworkManager> createNetwork(int numberOfNodes) { IFileConfiguration fileConfig = new TestFileConfiguration(); List<NetworkManager> nodes = new ArrayList<NetworkManager>(numberOfNodes); // create the first node (initial) FSTSerializer serializer = new FSTSerializer(); H2HDummyEncryption encryption = new H2HDummyEncryption(); NetworkManager initial = new NetworkManager(encryption, serializer, fileConfig); INetworkConfiguration netConfig = NetworkConfiguration.createInitialLocalPeer("Node A"); initial.connect(netConfig); nodes.add(initial); // create the other nodes and bootstrap them to the initial peer char letter = 'A'; for (int i = 1; i < numberOfNodes; i++) { NetworkManager node = new NetworkManager(encryption, serializer, fileConfig); INetworkConfiguration otherNetConfig = NetworkConfiguration.createLocalPeer(String.format("Node %s", ++letter), initial.getConnection().getPeer().peer()); node.connect(otherNetConfig); nodes.add(node); } return nodes; }
networkConfig.setPort(Integer.parseInt(bindPort));
node1.connect(NetworkConfiguration.createInitial()); node2.connect(NetworkConfiguration.create(InetAddress.getLocalHost()));
IFileConfiguration fileConfig = new TestFileConfiguration(); IH2HNode initial = H2HNode.createNode(fileConfig, encryption, serializer); initial.connect(NetworkConfiguration.createInitial("initial")); for (int i = 1; i < numberOfNodes; i++) { IH2HNode node = H2HNode.createNode(fileConfig, encryption, serializer); node.connect(NetworkConfiguration.create("node " + i, bootstrapAddress)); nodes.add(node);
/** * Create network configuration for initial peer with random node id * * @return the network configuration */ public static NetworkConfiguration createInitial() { return createInitial(UUID.randomUUID().toString()); }
/** * 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); }
node1.connect(NetworkConfiguration.createInitial()); node2.connect(NetworkConfiguration.create(InetAddress.getLocalHost()));
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()); } }
public static void main(String[] args) throws Exception { IFileConfiguration fileConfiguration = FileConfiguration.createDefault(); // Create two nodes and open a new overlay network IH2HNode node1 = H2HNode.createNode(fileConfiguration); IH2HNode node2 = H2HNode.createNode(fileConfiguration); node1.connect(NetworkConfiguration.createInitial()); node2.connect(NetworkConfiguration.create(InetAddress.getLocalHost())); // These two file agents are used to configure the root directory of the logged in user ExampleFileAgent node1FileAgent = new ExampleFileAgent(); ExampleFileAgent node2FileAgent = new ExampleFileAgent(); // Register user 'Alice' and login her at node 1 and 2 UserCredentials alice = new UserCredentials("Alice", "password", "pin"); node1.getUserManager().createRegisterProcess(alice).execute(); node1.getUserManager().createLoginProcess(alice, node1FileAgent).execute(); node2.getUserManager().createLoginProcess(alice, node2FileAgent).execute(); // In this example, a file event listener is registered at node 2. Therefore, we will listen to events // that happen by actions taken by node 1. node2.getFileManager().subscribeFileEvents(new ExampleEventListener(node2.getFileManager())); // To demonstrate the 'add' event, we will add a new file with node 1 // Let's create a file and upload it at node 1 File fileAtNode1 = new File(node1FileAgent.getRoot(), "test-file-event.txt"); FileUtils.write(fileAtNode1, "Hello"); // add some content node1.getFileManager().createAddProcess(fileAtNode1).execute(); // Let's trigger a deletion event fileAtNode1.delete(); node1.getFileManager().createDeleteProcess(fileAtNode1).execute(); }
@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); } }