Advertisement
Shailrshah

Berkeley Algorithm for Time Synchronization

Apr 24th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import java.util.Scanner;
  4. class Berkeley{
  5.     int time, avg = 0;
  6.     int[] delays;
  7.     Berkeley(String str){
  8.         String[] s = str.split(":");
  9.         time = 3600*Integer.parseInt(s[0]) + 60*Integer.parseInt(s[1]) + Integer.parseInt(s[2]);
  10.     }
  11.     void setDelays(String[] str){
  12.         delays = new int[str.length];
  13.         for(int i = 0; i < str.length; i++){
  14.             String[] s = str[i].split(":");
  15.             delays[i] = 3600*Integer.parseInt(s[0]) + 60*Integer.parseInt(s[1]) + Integer.parseInt(s[2])-time;
  16.             System.out.println("The delay of node "+i+" is "+getTime(delays[i], true));
  17.         }
  18.         System.out.println("These delays are sent to the master.");
  19.     }
  20.     void setAvg(){
  21.         int sum = 0, n = delays.length;
  22.         for(int i = 0; i < delays.length; i++)
  23.             if(Math.abs(delays[i])<(7200))sum += delays[i]; //if delay is more than 2 hours, ignore it
  24.             else{
  25.                 System.out.println("Delay of Node "+i+"is too large. So not counting it in calculating average delay.");
  26.                 n--;
  27.             }
  28.         avg =  Math.round(sum/(n+1));
  29.         System.out.println("The average delay is "+getTime(avg, true));
  30.     }
  31.     String getTime(int seconds, boolean eng){
  32.         int h=seconds/3600;
  33.         int m=(seconds-(h*3600))/60;
  34.         int s=(seconds-(h*3600))-(m*60);
  35.         if(eng)
  36.             if(seconds>=0)return  h+" hours "+m+" minutes "+s+" seconds";
  37.             else return "-("+Math.abs(h)+" hours "+ Math.abs(m)+ " minutes "+Math.abs(s)+"  seconds)";
  38.         else
  39.             if(seconds>=0) return String.format("%02d", h)+":"+String.format("%02d", m)+":"+String.format("%02d", s);
  40.             else return "-("+String.format("%02d", h)+":"+String.format("%02d", m)+":"+String.format("%02d", s)+")";
  41.     }
  42.     void calcCorr(){
  43.         System.out.println("The master adjusts its time by " +getTime(avg, true));
  44.         for(int i = 0; i < delays.length; i++)
  45.             System.out.println("Master suggests Node "+i+" to adjust its time by "+getTime(avg-delays[i], true));
  46.         System.out.println("All nodes, including the master show "+getTime(time+avg,false));
  47.     }
  48. }
  49. public class Main {
  50.     public static void main(String[] args) {
  51.         Scanner sc = new Scanner(System.in);
  52.         System.out.print("Enter the number of nodes: ");
  53.         int n = sc.nextInt();
  54.         System.out.println("Enter the times of the other nodes.");
  55.         String[]inp = new String[n];
  56.         for(int i = 0; i < n; i++) inp[i] = sc.next();
  57.         Berkeley b = new Berkeley(new SimpleDateFormat("hh:mm:ss").format(new Date()));
  58.         System.out.println("The master sends its time "+b.getTime(b.time, false)+" to all nodes.");
  59.         b.setDelays(inp);
  60.         b.setAvg();
  61.         b.calcCorr();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement