Skip to content

Commit

Permalink
Integrated OO techniques, greater readability
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePurpleJedi committed Feb 18, 2022
1 parent 5d3c7c4 commit ed277c5
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 306 deletions.
9 changes: 7 additions & 2 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions project-mercurys.iml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,21 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
47 changes: 30 additions & 17 deletions src/com/mercurys/TextClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,30 @@ public class TextClient {

private Socket socket;
private DataOutputStream outputStream;
private PrintWriter writer;
private Scanner scanner;

private TextClient(final String address, final int port) {
private TextClient(final String serverAddress, final int serverPort) {
try {
this.socket = new Socket(address, port);
System.out.println("Connected to server!" + this.socket.getRemoteSocketAddress());

this.outputStream = new DataOutputStream(this.socket.getOutputStream());

connectToServer(serverAddress, serverPort);
initialiseIO();
} catch (final IOException u) {
System.out.println("Woops!");
u.printStackTrace();
}
}

private void initialiseIO() throws IOException {
this.outputStream = new DataOutputStream(this.socket.getOutputStream());
this.writer = new PrintWriter(this.outputStream, true);
scanner = new Scanner(System.in);
}

private void connectToServer(String address, int port) throws IOException {
this.socket = new Socket(address, port);
System.out.println("Connected to server!" + this.socket.getRemoteSocketAddress());
}

public static void main(final String[] args) {
final TextClient client = new TextClient(getHostAddress(), 4444);
client.talk();
Expand All @@ -40,29 +50,32 @@ private static String getHostAddress() {

private void talk() {
try {
final ReadThread readThread = new ReadThread(this.socket,
"[MHost" + this.socket.getLocalAddress().toString() + "]");

readThread.start();
final ReadThread readThread = initialiseAndGetReadThread();
this.writeToServer();

this.closeConnection(readThread);
} catch (final IOException e) {
e.printStackTrace();
}
}

private ReadThread initialiseAndGetReadThread() throws IOException {
final ReadThread readThread = new ReadThread(this.socket,
"[MHost" + this.socket.getLocalAddress().toString() + "]");
readThread.start();
return readThread;
}

private void writeToServer() {
final Encryption encryption = new Encryption();
final PrintWriter writer = new PrintWriter(this.outputStream, true);
final Scanner scanner = new Scanner(System.in);
String outGoingLine = "";
encryptAndPrintMessagesToServer(encryption);
writer.println(encryption.encrypt("Connection closed by client."));
}

while (!outGoingLine.equals("-x-")) {
outGoingLine = scanner.nextLine();
private void encryptAndPrintMessagesToServer(Encryption encryption) {
String outGoingLine;
while (!(outGoingLine = scanner.nextLine()).equals("-x-")) {
writer.println(encryption.encrypt(outGoingLine));
}
writer.println(encryption.encrypt("Connection closed by client."));
}

private void closeConnection(final ReadThread readThread) throws IOException {
Expand Down
65 changes: 43 additions & 22 deletions src/com/mercurys/TextServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,84 @@

public class TextServer {

private ServerSocket serverSocket;
private Socket socket;
private Scanner scanner;
private DataOutputStream outputStream;
private PrintWriter writer;

private TextServer(final int port) {
try (final ServerSocket serverSocket = new ServerSocket()) {
String hostAddress = "192.168.0.151"; //replace with one's own LAN IP address
serverSocket.bind(new InetSocketAddress(hostAddress, port));
System.out.println("""
Server initialised.
Waiting for client...""");

this.socket = serverSocket.accept();
System.out.println("Client accepted!");
this.outputStream = new DataOutputStream(this.socket.getOutputStream());
try {
serverSocket = new ServerSocket();
initialiseServerSocket(port);
acceptClient();
initialiseInputOutputObjects();

} catch (final IOException e) {
System.out.println("Woops!");
e.printStackTrace();
}
}

private void initialiseInputOutputObjects() throws IOException {
this.outputStream = new DataOutputStream(this.socket.getOutputStream());
this.writer = new PrintWriter(outputStream, true);
this.scanner = new Scanner(System.in);
}

private void acceptClient() throws IOException {
this.socket = serverSocket.accept();
System.out.println("Client accepted!");
}

private void initialiseServerSocket(final int serverPort) throws IOException {
String hostAddress = "192.168.0.151"; //replace with one's own LAN IP address
serverSocket.bind(new InetSocketAddress(hostAddress, serverPort));
System.out.println("""
Server initialised.
Waiting for client...""");
}

public static void main(final String[] args) {
final TextServer textServer = new TextServer(4444);
textServer.talk();

System.out.println("Thank you for using Project Mercurys!");
}

private void talk() {
try {
final ReadThread readThread = new ReadThread(this.socket,
"[MClient1" + this.socket.getInetAddress().toString() + "]");

readThread.start();
final ReadThread readThread = initialiseAndGetReadThread();
this.writeToClient();

this.closeConnection(readThread);
} catch (final IOException e) {
e.printStackTrace();
}
}

private ReadThread initialiseAndGetReadThread() throws IOException {
final ReadThread readThread = new ReadThread(this.socket,
"[MClient1" + this.socket.getInetAddress().toString() + "]");

readThread.start();
return readThread;
}

private void writeToClient() {
final Encryption encryption = new Encryption();
final PrintWriter writer = new PrintWriter(this.outputStream, true);
final Scanner scanner = new Scanner(System.in);
String outGoingLine = scanner.nextLine();
encryptAndPrintMessagesToClient(encryption);
writer.println(encryption.encrypt("Connection closed by server."));
}

while (!outGoingLine.equals("-x-")) {
private void encryptAndPrintMessagesToClient(Encryption encryption) {
String outGoingLine;
while (!(outGoingLine = scanner.nextLine()).equals("-x-")) {
writer.println(encryption.encrypt(outGoingLine));
outGoingLine = scanner.nextLine();
}
writer.println(encryption.encrypt("Connection closed by server."));
}

private void closeConnection(final ReadThread readThread) throws IOException {
readThread.exitThread();
this.serverSocket.close();
this.socket.close();
this.outputStream.close();
System.out.println("Closing connection... Goodbye!");
Expand Down
Loading

0 comments on commit ed277c5

Please sign in to comment.