Commits

Steven! Ragnarök committed 6220e9b

Phase one and Phase two nearly working.

Comments (0)

Files changed (5)

src/main/java/com/nuclearsandwich/messageme/App.java

 	public static void printCommands() {
 		System.out.println("Available commands are:");
 		printInboxUsage();
+		printSendUsage();
+		printExitUsage();
+		printConnectDisconnectUsage();
 	}
 
 	public static void loop() {
 			System.out.print(promptString);
 			String nextCommand = scanner.nextLine();
 			runCommand(nextCommand);
+			Thread.currentThread().yield();
 		}
 	}
 
 		} else if (command.equals("inbox")) {
 			inbox(argList);
 		} else if (command.equals("connect")) {
-			System.out.println("Pending");
+			connect(argList);
 		} else if (command.equals("disconnect")) {
-			System.out.println("Pending");
+			disconnect(argList);
 		}
 	}
 
 		User sender = User.by(args[0]);
 		User recipient = User.by(args[1]);
 		String body = args[2].replace("'", "");
-		recipient.getInbox().add(new Message(sender, recipient, body));
+		Message msg = new Message(sender, recipient, body);
+		recipient.getInbox().add(msg);
+		recipient.getPhone().newMessage(msg);
 	}
 
 	public static void inbox(String argList) {
 		User.by(argList).getInbox().display();
 	}
 
+	public static void connect(String argList) {
+		User.by(argList).connectPhone();
+	}
+
+	public static void disconnect(String argList) {
+		User.by(argList).disconnectPhone();
+	}
+
+
 	public static void printInboxUsage() {
 		System.out.println("inbox USER_NAME");
 	}
 
-	private static ArrayList<User> users;
+	public static void printSendUsage() {
+		System.out.println("send SENDER_NAME RECIPIENT_NAME MESSAGE");
+	}
+
+	public static void printExitUsage() {
+		System.out.println("exit");
+	}
+
+	public static void printConnectDisconnectUsage() {
+		System.out.println("connect USER_NAME");
+		System.out.println("disconnect USER_NAME");
+	}
 }
 

src/main/java/com/nuclearsandwich/messageme/Inbox.java

 
 	public void add(Message m) {
 		messages.push(m);
+		if (owner.phoneIsConnected()) {
+			owner.notify(m);
+		}
 	}
 
 	public void display() {
 		}
 	}
 
+	public void displayUnread() {
+		LinkedList<Message> unread = unreadMessages();
+		System.out.println(unread.size() +
+				" messages arrived since you last connected:");
+		for (Message m : unread) {
+			m.markRead();
+			m.display();
+		}
+	}
+
+	public LinkedList<Message> unreadMessages() {
+		LinkedList<Message> unread = new LinkedList<Message>();
+		for (Message m : messages) {
+			if (m.isUnread()) {
+				unread.push(m);
+			}
+		}
+
+		return unread;
+	}
+
 	private User owner;
 	private LinkedList<Message> messages;
 }

src/main/java/com/nuclearsandwich/messageme/Message.java

 		this.read = false;
 	}
 
+	public boolean isUnread() {
+		return !read;
+	}
+
 	public void display() {
 		System.out.println("	From: " + sender.toString() + " | Message: " + body);
 	}
 
+	public void displayNotification() {
+		System.out.println(" ** Notification: " + recipient +
+				" has a new message from " + sender);
+	}
+
 	public void markRead() {
 		read = true;
 	}

src/main/java/com/nuclearsandwich/messageme/Phone.java

+package com.nuclearsandwich.messageme;
+import java.util.LinkedList;
+
+public class Phone extends Thread {
+	public Phone(User owner) {
+		super("Phone of " + owner.toString());
+		this.connected = false;
+		this.owner = owner;
+		this.newMessages = new LinkedList<Message>();
+		this.start();
+	}
+
+	public boolean isConnected() {
+		return connected;
+	}
+
+	public void newMessage(Message msg) {
+		newMessages.push(msg);
+	}
+
+	public void connect() {
+		if (connected) {
+			return;
+		}
+
+		owner.getInbox().displayUnread();
+		connected = true;
+		newMessages.clear();
+	}
+
+	public void disconnect() {
+		connected = false;
+	}
+
+	public void run() {
+		while (true) {
+			if (connected) {
+				while (!newMessages.isEmpty()) {
+					newMessages.pop().displayNotification();
+					yield();
+				}
+			}
+			else {
+				yield();
+			}
+		}
+	}
+
+	private LinkedList<Message> newMessages;
+	private User owner;
+	private boolean connected;
+}
+

src/main/java/com/nuclearsandwich/messageme/User.java

 	public User(String name) {
 		this.name = name;
 		this.inbox = new Inbox(this);
+		this.phone = new Phone(this);
 		userList.add(this);
 	}
 
 		return inbox;
 	}
 
+	public Phone getPhone() {
+		return phone;
+	}
+
 	public String toString() {
 		return name;
 	}
 		return name;
 	}
 
+	public void notify(Message msg) {
+		phone.newMessage(msg);
+	}
+
+	public boolean phoneIsConnected() {
+		return phone.isConnected();
+	}
+
+	public void connectPhone() {
+		phone.connect();
+	}
+
+	public void disconnectPhone() {
+		phone.disconnect();
+	}
+
 	private String name;
 	private Inbox inbox;
+	private Phone phone;
 
-	public static User by(String name) {
+	public static final User by(String name) {
 		for (User u : userList) {
 			if (u.getName().equals(name)) {
 				return u;