Question
How to join the ring in java ? Only edit chorduser.java for case 1 I need to join the ring. import java.rmi.*; import java.rmi.registry.*; import
How to join the ring in java ? Only edit chorduser.java for case 1 I need to join the ring.
import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; import java.net.*; import java.util.*; import java.io.*; 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 { return predecessor;
} public void joinRing(String ip, int port) throws RemoteException { } public void stabilize() { } public void notify(ChordMessageInterface j) throws RemoteException { } public void fixFingers() {
} public void checkPredecessor() { } public Chord(final int port) throws RemoteException { int j; finger = new ChordMessageInterface[M]; for (j=0;j 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(text.equals("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!"); } 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; } }//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.io.*; 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started