Added functionality of polling events from queue and connect to server if button at view was pressed.

This commit is contained in:
WickedJack99
2023-12-04 01:31:38 +01:00
parent 948a1d0458
commit cbf2619ccc

View File

@@ -4,12 +4,15 @@
*/
package controller.src;
import java.util.Queue;
import connect.src.ClientThread;
import data.src.ViewToControllerData;
import data.src.ViewToControllerData.ViewEvent;
import gui.src.View;
import logger.src.MessageLogger;
import model.src.ModelRepresentation;
import queues.src.ViewToControllerQueue;
public class ControllerThread extends Thread implements Controller {
private static final String className = "ControllerThread";
@@ -17,26 +20,29 @@ public class ControllerThread extends Thread implements Controller {
private View view;
private ModelRepresentation model;
private Queue<ControllerToViewData> controllerToViewQueue;
private Queue<ViewToControllerData> viewToControllerQueue;
public ControllerThread(View view, ModelRepresentation model) {
this.view = view;
this.model = model;
}
public void run() {
ClientThread clientThread = new ClientThread();
MessageLogger.printMessage(className, "Client thread was started.");
clientThread.start();
ViewToControllerData viewToControllerData = new ViewToControllerData();
// Run until view sends event quit application
while (viewToControllerData.getViewEvent() != ViewEvent.QuitApplication) {
// Wait for a message from view.
while ((viewToControllerData = ViewToControllerQueue.getInstance().poll()) == null) {}
switch (viewToControllerData.getViewEvent()) {
case ViewEvent.ConnectToServerButtonWasPressed: {
ClientThread clientThread = new ClientThread();
MessageLogger.printMessage(className, "Client thread was started.");
clientThread.start();
}break;
default: {
}break;
}
}
MessageLogger.printMessage(className, "Exit controller thread.");
}
public void setControllerToViewQueue(Queue<ControllerToViewData> controllerToViewQueue) {
this.controllerToViewQueue = controllerToViewQueue;
}
public void setViewToControllerQueue(Queue<ViewToControllerData> viewToControllerQueue) {
this.viewToControllerQueue = viewToControllerQueue;
}
}