Advertisement
MayurTolani

Bully Algorithm simulation fully encapsulated

Apr 24th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class BullyAlgorithm
  5. {
  6.     boolean process[];
  7.     int coordinator,n;
  8.     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  9.     void ip()
  10.     {
  11.         System.out.print("Enter number of Processes ");
  12.         try
  13.         {
  14.             n=Integer.parseInt(br.readLine());
  15.             process=new boolean[n];
  16.             for(int i=0;i<n;i++)
  17.                 process[i]=true;
  18.         }
  19.         catch(IOException ie)
  20.         {
  21.             System.out.println("Error");
  22.         }
  23.         coordinator=n;
  24.     }
  25.     void election()
  26.     {
  27.         int comm=0,max=0;
  28.         try
  29.         {  
  30.             comm=Integer.parseInt(br.readLine());
  31.             if(!process[comm-1])
  32.                 System.out.println("Dead Process");
  33.             else
  34.             {
  35.                 for(int i=comm;i<=n;i++)
  36.                 System.out.println("Request Message to Process "+i);
  37.                 for(int i=comm;i<=n;i++)
  38.                 {
  39.                     if(process[i-1])
  40.                     {
  41. `                           if(max<i)
  42.                         max=i;
  43.                         System.out.println("Reply Message to Process "+i);         
  44.                     }
  45.                 }
  46.                 System.out.println("The Co-ordinator is "+max);
  47.                 coordinator=max;
  48.             }
  49.         }
  50.         catch(IOException ie)
  51.         {
  52.             System.out.println("Error");
  53.         }
  54.     }
  55.     void menu()
  56.     {
  57.         int choice=0,id=0;
  58.         do
  59.         {
  60.             System.out.println("Enter");
  61.             System.out.println("1. Crash");
  62.             System.out.println("2. Recover");
  63.             System.out.println("3. Check Coordinator");
  64.             System.out.println("4. Increase Number of Processes");
  65.             System.out.println("0. Exit");
  66.             try
  67.             {  
  68.                 choice=Integer.parseInt(br.readLine());
  69.             }
  70.             catch(IOException ie)
  71.             {
  72.                 System.out.println("Error");
  73.             }
  74.             switch(choice)
  75.             {
  76.                 case 1:
  77.                 {
  78.                     try
  79.                     {
  80.                         id=Integer.parseInt(br.readLine());
  81.                     }
  82.                     catch(IOException ie)
  83.                     {
  84.                         System.out.println("Error");
  85.                     }
  86.                     if(process[id-1])
  87.                     {
  88.                         process[id-1]=false;
  89.                         System.out.println("Process Crashed ");
  90.                         if(id==coordinator)
  91.                             election();
  92.                     }
  93.                     else
  94.                     {
  95.                         System.out.println("Process Already Dead");
  96.                     }          
  97.                     break;
  98.                 }
  99.                 case 2:
  100.                 {
  101.                     try
  102.                     {
  103.                         id=Integer.parseInt(br.readLine());
  104.                     }
  105.                     catch(IOException ie)
  106.                     {
  107.                         System.out.println("Error");
  108.                     }
  109.                     if(!process[id-1])
  110.                     {
  111.                         process[id-1]=true;
  112.                         System.out.println("Process Recovered ");
  113.                         if(id>coordinator)
  114.                         {
  115.                             System.out.println("Process "+id+" Bullies Process "+ coordinator);
  116.                             coordinator=id;
  117.                         }
  118.                     }
  119.                     else
  120.                     {
  121.                         System.out.println("Process Already Alive");
  122.                     }          
  123.                     break;
  124.                 }
  125.                 case 3:
  126.                 {
  127.                     System.out.println("The Co-ordinator is "+coordinator);
  128.                     break;
  129.                 }
  130.                 case 4:
  131.                 {
  132.                     ip();
  133.                     break;
  134.                 }
  135.                 case 0:
  136.                 {
  137.                     break;
  138.                 }
  139.                 default:
  140.                 {
  141.                     System.out.println("Invalid Choice");
  142.                     break;
  143.                 }
  144.             }
  145.         }while(choice!=0);         
  146.     }
  147.     public static void main(String args[])
  148.     {
  149.         BullyAlgorithm b=new BullyAlgorithm();
  150.         b.ip();
  151.         b.menu();
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement