Skip to content

Commit 87a4fb4

Browse files
committed
ThreadPool init
1 parent ab833e4 commit 87a4fb4

4 files changed

Lines changed: 123 additions & 1 deletion

File tree

networking/ThreadPoolClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
public class ThreadPoolClient {
3+
4+
}

networking/ThreadPoolServer.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.IOException;
2+
import java.net.ServerSocket;
3+
import java.net.Socket;
4+
import java.util.concurrent.ExecutorService;
5+
6+
// From http://tutorials.jenkov.com/java-multithreaded-servers/thread-pooled-server.html
7+
8+
public class ThreadPoolServer implements Runnable {
9+
10+
protected ServerSocket serverSocket;
11+
protected Thread runningThread;
12+
protected ExecutorService threadPool;
13+
14+
15+
protected int serverPort = 9000;
16+
protected boolean isStopped = false;
17+
18+
public ThreadPoolServer(int port, ExecutorService threadPool) {
19+
this.serverPort = port;
20+
this.threadPool = threadPool;
21+
}
22+
23+
@Override
24+
public void run() {
25+
synchronized(this) {
26+
this.runningThread = Thread.currentThread();
27+
}
28+
this.openServerSocket();
29+
while (! this.isStopped()) {
30+
Socket clientSocket = null;
31+
try {
32+
clientSocket = this.serverSocket.accept();
33+
} catch (IOException e) {
34+
if (this.isStopped()) {
35+
break;
36+
}
37+
throw new RuntimeException("Error accepting client connection" + e);
38+
}
39+
this.threadPool.execute(new WorkerRunnable(clientSocket, "Thread Pooled Server"));
40+
}
41+
this.threadPool.shutdown();
42+
System.out.println("Server Stopped");
43+
}
44+
45+
public synchronized void stop() {
46+
this.isStopped = true;
47+
try {
48+
this.serverSocket.close();
49+
} catch (IOException e) {
50+
throw new RuntimeException("Error closing server", e);
51+
}
52+
}
53+
54+
private synchronized boolean isStopped() {
55+
return this.isStopped;
56+
}
57+
58+
private void openServerSocket() {
59+
try {
60+
this.serverSocket = new ServerSocket(this.serverPort);
61+
} catch (IOException e) {
62+
throw new RuntimeException("Cannot open port " + this.serverPort + ":" + e);
63+
}
64+
}
65+
66+
}

networking/WorkerRunnable.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.IOException;
2+
import java.io.InputStream;
3+
import java.io.OutputStream;
4+
import java.net.Socket;
5+
6+
// Code used for testing my thread pool
7+
// From https://www.programcreek.com/java-api-examples/index.php?source_dir=Android_H264Stream-master/src/tw/jwzhuang/ipcam/server/WorkerRunnable.java
8+
public class WorkerRunnable implements Runnable {
9+
10+
protected Socket clientSocket = null;
11+
protected String serverText = null;
12+
13+
public WorkerRunnable(Socket clientSocket, String serverText) {
14+
this.clientSocket = clientSocket;
15+
this.serverText = serverText;
16+
}
17+
18+
@Override
19+
public void run() {
20+
try {
21+
InputStream input = clientSocket.getInputStream();
22+
OutputStream output = clientSocket.getOutputStream();
23+
long time = System.currentTimeMillis();
24+
output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: "
25+
+ this.serverText + " - " + time + "").getBytes());
26+
output.close();
27+
input.close();
28+
System.out.println("Request processed: " + time);
29+
} catch (IOException e) {
30+
// report exception somewhere.
31+
e.printStackTrace();
32+
}
33+
}
34+
}

rectangles/Rectangles.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11

22
import java.util.ArrayList;
3+
import java.util.concurrent.ExecutorService;
34

45
import processing.core.PApplet;
56
import processing.core.PShape;
67
import processing.core.PVector;
78

89
public class Rectangles extends PApplet {
910

11+
private boolean isServer;
12+
private ThreadPoolServer server;
13+
private ThreadPoolClient client;
14+
private ExecutorService threadPool;
1015
private GameObj floor;
1116
private GameObj ceiling;
1217
private GameObj leftWall;
@@ -16,6 +21,10 @@ public class Rectangles extends PApplet {
1621

1722
private ArrayList<GameObj> objects = new ArrayList<GameObj>();
1823

24+
public Rectangles(boolean isServer) {
25+
this.isServer = isServer;
26+
}
27+
1928
public void settings() {
2029
size(640, 360);
2130
}
@@ -51,6 +60,10 @@ public void setup() {
5160

5261
// TODO: This will need to be reworked for server-client
5362
this.objects.add(this.rectangle);
63+
64+
// Setup Server
65+
this.server = new ThreadPoolServer(9000, threadPool);
66+
new Thread(this.server).start();
5467

5568
}
5669

@@ -77,6 +90,11 @@ public void draw() {
7790

7891
}
7992

93+
public void dispose() {
94+
System.out.println("Stopping Server");
95+
this.server.stop();
96+
}
97+
8098
public void keyPressed() {
8199
if (key == CODED) {
82100
if (keyCode == LEFT) {
@@ -95,7 +113,7 @@ public void keyPressed() {
95113

96114
public static void main(String[] args) {
97115
String[] processingArgs = {"Rectangles"};
98-
Rectangles sketch = new Rectangles();
116+
Rectangles sketch = new Rectangles(args[0].toLowerCase() == "server");
99117
PApplet.runSketch(processingArgs, sketch);
100118
}
101119
}

0 commit comments

Comments
 (0)