implemented Multithreading

This commit is contained in:
darkress
2023-06-10 02:02:59 +02:00
parent fa7e6eb3b7
commit fff2b72ecd

View File

@@ -7,6 +7,32 @@ import java.io.*;
import java.net.*; import java.net.*;
import java.util.ArrayList; import java.util.ArrayList;
class Worker extends Thread {
Socket socket;
String[] pixelArray;
public Worker(String HOSTNAME, int PORT, String[] pixelArray) {
try {
this.socket = new Socket(HOSTNAME, PORT);
} catch (IOException e) {
e.printStackTrace();
}
this.pixelArray = pixelArray;
}
public void run() {
while(true) {
for(int i = 0; i < pixelArray.length; i++) {
byte[] payload = pixelArray[i].getBytes();
try {
socket.getOutputStream().write(payload);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
class Main class Main
{ {
private static BufferedImage processImage(String imageFile) { private static BufferedImage processImage(String imageFile) {
@@ -21,11 +47,11 @@ class Main
return null; return null;
} }
private static ArrayList<String> prepareArray(BufferedImage image, int xOffset, int yOffset) { private static String[] prepareArray(BufferedImage image, int xOffset, int yOffset) {
ArrayList<String> pixelArray = new ArrayList<>();
int width = image.getWidth(); int width = image.getWidth();
int height = image.getHeight(); int height = image.getHeight();
ArrayList<String> pixelArray = new ArrayList<>();
// Iterate over each pixel // Iterate over each pixel
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
@@ -35,22 +61,21 @@ class Main
// Extract individual color components // Extract individual color components
int alpha = (pixel >> 24) & 0xFF; int alpha = (pixel >> 24) & 0xFF;
int red = (pixel >> 16) & 0xFF;
int green = (pixel >> 8) & 0xFF;
int blue = pixel & 0xFF;
String rgbValue = String.format("%06X", (pixel & 0xFFFFFF)); String rgbValue = String.format("%06X", (pixel & 0xFFFFFF));
String messageParameters = String.format("PX %s %s %s\n", x+xOffset, y+yOffset, rgbValue); String messageParameters = String.format("PX %s %s %s\n", x+xOffset, y+yOffset, rgbValue);
if(alpha != 0) if(alpha != 0)
{ {
pixelArray.add(messageParameters); pixelArray.add(messageParameters);
} }
// Print RGBA values
System.out.println("Pixel at (" + x + ", " + y + "):");
System.out.println(rgbValue);
} }
} }
return pixelArray; String[] payloadArray = new String[pixelArray.size()];
for(int i = 0; i < payloadArray.length; i++)
{
payloadArray[i] = pixelArray.get(i);
}
return payloadArray;
} }
//Syntax blabla.jar x y Hostname Port Image.png //Syntax blabla.jar x y Hostname Port Image.png
@@ -63,20 +88,37 @@ class Main
// Read the PNG file // Read the PNG file
BufferedImage image = processImage(imageName); BufferedImage image = processImage(imageName);
ArrayList<String> pixelArray = prepareArray(image, xOffset, yOffset); String[] pixelArray = prepareArray(image, xOffset, yOffset);
try { Worker worker0 = new Worker(HOSTNAME, PORT, pixelArray);
/*Worker worker1 = new Worker(HOSTNAME, PORT, pixelArray);
Worker worker2 = new Worker(HOSTNAME, PORT, pixelArray);
Worker worker3 = new Worker(HOSTNAME, PORT, pixelArray);
Worker worker4 = new Worker(HOSTNAME, PORT, pixelArray);
Worker worker5 = new Worker(HOSTNAME, PORT, pixelArray);
Worker worker6 = new Worker(HOSTNAME, PORT, pixelArray);
Worker worker7 = new Worker(HOSTNAME, PORT, pixelArray);*/
worker0.start();
/*worker1.start();
worker2.start();
worker3.start();
worker4.start();
worker5.start();
worker6.start();
worker7.start();*/
/*try {
Socket socket = new Socket(HOSTNAME, PORT); Socket socket = new Socket(HOSTNAME, PORT);
//PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while(true) { while(true) {
for(int i = 0; i < pixelArray.size(); i++) { for(int i = 0; i < pixelArray.length; i++) {
//out.println(pixelArray.get(i)); byte[] payload = pixelArray[i].getBytes();
byte[] payload = pixelArray.get(i).getBytes();
socket.getOutputStream().write(payload); socket.getOutputStream().write(payload);
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }*/
} }
} }