Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a simplified Chord network following the directions below. The Chord n

ID: 3832047 • Letter: I

Question

Implement a simplified Chord network following the directions below. The Chord network will be m 8-bit identifiers (IDs). Thus, node identifier keys and data identifier keys in this network will map to an integer in [0, 2Am 11 Data keys [0, 1, 255] (K0, K1, K255) Node keys [0, 1, 255] (NO, N1, N255) The Chord network will have 25 nodes. In the DATA section below, 25 IP addresses are provided that can be used as input to the hash routine, h0, also shown below, to derive the node keys. Do not use any other IP addresses except the ones given in the data section. Use the following as a hash function: public int h(String s) long int len s length() for (int i 0; i len; i++) h 31 h s.charAt(i); return Math abs (h 256); In case of overflow and negative numbers Construct your Chord network with the 25 nodes. Each node in your Chord network will be an instance of the following class: public class Chord-node private static int m 8; Chord-node successor; Node's successor Chord-node predecessor; Node's predecessor Chord-node finger-table; Finger table of at most m entries int node-index; Integer index of this node [0 255] String node-name; Name of node, e.g., N20 or N190

Explanation / Answer

Chord_Node:

package Chord_Node;

import java.net.MalformedURLException;
import java.util.Set;
import java.util.Map;
import Application.FIDEntry;
import Utilities.networking.ProxyBinderInt;
import Application.Node;
import Application.NodeImpl;
import DHash.IdKey;
import DHash.Key;
import Exceptions.AlreadyConnectedException;
import Exceptions.IDNotFoundException;
import Utilities.Log;
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Chord_Node implements Remote
{

   public static void create(Node node) throws AlreadyConnectedException, IDNotFoundException
   {
       try
       {
           if (node.isConnected())
           {
               throw new AlreadyConnectedException("Node Already connected");
           }
           else if (node.getLocalID() == null)
           {
               throw new IDNotFoundException("Unable to create ring, IDKey not defined");
           }

           node.setConnected(true);
           node.addSuccessor(node.getLocalID(), 0);
           node.setPredecessor(null);
       }
       catch (RemoteException e)
       {
           Log.addMessage("A problem occurred when creating the ring. Please try again later!", Log.ERROR);
       }
   }

  
   public static void notifyNode(Node self, Node possiblePredecessor) throws RemoteException
   {
      
       if (self.getPredecessor() == null || Key.isBetweenNotify(possiblePredecessor.getLocalID(), self.getPredecessor(), self.getLocalID())) // updates the local variable of current Node
       {
           self.setPredecessor(possiblePredecessor.getLocalID());
       }
   }

   public static void join(Node self, Node other) throws RemoteException, MalformedURLException,NullPointerException
   {
       System.out.println("chord node join");

       self.setPredecessor(null);

       IdKey successorID = null;

       try
       {
           successorID = other.find_successor_ID(self.getLocalID());
       }
       catch (RemoteException ex)
       {
           Log.addMessage("join-Rexc[0]: A problem occurred while attempting to find my successor. Try connecting later", Log.ERROR);
       }
       self.addSuccessor(successorID, 0);
       if (self.getFingerSize() > 1)
       {
           self.clearFingers();
       }
       self.setFinger(successorID, 0);

       try
       {
          
           Node n = (Node) Naming.lookup("/" + other.getLocalID().getIP() + ":1099/" + String.valueOf(successorID.getPID()));
           Chord_Node.notifyNode(self, n);
       }
       catch (NotBoundException ex)
       {
           Log.addMessage("Node's successor is not bound or was unbound." , Log.ERROR);
       }
   }

   public static Node find_successor(Node node, Key id) throws RemoteException, MalformedURLException ,NullPointerException
   {
       long startTime = System.currentTimeMillis();

       System.out.println("chord node find_successor");
       Node tempNode = find_predecessor(node, id);
       System.out.println("Predecessor is : " + tempNode.getLocalID().hashKeytoHexString());

       IdKey successorsID = tempNode.getImmediateSuccessor();
       java.rmi.registry.Registry remote = java.rmi.registry.LocateRegistry.getRegistry("rmi:/" + node.getLocalID().getIP());
       try
       {
           node.addTime(System.currentTimeMillis()-startTime);
           return (Node) Naming.lookup("/" + successorsID.getIP() + ":1099/" + String.valueOf(successorsID.getPID()));
       }
       catch (RemoteException ex)
       {
           Log.addMessage("find_successor-Rexc[0]: Remote Exception from Find Successor", Log.ERROR);
       }
       catch (NotBoundException ex)
       {
           Log.addMessage("find_successor-NBExc[0]: A problem occurred when returning the successor for: " + id.toString() + " " + successorsID.toString() + " is not bound or was unbound."
           + "Try connecting later", Log.ERROR);
       }

  
       return null;
   }

  
   public static Node find_predecessor(Node node, Key id) throws RemoteException, MalformedURLException
   {

       Node successor = null;
       Node startNode = node;

       Registry remote = LocateRegistry.getRegistry("rmi:/" + node.getLocalID().getIP() + ":1099/");
       System.out.println(node.getLocalID().getIP());
       try
       {
           IdKey succID = node.getImmediateSuccessor();
           try
           {
              
               successor = (Node) Naming.lookup("/" + succID.getIP() + ":1099/" +String.valueOf(succID.getPID()));
           }
           catch (MalformedURLException ex)
           {
               Log.addMessage("MalformedURLException", Log.ERROR);
           }
       }
       catch (RemoteException ex)
       {
           Log.addMessage("find_predecessor-RExc[0]:A problem occurred while attempting to find the appropriate predecessor. It will be corrected in the next repetition of the stabilize.", Log.ERROR);
           throw new NullPointerException();
          
       }
       catch (NotBoundException ex)
       {
           Log.addMessage("find_predecessor-NBexc[0]:A problem occurred when finding the appropriate predecessor for: " + id.toString() + " " + "The successor of " + node.getLocalID().toString() + " is not bound or was unbound. "
           + "Try connecting later", Log.ERROR);
           throw new NullPointerException();
       }

      
       Node checkNode = null;

      
       int hops = 0;

       try
       {
          
           while (!Key.isBetweenSuccessor(id, node.getLocalID(), node.getImmediateSuccessor()))
           {
               if (!(checkNode == null))
               {
                   if (checkNode.getLocalID().equals(node.getLocalID()))
                   {
                       break;
                   }
               }
               hops++;
               checkNode = node;
              

               node = (Node) closest_preceding_finger(node, id);
              

               try
               {
                   IdKey key = node.getImmediateSuccessor();

                   successor = (Node) Naming.lookup("/" + key.getIP() + ":1099/" + String.valueOf(node.getImmediateSuccessor().getPID()));
               }
               catch (RemoteException ex)
               {
                   Log.addMessage("find_predecessor-RExc[1]: A problem occurred while attempting to find the appropriate successor of predecessor. It will be corrected in the next repetition of the stabilize.", Log.ERROR);

               }
               catch (NotBoundException ex)
               {
                   Log.addMessage("find_predecessor-NBExc[1]: A problem occurred when finding the appropriate predecessor for: " + id.toString() +
                   " " + "The successor of " + node.getLocalID().toString() + " is not bound or was unbound. "
                   + "Try connecting later", Log.ERROR);
               }
           }
       }
       catch (RemoteException rexc)
       {
           Log.addMessage("find_predecessor-RExc[2]: A problem occurred while attempting to find the appropriate successor of predecessor. It will be corrected in the next repetition of the stabilize.", Log.ERROR);
          
           throw new NullPointerException();
       }
       startNode.addHops(hops);
       return node;
   }

  
   public static Node closest_preceding_finger(Node node, Key id) throws RemoteException
   {
       IdKey finger;

      
       if (node.getFingerSize() == 0)
       {
           return node;
       }
       for (int i = (node.getFingerSize() - 1); i >= 0; i--)
       {
           finger = node.getFinger(i);

           if (finger == null)
           {
               continue;
           }

          
           if (Key.isBetween(finger, node.getLocalID(), id))
           {
               try
               {
                   try
                   {
                       return (Node) Naming.lookup("/" + finger.getIP() + ":1099/" + String.valueOf(finger.getPID()));
                   }
                   catch (MalformedURLException ex)
                   {
                       Log.addMessage("MalformedURLException", Log.ERROR);
                   }
                  
               }
               catch (RemoteException ex)
               {
                   Log.addMessage("closest_preceding_finger:Rexc[0]: A problem occurred while attempting to find the closest preceding finger of node: "
                   + node.getLocalID().toString() + "and id :" + id.toString() + " It will be corrected in on of the next repetitions of the fix fingers.", Log.ERROR);
               }
               catch (NotBoundException ex)
               {
                   Log.addMessage("closest_preceding_finger-NBExc[0]: A problem occurred while attempting to find the closest preceding finger. The finger: is not bound or was unbound. Try connecting later.", Log.ERROR);
               }
           }

       }
       return node;
   }

  
   public static void distribute_keys(Node node) throws RemoteException
   {
       NodeImpl thisNode = (NodeImpl) node;
       IdKey predecessor = thisNode.getPredecessor();
       IdKey successor = thisNode.getImmediateSuccessor();

       byte[] step = toByteArray(1);

       Node predecessorNode = null, successorNode = null;

       try
       {
           try
           {
               predecessorNode = (Node) Naming.lookup("/" + predecessor.getIP() + ":1099/" + String.valueOf(predecessor.getPID()));
           }
           catch (MalformedURLException ex)
           {
               Log.addMessage("Malformed URL in distribute_keys, while looking up for predecessor", Log.ERROR);
           }
       }
       catch (ConnectException ce)
       {
           System.out.println("Connect Exception in distribute_keys, while looking up for predecessor");
           Log.addMessage("Could not connect to predecessor in distribute_keys", Log.ERROR);
       }
       catch (NotBoundException ex)
       {
           System.out.println("A problem occurred in distribute_keys. Predecessor is not bound or was unbound");
           Log.addMessage("A problem occurred in distribute_keys. Predecessor is not bound or was unbound", Log.ERROR);
       }
       catch (AccessException ex)
       {
           System.out.println("Access Exception in distribute_keys, while looking up for predecessor");
           Log.addMessage("Could not access the predecessor in distribute_keys.", Log.ERROR);
       }

       try
       {
           try
           {
               successorNode = (Node) Naming.lookup("/" + successor.getIP() + ":1099/" +String.valueOf(successor.getPID()));
           }
           catch (MalformedURLException ex)
           {
               Log.addMessage("Malformed URL in distribute_keys, while looking up for successor", Log.ERROR);
           }
       }
       catch (ConnectException ce)
       {
           System.out.println("Connect Exception in distribute_keys, while looking up for successor");
           Log.addMessage("Could not connect to successor in distribute_keys", Log.ERROR);
       }
       catch (NotBoundException ex)
       {
           System.out.println("A problem occurred in distribute_keys. Successor is not bound or was unbound");
           Log.addMessage("A problem occurred in distribute_keys. Successor is not bound or was unbound", Log.ERROR);
       }
       catch (AccessException ex)
       {
           System.out.println("Access Exception in distribute_keys, while looking up for successor");
           Log.addMessage("Could not access the successor in distribute_keys.", Log.ERROR);
       }

      
       Map<IdKey, Set<FIDEntry>> entries = node.getAllEntries();

       for (IdKey counter : entries.keySet())
       {
           successorNode.addEntries(counter, entries.get(counter));
       }

       successorNode.setPredecessor(predecessor);
       predecessorNode.addSuccessor(successor, 0);
       predecessorNode.removeSuccessor(1);

   }

  
   public static byte[] toByteArray(int number)
   {
       Integer num = (Integer) number;
       byte[] tempArray = new byte[1];
       tempArray[0] = num.byteValue();
       return tempArray;
   }

   public static void disconnect(Node node) throws RemoteException
   {
       Node predecessor = null, successor = null;
       try
       {
           try
           {
               predecessor = (Node) Naming.lookup("/" + node.getPredecessor().getIP() + ":1099/" + String.valueOf(node.getPredecessor().getPID()));
           }
           catch (MalformedURLException ex)
           {
               Log.addMessage("Malformed URL in disconnect, while looking up for predecessor.", Log.ERROR);
           }
       }
       catch (ConnectException ce)
       {
           System.out.println("Connect Exception in disconnect, while looking up for predecessor");
           Log.addMessage("Could not connect to predecessor in disconnect", Log.ERROR);
       }
       catch (NotBoundException ex)
       {
           System.out.println("A problem occurred in disconnect. Predecessor is not bound or was unbound");
           Log.addMessage("A problem occurred in disconnect. Predecessor is not bound or was unbound", Log.ERROR);
       }
       catch (AccessException ex)
       {
           System.out.println("Access Exception in disconnect, while looking up for predecessor");
           Log.addMessage("Could not access the predecessor in disconnect.", Log.ERROR);
       }

       try
       {
           try
           {
               successor = (Node) Naming.lookup("/" + node.getImmediateSuccessor().getIP() + ":1099/" + String.valueOf(node.getImmediateSuccessor().getPID()));
           }
           catch (MalformedURLException ex)
           {
               Log.addMessage("Malformed URL in disconnect, while looking up for successor", Log.ERROR);
           }
       }
       catch (ConnectException ce)
       {
           System.out.println("Connect Exception in disconnect, while looking up for successor");
           Log.addMessage("Could not connect to successor in disconnect", Log.ERROR);
       }
       catch (NotBoundException ex)
       {
           System.out.println("A problem occurred in disconnect. Successor is not bound or was unbound");
           Log.addMessage("A problem occurred in disconnect. Successor is not bound or was unbound", Log.ERROR);
       }
       catch (AccessException ex)
       {
           System.out.println("Access Exception in disconnect, while looking up for successor");
           Log.addMessage("Could not access the successor in disconnect.", Log.ERROR);
       }

       successor.setPredecessor(predecessor.getLocalID());
       predecessor.removeSuccessor(0);
       predecessor.addSuccessor(successor.getLocalID(), 0);

       System.out.println("Exiting node " + node.getLocalID().hashKeytoHexString());
       System.out.println("New successor of " + predecessor.getLocalID().hashKeytoHexString() + " is " + predecessor.getImmediateSuccessor().hashKeytoHexString());
       System.out.println("New predecessor of " + successor.getLocalID().hashKeytoHexString() + " is " + successor.getPredecessor().hashKeytoHexString());

       predecessor.removeSuccessor(1);

       Chord_Node.distribute_keys(node);

      
       try
       {
           Thread.sleep(5000);
       }
       catch (InterruptedException ex)
       {
           Logger.getLogger(Chord_Node.class.getName()).log(Level.SEVERE, null, ex);
       }
      
       try
       {
           Registry local = LocateRegistry.getRegistry();
           local.unbind(node.getLocalID().getPID() +"");
       }
       catch (NotBoundException ex)
       {
           System.out.println("Node is not bound or was unbound. Message from the beyond...");
           Log.addMessage("Node is not bound or was unbound. Message from the beyond...", Log.ERROR);

       }
       catch (AccessException ex)
       {
           System.out.println("Node cannot access. Node's a slave");
           Log.addMessage("Node cannot access. Node's a slave", Log.ERROR);
       }
       finally
       {
           System.out.println("D.O.A.");
           System.exit(0);
       }
   }
}

finger_table:

package Application;

import DHash.IdKey;

public class FingerTable implements Comparable<FingerTable>
{

   private IdKey idKey;
   private int lastElement;
   private int firstElement;

   public FingerTable(IdKey idKey , int firstElement , int lastElement)
   {
       this.idKey = idKey;
       this.lastElement = lastElement;
       this.firstElement = firstElement;
   }


   public IdKey getKey()
   {
       return idKey;
   }

   public int getLastElement()
   {
       return lastElement;
   }

   public void setLastElement(int element)
   {
       lastElement = element;
   }

   public int getFirstElement()
   {
       return this.firstElement;
   }

   public void setFirstElement(int element)
   {
       firstElement = element;
   }

   public boolean contains(IdKey idKey)
   {
       return this.idKey.equals(idKey);
   }


   public int compareTo(FingerTable o)
   {
       FingerTable obj = (FingerTable)o;

       if (lastElement > obj.getLastElement())
           return 1;
       else if (lastElement < obj.getLastElement())
           return -1;
       else
       {
           return this.getKey().getHashKey().compareTo(o.getKey().getHashKey());
       }
   }
}

Node:

package Application;

import DHash.IdKey;
import DHash.FileNameKey;
import Exceptions.AlreadyConnectedException;
import Exceptions.IDNotFoundException;
import java.util.Map;
import java.rmi.*;
import java.util.Set;
import java.util.List;
import DHash.Key;

public interface Node extends Remote
{

   public void create() throws AlreadyConnectedException, IDNotFoundException, RemoteException;

   public void notifyNode(Node possiblePredecessor) throws RemoteException;

   public void join(Node connectedNode) throws RemoteException;

   public Node find_successor(Key id) throws RemoteException;

   public IdKey find_successor_ID(Key id) throws RemoteException;

   public void disconnect() throws RemoteException;

   public IdKey getLocalID() throws RemoteException;

   java.rmi.registry.Registry getRMIHandle() throws RemoteException;

   public boolean isConnected() throws RemoteException;

   public void addSuccessor(IdKey id, int index) throws RemoteException;

   public IdKey getSuccessor(IdKey idKey) throws RemoteException;

   public void addEntries(IdKey idKey, Set<FIDEntry> entries) throws RemoteException;

   public boolean isAlive() throws RemoteException;

   public IdKey getPredecessor() throws RemoteException;

   public IdKey getImmediateSuccessor() throws RemoteException;

   public void setPredecessor(IdKey idKey) throws RemoteException;

   public IdKey[] getFingers() throws RemoteException;

   public IdKey getFinger(int index) throws RemoteException;

   public void setFinger(IdKey key, int index) throws RemoteException;

   public Map<IdKey, Set<FIDEntry>> giveEntries(IdKey predecessorID) throws RemoteException;

   public void setEntries(Map<IdKey, Set<FIDEntry>> mEntries) throws RemoteException;

   public void removeSuccessor(int index) throws RemoteException;

   public List<IdKey> copySuccessorsList() throws RemoteException;

   public void setSuccessorsList(List<IdKey> newList) throws RemoteException;

   public void checkIntegrityOfSucList() throws RemoteException;

   public IdKey getSucList(int index) throws RemoteException;

   public void restartThreads() throws RemoteException;

   public List<IdKey> getWholeSucList() throws RemoteException;

   public Map<IdKey, Set<FIDEntry>> getAllEntries() throws RemoteException;

   public void setConnected(boolean connected) throws RemoteException;

   public int getFingerSize() throws RemoteException;

   public int getSucListSize() throws RemoteException;

   public void addEntry(IdKey idKey, FIDEntry entry) throws RemoteException;

   public void removeFinger(IdKey key) throws RemoteException;

   public void removeFinger(int index) throws RemoteException;

   public List<FingerEntry> getFingerEntries() throws RemoteException;

   public boolean existsInEntries(FIDEntry fid, IdKey id) throws RemoteException;

   public void removeEntrySet(IdKey id) throws RemoteException;

   public FIDEntry getEntry(IdKey owner, FileNameKey fkey) throws RemoteException;

   public FIDEntry getEntry(FileNameKey fkey) throws RemoteException;

   public void clearSuccessorList() throws RemoteException;

   public List<IdKey> getFingerKeys() throws RemoteException;

   public String[] getWholeLog() throws RemoteException;

   public void clearFingers() throws RemoteException;

   public int getNumberOfEntries() throws RemoteException;

   public int getNumberOfFiles() throws RemoteException;

   public NodeProperties getProperties() throws RemoteException;

   public void getNodeStatistics() throws RemoteException;

   public void getRingStatistics() throws RemoteException;

   public long getTime() throws RemoteException;

   public int getHops() throws RemoteException;

   public int getNumSearches() throws RemoteException;

   public void addTime(long time) throws RemoteException;

   public void addHops(int hops) throws RemoteException;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote