Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: theicfire/005-project-2
base: 8d99f40c03
...
head fork: theicfire/005-project-2
compare: faa1288c46
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 13 files changed
  • 0 commit comments
  • 1 contributor
View
22 src/Utils/Utils.java
@@ -18,26 +18,4 @@ public static Timestamp getCurrentTimestamp() {
Date date= new Date();
return new Timestamp(date.getTime());
}
-
- @Deprecated
- /**
- * Helper function to parse a message from a string. Was eventually just
- * essentially copy pasted into the code for Server/Client.
- * Kept just in case we want to do something with it in the future.
- * @param input
- * @return
- * @throws Exception
- */
- public static Message parseStringMessage(String input) throws Exception{
- if (ConnectionMessage.isConnectionMessage(input)){
- return ConnectionMessage.parseStringMessage(input);
- } else if (RequestMessage.isRequestMessage(input)){
- return RequestMessage.parseStringMessage(input);
- } else if (TextMessage.isTextMessage(input)){
- return TextMessage.parseStringMessage(input);
- } else if (TypingMessage.isTypingMessage(input)){
- return TypingMessage.parseStringMessage(input);
- }
- throw new Exception ("Utils.parseStringMessage: Invalid Message");
- }
}
View
19 src/client/ReceiveFromServerConnection.java
@@ -53,16 +53,14 @@ public void run() {
*/
private void handleConnection(Socket socket) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
try {
for (String line = in.readLine(); line != null; line = in.readLine()) {
- System.out.println("ReceiveFromServer line input: " + line);
+ System.out.println("ReceiveFromServer line input to " + username + ": " + line);
handleRequest(line);
}
} finally {
System.out.println("connection closed");
in.close();
- out.close();
}
}
@@ -77,9 +75,12 @@ private void handleRequest(String input) throws Exception {
ConnectionMessage message = ConnectionMessage.parseStringMessage(input);
System.out.println(message.getStringMessage());
Client.handleConnectionMessage(message);
- } else if (RequestMessage.isRequestMessage(input)){
- RequestMessage message = RequestMessage.parseStringMessage(input);
- Client.handleRequestMessage(message);
+ } else if (AddToGroupMessage.isAddToGroupMessage(input)){
+ AddToGroupMessage message = AddToGroupMessage.parseStringMessage(input);
+ Client.handleAddToGroupMessage(message);
+ } else if (NoticeMessage.isNoticeMessage(input)){
+ NoticeMessage message = NoticeMessage.parseStringMessage(input);
+ Client.handleNoticeMessage(message);
} else if (TextMessage.isTextMessage(input)){
TextMessage message = TextMessage.parseStringMessage(input);
Client.handleTextMessage(message);
@@ -92,11 +93,9 @@ private void handleRequest(String input) throws Exception {
} else if (input.equals("GOOD_LOGIN")) {
// call login
Client.login(username);
+ } else {
+ throw new Exception("Could not parse the sent message: " + input);
}
}
- public static void main(String[] args) {
- System.out.println("hello");
-// handleRequest("REQUEST chase");
- }
}
View
6 src/client/SendToServerConnection.java
@@ -74,9 +74,9 @@ private void handleConnection(Socket socket) throws IOException, InterruptedExce
}
/**
- * handler for client output
- * @param input
- * @return
+ * Sends the information to the server
+ * @param message what to send
+ * @param out what to write to to send the message
*/
private static void handleRequest(Message message, PrintWriter out) {
out.println(message.getStringMessage());
View
96 src/main/Client.java
@@ -42,12 +42,7 @@ public Client(String username, String host, String port) throws UnknownHostExcep
int portNum = Integer.parseInt(port);
this.username = username;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- // prompt the user to enter their name
- Socket socket;
- socket = new Socket(host, portNum);
- // probably won't use this variable; everything happens inside of
- // the thread
+ Socket socket = new Socket(host, portNum);
SendToServerConnection sender = new SendToServerConnection(socket, queue, username);
sender.start();
ReceiveFromServerConnection receiver = new ReceiveFromServerConnection(socket, username);
@@ -70,7 +65,6 @@ public static void handleConnectionMessage(ConnectionMessage message) {
} else if (message.type == ConnectionMessage.types.DISCONNECT) {
buddyList.buddyLogout(message.getFromUsername());
}
- updateOnline();
} catch (Exception e) {
e.printStackTrace();
}
@@ -80,31 +74,41 @@ public static void handleConnectionMessage(ConnectionMessage message) {
* Handles Request messages by calling acceptRequest. If the request is
* accepted, it loggedIn/loggedOut.
*/
- public static void handleRequestMessage(final RequestMessage message) {
+// public static void handleRequestMessage(final RequestMessage message) {
+// if (!chats.containsKey(message.getRoomID())){
+// if (message.type == RequestMessage.types.REQUEST) {
+// if (acceptRequest(message)) {
+// // ArrayBlockingQueue<Message> queue = new
+// // ArrayBlockingQueue<Message>(100);
+// // Open up a new chat window!
+// ConvoGUI convoGUI = new ConvoGUI(message.getToUsername(), message.getRoomID());
+// chats.put(message.getRoomID(),convoGUI);
+//// convoGUI.setVisible(true); // will be set to true when other person talks first
+// } else {
+// // send the requester that you have rejected them
+// // we are switching the order of from/to on purpose to send the message back
+// Client.getQueue().offer(
+// new RequestMessage(message.getToUsername(), message.getFromUsername(), message.getRoomID(),
+// RequestMessage.types.REJECT_REQUEST));
+// }
+// } else {
+// // you have requested someone to chat but they rejected you :(
+// // TODO
+// chats.get(message.getRoomID()).dispose();
+// }
+// }
+// }
+
+ public static void handleAddToGroupMessage(AddToGroupMessage message) {
if (!chats.containsKey(message.getRoomID())){
- if (message.type == RequestMessage.types.REQUEST) {
- if (acceptRequest(message)) {
- // ArrayBlockingQueue<Message> queue = new
- // ArrayBlockingQueue<Message>(100);
- // Open up a new chat window!
- ConvoGUI convoGUI = new ConvoGUI(message.getToUsername(), message.getRoomID());
- chats.put(message.getRoomID(),convoGUI);
-// convoGUI.setVisible(true); // will be set to true when other person talks first
- } else {
- // send the requester that you have rejected them
- // we are switching the order of from/to on purpose to send the message back
- Client.getQueue().offer(
- new RequestMessage(message.getToUsername(), message.getFromUsername(), message.getRoomID(),
- RequestMessage.types.REJECT_REQUEST));
- }
- } else {
- // you have requested someone to chat but they rejected you :(
- // TODO
- chats.get(message.getRoomID()).dispose();
- }
+ System.out.println("making new convo gui");
+ ConvoGUI convoGUI = new ConvoGUI(message.getToUsername(), message.getRoomID());
+ chats.put(message.getRoomID(),convoGUI);
+// convoGUI.setVisible(true);
+ } else {
+ System.out.println("This client already has the added room ID");
}
}
-
/*
* Handles Text messages by forwarding them to the proper ArrayBlockingQueue
* Throws an exception if you receive a message for which you do not
@@ -114,13 +118,22 @@ public static void handleTextMessage(TextMessage message) throws Exception {
try {
System.out.println("looking for : " + message.getRoomID());
System.out.println("found: " + chats.get(message.getRoomID()));
- chats.get(message.getRoomID()).handleTextMessage(message);
+ if (chats.get(message.getRoomID()) != null) {
+ chats.get(message.getRoomID()).handleTextMessage(message);
+ } else {
+ throw new RuntimeException("Received a text message but did not have the room for it");
+ }
+
} catch (NullPointerException e) {
throw new Exception(
"handleTextMessage: message received from person not connected to");
}
}
+ public static void handleNoticeMessage(NoticeMessage message) throws Exception {
+ handleTextMessage(new TextMessage(message.getFromUsername(), message.getRoomID(), message.getNotice()));
+ }
+
/*
* TODO - write shit
*/
@@ -135,9 +148,8 @@ public static void handleTypingMessage(TypingMessage message) throws Exception {
}
}
- private static void updateOnline() {
- // TODO Auto-generated method stub
- }
+
+
/*
* Called by handleRequestMessage, when a user requests to start a chat, and
@@ -146,14 +158,14 @@ private static void updateOnline() {
*
* @returns true if the user wishes to accept the chat, and false otherwise.
*/
- public static boolean acceptRequest(RequestMessage message) {
- // This makes a popup dialog; disable for now
- if (true) {
- return true;
- }
- DialogGUI dialog = new DialogGUI();
- return dialog.makeDialog();
- }
+// public static boolean acceptRequest(RequestMessage message) {
+// // This makes a popup dialog; disable for now
+// if (true) {
+// return true;
+// }
+// DialogGUI dialog = new DialogGUI();
+// return dialog.makeDialog();
+// }
/**
* Start a GUI chat client.
@@ -180,4 +192,6 @@ public void run() {
public static BuddyList getBuddyList() {
return buddyList;
}
+
+
}
View
1  src/main/Server.java
@@ -88,6 +88,7 @@ public static void sendMsgToClients(ToMessage msg) {
if(!msg.getFromUsername().equals(client)){
ArrayBlockingQueue<Message> queue = messages.get(client);
try {
+ System.out.println("Send message to: " + client);
queue.offer(msg);
} catch (Exception e) {
// queue is probably null
View
50 src/messages/AddToGroupMessage.java
@@ -0,0 +1,50 @@
+package messages;
+
+import java.sql.Timestamp;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class AddToGroupMessage extends ToMessage {
+ public static String patternStr = "ADD\\|([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)";
+
+ public AddToGroupMessage(String fromUsername, String toUsername, int roomID, Timestamp timestamp) {
+ super(fromUsername, toUsername, roomID, timestamp);
+ }
+
+ public AddToGroupMessage(String fromUsername, String toUsername, int roomID) {
+ super(fromUsername, toUsername, roomID);
+ }
+
+ public String getStringMessage() {
+ return "ADD" + super.getStringMessageSuffix();
+ }
+
+ public static AddToGroupMessage parseStringMessage(String input) throws Exception {
+ Pattern pattern = Pattern.compile(patternStr);
+ Matcher matcher = pattern.matcher(input);
+ if(matcher.matches()){
+ matcher.group();
+ return new AddToGroupMessage(matcher.group(1),
+ matcher.group(2),
+ Integer.parseInt(matcher.group(3)),
+ Timestamp.valueOf(matcher.group(4))
+ );
+ }
+ throw new Exception("Invalid AddToGroupMessage string: " + input);
+ }
+
+ public static boolean isAddToGroupMessage(String input){
+ Pattern pattern = Pattern.compile(patternStr);
+ Matcher matcher = pattern.matcher(input);
+ return matcher.matches();
+ }
+
+ @Override
+ public String toString() {
+ return "AddToGroupMessage [getRoomID()=" + getRoomID()
+ + ", getFromUsername()=" + getFromUsername()
+ + ", getTimestamp()=" + getTimestamp() + ", getClass()="
+ + getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ + super.toString() + "]";
+ }
+}
View
68 src/messages/NoticeMessage.java
@@ -0,0 +1,68 @@
+package messages;
+
+import java.sql.Timestamp;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class NoticeMessage extends ToMessage {
+ public static String patternStr = "NOTICE\\|([^\\|]*)\\|([^\\|]*)\\|([0-9]*)\\|([^\\|]*)\\|([^\\|]*)";
+ private String notice;
+
+ public NoticeMessage(String fromUsername, String toUsername, int roomID, String notice, Timestamp timestamp) {
+ super(fromUsername, toUsername, roomID, timestamp);
+ this.notice = notice;
+ }
+
+ public NoticeMessage(String fromUsername, String toUsername, int roomID, String notice) {
+ super(fromUsername, toUsername, roomID);
+ this.notice = notice;
+ }
+
+ public String getStringMessage() {
+ return "NOTICE" + super.getStringMessageSuffix() + "|" + notice;
+ }
+
+ public static NoticeMessage parseStringMessage(String input) throws Exception {
+ Pattern pattern = Pattern.compile(patternStr);
+ Matcher matcher = pattern.matcher(input);
+ System.out.println("compare input" + input);
+ if(matcher.matches()){
+ matcher.group();
+ System.out.println("parse int of " + matcher.group(3));
+ return new NoticeMessage(matcher.group(1),
+ matcher.group(2),
+ Integer.parseInt(matcher.group(3)),
+ matcher.group(5),
+ Timestamp.valueOf(matcher.group(4))
+ );
+ }
+ throw new Exception("Invalid NoticeMessage string: " + input);
+ }
+
+ public static boolean isNoticeMessage(String input){
+ Pattern pattern = Pattern.compile(patternStr);
+ Matcher matcher = pattern.matcher(input);
+ System.out.println("check" + input + "----" + patternStr);
+ System.out.println("true/false: " + matcher.matches());
+ return matcher.matches();
+ }
+
+ public String getNotice() {
+ return notice;
+ }
+
+ @Override
+ public String toString() {
+ return "NoticeMessage [getRoomID()=" + getRoomID()
+ + ", getFromUsername()=" + getFromUsername()
+ + ", getTimestamp()=" + getTimestamp() + ", getClass()="
+ + getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ + super.toString() + "]";
+ }
+
+ public static void main(String[] args) throws Exception {
+ System.out.println("ehy");
+ NoticeMessage n = new NoticeMessage("from", "to", 222, "my notice");
+ System.out.println(NoticeMessage.parseStringMessage(n.getStringMessage()).getStringMessage());
+ }
+}
View
128 src/messages/RequestMessage.java
@@ -1,64 +1,64 @@
-package messages;
-
-import java.sql.Timestamp;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public class RequestMessage extends ToMessage {
- public static String patternStr = "(REQUEST|REJECT_REQUEST)\\|([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)";
- public RequestMessage.types type;
- public enum types {
- REQUEST,
- REJECT_REQUEST
- }
- public RequestMessage(String fromUsername, String toUsername, int roomID, RequestMessage.types type,
- Timestamp timestamp) {
- super(fromUsername, toUsername, roomID, timestamp);
- this.type = type;
- }
- public RequestMessage(String fromUsername, String toUsername, int roomID, RequestMessage.types type) {
- super(fromUsername, toUsername, roomID);
- this.type = type;
- }
-
- public String getStringMessage() {
- return ((type == types.REQUEST) ? "REQUEST" : "REJECT") + super.getStringMessageSuffix();
- }
-
- public static RequestMessage parseStringMessage(String input) throws Exception {
- Pattern pattern = Pattern.compile(patternStr);
- Matcher matcher = pattern.matcher(input);
- if(matcher.matches()){
- matcher.group();
- return new RequestMessage(matcher.group(2),
- matcher.group(3),
- Integer.parseInt(matcher.group(4)),
- matcher.group(1).equals("REQUEST") ?
- RequestMessage.types.REQUEST : RequestMessage.types.REJECT_REQUEST,
- Timestamp.valueOf(matcher.group(5))
- );
- }
- throw new Exception("Invalid RequestMessage string: " + input);
- }
-
- public static boolean isRequestMessage(String input){
- Pattern pattern = Pattern.compile(patternStr);
- Matcher matcher = pattern.matcher(input);
- return matcher.matches();
- }
-
- @Override
- public String toString() {
- return "RequestMessage [getRoomID()=" + getRoomID()
- + ", getFromUsername()=" + getFromUsername()
- + ", getTimestamp()=" + getTimestamp() + ", getClass()="
- + getClass() + ", hashCode()=" + hashCode() + ", toString()="
- + super.toString() + "]";
- }
-
- public static void main(String[] args) throws Exception{
- RequestMessage requestMessage = new RequestMessage("from", "to", 0, RequestMessage.types.REQUEST);
- System.out.println(requestMessage.toString());
- System.out.println(parseStringMessage(requestMessage.getStringMessage()).toString());
- }
-}
+//package messages;
+//
+//import java.sql.Timestamp;
+//import java.util.regex.Matcher;
+//import java.util.regex.Pattern;
+//
+//public class RequestMessage extends ToMessage {
+// public static String patternStr = "(REQUEST|REJECT_REQUEST)\\|([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)";
+// public RequestMessage.types type;
+// public enum types {
+// REQUEST,
+// REJECT_REQUEST
+// }
+// public RequestMessage(String fromUsername, String toUsername, int roomID, RequestMessage.types type,
+// Timestamp timestamp) {
+// super(fromUsername, toUsername, roomID, timestamp);
+// this.type = type;
+// }
+// public RequestMessage(String fromUsername, String toUsername, int roomID, RequestMessage.types type) {
+// super(fromUsername, toUsername, roomID);
+// this.type = type;
+// }
+//
+// public String getStringMessage() {
+// return ((type == types.REQUEST) ? "REQUEST" : "REJECT") + super.getStringMessageSuffix();
+// }
+//
+// public static RequestMessage parseStringMessage(String input) throws Exception {
+// Pattern pattern = Pattern.compile(patternStr);
+// Matcher matcher = pattern.matcher(input);
+// if(matcher.matches()){
+// matcher.group();
+// return new RequestMessage(matcher.group(2),
+// matcher.group(3),
+// Integer.parseInt(matcher.group(4)),
+// matcher.group(1).equals("REQUEST") ?
+// RequestMessage.types.REQUEST : RequestMessage.types.REJECT_REQUEST,
+// Timestamp.valueOf(matcher.group(5))
+// );
+// }
+// throw new Exception("Invalid RequestMessage string: " + input);
+// }
+//
+// public static boolean isRequestMessage(String input){
+// Pattern pattern = Pattern.compile(patternStr);
+// Matcher matcher = pattern.matcher(input);
+// return matcher.matches();
+// }
+//
+// @Override
+// public String toString() {
+// return "RequestMessage [getRoomID()=" + getRoomID()
+// + ", getFromUsername()=" + getFromUsername()
+// + ", getTimestamp()=" + getTimestamp() + ", getClass()="
+// + getClass() + ", hashCode()=" + hashCode() + ", toString()="
+// + super.toString() + "]";
+// }
+//
+// public static void main(String[] args) throws Exception{
+// RequestMessage requestMessage = new RequestMessage("from", "to", 0, RequestMessage.types.REQUEST);
+// System.out.println(requestMessage.toString());
+// System.out.println(parseStringMessage(requestMessage.getStringMessage()).toString());
+// }
+//}
View
15 src/server/ReceiveFromClientConnection.java
@@ -97,11 +97,18 @@ private void handleRequest(String input) throws Exception {
else
System.out.println("Shouldn't reach here... textMessage, but no chatRoom");
return;
- } else if (RequestMessage.isRequestMessage(input)) {
- RequestMessage msg = RequestMessage.parseStringMessage(input);
+ } else if (AddToGroupMessage.isAddToGroupMessage(input)) {
+ AddToGroupMessage msg = AddToGroupMessage.parseStringMessage(input);
if(Server.getChatRooms().containsKey(msg.getRoomID())){
- Server.getChatRooms().get(msg.getRoomID()).add(msg.getToUsername());
- Server.sendMsgToClient(msg);
+ if (Server.getChatRooms().get(msg.getRoomID()).contains(msg.getToUsername())) {
+ // this person is already added
+ Server.sendMsgToClient(new NoticeMessage("server", msg.getFromUsername(), msg.getRoomID(), "Already added"));
+ } else {
+ Server.getChatRooms().get(msg.getRoomID()).add(msg.getToUsername());
+ Server.sendMsgToClient(msg);
+ Server.sendMsgToClients((new TextMessage("server", msg.getRoomID(),
+ msg.getToUsername() + " has been added by " + msg.getFromUsername())));
+ }
} else {
ArrayList<String> clients = new ArrayList<String>();
clients.add(msg.getFromUsername());
View
2  src/server/SendToClientConnection.java
@@ -56,8 +56,6 @@ public void run() {
*/
private void handleConnection(Socket socket) throws IOException, InterruptedException {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- out.println("Success! Start chatting.");
-
try {
for (Message message = queue.take(); message != null; message = queue.take()) {
if (isKilled) {
View
9 src/ui/BuddyList.java
@@ -10,7 +10,7 @@
import javax.swing.event.*;
import main.Client;
-import messages.RequestMessage;
+import messages.AddToGroupMessage;
public class BuddyList extends JFrame implements ListSelectionListener,
ActionListener {
@@ -136,8 +136,7 @@ public void buddyLogin(String fromUsername) {
online.add(fromUsername);
return;
}
- throw new RuntimeException(
- "buddyLogin: Logged in user that was already logged in.");
+ throw new RuntimeException("buddyLogin: Logged in user that was already logged in.");
}
public void buddyLogout(String fromUsername) {
@@ -174,9 +173,7 @@ public void request(String toUsername) {
convoGUI.setVisible(true);
Client.getChats().put(randomInt, convoGUI);
- Client.getQueue().offer(
- new RequestMessage(username, toUsername, randomInt,
- RequestMessage.types.REQUEST));
+ Client.getQueue().offer(new AddToGroupMessage(username, toUsername, randomInt));
// now send a request message to the other user
}
}
View
18 src/ui/ConvoGUI.java
@@ -20,7 +20,7 @@
import javax.swing.text.Document;
import main.Client;
-import messages.RequestMessage;
+import messages.AddToGroupMessage;
import messages.TextMessage;
import messages.TypingMessage;
@@ -79,6 +79,7 @@ public void actionPerformed(ActionEvent a) {
setPreferredSize(new Dimension(450, 450));
createAndShowGUI();
+ System.out.println("done making convo gui");
}
@@ -134,25 +135,20 @@ private void enterTextFromField(){
String text = newText.getText();
if(text.equals(""))
return;
- String toAdd;
if(text.startsWith("/add ")){
String toUsername = text.substring(5);
if(Client.getBuddyList().getOnline().contains(toUsername)){
- Client.getQueue().offer(new RequestMessage(fromUsername, toUsername, roomID, RequestMessage.types.REQUEST));
- toAdd = fromUsername + ": I added " + toUsername + " to the chat!" + newline;
-
- //TODO - this is retarded
- Client.getQueue().offer(new TextMessage(fromUsername, roomID, "I added " + toUsername + " to the chat!"));
+ Client.getQueue().offer(new AddToGroupMessage(fromUsername, toUsername, roomID));
} else {
- toAdd = fromUsername + ": " + toUsername + " is not online!" + newline;
+ convo.append(fromUsername + ": " + toUsername + " is not online!" + newline);
}
} else {
- toAdd = fromUsername + ": "+ text + newline;
+ convo.append(fromUsername + ": "+ text + newline);
+ Client.getQueue().offer(new TextMessage(fromUsername, roomID, newText.getText()));
}
- convo.append(toAdd);
- Client.getQueue().offer(new TextMessage(fromUsername, roomID, newText.getText()));
+
newText.setText("");
scrollDown();
}
View
7 src/ui/LoginGUI.java
@@ -122,9 +122,10 @@ private void paintComponent(Graphics g, Image img){
private void login() {
String userInp = username.getText();
- String firstLetter = userInp.substring(0, 1);
- String end = userInp.substring(1);
- String newUsername = firstLetter.toUpperCase()+end;
+// String firstLetter = userInp.substring(0, 1);
+// String end = userInp.substring(1);
+// String newUsername = firstLetter.toUpperCase()+end;
+ String newUsername = userInp;
if (! VALID_USERNAME.matcher(newUsername).matches()) {
// bad login pattern
showErrorPopup("Please provide a username with only letters and numbers, between 3 and 12 characters.");

No commit comments for this range

Something went wrong with that request. Please try again.