Removed testdata and implemented data flow from ui to client.
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
/**
|
||||
* @author Aaron Moser
|
||||
*/
|
||||
package connect.src;
|
||||
|
||||
import data.src.ControllerToConnectionClient.ControllerToConnectionClientData;
|
||||
|
||||
public interface Client {
|
||||
public void setClientData(ClientInformation clientData);
|
||||
public void createConnection();
|
||||
public void sendData(Data data);
|
||||
public boolean createConnection(ControllerToConnectionClientData controllerToConnectionClientData);
|
||||
public void sendData(String data);
|
||||
public void receiveData();
|
||||
public void closeConnection();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/**
|
||||
* @author Aaron Moser
|
||||
*/
|
||||
package connect.src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -5,7 +8,9 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
@@ -17,27 +22,25 @@ import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
|
||||
public final class TLSClient implements Client {
|
||||
import data.src.ControllerToConnectionClient.ConnectToServer;
|
||||
import data.src.ControllerToConnectionClient.ControllerToConnectionClientData;
|
||||
|
||||
private TLSClientInformation clientInformation;
|
||||
public final class TLSClient implements Client {
|
||||
|
||||
private SSLSocket sslSocket;
|
||||
private BufferedReader reader;
|
||||
private PrintWriter writer;
|
||||
|
||||
public TLSClient(ClientInformation clientInformation) {
|
||||
this.setClientData(clientInformation);
|
||||
}
|
||||
|
||||
public void setClientData(ClientInformation clientInformation) {
|
||||
this.clientInformation = (TLSClientInformation)clientInformation;
|
||||
}
|
||||
|
||||
public void createConnection() {
|
||||
public boolean createConnection(ControllerToConnectionClientData controllerToConnectionClientData) {
|
||||
try {
|
||||
ConnectToServer connectToServer = ((ConnectToServer)controllerToConnectionClientData);
|
||||
String ipAddress = connectToServer.getIPAddress();
|
||||
int port = connectToServer.getPort();
|
||||
String trustStorePath = connectToServer.getTruststorePath();
|
||||
String trustStorePassword = connectToServer.getTruststorePassword();
|
||||
|
||||
KeyStore truststore = KeyStore.getInstance("JKS");
|
||||
truststore.load(new FileInputStream(this.clientInformation.truststorePath()), this.clientInformation.truststorePassword());
|
||||
truststore.load(new FileInputStream(trustStorePath), trustStorePassword.toCharArray());
|
||||
|
||||
// Create a TrustManager that trusts the certificates in the truststore
|
||||
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
@@ -50,45 +53,50 @@ public final class TLSClient implements Client {
|
||||
// Set the SSL context on the SSLSocketFactory
|
||||
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
|
||||
|
||||
sslSocket = (SSLSocket) sslSocketFactory.createSocket("127.0.0.1", 5000);
|
||||
reader = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
|
||||
writer = new PrintWriter(sslSocket.getOutputStream());
|
||||
sslSocket = (SSLSocket) sslSocketFactory.createSocket(ipAddress, port);
|
||||
return true;
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (CertificateException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (KeyStoreException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (KeyManagementException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void sendData(Data data) {
|
||||
if (sslSocket != null && sslSocket.isConnected() && reader != null && writer != null) {
|
||||
writer.println("Hello Server!");
|
||||
writer.flush();
|
||||
if (null != data) {
|
||||
writer.println(data.toString());
|
||||
public void sendData(String data) {
|
||||
try {
|
||||
writer = new PrintWriter(sslSocket.getOutputStream());
|
||||
if (sslSocket != null && sslSocket.isConnected() && writer != null) {
|
||||
writer.println("Hello Server!");
|
||||
writer.flush();
|
||||
if (null != data) {
|
||||
writer.println(data);
|
||||
writer.flush();
|
||||
}
|
||||
} else {
|
||||
System.out.println("Connection is not established.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Connection is not established.");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void receiveData() {
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
|
||||
if (null != reader) {
|
||||
String response;
|
||||
while ((response = reader.readLine()) != null) {
|
||||
@@ -96,7 +104,6 @@ public final class TLSClient implements Client {
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -110,7 +117,6 @@ public final class TLSClient implements Client {
|
||||
writer.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
@@ -118,7 +124,6 @@ public final class TLSClient implements Client {
|
||||
sslSocket.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,22 @@
|
||||
*/
|
||||
package controller.src;
|
||||
|
||||
import connect.src.ClientThread;
|
||||
import connect.src.Client;
|
||||
import connect.src.TLSClient;
|
||||
|
||||
import controller.src.Validate.Validate;
|
||||
|
||||
import data.src.ControllerToConnectionClient.ConnectToServer;
|
||||
import data.src.ControllerToConnectionClient.ControllerToConnectionClientData;
|
||||
|
||||
import data.src.ControllerToView.ConnectToServerSuccessful;
|
||||
import data.src.ControllerToView.ControllerToViewData;
|
||||
|
||||
import data.src.ViewToController.ConnectToServerData;
|
||||
import data.src.ViewToController.EmptyViewToControllerData;
|
||||
import data.src.ViewToController.ViewToControllerData;
|
||||
import data.src.ViewToController.ViewToControllerData.ViewEvent;
|
||||
|
||||
import gui.src.View;
|
||||
|
||||
import logger.src.MessageLogger;
|
||||
@@ -24,30 +34,31 @@ public class ControllerThread extends Thread implements Controller {
|
||||
private View view;
|
||||
private ModelRepresentation model;
|
||||
|
||||
private Client client;
|
||||
|
||||
public ControllerThread(View view, ModelRepresentation model) {
|
||||
this.view = view;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
MessageLogger.printMessage(className, "Controller Thread was started.");
|
||||
ViewToControllerData viewToControllerData = new EmptyViewToControllerData();
|
||||
|
||||
// Run until view sends event quit application
|
||||
while (viewToControllerData.getViewEvent() != ViewEvent.QuitApplication) {
|
||||
|
||||
// Wait for a message from view.
|
||||
while ((viewToControllerData = ViewToControllerQueue.getInstance().poll()) == null) {}
|
||||
|
||||
MessageLogger.printMessage(className, "Got new data from ViewToControllerQueue.");
|
||||
MessageLogger.printMessage(className, "Event is: " + viewToControllerData.getViewEvent());
|
||||
|
||||
switch (viewToControllerData.getViewEvent()) {
|
||||
case ViewEvent.ConnectToServerButtonWasPressed: {
|
||||
String ipAddress = ((ConnectToServerData)viewToControllerData).getIPAddress();
|
||||
String port = ((ConnectToServerData)viewToControllerData).getPort();
|
||||
System.out.println(ipAddress);
|
||||
System.out.println(port);
|
||||
//ControllerToViewQueue.getInstance().add(new );
|
||||
//if (Validate.)
|
||||
//ClientThread clientThread = new ClientThread();
|
||||
//MessageLogger.printMessage(className, "Client thread was started.");
|
||||
//clientThread.start();
|
||||
handleConnectToServerButtonWasPressed(viewToControllerData);
|
||||
}break;
|
||||
|
||||
default: {
|
||||
|
||||
}break;
|
||||
@@ -55,4 +66,36 @@ public class ControllerThread extends Thread implements Controller {
|
||||
}
|
||||
MessageLogger.printMessage(className, "Exit controller thread.");
|
||||
}
|
||||
|
||||
private void handleConnectToServerButtonWasPressed(ViewToControllerData viewToControllerData) {
|
||||
String ipAddress = ((ConnectToServerData)viewToControllerData).getIPAddress();
|
||||
int port = ((ConnectToServerData)viewToControllerData).getPort();
|
||||
|
||||
boolean validIP = Validate.isValidIPv4Address(ipAddress);
|
||||
boolean validPort = Validate.isValidPort(port, 1, 60000);
|
||||
MessageLogger.printMessage(className, "IP: " + validIP + ", Port: " + validPort);
|
||||
|
||||
if (validIP) {
|
||||
if (validPort) {
|
||||
ControllerToConnectionClientData connectToServerData;
|
||||
connectToServerData = new ConnectToServer();
|
||||
((ConnectToServer)connectToServerData).setIPAddress(ipAddress);
|
||||
((ConnectToServer)connectToServerData).setPort(port);
|
||||
String trustStorePath = ((ConnectToServerData)viewToControllerData).getTrustStorePath();
|
||||
((ConnectToServer)connectToServerData).setTruststorePath(trustStorePath);
|
||||
String trustStorePassword = ((ConnectToServerData)viewToControllerData).getTrustStorePassword();
|
||||
((ConnectToServer)connectToServerData).setTruststorePassword(trustStorePassword);
|
||||
|
||||
this.client = new TLSClient();
|
||||
if (this.client.createConnection(connectToServerData) == true) {
|
||||
ControllerToViewData controllerToViewData = new ConnectToServerSuccessful();
|
||||
ControllerToViewQueue.getInstance().add(controllerToViewData);
|
||||
}
|
||||
} else {
|
||||
//TODO send event to view that port is not valid.
|
||||
}
|
||||
} else {
|
||||
//TODO send event to view that ip is not valid.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ public class Validate {
|
||||
// Maximum length of ipv4 address is 13 characters, only evaluate if
|
||||
// length of string is less or equal to 13.
|
||||
if (ipv4Address.length() <= 13) {
|
||||
String[] numbersAsString = ipv4Address.split(".");
|
||||
String[] numbersAsString = ipv4Address.split("\\.");
|
||||
// If ip address contains exactly 4 parts splitted by dots:
|
||||
if (numbersAsString.length == 4) {
|
||||
// Transform each part to integer and check if in valid range.
|
||||
@@ -62,8 +62,7 @@ public class Validate {
|
||||
* @param endOfRange
|
||||
* @return
|
||||
*/
|
||||
public static boolean isValidPort(String port, int startOfRange, int endOfRange) {
|
||||
int portAsInt = Integer.valueOf(port);
|
||||
return (portAsInt >= startOfRange && portAsInt <= endOfRange);
|
||||
public static boolean isValidPort(int port, int startOfRange, int endOfRange) {
|
||||
return (port >= startOfRange && port <= endOfRange);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* @author Aaron Moser
|
||||
* @date 08.12.2023
|
||||
*/
|
||||
package data.src.ControllerToConnectionClient;
|
||||
|
||||
public class ConnectToServer implements ControllerToConnectionClientData {
|
||||
private String ipAddress;
|
||||
private int port;
|
||||
|
||||
String truststorePath;
|
||||
String truststorePassword;
|
||||
|
||||
@Override
|
||||
public ConnectionClientEvent getConnectionClientEvent() {
|
||||
return ConnectionClientEvent.ConnectToServer;
|
||||
}
|
||||
|
||||
public String getIPAddress() {
|
||||
return this.ipAddress;
|
||||
}
|
||||
|
||||
public void setIPAddress(String ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getTruststorePath() {
|
||||
return this.truststorePath;
|
||||
}
|
||||
|
||||
public void setTruststorePath(String truststorePath) {
|
||||
this.truststorePath = truststorePath;
|
||||
}
|
||||
|
||||
public String getTruststorePassword() {
|
||||
return this.truststorePassword;
|
||||
}
|
||||
|
||||
public void setTruststorePassword(String truststorePassword) {
|
||||
this.truststorePassword = truststorePassword;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @author Aaron Moser
|
||||
* @date 08.12.2023
|
||||
*/
|
||||
package data.src.ControllerToConnectionClient;
|
||||
|
||||
public interface ControllerToConnectionClientData {
|
||||
public static enum ConnectionClientEvent {
|
||||
ConnectToServer,
|
||||
};
|
||||
|
||||
public ConnectionClientEvent getConnectionClientEvent();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package data.src.ControllerToView;
|
||||
|
||||
public class ConnectToServerSuccessful implements ControllerToViewData {
|
||||
|
||||
@Override
|
||||
public void setControllerEvent(ControllerEvent controllerEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ControllerEvent getControllerEvent() {
|
||||
return ControllerEvent.ConnectToServerWasSuccessful;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,11 +7,16 @@ package data.src.ViewToController;
|
||||
public class ConnectToServerData implements ViewToControllerData {
|
||||
|
||||
private String ipAddress;
|
||||
private String port;
|
||||
private int port;
|
||||
|
||||
public ConnectToServerData(String ipAddress, String port) {
|
||||
private String trustStorePath;
|
||||
private String trustStorePassword;
|
||||
|
||||
public ConnectToServerData(String ipAddress, int port, String trustStorePath, String trustStorePassword) {
|
||||
this.ipAddress = ipAddress;
|
||||
this.port = port;
|
||||
this.trustStorePath = trustStorePath;
|
||||
this.trustStorePassword = trustStorePassword;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -28,7 +33,23 @@ public class ConnectToServerData implements ViewToControllerData {
|
||||
return this.ipAddress;
|
||||
}
|
||||
|
||||
public String getPort() {
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public String getTrustStorePath() {
|
||||
return this.trustStorePath;
|
||||
}
|
||||
|
||||
public void setTrustStorePath(String trustStorePath) {
|
||||
this.trustStorePath = trustStorePath;
|
||||
}
|
||||
|
||||
public String getTrustStorePassword() {
|
||||
return this.trustStorePassword;
|
||||
}
|
||||
|
||||
public void setTrustStorePassword(String trustStorePassword) {
|
||||
this.trustStorePassword = trustStorePassword;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
*/
|
||||
package gui.src;
|
||||
|
||||
import data.src.ControllerToView.ControllerToViewData;
|
||||
|
||||
public interface View {
|
||||
public void start();
|
||||
public void notify(ControllerToViewData controllerToViewData);
|
||||
}
|
||||
|
||||
@@ -4,21 +4,31 @@
|
||||
*/
|
||||
package gui.src;
|
||||
|
||||
import data.src.ControllerToView.ControllerToViewData;
|
||||
import data.src.ControllerToView.ControllerToViewData.ControllerEvent;
|
||||
import gui.src.frames.MainFrame;
|
||||
|
||||
import logger.src.MessageLogger;
|
||||
import queues.src.ControllerToViewQueue;
|
||||
|
||||
public class ViewThread extends Thread implements View {
|
||||
private static final String className = "ViewThread";
|
||||
|
||||
private boolean isRunning = true;
|
||||
|
||||
public void run() {
|
||||
MainFrame.getInstance();
|
||||
printMessage("Swing ui thread was started.");
|
||||
MessageLogger.printMessage(className, "Swing ui thread was started.");
|
||||
|
||||
while (isRunning) {
|
||||
ControllerToViewData controllerToViewData = ControllerToViewQueue.getInstance().poll();
|
||||
if (controllerToViewData != null) {
|
||||
notify(controllerToViewData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints a given message.
|
||||
* @param message to print.
|
||||
*/
|
||||
private static void printMessage(String message) {
|
||||
System.out.println(className + ": " + message);
|
||||
public void notify(ControllerToViewData controllerToViewData) {
|
||||
MainFrame.getInstance().notify(controllerToViewData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ package gui.src.frames;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import data.src.ControllerToView.ControllerToViewData;
|
||||
import data.src.ControllerToView.ControllerToViewData.ControllerEvent;
|
||||
import gui.src.constants.GUIConstants.Panel;
|
||||
import gui.src.constants.GUIConstants.Theme;
|
||||
import gui.src.interfaces.Themeable;
|
||||
@@ -102,5 +104,17 @@ public final class MainFrame extends JFrame implements Themeable {
|
||||
this.setThemeColor(Theme.Light);
|
||||
}
|
||||
}
|
||||
|
||||
public void notify(ControllerToViewData controllerToViewData) {
|
||||
ControllerEvent controllerEvent = controllerToViewData.getControllerEvent();
|
||||
switch (controllerEvent) {
|
||||
case ConnectToServerWasSuccessful: {
|
||||
ConnectToServerPanel.getInstance().enableFields();
|
||||
}break;
|
||||
default: {
|
||||
|
||||
}break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,29 +32,21 @@ public class ConnectToServerButtonListener implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println("Connect to server button was pressed.");
|
||||
|
||||
// Denies user to change fields while reading
|
||||
this.ipAddressTextField.setEditable(false);
|
||||
this.portTextField.setEditable(false);
|
||||
ConnectToServerPanel.getInstance().disableFields();
|
||||
|
||||
// Reads in fields text
|
||||
String ipAddress = this.ipAddressTextField.getText();
|
||||
String port = this.portTextField.getText();
|
||||
int port = Integer.valueOf(this.portTextField.getText());
|
||||
|
||||
|
||||
String trustStorePath = "cacerts";
|
||||
String trustStorePassword = "qwerty";
|
||||
|
||||
// Sends data to queue
|
||||
ViewToControllerQueue.getInstance().add(new ConnectToServerData(ipAddress, port));
|
||||
|
||||
System.out.println("Waiting for controller to respond.");
|
||||
ControllerToViewData controllerToViewData;
|
||||
//while ((controllerToViewData = ControllerToViewQueue.getInstance().peek()) == null) {}
|
||||
//if (controllerToViewData.getControllerEvent() == ControllerEvent.ConnectToServerWasSuccessful) {
|
||||
// ControllerToViewQueue.getInstance().poll();
|
||||
// MainFrame.getInstance().setPanel(Panel.MonitoringPanel);
|
||||
//} else {
|
||||
// Allows user to change fields if connection
|
||||
// this.ipAddressTextField.setEditable(true);
|
||||
// this.portTextField.setEditable(true);
|
||||
//}
|
||||
ViewToControllerQueue.getInstance().add(new ConnectToServerData(
|
||||
ipAddress,
|
||||
port,
|
||||
trustStorePath,
|
||||
trustStorePassword
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import javax.swing.JRadioButton;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import data.src.ControllerToView.ControllerToViewData;
|
||||
import data.src.ControllerToView.ControllerToViewData.ControllerEvent;
|
||||
import data.src.ViewToController.ViewToControllerData;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
@@ -359,4 +360,22 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
|
||||
public static void setViewToControllerQueue(Queue<ViewToControllerData> newViewToControllerQueue) {
|
||||
viewToControllerQueue = newViewToControllerQueue;
|
||||
}
|
||||
|
||||
public void enableFields() {
|
||||
for (JRadioButton radioButton : ipVersionRadioButtons) {
|
||||
radioButton.setEnabled(true);
|
||||
}
|
||||
ipAddressTextField.setEditable(true);
|
||||
portTextField.setEditable(true);
|
||||
connectButton.setEnabled(true);
|
||||
}
|
||||
|
||||
public void disableFields() {
|
||||
for (JRadioButton radioButton : ipVersionRadioButtons) {
|
||||
radioButton.setEnabled(false);
|
||||
}
|
||||
ipAddressTextField.setEditable(false);
|
||||
portTextField.setEditable(false);
|
||||
connectButton.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user