package city.smug.projectmercury.messaging;

import java.util.ArrayList;

import city.smug.projectmercury.sms.SmsHandler;

public class Messenger {
    protected ArrayList<Group> groups = new ArrayList<>();

    protected static Messenger instance = new Messenger();
    public static Messenger getInstance() {
        return instance;
    }

    protected Messenger() {}

    public void send(Message message) {
        if (ConnectionStatus.getInstance().isConnected()) {
            // Send over TCP
        }
        else {
            SmsHandler.getInstance().send(message);
        }
    }

    public void receive(Message message) {
        for (Group group : groups) {
            if (group.getId() == message.getTo().getId()) {
                group.getQueue().insert(message);
                break;
            }
        }
    }
}

@@ -54,24 +54,247 @@
 ava.
-util.ArrayList;%0A
+io.BufferedReader;%0Aimport java.io.DataInputStream;%0Aimport java.io.DataOutputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.net.Socket;%0Aimport java.util.ArrayList;%0Aimport java.util.Calendar;%0A%0Aimport city.smug.projectmercury.api.Rest;
 %0Aimp
@@ -422,16 +422,81 @@
 ist%3C%3E();
+%0A    protected NetworkRunnable netRunner = new NetworkRunnable();
 %0A%0A    pr
@@ -642,152 +642,1513 @@
 ted 
-Messenger() %7B%7D%0A%0A    public void send(Message message) %7B%0A        if (ConnectionStatus.getInstance().isConnected()) %7B%0A            // Send over TCP
+class NetworkRunnable implements Runnable %7B%0A        Socket socket = null;%0A%0A        public void send(Message message) %7B%0A            if (socket == null)%0A                return;%0A%0A            try %7B%0A                DataOutputStream stream = new DataOutputStream(socket.getOutputStream());%0A                stream.writeLong(-1L);%0A                stream.writeLong(message.getFrom().getId());%0A                stream.writeLong(message.getTo().getId());%0A                stream.writeUTF(message.getContent());%0A                stream.flush();%0A            %7D%0A            catch (Exception ignored) %7B%7D%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            try %7B%0A                Socket socket = new Socket(%22pdc-amd01.poly.edu%22, 8080);%0A                DataInputStream stream = new DataInputStream(socket.getInputStream());%0A%0A                while (true) %7B%0A                    receive(new Message(stream.readLong(), Rest.getUser(stream.readLong()), Rest.getGroup(stream.readLong()),%0A                            Calendar.getInstance().getTime(), stream.readUTF()));%0A                %7D%0A            %7D%0A            catch (Exception ignored) %7B%7D%0A        %7D%0A    %7D%0A%0A    protected Messenger() %7B%0A        new Thread(netRunner).start();%0A    %7D%0A%0A    public void send(Message message) %7B%0A        if (ConnectionStatus.getInstance().isConnected()) %7B%0A            try %7B%0A                netRunner.send(message);%0A            %7D%0A            catch (Exception ignored) %7B%0A                SmsHandler.getInstance().send(message);%0A            %7D
 %0A   

Send and receive TCP messages