Skip to content

Instantly share code, notes, and snippets.

@Pipe-Runner
Last active January 27, 2023 11:48
Show Gist options
  • Save Pipe-Runner/b56d3315f5c9436992d57643c94052e5 to your computer and use it in GitHub Desktop.
Save Pipe-Runner/b56d3315f5c9436992d57643c94052e5 to your computer and use it in GitHub Desktop.
Samples and Hints for OS Assignment 1 - NTNU Ålesund
package computation;
public class AsyncSearchSimulator implements Runnable {
protected Socket clientSocket = null;
protected String serverText = null;
public AsyncSearchSimulator(Socket clientSocket, String serverText) {
this.clientSocket = clientSocket;
this.serverText = serverText;
}
public void run() {
}
}
.
├── computation
│   ├── AsyncSearchSimulator.java
│   └── SearchSimulator.java
├── Main.java
├── servers
│   ├── MultiThreadedServer.java
│   └── SingleThreadedServer.java
└── utils
└── ResponseGenerator.java
class Main {
public static void main(String[] args) {
System.out.println("Starting server");
// Start your server here
}
}
package servers;
public class MultiThreadedServer implements Runnable {
protected int serverPort = 8080;
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
public SingleThreadedServer(int port) {
this.serverPort = port;
}
public void run() {
openServerSocket();
while (!isStopped()) {
// wait for a connection
// on receiving a request, execute the heavy computation in a new thread
new Thread(
new AsyncSearchSimulator(
clientSocket,
"Multithreaded Server"
)
).start();
}
System.out.println("Server Stopped.");
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
// implementation to stop the server from the main thread if needed
}
private void openServerSocket() {
// open server socket here
}
}
package utils;
public class ResponseGenerator {
public static String generatorResponseHTML(long time1, long time2) {
return ("<html><body>" +
"Singlethreaded Server: " +
time1 + " - " + time2 +
"</body></html>");
}
public static String generatorResponseHeader(int contentLength) {
return ("HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n" +
"Content-Length: " + contentLength +
"\r\n\r\n");
}
}
package computation;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import utils.ResponseGenerator;
public class SearchSimulator {
public static void processClientRequest() throws Exception {
long time1 = System.currentTimeMillis();
System.out.println("Request processing started at: " + time1);
Thread.sleep(10 * 1000);
long time2 = System.currentTimeMillis();
System.out.println("Request processing ended at: " + time2);
}
}
package servers;
public class SingleThreadedServer implements Runnable {
protected int serverPort = 8080;
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
public SingleThreadedServer(int port) {
this.serverPort = port;
}
public void run() {
openServerSocket();
while (!isStopped()) {
// wait for a connection
// on receiving a request, execute the heavy computation
}
System.out.println("Server Stopped.");
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
// implementation to stop the server from the main thread if needed
}
private void openServerSocket() {
// open server socket here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment