Code Java de la Procuration pour le système de quotation (Proxy + Architecture Client-Serveur)

Sur le serveur (fichier QuotationServer ie. QuotationReelle):

/*
 * Copyright (c) 2019
 *  ENSICAEN, École publique d'ingénieurs et centres de recherche, Caen, FRANCE.
 *  https://www.ensicaen.fr
 *
 *  This file is licenced under the MIT License.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package fr.ensicaen.gl2a.td.quotation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

interface IQuotation {
    String getGoldPrice();
    String getSilverPrice();
    String getDollarToRupee();
}
public class QuotationServer implements IQuotation {
    static final int PORT = 9999;
    public String getGoldPrice() {
        return "100"; // This value should come from a DB typically
    }
    public String getSilverPrice() {
        return "5"; // This value should come from a DB typically
    }
    public String getDollarToRupee() {
        return "50"; // This value should come from a DB typically
    }
    public static void main( String[] args ) {
        ServerSocket serverSocket;
        try {
            serverSocket = new ServerSocket(PORT);
            for (int i = 0; i < 3; i++) {
                System.out.println("Waiting .....");
                Socket socket = serverSocket.accept();
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String inputText = reader.readLine();
                IQuotation realSubject = new QuotationServer();
                String returnValue = "";
                switch (inputText) {
                    case "g":
                        returnValue = realSubject.getGoldPrice();
                        break;
                    case "s":
                        returnValue = realSubject.getSilverPrice();
                        break;
                    case "d":
                        returnValue = realSubject.getDollarToRupee();
                        break;
                }
                PrintWriter writer = new PrintWriter(socket.getOutputStream());
                writer.println(returnValue);
                writer.flush();
                reader.close();
                writer.close();
                socket.close();
                System.out.println("Response Sent ....." + returnValue);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Sur le client (fichier QuotationClient.java ie. QuotationProxy) :

/*
 * Copyright (c) 2019
 *  ENSICAEN, École publique d'ingénieurs et centres de recherche, Caen, FRANCE.
 *  https://www.ensicaen.fr
 *
 *  This file is licenced under the MIT License.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package fr.ensicaen.gl2a.td.quotation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/*
 * The important thing to note here is that the Proxy exposes the same interface as the real subject
 * and encapsulate the communication.
 */
public class QuotationClient implements IQuotation {
    public String getGoldPrice() {
        return getResponseFromServer("g");
    }
    public String getSilverPrice() {
        return getResponseFromServer("s");
    }
    public String getDollarToRupee() {
        return getResponseFromServer("d");
    }
    public static void main( String[] args ) {
        IQuotation proxy = new QuotationClient();
        System.out.println("Gold Price: " + proxy.getGoldPrice());
        System.out.println("Silver Price: " + proxy.getSilverPrice());
        System.out.println("Dollar to Ruppe Conversion: " + proxy.getDollarToRupee());
    }
    private String getResponseFromServer( String input ) {
        String result = "";
        try {
            Socket socket = new Socket("127.0.0.1", QuotationServer.PORT);
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            writer.println(input);
            writer.flush();
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            result = reader.readLine();
            reader.close();
            writer.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
Modifié le: Monday 28 October 2019, 10:12