Exemplo n.º 1
0
 public void run() {
   boolean flag = true;
   while (flag) {
     if (!isAlive(neighbours.getPreIp())) // Predecessor
     updatePredecessor();
     if (!isAlive(neighbours.getPrePreIp())) // PrePredecessor
     updatePrePredecessor();
     if (!isAlive(neighbours.getSucIp())) // Successor
     updateSuccessor();
   }
 }
Exemplo n.º 2
0
  private void updatePrePredecessor() {

    // Initialise socket and read/write
    try {
      socket = new Socket(neighbours.getPreIp(), 4017); // speaking to predecessor
      sender = new PrintWriter(socket.getOutputStream(), true);
      receiver = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Ask for his predecessor
    sender.println(protocol.getPredecessorQuery());

    // Listen for his predecessor
    String response = null;
    try {
      response = receiver.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // If correct reply, update
    if (response.contains(protocol.getPredecessorResponse())) {
      int whiteSpace1 = response.indexOf(" ");
      String usefulResponse = response.substring(whiteSpace1 + 1, response.length());
      int whiteSpace2 = usefulResponse.indexOf(" ");
      neighbours.setPrePreId(usefulResponse.substring(0, whiteSpace2));
      neighbours.setPrePreIp(usefulResponse.substring(whiteSpace2 + 1, usefulResponse.length()));
    }

    // Close socket and read/write
    try {
      receiver.close();
      sender.close();
      socket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 3
0
  private void updateSuccessor() {
    // Initialise socket and read/write
    try {
      socket = new Socket(neighbours.getSucSucIp(), 4017); // speaking to suc-successor
      sender = new PrintWriter(socket.getOutputStream(), true);
      receiver = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Ask for his successor
    sender.println(protocol.getSuccessorQuery());

    // Listen for his successor
    String response = null;
    try {
      response = receiver.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // If correct reply, update
    if (response.contains(protocol.getSuccessorResponse())) {
      // Assign suc-successor to successor
      neighbours.setSucId(neighbours.getSucSucId());
      neighbours.setSucIp(neighbours.getSucSucIp());

      // Assign sucsuccessor's sucessor to the sucsuccessor
      int firstWhiteSpace = response.indexOf(" ");
      String usefulResponse = response.substring(firstWhiteSpace + 1, response.length());
      int secondWhiteSpace = usefulResponse.indexOf(" ");
      neighbours.setSucSucId(usefulResponse.substring(0, secondWhiteSpace));
      neighbours.setSucSucIp(
          usefulResponse.substring(secondWhiteSpace + 1, usefulResponse.length()));
    }

    // Close socket and read/write
    try {
      receiver.close();
      sender.close();
      socket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }