INITIAL COMMIT
This commit is contained in:
commit
914edad949
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="16" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/s9-grn-programmieraufgaben.iml" filepath="$PROJECT_DIR$/.idea/s9-grn-programmieraufgaben.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
11
.idea/s9-grn-programmieraufgaben.iml
Normal file
11
.idea/s9-grn-programmieraufgaben.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/a1/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
BIN
a1/src/.DS_Store
vendored
Normal file
BIN
a1/src/.DS_Store
vendored
Normal file
Binary file not shown.
@ -0,0 +1,71 @@
|
||||
package de.uulm.in.vs.vns.p1a;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
/*
|
||||
*
|
||||
* Implementieren Sie eine Klasse NumberGuessingGameRequestHandler im Paket de.uulm.in.vs.vns.p1a welche die Spiellogik des Single-Threaded Servers aus der Übung enthält.
|
||||
* Darüber hinaus soll die Klasse aber als neuer Thread ausführbar sein, dafür muss sie das Interface Runnable implementieren.
|
||||
* Implementieren Sie eine Klasse NumberGuessingGameThreadedServer im Paket de.uulm.in.vs.vns.p1a, die nach einem Verbindungsaufbau einen neuen Spielprozess (Thread) startet. Verwenden Sie dazu die Java Thread-Klasse.
|
||||
* Um den Server vor einer Überlastung durch zu viele Verbindungen zu schützen, begrenzen Sie die maximale Anzahl von gleichzeitig laufenden Spielprozessen auf 4.
|
||||
* Erweitern Sie dafür die Klasse NumberGuessingGameThreadedServer um einen Threadpool durch Verwendung der Klassen ExecutorService bzw. Executor. Kommentieren Sie Ihre ursprüngliche Lösung ohne Threadpool aus, statt diese zu löschen.
|
||||
* (Hinweis: Die Klasse NumberGuessingGameRequestHandler muss für diese Aufgabe nicht mehr verändert werden.)
|
||||
*
|
||||
*/
|
||||
|
||||
public class NumberGuessingGameRequestHandler implements Runnable {
|
||||
private final Socket connectionSocket;
|
||||
|
||||
public NumberGuessingGameRequestHandler(Socket socket) {
|
||||
this.connectionSocket = socket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(connectionSocket.getInputStream()));
|
||||
PrintWriter writer = new PrintWriter(
|
||||
new OutputStreamWriter(connectionSocket.getOutputStream()))) {
|
||||
|
||||
writer.println("hey client, guess my secret number!");
|
||||
writer.flush();
|
||||
|
||||
int secret = ThreadLocalRandom.current().nextInt(50) + 1;
|
||||
int tries = 6;
|
||||
|
||||
String input;
|
||||
while ((input = reader.readLine()) != null) {
|
||||
try {
|
||||
int guess = Integer.parseInt(input);
|
||||
if (guess == secret) {
|
||||
writer.println("you've won");
|
||||
break;
|
||||
} else if (guess > secret) {
|
||||
writer.println("your guess is too high");
|
||||
} else {
|
||||
writer.println("your guess is too low");
|
||||
}
|
||||
tries -= 1;
|
||||
if (tries == 0) {
|
||||
writer.println("You lose!");
|
||||
break;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
writer.println("Please enter a valid number!");
|
||||
} finally {
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,34 @@
|
||||
package de.uulm.in.vs.vns.p1a;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class NumberGuessingGameThreadedServer {
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
// Erzeugen eines ExecutorService mit einem Threadpool von 4 Threads
|
||||
ExecutorService threadPool = Executors.newFixedThreadPool(4); // Alternative .newCachedThreadPool();
|
||||
|
||||
try (ServerSocket serverSocket = new ServerSocket(5555)) {
|
||||
while (true) {
|
||||
System.out.println("waiting for connection");
|
||||
Socket connectionSocket = serverSocket.accept();
|
||||
System.out.println("connection accepted");
|
||||
|
||||
// ExecutorService, um die Anfrage zu bearbeiten
|
||||
threadPool.execute(new NumberGuessingGameRequestHandler(connectionSocket));
|
||||
|
||||
/*
|
||||
/ SOLUTION: Ohne Threadpool
|
||||
Thread thread = new Thread(new NumberGuessingGameRequestHandler(connectionSocket));
|
||||
thread.start();
|
||||
*/
|
||||
}
|
||||
} finally {
|
||||
threadPool.shutdown(); // Wichtig, um den ExecutorService ordnungsgemäß herunterzufahren
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user