-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from hoaikhanhnguyen/dev
Dev
- Loading branch information
Showing
41 changed files
with
2,383 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
![gui](examplechat.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/main/java/com/example/cs4b_project/ChatRoom/ChatClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.example.cs4b_project.ChatRoom; | ||
|
||
import com.example.cs4b_project.ClientHandler; | ||
import javafx.application.Platform; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.layout.VBox; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
|
||
|
||
public class ChatClient { | ||
private Socket socket; | ||
public BufferedReader bufferedReader; | ||
public BufferedWriter bufferedWriter; | ||
private String userName; | ||
private static String trueUser; | ||
private String internalName; | ||
private static String senderNum; | ||
|
||
private boolean closing = false; | ||
|
||
|
||
public ChatClient(Socket socket, String userName) { | ||
try { | ||
this.socket = socket; | ||
this.bufferedWriter = new BufferedWriter(new OutputStreamWriter((socket.getOutputStream()))); | ||
this.bufferedReader = new BufferedReader((new InputStreamReader(socket.getInputStream()))); | ||
this.userName = userName; | ||
} catch (IOException e) { | ||
closeEverything(); | ||
} | ||
} | ||
|
||
//this takes the username from the client handler in order to close it using the close everything method at the bottom | ||
public static void setSender(String userT){ | ||
trueUser = userT; | ||
senderNum = trueUser.substring(trueUser.length() - 1); //retrieves the client# for printing to UI | ||
System.out.println(trueUser); | ||
} | ||
|
||
public void sendMessage(String Message) { | ||
try { | ||
//bufferedWriter.write(userName); | ||
//bufferedWriter.newLine(); | ||
//bufferedWriter.flush(); | ||
|
||
//Scanner scanner = new Scanner(System.in); | ||
String messageToSend = Message; | ||
bufferedWriter.write(messageToSend); | ||
bufferedWriter.newLine(); | ||
bufferedWriter.flush(); | ||
} catch(IOException e) { | ||
closeEverything(); | ||
} | ||
} | ||
|
||
private void setInternalName(String internalName) { | ||
System.out.println("INTERNAL NAME SET TO: " + internalName); | ||
this.internalName = internalName; | ||
} | ||
public void listenForMessage(Socket socket, VBox box) { | ||
//create new thread to listen to messages | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
String msgFromGroupChat; | ||
|
||
while (socket.isConnected() && !closing) { | ||
System.out.println("listening..."); | ||
try { | ||
msgFromGroupChat = bufferedReader.readLine(); //possible threadlock | ||
|
||
// Synchronize username from ClientHandler | ||
// so the ChatClient can close it. | ||
if (msgFromGroupChat.indexOf("SERVERNAME:") == 0) { | ||
setInternalName(msgFromGroupChat.substring(11)); | ||
continue; | ||
} | ||
|
||
// Update on-screen chat | ||
final String finalMsgFromGroupChat = msgFromGroupChat; | ||
Platform.runLater(() -> { //runs back on main thread instead of "worker" thread | ||
Label newLabel = new Label(finalMsgFromGroupChat); | ||
newLabel.setWrapText(true); | ||
box.getChildren().add(newLabel); //makes object to display in gui | ||
}); | ||
} catch (IOException e) { | ||
//e.printStackTrace(); | ||
closeEverything(); | ||
break; | ||
} | ||
} | ||
|
||
System.out.println("Listener Thread on " + userName + " closed."); | ||
|
||
} | ||
}).start(); | ||
} | ||
//------------------------------------------------------ | ||
//FOR SOME REASON THE PROGRAM CRASHES (PERHAPS DUE TO THREADLOCKING) WHEN CLOSING BUFFERED READER | ||
//------------------------------------------------------ | ||
public synchronized void closeEverything() { | ||
if (closing) return; | ||
System.out.println("START CLOSING ON " + userName); | ||
|
||
closing = true; | ||
try { | ||
System.out.println("Attempting to close socket..."); | ||
if (socket != null) { | ||
socket.close(); | ||
} | ||
System.out.println("socket closed!"); | ||
|
||
System.out.println("Attempting to close bufferedReader..."); | ||
if (bufferedReader != null) { | ||
bufferedReader.close(); | ||
} | ||
System.out.println("bufferedReader closed!"); | ||
|
||
System.out.println("Attempting to close bufferedWriter..."); | ||
if (bufferedWriter != null) { | ||
bufferedWriter.close(); | ||
} | ||
System.out.println("bufferedWriter closed!"); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
System.out.println("closing " + internalName); //closing everything here | ||
|
||
ClientHandler.removeClientHandler(internalName); | ||
System.out.println("FINISHED CLOSING ON " + userName); | ||
} | ||
|
||
|
||
|
||
// public static void main(String[] args) throws IOException { | ||
// | ||
// Scanner scanner = new Scanner(System.in); | ||
// System.out.println("Enter your username for the group chat: "); | ||
// String username = scanner.nextLine(); | ||
// Socket socket = new Socket("localhost", 1234); | ||
// ChatClient client = new ChatClient(socket, username); | ||
// | ||
// // Note: these are on separate threads | ||
// client.listenForMessage(); | ||
// client.sendMessage(); | ||
// } | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/example/cs4b_project/ChatRoom/ChatServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//package com.example.cs4b_project.ChatRoom; | ||
// | ||
//import java.io.IOException; | ||
//import java.net.ServerSocket; | ||
//import java.net.Socket; | ||
//public class ChatServer { | ||
// | ||
// final private ServerSocket serverSocket; | ||
// | ||
// public ChatServer(ServerSocket serverSocket) { | ||
// this.serverSocket = serverSocket; | ||
// } | ||
// | ||
// public void startServer() { | ||
// try { | ||
// while (!serverSocket.isClosed()) { | ||
// Socket socket = serverSocket.accept(); | ||
// System.out.println("A new client has connected."); | ||
// | ||
// // separate handler method to a class that implements runnable interface. | ||
// // runnable interface runs each instance of a class on a separate thread | ||
// ClientHandler clientHandler = new ClientHandler(socket, "server"); | ||
// | ||
// Thread thread = new Thread(clientHandler); | ||
// thread.start(); | ||
// } | ||
// } catch (IOException e) { | ||
// closeServerSocket(); | ||
// } | ||
// } | ||
// | ||
// // separate error handler class to avoid nested try catch blocks | ||
// public void closeServerSocket() { | ||
// try { | ||
// if (serverSocket != null) | ||
// serverSocket.close(); | ||
// } catch (IOException e){ | ||
// throw new RuntimeException(e); // instead of e.printStackTrace() | ||
// } | ||
// } | ||
// | ||
// public static void main(String[] args) throws IOException { | ||
// ServerSocket serverSocket = new ServerSocket (1234); | ||
// ChatServer server = new ChatServer(serverSocket); | ||
// server.startServer(); | ||
// } | ||
//} |
Oops, something went wrong.