dimanche 22 mars 2015

IF conditions are being skipped

The code basically implements Bully Election algorithm. The code runs fine, but only on the first go. On the second go, it skips the "if" statements in "notice" function. I know the code is long, but I don't think you would understand if I post only the "if" conditions.



import java.util.Scanner;


public class ElectionBully {



static int nop, iap, cood;
//static Scanner pri = new Scanner(System.in);
public static void main(String pribhat[]) throws InterruptedException{
Scanner priMain = new Scanner(System.in);
System.out.println("Enter number of processes: ");
nop = priMain.nextInt();
cood = nop;
System.out.println("Process "+cood+" is the co-ordinator");
menu();
}

public static void menu() throws InterruptedException{
Scanner priMenu = new Scanner(System.in);

System.out.println("Menu:");
System.out.println("1. Inactivate a Process. ");
System.out.println("2. Activate a Process. ");
System.out.println("3. Exit. ");
int choice, noti;
choice = priMenu.nextInt();

switch(choice){

case 1: System.out.println("Enter the process to be deactivated: ");
choice = priMenu.nextInt();
System.out.println("Process "+choice+" has been successfully deactivated!");
iap = choice;
if(choice == cood){
System.out.println("Enter the process which detects co-ordinator failure: ");
noti = priMenu.nextInt();
runBullyRun(noti, 1);
}
else
System.out.println("No change in co-ordinator!");
break;

case 2: System.out.println("Enter the process to be activated: ");
choice = priMenu.nextInt();
System.out.println("Process "+choice+" has been successfully activated!");

if(iap == choice)
iap=0;

if(choice > cood) {
System.out.println("Enter the process which detects co-ordinator failure: ");
noti = priMenu.nextInt();
runBullyRun(noti, 2);
}

break;

case 3: System.out.println("Buh-Bye! Have a Good day!");
break;

default: System.out.println("WRONG CHOiCE! Let's try this again...");
menu();
break;
}

}

static void runBullyRun(int node, int mode) throws InterruptedException{
Scanner priRBR = new Scanner(System.in);

if(mode == 1){
System.out.println("Process "+node+" detected failure of Co-ordinator"+cood);
System.out.println("Process "+node+" starts sending election notice...");
}


if(mode == 2 && node>cood){
System.out.println("Since Process "+node+" has greate priority than current co-ordinator...");
System.out.println("Process "+node+" starts sending election notice...");
}


notice(node);
System.out.println("Do you want to continue? (y/n) ");
String lul;
lul = priRBR.nextLine();
if(lul.equals("y"))
menu();
else
System.out.println("Buh-Bye! Have a good day!");
}


public static void notice(int sender) throws InterruptedException{

int i, j=0;
if(nop == iap){
for(i = sender; i <= nop; i++){

if(isActive(i)){
for(j = i; j <= nop; j++){

System.out.println("Process "+i+" sends election notice to Process "+j+"...");

if(isActive(j)){
//Thread.sleep(1000);
System.out.println("Response from Process "+j);

}

}

if((i+1) != iap){
//Thread.sleep(1000);
System.out.println("Process "+i+" quits since higher priority processes exist");
}

}

else{
//Thread.sleep(5000);
System.out.println("No response from Process "+(j-1));
cood = (i-1);
System.out.println("Process "+(i-1)+" is the new co-ordinator");
}

}


}

}

public static void response(int receiver){

System.out.println("Response from "+receiver);


}
static boolean isActive(int process){
boolean result = false;

if(process != iap)
result = true;

return result;
}


}


How the code works?



  1. Starts with asking you number of processes

  2. You select the process to be inactivated/activated

  3. Election notice and response is sent/received depending on choice from user.

  4. Highest active process is made co-ordinator.

  5. If the user chooses to continue, he can choose to activate the previously inactive process. THiS will trigger step 3 and so on.


The issue I'm facing is that Steps 1-4 work fine.. But on step 5 when user chooses to continue, Steps 1-2 are working.. but the if conditions in function "notice" are skipped.


What I've tried so far:



  • Executing the if statements in runBullyRun function

  • Individual Scanners

  • Googling

  • StackOverflow


P.S. I'm not so great at Java, if this is a duplicate then kindly direct me to the original so I can fix this. I would delete this. Thank You.


Aucun commentaire:

Enregistrer un commentaire