mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Cnaged commandqueue to use an actually thread safe queue. Server now using String for setting backend.
This commit is contained in:
@@ -16,7 +16,11 @@ public class Main {
|
|||||||
throw new RuntimeException("A event could not be initialized");
|
throw new RuntimeException("A event could not be initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
Server.start("remote", "127.0.0.1", "5001");
|
Server.start("REMOTE", "127.0.0.1", "5001");
|
||||||
|
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.command("HELP", "THANK"));
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.command("GET"));
|
||||||
|
|
||||||
Window.start("");
|
Window.start("");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import org.toop.server.backend.remote.TcpClient;
|
|||||||
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.List;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
public class Server extends Thread {
|
public class Server extends Thread {
|
||||||
|
|
||||||
@@ -33,20 +33,23 @@ public class Server extends Thread {
|
|||||||
String ip;
|
String ip;
|
||||||
String port;
|
String port;
|
||||||
IBackend backend;
|
IBackend backend;
|
||||||
List<String> commandQueue;
|
BlockingQueue<String> commandQueue;
|
||||||
|
|
||||||
public Server(ServerBackend set_backend, String set_ip, String set_port) {
|
public Server(String set_backend, String set_ip, String set_port) {
|
||||||
ip = set_ip;
|
ip = set_ip;
|
||||||
port = set_port;
|
port = set_port;
|
||||||
setBackend(set_backend);
|
this.setBackend(set_backend);
|
||||||
this.initEvents();
|
this.initEvents();
|
||||||
this.commandQueue = new LinkedList<>();
|
this.commandQueue = new LinkedBlockingQueue<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBackend getBackend() {
|
public IBackend getBackend() {
|
||||||
return backend;
|
return backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param backend The backend to change to.
|
||||||
|
*/
|
||||||
public void setBackend(ServerBackend backend) {
|
public void setBackend(ServerBackend backend) {
|
||||||
if (backend == ServerBackend.LOCAL) {
|
if (backend == ServerBackend.LOCAL) {
|
||||||
this.backend = new Local();
|
this.backend = new Local();
|
||||||
@@ -58,10 +61,25 @@ public class Server extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBackend(String backend) {
|
||||||
|
if (backend.equalsIgnoreCase("REMOTE")) {
|
||||||
|
this.backend = new Remote();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerBackend(ServerBackend.REMOTE));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.backend = new Local();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerBackend(ServerBackend.LOCAL));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public String getIp() {
|
public String getIp() {
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ip The ip to change to.
|
||||||
|
*/
|
||||||
public void setIp(String ip) {
|
public void setIp(String ip) {
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerIp(ip));
|
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerIp(ip));
|
||||||
@@ -71,11 +89,22 @@ public class Server extends Thread {
|
|||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param port The port to change to.
|
||||||
|
*/
|
||||||
public void setPort(String port) {
|
public void setPort(String port) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerPort(port));
|
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerPort(port));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Sends a command to the server.
|
||||||
|
*
|
||||||
|
* @param command The command to send to the server.
|
||||||
|
* @param args The arguments for the command.
|
||||||
|
*/
|
||||||
private void sendCommandByString(String command, String... args) {
|
private void sendCommandByString(String command, String... args) {
|
||||||
if (!ServerCommand.isValid(command)) {
|
if (!ServerCommand.isValid(command)) {
|
||||||
return;
|
return;
|
||||||
@@ -91,10 +120,10 @@ public class Server extends Thread {
|
|||||||
String[] fullCommand = new String[args.length + 1];
|
String[] fullCommand = new String[args.length + 1];
|
||||||
fullCommand[0] = command;
|
fullCommand[0] = command;
|
||||||
System.arraycopy(args, 0, fullCommand, 1, args.length);
|
System.arraycopy(args, 0, fullCommand, 1, args.length);
|
||||||
|
command = String.join(" ", fullCommand);
|
||||||
|
|
||||||
this.commandQueue.add(Arrays.toString(fullCommand)); // TODO Dunno if correct
|
this.commandQueue.add(command);
|
||||||
|
logger.info("Command {} added to the queue", command);
|
||||||
logger.info("Command {} added to the queue", Arrays.toString(fullCommand));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,9 +161,13 @@ public class Server extends Thread {
|
|||||||
-> this.setPort(event.port()));
|
-> this.setPort(event.port()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DO NOT USE, USE START INSTEAD.
|
||||||
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
TcpClient client = new TcpClient(this.getIp(), Integer.parseInt(this.getPort())); // TODO This is unsafe
|
TcpClient client = new TcpClient(this.getIp(), Integer.parseInt(this.getPort())); // TODO Parsing to int is unsafe
|
||||||
theRemoteServerTimeline(client);
|
theRemoteServerTimeline(client);
|
||||||
} catch (UnknownHostException | InterruptedException e) { // TODO Better error handling.
|
} catch (UnknownHostException | InterruptedException e) { // TODO Better error handling.
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@@ -142,26 +175,35 @@ public class Server extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void theRemoteServerTimeline(TcpClient client) throws InterruptedException {
|
private void theRemoteServerTimeline(TcpClient client) throws InterruptedException {
|
||||||
|
sleep(1000);
|
||||||
while (true) {
|
while (true) {
|
||||||
sleep(500); // 1s delay to not overload server.
|
try {
|
||||||
if (!commandQueue.isEmpty()) {
|
String command = this.commandQueue.take(); // waits until an element is available
|
||||||
String command = commandQueue.removeFirst();
|
|
||||||
logger.info("Sending command: {}", command);
|
logger.info("Sending command: {}", command);
|
||||||
try {
|
client.sendMessage(command);
|
||||||
client.sendMessage(command); // TODO: Will block.
|
client.readLine();
|
||||||
client.readLine(); // TODO Does this need to wait?
|
} catch (InterruptedException e) {
|
||||||
} catch (Exception e) {
|
Thread.currentThread().interrupt();
|
||||||
// TODO: Error handling.
|
break;
|
||||||
}
|
} catch (Exception e) {
|
||||||
|
logger.error("Error in remote server timeline", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Starts a server thread.
|
||||||
|
*
|
||||||
|
* @param backend The backend to use {remote, local}
|
||||||
|
* @param ip The address of the server to contact.
|
||||||
|
* @param port The port of the server.
|
||||||
|
*/
|
||||||
public static void start(String backend, String ip, String port) {
|
public static void start(String backend, String ip, String port) {
|
||||||
try {
|
try {
|
||||||
new Server(ServerBackend.valueOf(backend.toUpperCase()), ip, port).start();
|
new Server(backend, ip, port).start();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
new Server(ServerBackend.LOCAL, "127.0.0.1", "5001").start();
|
new Server(backend, "127.0.0.1", "5001").start(); // TODO: Doesn't do anything.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,5 +4,6 @@ import org.toop.server.Server;
|
|||||||
|
|
||||||
public interface IBackend {
|
public interface IBackend {
|
||||||
Server.Message login(String username);
|
Server.Message login(String username);
|
||||||
|
// boolean isValidBackend(String backend);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class ServerTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
server = new Server(Server.ServerBackend.LOCAL, "127.0.0.1", "8080");
|
server = new Server("LOCAL", "127.0.0.1", "8080");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user