Skip to content

Commit 8ee8b00

Browse files
committed
First Complete Version of the Chord Library:
- Implemented the base for the Chord Ring protocol; - Implemented the Stabilization Protocol; - Implemented the management for the Join & Leave of Nodes; - Implemented the management of item placement and retrieval; - Implemented the management of the items transportation in case of Join of new Nodes; - Implemented two classes used for testing the automatic placement and retrieval of items;
1 parent 900f94c commit 8ee8b00

10 files changed

+1589
-0
lines changed

.idea/compiler.xml

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/org_apache_directory_studio_org_apache_commons_codec_1_8.xml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.distribsystems.p2p</groupId>
8+
<artifactId>p2p-chord</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>p2p-chord</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.7</maven.compiler.source>
18+
<maven.compiler.target>1.7</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.11</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
32+
<plugins>
33+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
34+
<plugin>
35+
<artifactId>maven-clean-plugin</artifactId>
36+
<version>3.1.0</version>
37+
</plugin>
38+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
39+
<plugin>
40+
<artifactId>maven-resources-plugin</artifactId>
41+
<version>3.0.2</version>
42+
</plugin>
43+
<plugin>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.8.0</version>
46+
</plugin>
47+
<plugin>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>2.22.1</version>
50+
</plugin>
51+
<plugin>
52+
<artifactId>maven-jar-plugin</artifactId>
53+
<version>3.0.2</version>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-install-plugin</artifactId>
57+
<version>2.5.2</version>
58+
</plugin>
59+
<plugin>
60+
<artifactId>maven-deploy-plugin</artifactId>
61+
<version>2.8.2</version>
62+
</plugin>
63+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
64+
<plugin>
65+
<artifactId>maven-site-plugin</artifactId>
66+
<version>3.7.1</version>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-project-info-reports-plugin</artifactId>
70+
<version>3.0.0</version>
71+
</plugin>
72+
</plugins>
73+
</pluginManagement>
74+
</build>
75+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.distribsystems.p2p;
2+
3+
import java.util.Scanner;
4+
5+
public class Chord {
6+
public final static int MAXIMUM_FINGER_TABLE_SIZE = 160;
7+
public final static int FINGER_TABLE_SIZE = 8; //Max 160
8+
public final static int STABILIZATION_DELAY = 8; //[seconds]
9+
public final static int PING_DELAY = 3; //[seconds]
10+
11+
public final static String NEW_PREDECESSOR = "NEW_PREDECESSOR";
12+
public final static String FIND_FINGER = "FIND_FINGER"; //FIND_NODE
13+
public final static String FINGER_FOUND = "FINGER_FOUND"; //FINGER_FOUND
14+
public static final String REQUEST_PREDECESSOR = "REQUEST_PREDECESSOR";
15+
public final static String PING = "PING";
16+
public final static String PONG = "PONG";
17+
public static final String FIND_ITEM = "FIND_ITEM";
18+
public static final String ITEM_FOUND = "ITEM_FOUND";
19+
public static final String PLACE_ITEM = "PLACE_ITEM";
20+
public static final String ITEM_PLACED = "ITEM_PLACED";
21+
public static final String NOT_FOUND = "NOT_FOUND";
22+
23+
24+
public static void main(String[] args){
25+
String ip = "127.0.0.1";
26+
int port = 8000;
27+
28+
/**
29+
* Program Arguments
30+
* args[0] = port of the newly created Node
31+
* args[1] = ip address of the Node to connect to
32+
* args[2] = port of the Node to connect to
33+
*/
34+
if(args.length > 0){
35+
port = Integer.parseInt(args[0]);
36+
}
37+
38+
if (args.length == 1) {
39+
// Create new node
40+
new Node(ip, port);
41+
} else if (args.length == 3) {
42+
String existingIpAddress = args[1];
43+
int existingPort = Integer.parseInt(args[2]);
44+
45+
// Create new node
46+
new Node(ip, port, existingIpAddress, existingPort);
47+
} else {
48+
// User Input
49+
Scanner myObj = new Scanner(System.in);
50+
51+
System.out.println("Create a Node within a new ChordRing? [y/n]\n");
52+
String resp = myObj.nextLine();
53+
//New Chord Ring
54+
if(resp.equals("y")){
55+
System.out.println("\n Insert Node's PortNumber: ");
56+
port = myObj.nextInt();
57+
myObj.nextLine();
58+
59+
// Create new node
60+
new Node(ip, port);
61+
}
62+
//Add Node to Existing ChordRing
63+
else{
64+
System.out.println("\n Insert Node's PortNumber: ");
65+
port = myObj.nextInt();
66+
myObj.nextLine();
67+
68+
System.out.println("\n Insert the IP Address of the already existing Node: ");
69+
String existingIpAddress = myObj.nextLine();
70+
71+
System.out.println("\n Insert the PortNumber of the already existing Node: ");
72+
int existingPort = myObj.nextInt();
73+
myObj.nextLine();
74+
75+
// Create new node
76+
new Node(ip, port, existingIpAddress, existingPort);
77+
}
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.distribsystems.p2p;
2+
3+
4+
// Java implementation for a client
5+
// Save file as Client.java
6+
7+
import java.io.*;
8+
import java.net.*;
9+
import java.util.Scanner;
10+
11+
// Client class
12+
public class Client extends Thread
13+
{
14+
public final static int STARTING_PORT = 5056;
15+
private int serverPort;
16+
17+
public Client(int sPort){
18+
serverPort = sPort;
19+
}
20+
21+
@Override
22+
public void run() {
23+
if(serverPort != STARTING_PORT) {
24+
try {
25+
Scanner scn = new Scanner(System.in);
26+
27+
// getting localhost ip
28+
InetAddress ip = InetAddress.getByName("localhost");
29+
30+
// establish the connection with server port 5056
31+
Socket s = new Socket(ip, STARTING_PORT);
32+
33+
// obtaining input and out streams
34+
DataInputStream dis = new DataInputStream(s.getInputStream());
35+
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
36+
37+
// the following loop performs the exchange of
38+
// information between client and client handler
39+
while (true) {
40+
System.out.println(dis.readUTF());
41+
String tosend = scn.nextLine();
42+
dos.writeUTF(tosend);
43+
44+
// If client sends exit,close this connection
45+
// and then break from the while loop
46+
if (tosend.equals("Exit")) {
47+
System.out.println("Closing this connection : " + s);
48+
s.close();
49+
System.out.println("Connection closed");
50+
break;
51+
}
52+
53+
// printing date or time as requested by client
54+
String received = dis.readUTF();
55+
System.out.println(received);
56+
}
57+
58+
// closing resources
59+
scn.close();
60+
dis.close();
61+
dos.close();
62+
} catch (Exception e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
}
67+
}
68+

0 commit comments

Comments
 (0)