import java.util.ArrayList;

import javax.swing.JOptionPane;

   class TallyProcessor {
      private static java.util.Random rn = new java.util.Random();
      private static int Id = 0;
      public int id = Id++;
      public TallyProcessor next;
      
      
      public void add( TallyProcessor nextP ) {
         if (next != null) next.add( nextP );
         else next = nextP;
      }

      public int handle(int number)
      {
    	  int i;
    	  
    	  //prime check
    	  if(isPrime(number))
    		  {
    		  
    		  
    		 
	    		   ServerFace srv = new CNetServer(1998);
	    		  
	    	//	  System.out.println("Here!!!!");
	    		  
	    		  i = (int)srv.mashNum(number);
	    		//  System.out.println("Here!!!!2");
	    		  JOptionPane.showMessageDialog(null, "Original Number = " + number + ", Function value = " + i+" remote'");
	    		 
	    		  return 0;
    		  }
    	  else
    	  {
    		  if(number%2 != 0 && id == 1)
    		  {
    			  ServerFace srv = new ServerProxy();
    			  i = (int)srv.mashNum(number);
    			  JOptionPane.showMessageDialog(null, "Original Number = " + number + ", Function value = " + i+" "+srv.handle("a"));
    			  return 0;
    		  }
    		  else if(id<2)
    		  {  next.handle(number);
    		  return 0;}
    		  else
    		  {
    			  
    			  ServerFace srv = new ServerProxy6();
    			  i = (int)srv.mashNum(number);
    			  JOptionPane.showMessageDialog(null, "Original Number = " + number + ", Function value = " + i+" local");
    			  return 0;
    			  
    		  }
    		  
    		  
    	  }
    	  
      }
      

   static public TallyProcessor setUpChain(int num) {
      TallyProcessor root = new TallyProcessor();
      for (int i=0; i < num-1; i++)
    	  	root.add( new TallyProcessor() );
      return root;
   }
   
   boolean isPrime(int n) {
	    //check if n is a multiple of 2
	    if (n%2==0) return false;
	    //if not, then just check the odds
	    for(int i=3;i*i<=n;i+=2) {
	        if(n%i==0)
	            return false;
	    }
	    return true;
	}
   
   

}