package dk.itu.real.ooe.services;

public class Communication {
    private String message;
    private boolean messageSent = false;

    public synchronized String read() {
        //Ignore any previous message
        messageSent = false;
        // Wait until a message is sent
        while (!messageSent) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // Reset messageSent flag
        messageSent = false;
        // Return the message
        return message;
    }

    public synchronized void send(String msg) {
        // Set the message
        this.message = msg;
        // Notify waiting thread that message is sent
        messageSent = true;
        notify();
    }
}
