Skip to content

File CentralizedClient.java

File List > centralized > CentralizedClient.java

Go to the documentation of this file

package skydata.centralized;
import java.io.*;
import java.net.*;
import java.util.HashSet;
import java.util.Set;

import skydata.internal.message.SKAID;

public class CentralizedClient {
    private static final String SERVER_ADDRESS = System.getenv("CENTRALIZED_ADDRESS"); // Server's IP
    private static final int SERVER_PORT = 9999; // Server's port

    Socket socket;


    public CentralizedClient() {
        try {
            socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addTuple(String addr, String mtp) {
        try {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.println("ADD " + addr + " " + mtp);

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String response = in.readLine();
            System.out.println("Answer : " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Set<SKAID> getTuples() {
        Set<SKAID> harbours = new HashSet<SKAID>();
        try {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.println("GET");

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line;
            while ((line = in.readLine()) != null && !line.equals("EOF")) {
                System.out.println(line);
                String[] splitted = line.split(" ");
                SKAID a = new SKAID(splitted[0], splitted[1]);
                harbours.add(a);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return harbours;
    }
}