/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.polymtl.inf4410.tp2.client;
import java.rmi.ConnectException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import ca.polymtl.inf4410.tp2.shared.CalculServerInterface;
import ca.polymtl.inf4410.tp2.shared.OverloadedServerException;

/**
 * CalculServer implement the server that will procceed calculation
 * 
 * @author robinroyer
 */
public class CalculServer implements CalculServerInterface{

    int capacity;
    int confidence;
    
    
    /**
    * Main to run the server.
    * 
    * @param args
    */
    public static void main(String[] args) {
        //TODO: add args to constructor
        CalculServer server = new CalculServer();
        server.run();
    }
    
    /**
    * Main method to run the server.
    */
    private void run() {
        if (System.getSecurityManager() == null) {
                System.setSecurityManager(new SecurityManager());
        }

        try {
                CalculServerInterface stub = (CalculServerInterface) UnicastRemoteObject.exportObject(this, 0);
                Registry registry = LocateRegistry.getRegistry();
                registry.rebind("server", stub);
                System.out.println("Server ready.");
        } catch (ConnectException e) {
                System.err.println("Impossible de se connecter au registre RMI. Est-ce que rmiregistry est lance ?");
                System.err.println();
                System.err.println("Erreur: " + e.getMessage());
        } catch (Exception e) {
                System.err.println("Erreur: " + e.getMessage());
        }
    }
    
    
    /**
     * Constructor with args
     */
    public CalculServer(int port){        
    }  
    
    /**
     * Deafault Constructor
     */
    public CalculServer(){        
    }    

    @Override
    public int calculate(String[] operations) throws RemoteException, OverloadedServerException {
        // TODO: implement calcul from operations
        return -1;
    }
    
}

@@ -403,16 +403,41 @@
 eObject;
+%0Aimport java.util.Random;
 %0A%0Aimport
@@ -726,25 +726,188 @@
     
-int capacity;%0A   
+%0A    %0A    /**%0A     * Maximum capacity of the instance of CalculServer%0A     */%0A    private int capacity;%0A    %0A    /**%0A     * Percentage of trusted return message%0A     */%0A    private
  int
@@ -1131,16 +1131,20 @@
 lServer(
+args
 );%0A     
@@ -2084,17 +2084,69 @@
 tor 
-with args
+from Args%0A     * %0A     * @param Args Args where we should fin
 %0A   
@@ -2142,32 +2142,37 @@
 uld fin%0A     */%0A
+    %0A
     public Calcu
@@ -2183,26 +2183,293 @@
 ver(
-int port)%7B        
+String %5B%5D Args)%7B%0A        %0A    %7D%0A    %0A    /**%0A     * Constructor with confidence and capacity%0A     * @param confidence%0A     * @param capacity%0A     */%0A    private CalculServer( int confidence, int capacity)%7B        %0A        this.confidence = confidence;%0A        this.capacity = capacity;
 %0A   
@@ -2495,17 +2495,16 @@
     * De
-a
 fault Co
@@ -2759,10 +2759,698 @@
  %7D%0A    %0A
+    %0A    %0A    /**%0A     * Test if the server is overloaded, in order to accept or refuse the opertions%0A     * %0A     * @param operationNumber  Number of operations sent to the CalculServer%0A     * @return True if the operations is not be accepted%0A     */%0A    private boolean isOverloaded(int operationNumber)%7B                %0A        //Algorythm to calculate refusingRate:  T = (U-Q)/(4*Q) * 100                 %0A        double refusingRate = (operationNumber - capacity) / (4 * capacity) * 100;        %0A        // Using a random generator for refusing%0A        Random rand = new Random(System.currentTimeMillis());        %0A        return 100 * rand.nextDouble() %3E refusingRate;  %0A    %7D%0A    %0A
 %7D%0A

implement isOverloaded method to check CalculServer