diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb
index 6b656c7..3a2de55 100644
--- a/.idea/sonarlint/issuestore/index.pb
+++ b/.idea/sonarlint/issuestore/index.pb
@@ -1,8 +1,4 @@
-D
-project-mercurys.iml,1/c/1c3eb49b6844e2134ee9434a8a0c6cd160905cd4
-W
-'src/com/mercurys/unfinished/ImagePixels,2/5/25ea4ac3533974b717c6c78a6993cdfb5ef3d110
T
$.idea/codeStyles/codeStyleConfig.xml,d/d/dd7b3350c29c49497756c7f32c69a3bed18b1cba
X
diff --git a/out/.gitignore b/out/.gitignore
deleted file mode 100644
index 757f2ed..0000000
--- a/out/.gitignore
+++ /dev/null
@@ -1,3 +0,0 @@
-# Default ignored files
-/shelf/
-/workspace.xml
\ No newline at end of file
diff --git a/project-mercurys.iml b/project-mercurys.iml
index 87da0ae..d6ce961 100644
--- a/project-mercurys.iml
+++ b/project-mercurys.iml
@@ -5,6 +5,14 @@
+
+
+
+
+
+
+
+
diff --git a/src/com/mercurys/threads/ReadMediaThread.java b/src/com/mercurys/threads/ReadMediaThread.java
index d471ad5..1e3d795 100644
--- a/src/com/mercurys/threads/ReadMediaThread.java
+++ b/src/com/mercurys/threads/ReadMediaThread.java
@@ -1,15 +1,11 @@
package com.mercurys.threads;
import com.mercurys.encryption.Decryption;
+import com.mercurys.unfinished.MImageHandler;
-import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
-import java.nio.ByteBuffer;
-import java.nio.file.*;
-import java.text.*;
-import java.util.Calendar;
public class ReadMediaThread extends Thread {
@@ -35,7 +31,6 @@ public void run() {
if (this.exit) {
this.interrupt();
}
-
this.readAndPrintMessages(reader);
this.exitThread();
@@ -54,49 +49,22 @@ private void readAndPrintMessages(final BufferedReader reader) throws IOExceptio
if (inBoundLine == null) {
break;
} else if (decryption.decrypt(inBoundLine).startsWith("/image")) {
- final BufferedImage image = this.getImageAsBufferedImage();
- this.downloadBufferedImage(image);
+ downloadIncomingImage();
} else if (decryption.getKeyFromMessage(inBoundLine.split(" +")).length() == 88) {
System.out.println(this.sentBy + ": " + this.decryption.decrypt(inBoundLine));
}
}
}
- private void downloadBufferedImage(final BufferedImage image) {
- if (image == null) {
- return;
- }
- final String s = System.getProperty("file.separator");
- final String downloadPath =
- MessageFormat.format("{0}{1}Pictures{1}Mercurys{1}{3}{1}ReceivedImage{2}.png",
- System.getProperty("user.home"), s, new SimpleDateFormat("HHmmss")
- .format(Calendar.getInstance().getTime()), new SimpleDateFormat("dd.MM.yyyy")
- .format(Calendar.getInstance().getTime()));
- try {
- Files.createDirectories(Paths.get(downloadPath.substring(0, downloadPath.lastIndexOf(s)) + s));
- ImageIO.write(image, "png", new File(downloadPath));
- } catch (final IOException e) {
- e.printStackTrace();
- }
- System.out.println("[M. Console]: The image file has been downloaded as " + downloadPath);
- }
-
- private BufferedImage getImageAsBufferedImage() {
+ private void downloadIncomingImage() {
+ MImageHandler imageHandler = new MImageHandler(inputStream);
System.out.println("[M. Console]: Attention! " +
"The user at the other end is attempting to send you an image file.");
System.out.println("[M. Console]: Reading image file...");
- try {
- final byte[] imgSize = new byte[4];
- this.inputStream.readFully(imgSize);
- final byte[] imgArr = new byte[ByteBuffer.wrap(imgSize).asIntBuffer().get()];
- this.inputStream.readFully(imgArr);
- return ImageIO.read(new ByteArrayInputStream(imgArr));
-
- } catch (final IOException e) {
- System.out.println("Exception Occurred!");
- e.printStackTrace();
- return null;
- }
+ final BufferedImage image = imageHandler.getImageAsBufferedImage();
+ imageHandler.downloadBufferedImage(image);
+ System.out.println("[M. Console]: The image file has been downloaded as "
+ + imageHandler.getImageDownloadPath());
}
public void exitThread() {
diff --git a/src/com/mercurys/unfinished/MImageHandler.java b/src/com/mercurys/unfinished/MImageHandler.java
new file mode 100644
index 0000000..c63d39f
--- /dev/null
+++ b/src/com/mercurys/unfinished/MImageHandler.java
@@ -0,0 +1,73 @@
+package com.mercurys.unfinished;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.nio.file.*;
+import java.text.*;
+import java.util.Calendar;
+
+public class MImageHandler {
+
+ DataOutputStream outputStream;
+ DataInputStream inputStream;
+ String imageDownloadPath;
+
+ public MImageHandler(DataOutputStream outputStream) {
+ this.outputStream = outputStream;
+ }
+
+ public MImageHandler(DataInputStream inputStream) {
+ this.inputStream = inputStream;
+ }
+
+ public void sendImageFileAsByteStream(final String imageFileName) throws IOException {
+ final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ final BufferedImage bufferedImage = ImageIO.read(new File(imageFileName));
+ ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
+
+ final byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
+
+ this.outputStream.write(size);
+ this.outputStream.write(byteArrayOutputStream.toByteArray());
+ }
+
+ public BufferedImage getImageAsBufferedImage() {
+ try {
+ final byte[] imgSize = new byte[4];
+ this.inputStream.readFully(imgSize);
+ final byte[] imgArr = new byte[ByteBuffer.wrap(imgSize).asIntBuffer().get()];
+ this.inputStream.readFully(imgArr);
+ return ImageIO.read(new ByteArrayInputStream(imgArr));
+
+ } catch (final IOException e) {
+ System.out.println("Exception Occurred!");
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public void downloadBufferedImage(final BufferedImage image) {
+ if (image == null) {
+ return;
+ }
+ final String s = System.getProperty("file.separator");
+ final String downloadPath =
+ MessageFormat.format("{0}{1}Pictures{1}Mercurys{1}{3}{1}ReceivedImage{2}.png",
+ System.getProperty("user.home"), s, new SimpleDateFormat("HHmmss")
+ .format(Calendar.getInstance().getTime()), new SimpleDateFormat("dd.MM.yyyy")
+ .format(Calendar.getInstance().getTime()));
+ try {
+ Files.createDirectories(Paths.get(downloadPath.substring(0, downloadPath.lastIndexOf(s)) + s));
+ ImageIO.write(image, "png", new File(downloadPath));
+ this.imageDownloadPath = downloadPath;
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public String getImageDownloadPath() {
+ return imageDownloadPath;
+ }
+}
diff --git a/src/com/mercurys/unfinished/MediaClient.java b/src/com/mercurys/unfinished/MediaClient.java
index 8579c79..3917412 100644
--- a/src/com/mercurys/unfinished/MediaClient.java
+++ b/src/com/mercurys/unfinished/MediaClient.java
@@ -37,24 +37,23 @@ private static String getHostAddress() {
final Scanner sc = new Scanner(System.in);
System.out.println("Enter server host address [IP] or press 1 for localhost:");
final String hostAddress = sc.next();
- return (hostAddress.equals("1"))? "localhost" : hostAddress;
+ return (hostAddress.equals("1"))? "192.168.0.151" : hostAddress;
}
private void talk() {
try {
- final ReadMediaThread readMediaThread = new ReadMediaThread(this.socket,
- "client1" + this.socket.getLocalAddress().toString());
+ final ReadMediaThread incomingMessageReader = new ReadMediaThread(this.socket,
+ "M_Host" + this.socket.getLocalAddress().toString());
+ incomingMessageReader.start();
+ this.sendMessagesToServer();
- readMediaThread.start();
- this.writeToClient();
-
- this.closeConnection(readMediaThread);
+ this.closeConnection(incomingMessageReader);
} catch (final IOException e) {
e.printStackTrace();
}
}
- private void writeToClient() {
+ private void sendMessagesToServer() {
final Encryption encryption = new Encryption();
final PrintWriter writer = new PrintWriter(this.outputStream, true);
final Scanner scanner = new Scanner(System.in);
diff --git a/src/com/mercurys/unfinished/MediaServer.java b/src/com/mercurys/unfinished/MediaServer.java
index cefb142..9def337 100644
--- a/src/com/mercurys/unfinished/MediaServer.java
+++ b/src/com/mercurys/unfinished/MediaServer.java
@@ -3,11 +3,8 @@
import com.mercurys.encryption.Encryption;
import com.mercurys.threads.ReadMediaThread;
-import javax.imageio.ImageIO;
-import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
-import java.nio.ByteBuffer;
import java.util.Scanner;
public class MediaServer {
@@ -17,7 +14,8 @@ public class MediaServer {
private MediaServer(final int port) {
try (final ServerSocket serverSocket = new ServerSocket()) {
- serverSocket.bind(new InetSocketAddress("localhost", port));
+ String hostAddress = "192.168.0.151"; //replace with one's own LAN IP address
+ serverSocket.bind(new InetSocketAddress(hostAddress, port));
System.out.println("""
Server initiated.
Waiting for client...""");
@@ -34,26 +32,26 @@ private MediaServer(final int port) {
public static void main(final String[] args) {
final MediaServer mediaServer = new MediaServer(4444);
- mediaServer.talk();
+ mediaServer.startTalking();
System.out.println("Thank you for using Project Mercurys!");
}
- private void talk() {
+ private void startTalking() {
try {
- final ReadMediaThread readMediaThread = new ReadMediaThread(this.socket,
- "client1" + this.socket.getLocalAddress().toString());
+ final ReadMediaThread incomingMessageReader = new ReadMediaThread(this.socket,
+ "M_Client");
- readMediaThread.start();
- this.writeToClient();
+ incomingMessageReader.start();
+ this.sendMessages();
- this.closeConnection(readMediaThread);
+ this.closeConnection(incomingMessageReader);
} catch (final IOException e) {
e.printStackTrace();
}
}
- private void writeToClient() {
+ private void sendMessages() {
final Encryption encryption = new Encryption();
final PrintWriter writer = new PrintWriter(this.outputStream, true);
final Scanner scanner = new Scanner(System.in);
@@ -71,26 +69,18 @@ private void writeToClient() {
}
private void handleImageUpload(final PrintWriter writer, final String outGoingLine) {
+ MImageHandler imageHandler = new MImageHandler(outputStream);
try {
writer.println(new Encryption().encrypt("/image incoming!"));
- this.sendImageFileAsByteStream(outGoingLine);
- System.out.println("[Mercurys:] Image sent!");
+ imageHandler.sendImageFileAsByteStream(outGoingLine);
+ System.out.println("[M_Console:] Image sent!");
} catch (final IOException e) {
System.out.println("Exception Occurred!");
e.printStackTrace();
}
}
- private void sendImageFileAsByteStream(final String imageFileName) throws IOException {
- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final BufferedImage bufferedImage = ImageIO.read(new File(imageFileName));
- ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
- final byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
-
- this.outputStream.write(size);
- this.outputStream.write(byteArrayOutputStream.toByteArray());
- }
private void closeConnection(final ReadMediaThread readMediaThread) throws IOException {
readMediaThread.exitThread();