Java RMI- Join the ring and send text file to complete the case statements in Ch
ID: 3689814 • Letter: J
Question
Java RMI- Join the ring and send text file to complete the case statements in ChordUser.java
import java.rmi.*;
import java.net.*;
import java.util.*;
import java.io.*;
/**************************************************************************
* File: ChordUser.java
***************************************************************************/
public class ChordUser
{
int port;
public ChordUser(int p) {
port = p;
Timer timer1 = new Timer();
timer1.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
Chord chord = new Chord(port);
Scanner scan= new Scanner(System.in);
String delims = "[ ]+";
String command = "";
System.out.println(" Enter your command "
+ " 1) JOIN 2) PRINT 3) WRITE "
+ " 4) READ 5) DELETE 6) LEAVE ");
while (true)
{
String text= scan.nextLine();
String[] tokens = text.split(delims);
switch(text){
case "JOIN":
if (tokens.length == 2) {
chord.joinRing("localhost", Integer.parseInt(tokens[1])); System.out.println("Error joining the ring!");
}
System.out.println("Error joining the ring!");
break;
case "PRINT":
chord.Print();
break;
case "WRITE":
if (tokens.length == 2) {
String path = ".\"+ port +"\"+Integer.parseInt(tokens[1])+".txt"; // path to file
File f = new File(path);
FileInputStream fis = null;
fis = new FileInputStream(f);
byte[] data = new byte[fis.available()];
fis.read(data); // read data
fis.close();
chord.put(Integer.parseInt(tokens[1]), data); // put file into ring
}
System.out.println("Could not put file!");
break;
case "READ":
if (tokens.length == 2) {
System.out.println(chord.get(Integer.parseInt(tokens[1])));
}
System.out.println("Could not get file!");
break;
case "DELETE":
if (tokens.length == 2) {
chord.delete(Integer.parseInt(tokens[1]));
}
System.out.println("Could not delete file!");
break;
case "LEAVE":
System.out.println("Leaving ring");
System.exit(0);
return;
}//end switch
}//endwhiletrue
}//end try
catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//Do something
}//end finally
}//end run
}, 1000, 1000);
}//end constructor
public static void main(String args[])
{
ChordUser chordUser=new ChordUser(34673);
}//endmain
}//endchorduser
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
import java.util.*;
import java.io.*;
/**************************************************************************
* File: Chord.java
***************************************************************************/
public class Chord extends java.rmi.server.UnicastRemoteObject implements ChordMessageInterface
{
public static final int M = 2;
Registry registry; // rmi registry for lookup the remote objects.
ChordMessageInterface successor;
ChordMessageInterface predecessor;
ChordMessageInterface[] finger;
int nextFinger;
int i; // GUID
public ChordMessageInterface rmiChord(String ip, int port)
{
ChordMessageInterface chord = null;
try{
Registry registry = LocateRegistry.getRegistry(ip, port);
chord = (ChordMessageInterface)(registry.lookup("Chord"));
return chord;
} catch (RemoteException e) {
e.printStackTrace();
} catch(NotBoundException e){
e.printStackTrace();
}
return null;
}
public Boolean isKeyInSemiCloseInterval(int key, int key1, int key2)
{
if (key1 < key2)
return (key > key1 && key <= key2);
else
return (key > key2 || key <= key1);
}
public Boolean isKeyInOpenInterval(int key, int key1, int key2)
{
if (key1 < key2)
return (key > key1 && key < key2);
else
return (key > key2 || key < key1);
}
public void put(int guid, byte[] data) throws RemoteException {
}
public byte[] get(int guid) throws RemoteException {
return null;
}
public void delete(int guid) throws RemoteException {
}
public int getId() throws RemoteException {
return i;
}
public boolean isAlive() throws RemoteException {
return true;
}
public ChordMessageInterface getPredecessor() throws RemoteException {
return predecessor;
}
public ChordMessageInterface locateSuccessor(int key) throws RemoteException {
if (key == i)
throw new IllegalArgumentException("Key must be distinct that " + i);
if (successor.getId() != i)
{
if (isKeyInSemiCloseInterval(key, i, successor.getId()))
return successor;
ChordMessageInterface j = closestPrecedingNode(key);
if (j == null)
return null;
return j.locateSuccessor(key);
}
return successor;
}
public ChordMessageInterface closestPrecedingNode(int key) throws RemoteException {
int count = M-1;
if (key == i) throw new IllegalArgumentException("Key must be distinct that " + i);
for (count = M-1; count >= 0; count--) {
if (finger[count] != null && isKeyInSemiCloseInterval(finger[count].getId(), i, key))
return finger[count];
}
return successor;
}
public void joinRing(String ip, int port) throws RemoteException {
try{
System.out.println("Get Registry to joining ring");
Registry registry = LocateRegistry.getRegistry(ip, port);
ChordMessageInterface chord = (ChordMessageInterface)(registry.lookup("Chord"));
predecessor = null;
System.out.println("Locating successor to joining ring" + port);
successor = chord.locateSuccessor(i);
if (successor != null)
System.out.println("Successor " + successor.getId());
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
public void stabilize() {
try {
ChordMessageInterface x = successor.getPredecessor();
if (x != null && x.getId() != i &&
(isKeyInOpenInterval(x.getId(), i, successor.getId()) || i == successor.getId()))
{
successor = x;
}
if (successor.getId() != getId())
{
successor.notify(this);
}
} catch(RemoteException e) {
e.printStackTrace();
} catch(NullPointerException e) {
e.printStackTrace();
}
}
public void notify(ChordMessageInterface j) throws RemoteException {
if (predecessor == null || (predecessor != null && isKeyInOpenInterval(j.getId(), predecessor.getId(), i)))
// transfer keys in the range [j,i) to j;
predecessor = j;
}
public void fixFingers() {
if (finger[nextFinger] != null)
nextFinger = nextFinger + 1;
if (nextFinger >= M)
nextFinger = 0;
try {
finger[nextFinger] = locateSuccessor((i + (1 << nextFinger)));
} catch(RemoteException e){
e.printStackTrace();
}
}
public void checkPredecessor() {
try {
if (predecessor != null) {
if (!predecessor.isAlive())
predecessor = null;
}
} catch(RemoteException e) {
predecessor = null;
}
}
public Chord(final int port) throws RemoteException {
int j;
finger = new ChordMessageInterface[M];
for (j=0;j<M; j++){
finger[j] = null;
}
i = port;
predecessor = null;
successor = this;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
stabilize();
fixFingers();
checkPredecessor();
}
}, 500, 500);
try{
// create the registry and bind the name and object.
System.out.println("Starting RMI at port="+port);
registry = LocateRegistry.createRegistry( port );
registry.rebind("Chord", this);
}
catch(RemoteException e){
throw e;
}
}
void Print()
{
try {
if (successor != null)
System.out.println("successor "+ successor.getId());
if (predecessor != null)
System.out.println("predecessor "+ predecessor.getId());
}
catch(RemoteException e){
System.out.println("Cannot retrive id");
}
}
}
import java.rmi.*;
import java.io.*;
/**************************************************************************
* File: ChordMessageInterface.java
***************************************************************************/
public interface ChordMessageInterface extends Remote
{
public ChordMessageInterface getPredecessor() throws RemoteException;
ChordMessageInterface locateSuccessor(int key) throws RemoteException;
ChordMessageInterface closestPrecedingNode(int key) throws RemoteException;
public void joinRing(String Ip, int port) throws RemoteException;
public void notify(ChordMessageInterface j) throws RemoteException;
public boolean isAlive() throws RemoteException;
public int getId() throws RemoteException;
public void put(int guid, byte[] data) throws IOException, RemoteException;
public byte[] get(int id) throws IOException, RemoteException;
public void delete(int id) throws IOException, RemoteException;
}
Explanation / Answer
Answer:
Hi, I have modified your code.
ChordUser.java
import java.rmi.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class ChordUser
{
int port;
public ChordUser(int p) {
port = p;
Timer timer1 = new Timer();
timer1.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
Chord chord = new Chord(port);
Scanner scan= new Scanner(System.in);
String delims = "[ ]+";
String command = "";
System.out.println(" Enter your command "
+ " 1) JOIN 2) PRINT 3) WRITE "
+ " 4) READ 5) DELETE 6) LEAVE ");
while (true)
{
String text= scan.nextLine();
String[] tokens = text.split(delims);
if(tokens[0].equals("JOIN")){
if (tokens.length == 2) {
chord.joinRing("localhost", port, Integer.parseInt(tokens[1]));
System.out.println("Error joining the ring!");
}
System.out.println("Error joining the ring!");
}
else if(text.equals("PRINT")){
chord.Print();
}
else if(text.equals("WRITE")){
if (tokens.length == 2) {
String path = ".\"+ port +"\"+Integer.parseInt(tokens[1])+".txt"; // path to file
File f = new File(path);
FileInputStream fis = null;
fis = new FileInputStream(f);
byte[] data = new byte[fis.available()];
fis.read(data); // read data
fis.close();
chord.put(Integer.parseInt(tokens[1]), data); // put file into ring
}
System.out.println("Could not put file!");
}
else if(text.equals("READ")){
if (tokens.length == 2) {
System.out.println(chord.get(Integer.parseInt(tokens[1])));
}
System.out.println("Could not get file!");
}
else if(text.equals("DELETE")){
if (tokens.length == 2) {
chord.delete(Integer.parseInt(tokens[1]));
}
System.out.println("Could not delete file!");
}
else if(text.equals("LEAVE")){
System.out.println("Leaving ring");
break;
}//end switch
}//endwhiletrue
}//end try
catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//Do something
}//end finally
}//end run
}, 1000, 1000);
}//end constructor
public static void main(String args[])
{
ChordUser chordUser=new ChordUser(34674);
}//endmain
}//endchorduser
ChordMessageInterface.java
import java.rmi.*;
import java.io.*;
/**************************************************************************
* File: ChordMessageInterface.java
***************************************************************************/
public interface ChordMessageInterface extends Remote
{
public ChordMessageInterface getPredecessor() throws RemoteException;
ChordMessageInterface locateSuccessor(int key) throws RemoteException;
ChordMessageInterface closestPrecedingNode(int key) throws RemoteException;
public void joinRing(String Ip, int port, int key) throws RemoteException;
public void notify(ChordMessageInterface j) throws RemoteException;
public boolean isAlive() throws RemoteException;
public int getId() throws RemoteException;
public void put(int guid, byte[] data) throws IOException, RemoteException;
public byte[] get(int id) throws IOException, RemoteException;
public void delete(int id) throws IOException, RemoteException;
}
Chord.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
import java.util.*;
import java.io.*;
/**************************************************************************
* File: Chord.java
***************************************************************************/
public class Chord extends java.rmi.server.UnicastRemoteObject implements ChordMessageInterface
{
public static final int M = 2;
Registry registry; // rmi registry for lookup the remote objects.
ChordMessageInterface successor;
ChordMessageInterface predecessor;
ChordMessageInterface[] finger;
int nextFinger;
int i; // GUID
public ChordMessageInterface rmiChord(String ip, int port)
{
ChordMessageInterface chord = null;
try{
Registry registry = LocateRegistry.getRegistry(ip, port);
chord = (ChordMessageInterface)(registry.lookup("Chord"));
return chord;
} catch (RemoteException e) {
e.printStackTrace();
} catch(NotBoundException e){
e.printStackTrace();
}
return null;
}
public Boolean isKeyInSemiCloseInterval(int key, int key1, int key2)
{
if (key1 < key2)
return (key > key1 && key <= key2);
else
return (key > key2 || key <= key1);
}
public Boolean isKeyInOpenInterval(int key, int key1, int key2)
{
if (key1 < key2)
return (key > key1 && key < key2);
else
return (key > key2 || key < key1);
}
public void put(int guid, byte[] data) throws RemoteException {
}
public byte[] get(int guid) throws RemoteException {
return null;
}
public void delete(int guid) throws RemoteException {
}
public int getId() throws RemoteException {
return i;
}
public boolean isAlive() throws RemoteException {
return true;
}
public ChordMessageInterface getPredecessor() throws RemoteException {
return predecessor;
}
public ChordMessageInterface locateSuccessor(int key) throws RemoteException {
if (key == i)
throw new IllegalArgumentException("Key must be distinct that " + i);
if (successor.getId() != i)
{
if (isKeyInSemiCloseInterval(key, i, successor.getId()))
return successor;
ChordMessageInterface j = closestPrecedingNode(key);
if (j == null)
return null;
return j.locateSuccessor(key);
}
return successor;
}
public ChordMessageInterface closestPrecedingNode(int key) throws RemoteException {
int count = M-1;
if (key == i) throw new IllegalArgumentException("Key must be distinct that " + i);
for (count = M-1; count >= 0; count--) {
if (finger[count] != null && isKeyInSemiCloseInterval(finger[count].getId(), i, key))
return finger[count];
}
return successor;
}
public void joinRing(String ip, int port, int key) throws RemoteException {
try{
System.out.println("Get Registry to joining ring");
Registry registry = LocateRegistry.getRegistry(ip, port);
ChordMessageInterface chord = (ChordMessageInterface)(registry.lookup("Chord"));
predecessor = null;
System.out.println("Locating successor to joining ring" + port);
successor = chord.locateSuccessor(key);
if (successor != null)
System.out.println("Successor " + successor.getId());
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
public void stabilize() {
try {
ChordMessageInterface x = successor.getPredecessor();
if (x != null && x.getId() != i &&
(isKeyInOpenInterval(x.getId(), i, successor.getId()) || i == successor.getId()))
{
successor = x;
}
if (successor.getId() != getId())
{
successor.notify(this);
}
} catch(RemoteException e) {
e.printStackTrace();
} catch(NullPointerException e) {
e.printStackTrace();
}
}
public void notify(ChordMessageInterface j) throws RemoteException {
if (predecessor == null || (predecessor != null && isKeyInOpenInterval(j.getId(), predecessor.getId(), i)))
// transfer keys in the range [j,i) to j;
predecessor = j;
}
public void fixFingers() {
if (finger[nextFinger] != null)
nextFinger = nextFinger + 1;
if (nextFinger >= M)
nextFinger = 0;
try {
finger[nextFinger] = locateSuccessor((i + (1 << nextFinger)));
} catch(RemoteException e){
e.printStackTrace();
}
}
public void checkPredecessor() {
try {
if (predecessor != null) {
if (!predecessor.isAlive())
predecessor = null;
}
} catch(RemoteException e) {
predecessor = null;
}
}
public Chord(final int port) throws RemoteException {
int j;
finger = new ChordMessageInterface[M];
for (j=0;j<M; j++){
finger[j] = null;
}
i = port;
predecessor = null;
successor = this;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
stabilize();
fixFingers();
checkPredecessor();
}
}, 500, 500);
try{
// create the registry and bind the name and object.
System.out.println("Starting RMI at port="+port);
registry = LocateRegistry.createRegistry( port );
registry.rebind("Chord", this);
}
catch(RemoteException e){
throw e;
}
}
void Print()
{
try {
if (successor != null)
System.out.println("successor "+ successor.getId());
if (predecessor != null)
System.out.println("predecessor "+ predecessor.getId());
}
catch(RemoteException e){
System.out.println("Cannot retrive id");
}
}
}
Starting RMI at port=34674
Enter your command
1) JOIN
2) PRINT
3) WRITE
4) READ
5) DELETE
6) LEAVE
JOIN 1234
Get Registry to joining ring
Locating successor to joining ring34674
Successor 34674
Error joining the ring!
Error joining the ring!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.