本文整理汇总了Java中org.hive2hive.core.api.H2HNode类的典型用法代码示例。如果您正苦于以下问题:Java H2HNode类的具体用法?Java H2HNode怎么用?Java H2HNode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
H2HNode类属于org.hive2hive.core.api包,在下文中一共展示了H2HNode类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createNode
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
private IH2HNode createNode() throws UnknownHostException {
InetAddress bootstrapAddress = InetAddress.getByName(BOOTSTRAP_HOST_NAME); // InetAddress.getLocalHost();
INetworkConfiguration networkConf = null;
if (IS_INITIAL_NODE) {
networkConf = NetworkConfiguration.createInitial("node-"
+ BOOTSTRAP_HOST_NAME + "-" + NODE_NAME);
} else {
networkConf = NetworkConfiguration.create("node-"
+ BOOTSTRAP_HOST_NAME + "-" + NODE_NAME,
bootstrapAddress);
}
FSTSerializer serializer = new FSTSerializer();
IH2HNode node = H2HNode.createNode(FileConfiguration.createDefault(), new H2HDummyEncryption(), serializer);
node.connect(networkConf);
return node;
}
示例2: getH2HVersion
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
/**
* Get version of Hive2Hive.
*
* @return h2h version string (core) or null.
*/
public static String getH2HVersion() {
String pomProperties = "/META-INF/maven/org.hive2hive/org.hive2hive.core/pom.properties";
Class<H2HNode> clazz = H2HNode.class;
String version = readVersionFromPom(clazz, pomProperties);
return version;
}
示例3: main
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
public static void main(String[] args) throws UnknownHostException {
// Create a consistent file configuration for all nodes
IFileConfiguration fileConfiguration = FileConfiguration.createDefault();
// Initialize several nodes (not connected yet)
IH2HNode node1 = H2HNode.createNode(fileConfiguration);
IH2HNode node2 = H2HNode.createNode(fileConfiguration);
IH2HNode node3 = H2HNode.createNode(fileConfiguration);
IH2HNode node4 = H2HNode.createNode(fileConfiguration);
// Create a new P2P network at the first (initial) peer
node1.connect(NetworkConfiguration.createInitial());
// Connect the 2nd peer to the network. This peer bootstraps to node1 running at the local host
NetworkConfiguration node2Conf = NetworkConfiguration.create(InetAddress.getLocalHost());
node2.connect(node2Conf);
// The network configuration builder allows you to configure more details
// here we set a custom (non-random) node id and a custom port that the node 3 binds to
NetworkConfiguration node3Conf = NetworkConfiguration.create(InetAddress.getLocalHost()).setPort(4777)
.setNodeId("node3");
node3.connect(node3Conf);
// Nodes can bootstrap to any of the connected peers. Therefore, we set that node4 should connect to
// node3 to become a part of the P2P network
NetworkConfiguration node4Conf = NetworkConfiguration.create(InetAddress.getLocalHost()).setBootstrapPort(4777);
node4.connect(node4Conf);
// We can test the connection status of these nodes
System.out.println("Node 1 is connected: " + node1.isConnected());
System.out.println("Node 2 is connected: " + node2.isConnected());
System.out.println("Node 3 is connected: " + node3.isConnected());
System.out.println("Node 4 is connected: " + node4.isConnected());
}
示例4: main
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
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());
}
示例5: main
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
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();
}
示例6: getPeer
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
@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());
}
}
示例7: createH2HNetwork
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
/**
* Creates a <code>Hive2Hive</code> 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 Hive2Hive nodes where the first one is the bootstrapping node (initial)
*/
public static List<IH2HNode> createH2HNetwork(int numberOfNodes) {
if (numberOfNodes < 1)
throw new IllegalArgumentException("Invalid network size.");
List<IH2HNode> nodes = new ArrayList<IH2HNode>(numberOfNodes);
// create initial peer
FSTSerializer serializer = new FSTSerializer();
H2HDummyEncryption encryption = new H2HDummyEncryption();
IFileConfiguration fileConfig = new TestFileConfiguration();
IH2HNode initial = H2HNode.createNode(fileConfig, encryption, serializer);
initial.connect(NetworkConfiguration.createInitial("initial"));
nodes.add(initial);
try {
InetAddress bootstrapAddress = InetAddress.getLocalHost();
for (int i = 1; i < numberOfNodes; i++) {
IH2HNode node = H2HNode.createNode(fileConfig, encryption, serializer);
node.connect(NetworkConfiguration.create("node " + i, bootstrapAddress));
nodes.add(node);
}
} catch (UnknownHostException e) {
// should not happen
}
return nodes;
}
示例8: createNode
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
private synchronized void createNode() {
fileConfiguration = FileConfiguration.createDefault();
node = H2HNode.createNode(fileConfiguration);
}
示例9: main
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
public static void main(String[] args) {
try {
INetworkConfiguration netConfig = null;
if (args.length == 0) {
// initial peer
netConfig = NetworkConfiguration.createInitial();
logger.info("Start as initial peer");
} else if (args.length >= 1) {
// connect to node
String nodeId = UUID.randomUUID().toString();
InetAddress address = InetAddress.getByName(args[0]);
netConfig = NetworkConfiguration.create(nodeId, address);
logger.info("Connect to existing peer. Address: {}", address.toString());
}
IFileConfiguration fileConfig = FileConfiguration.createDefault();
IH2HNode peerNode = H2HNode.createNode(fileConfig);
boolean success = peerNode.connect(netConfig);
if (success) {
logger.info("Setup successful.");
} else {
logger.error("Could not setup node.");
System.exit(-1);
}
// do not exit
logger.info("Finished creating node.");
Thread.currentThread().join();
} catch (Exception e) {
logger.warn("Exception: {}", e.getMessage(), e);
}
}
示例10: main
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
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 users
ExampleFileAgent node1FileAgent = new ExampleFileAgent();
ExampleFileAgent node2FileAgent = new ExampleFileAgent();
// Register and login user 'Alice' at node 1
UserCredentials alice = new UserCredentials("Alice", "password", "pin");
node1.getUserManager().createRegisterProcess(alice).execute();
node1.getUserManager().createLoginProcess(alice, node1FileAgent).execute();
// Register and login user 'Bob' at node 2
UserCredentials bob = new UserCredentials("Bob", "password", "pin");
node2.getUserManager().createRegisterProcess(bob).execute();
node2.getUserManager().createLoginProcess(bob, node2FileAgent).execute();
// Let's create a folder at Alice and upload it
IFileManager fileManager1 = node1.getFileManager(); // The file management of Alice's peer
File folderAtAlice = new File(node1FileAgent.getRoot(), "shared-folder");
folderAtAlice.mkdirs();
fileManager1.createAddProcess(folderAtAlice).execute();
// Let's share the folder with Bob giving him write permission
fileManager1.createShareProcess(folderAtAlice, "Bob", PermissionType.WRITE).execute();
// Wait some time in order to get the file share event propagated to Bob
System.out.println("Alice shared a folder with Bob. We'll wait some time for its propagation...");
Thread.sleep(20000);
// Bob can now 'download' the folder (yes, sounds a little bit silly...)
IFileManager fileManager2 = node2.getFileManager(); // The file management of Bob's peer
File folderAtBob = new File(node2FileAgent.getRoot(), "shared-folder");
fileManager2.createDownloadProcess(folderAtBob).execute();
// Bob could for example upload a new file to the shared folder
File fileAtBob = new File(folderAtBob, "shared-file.txt");
FileUtils.write(fileAtBob, "This is a shared file of Alice and Bob");
fileManager2.createAddProcess(fileAtBob).execute();
// Wait some time in order to get the file share event propagated to Bob
System.out.println("Waiting that Alice sees the file from Bob...");
Thread.sleep(20000);
// Alice now can obtain the shared file
File fileAtAlice = new File(folderAtAlice, "shared-file.txt");
fileManager1.createDownloadProcess(fileAtAlice).execute();
System.out.println("Content of the file in the shared folder at Alice: " + FileUtils.readFileToString(fileAtAlice));
}
示例11: main
import org.hive2hive.core.api.H2HNode; //导入依赖的package包/类
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 users
ExampleFileAgent node1FileAgent = new ExampleFileAgent();
ExampleFileAgent node2FileAgent = new ExampleFileAgent();
// Register and login user 'Alice' at node 1
UserCredentials alice = new UserCredentials("Alice", "password", "pin");
node1.getUserManager().createRegisterProcess(alice).execute();
node1.getUserManager().createLoginProcess(alice, node1FileAgent).execute();
// Also login user 'Alice' at node 2
node2.getUserManager().createLoginProcess(alice, node2FileAgent).execute();
// All file management operations are located at the file manager. Here we get the file managers of
// each peer alice is connected to.
IFileManager fileManager1 = node1.getFileManager(); // for node 1
IFileManager fileManager2 = node2.getFileManager(); // for node 2
// Let's create a file and upload it at node 1
File fileAtNode1 = new File(node1FileAgent.getRoot(), "test-file.txt");
FileUtils.write(fileAtNode1, "Hello"); // add some content
fileManager1.createAddProcess(fileAtNode1).execute();
// Normally, the node 2 would be notified about the new file through the event bus (shown in another
// example). However, we just know that the file exists in the network and can download it at node 2.
// This is only possible because Alice is logged in into node 2 as well.
File fileAtNode2 = new File(node2FileAgent.getRoot(), "test-file.txt");
// this file does not exist yet (as we did not start the download process yet)
System.out.println("Existence of the file prior to download: " + fileAtNode2.exists());
fileManager2.createDownloadProcess(fileAtNode2).execute();
// We can now re-check whether the file exists or not
System.out.println("Existence of the file after download: " + fileAtNode2.exists());
System.out.println("Content of the first version at node 2: " + FileUtils.readFileToString(fileAtNode2));
// Now, let's modify the file at node 2 and re-upload a new version of it
FileUtils.write(fileAtNode2, " World!", true); // append the text
fileManager2.createUpdateProcess(fileAtNode2).execute();
// The file has now updated, therefore we should download the new version at node 1
fileManager1.createDownloadProcess(fileAtNode1).execute();
System.out.println("Content of the second version at node 1: " + FileUtils.readFileToString(fileAtNode1));
}