Rework almost Everything! YAY
This commit is contained in:
parent
e313988e73
commit
fc61af4b66
3
.gitignore
vendored
3
.gitignore
vendored
@ -45,3 +45,6 @@ bin/
|
|||||||
## Logs ##
|
## Logs ##
|
||||||
/server/logs/
|
/server/logs/
|
||||||
/client/logs/
|
/client/logs/
|
||||||
|
|
||||||
|
## Server Database ##
|
||||||
|
/server/server.db
|
||||||
|
|||||||
@ -18,14 +18,11 @@ import java.util.Objects;
|
|||||||
|
|
||||||
public class Xenon implements Runnable {
|
public class Xenon implements Runnable {
|
||||||
public static void main(String[] args) {new Xenon().start();}
|
public static void main(String[] args) {new Xenon().start();}
|
||||||
private static Socket mainSocket;
|
|
||||||
public Color buttonColour = new Color(55, 90, 129);
|
public Color buttonColour = new Color(55, 90, 129);
|
||||||
public Image xenonIcon;
|
public Image xenonIcon;
|
||||||
|
|
||||||
static final String propertiesFile = "client.properties";
|
static final String propertiesFile = "client.properties";
|
||||||
// Properties
|
// Properties
|
||||||
public static int PortVoIP = 49190;
|
|
||||||
public static int PortText = 49180;
|
|
||||||
public static int Width = 800;
|
public static int Width = 800;
|
||||||
public static int Height = 600;
|
public static int Height = 600;
|
||||||
|
|
||||||
@ -64,11 +61,10 @@ public class Xenon implements Runnable {
|
|||||||
System.setOut(systemOutput);
|
System.setOut(systemOutput);
|
||||||
System.setErr(systemOutput);
|
System.setErr(systemOutput);
|
||||||
|
|
||||||
|
|
||||||
Log.info("Starting Xenon Client!");
|
Log.info("Starting Xenon Client!");
|
||||||
// Get Property Values
|
// Get Property Values
|
||||||
PortVoIP = Objects.requireNonNull(Util.GetProperties(propertiesFile, "PortVoIP", Util.PROPERTY_TYPE_INT)).intValue;
|
Networking.PortVoIP = Objects.requireNonNull(Util.GetProperties(propertiesFile, "PortVoIP", Util.PROPERTY_TYPE_INT)).intValue;
|
||||||
PortText = Objects.requireNonNull(Util.GetProperties(propertiesFile, "PortText", Util.PROPERTY_TYPE_INT)).intValue;
|
Networking.PortText = Objects.requireNonNull(Util.GetProperties(propertiesFile, "PortText", Util.PROPERTY_TYPE_INT)).intValue;
|
||||||
Width = Objects.requireNonNull(Util.GetProperties(propertiesFile, "Width", Util.PROPERTY_TYPE_INT)).intValue;
|
Width = Objects.requireNonNull(Util.GetProperties(propertiesFile, "Width", Util.PROPERTY_TYPE_INT)).intValue;
|
||||||
Height = Objects.requireNonNull(Util.GetProperties(propertiesFile, "Height", Util.PROPERTY_TYPE_INT)).intValue;
|
Height = Objects.requireNonNull(Util.GetProperties(propertiesFile, "Height", Util.PROPERTY_TYPE_INT)).intValue;
|
||||||
|
|
||||||
@ -94,14 +90,16 @@ public class Xenon implements Runnable {
|
|||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
String IPAddress = JOptionPane.showInputDialog(null, "IP Address Of Server", "127.0.0.1");
|
String IPAddress = JOptionPane.showInputDialog(null, "IP Address Of Server", "127.0.0.1");
|
||||||
if (IPAddress != null) {
|
if (IPAddress != null) {
|
||||||
mainSocket = Networking.connectToServer(IPAddress, PortText);
|
// Start Text Client
|
||||||
|
Thread textClient = new Thread(new TextClient(IPAddress, nickname));
|
||||||
|
textClient.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
JMenuItem m22 = new JMenuItem(new AbstractAction("Disconnect From Server") {
|
JMenuItem m22 = new JMenuItem(new AbstractAction("Disconnect From Server") {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
Networking.disconnectFromServer(mainSocket);
|
Networking.disconnectFromServer(TextClient.socket);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
JMenuItem m23 = new JMenuItem(new AbstractAction("Change Nickname") {
|
JMenuItem m23 = new JMenuItem(new AbstractAction("Change Nickname") {
|
||||||
@ -176,11 +174,10 @@ public class Xenon implements Runnable {
|
|||||||
|
|
||||||
// Text Sending Handler
|
// Text Sending Handler
|
||||||
send.addActionListener(e -> {
|
send.addActionListener(e -> {
|
||||||
if (mainSocket == null) {JOptionPane.showMessageDialog(null, "Ensure you are Connected to a Server!", "Error", JOptionPane.ERROR_MESSAGE);}
|
if (TextClient.socket == null || !TextClient.socket.isConnected() || TextClient.socket.isClosed()) {JOptionPane.showMessageDialog(null, "Ensure you are Connected to a Server!", "Error", JOptionPane.ERROR_MESSAGE);}
|
||||||
else {
|
else {
|
||||||
Log.info("Sending " + sendField.getText());
|
Log.info("Sending " + sendField.getText());
|
||||||
Thread textClient = new Thread(new TextClient(mainSocket, nickname, sendField.getText()));
|
TextClient.messageBuffer = sendField.getText();
|
||||||
textClient.start();
|
|
||||||
SwingUtilities.invokeLater(() -> sendField.setText(""));
|
SwingUtilities.invokeLater(() -> sendField.setText(""));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,36 +3,17 @@ package net.xircon.xenon.client.networking;
|
|||||||
import net.xircon.xenon.client.io.Log;
|
import net.xircon.xenon.client.io.Log;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
|
|
||||||
public class Networking {
|
public class Networking {
|
||||||
|
public static final String NAME_TERMINATOR = "\uffff";
|
||||||
|
public static final String MESSAGE_TERMINATOR = "\ufffe";
|
||||||
@Nullable
|
public static int PortVoIP = 49190;
|
||||||
public static Socket connectToServer(String Address, int Port) {
|
public static int PortText = 49180;
|
||||||
try {
|
|
||||||
Log.info("Connecting To Server " + Address + ":" + Port);
|
|
||||||
Socket socket = new Socket();
|
|
||||||
SocketAddress address = new InetSocketAddress(Address, Port);
|
|
||||||
socket.connect(address, 5000);
|
|
||||||
Log.info("Connected to Server!");
|
|
||||||
socket.setKeepAlive(true);
|
|
||||||
return socket;
|
|
||||||
} catch (SocketTimeoutException e) {
|
|
||||||
Log.warn("Connection timed out!");
|
|
||||||
return null;
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.err("Failed to Connect to Server!");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void disconnectFromServer(Socket socket) {
|
public static void disconnectFromServer(Socket socket) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,33 +1,73 @@
|
|||||||
package net.xircon.xenon.client.networking.textclient;
|
package net.xircon.xenon.client.networking.textclient;
|
||||||
|
|
||||||
import net.xircon.xenon.client.io.Log;
|
import net.xircon.xenon.client.io.Log;
|
||||||
|
import net.xircon.xenon.client.networking.Networking;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
|
||||||
public class TextClient implements Runnable {
|
public class TextClient implements Runnable {
|
||||||
private final String nameTerminator = "\uffff";
|
|
||||||
private String nickname;
|
private String nickname;
|
||||||
private String message;
|
public static String messageBuffer = "";
|
||||||
private Socket socket;
|
private String address;
|
||||||
|
public static Socket socket;
|
||||||
|
|
||||||
public TextClient(Socket serverSocket, String nickname, String message) {
|
public TextClient(String address, String nickname) {
|
||||||
this.socket = serverSocket; this.message = message; this.nickname = nickname;
|
this.address = address; this.nickname = nickname;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
socket = connectToServer(address, Networking.PortText);
|
||||||
|
assert socket != null;
|
||||||
|
|
||||||
|
// BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||||
|
// PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
|
||||||
|
// output.println(nickname + Networking.NAME_TERMINATOR + messageBuffer + Networking.MESSAGE_TERMINATOR);
|
||||||
|
// String messageFromServer = input.readLine();
|
||||||
|
// Log.info("Server Message: " + messageFromServer);
|
||||||
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
|
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
|
||||||
output.println(nickname + nameTerminator + message);
|
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||||
String messageFromServer = input.readLine();
|
while(socket.isConnected()) {
|
||||||
Log.info("Server Message: " + messageFromServer);
|
if (!messageBuffer.isEmpty()) {
|
||||||
} catch (IOException e) {
|
output.println(nickname + Networking.NAME_TERMINATOR + messageBuffer + Networking.MESSAGE_TERMINATOR);
|
||||||
Log.warn("Could not Read/Send Message!");
|
Log.info("Sending! " + nickname + Networking.NAME_TERMINATOR + messageBuffer + Networking.MESSAGE_TERMINATOR);
|
||||||
|
messageBuffer = new String();
|
||||||
|
String messageFromServer = input.readLine();
|
||||||
|
Log.info("Server Message: " + messageFromServer);
|
||||||
|
} else {
|
||||||
|
Thread.sleep(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
Log.warn("Could not Read/Send Data!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static Socket connectToServer(String Address, int Port) {
|
||||||
|
try {
|
||||||
|
Log.info("Connecting To Server " + Address + ":" + Port);
|
||||||
|
Socket socket = new Socket();
|
||||||
|
SocketAddress address = new InetSocketAddress(Address, Port);
|
||||||
|
socket.connect(address, 5000);
|
||||||
|
socket.setKeepAlive(true);
|
||||||
|
Log.info("Connected to Server!");
|
||||||
|
return socket;
|
||||||
|
} catch (SocketTimeoutException e) {
|
||||||
|
Log.warn("Connection timed out!");
|
||||||
|
return null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.err("Failed to Connect to Server!");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,19 +8,17 @@ import java.io.IOException;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static net.xircon.xenon.server.networking.Networking.*;
|
||||||
|
|
||||||
//import java.net.
|
//import java.net.
|
||||||
|
|
||||||
public class Xenon implements Runnable {
|
public class Xenon implements Runnable {
|
||||||
private Thread serverThread;
|
private Thread serverThread;
|
||||||
public static void main(String[] args) {new Xenon().start();}
|
public static void main(String[] args) {new Xenon().start();}
|
||||||
|
|
||||||
private ServerSocket textServerSocket;
|
|
||||||
|
|
||||||
private static final String propertiesFile = "server.properties";
|
private static final String propertiesFile = "server.properties";
|
||||||
// Sever Properties
|
// Sever Properties
|
||||||
public static String ServerName = "Xenon Test Server";
|
|
||||||
public static int PortVoIP = 49190;
|
|
||||||
public static int PortText = 49180;
|
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
serverThread = new Thread(this, "serverThread");
|
serverThread = new Thread(this, "serverThread");
|
||||||
@ -44,8 +42,7 @@ public class Xenon implements Runnable {
|
|||||||
Log.err("IOException Whilst Fetching External IP! {" + e + "}");
|
Log.err("IOException Whilst Fetching External IP! {" + e + "}");
|
||||||
}
|
}
|
||||||
Log.info("Server Name is " + '"' + ServerName + '"');
|
Log.info("Server Name is " + '"' + ServerName + '"');
|
||||||
textServerSocket = Networking.createTextServer(PortText);
|
TextServer textServer = new TextServer();
|
||||||
TextServer textServer = new TextServer(textServerSocket);
|
|
||||||
Thread textServerThread = new Thread(textServer);
|
Thread textServerThread = new Thread(textServer);
|
||||||
textServerThread.start();
|
textServerThread.start();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,54 @@
|
|||||||
|
package net.xircon.xenon.server.io.database;
|
||||||
|
|
||||||
|
import net.xircon.xenon.server.io.Log;
|
||||||
|
import net.xircon.xenon.server.io.Util;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Database {
|
||||||
|
private static String DatabaseLocation = null;
|
||||||
|
public static final String NAME_TERMINATOR = "\uffff";
|
||||||
|
public static final String MESSAGE_TERMINATOR = "\ufffe";
|
||||||
|
|
||||||
|
static class DatabaseData {
|
||||||
|
String name;
|
||||||
|
String message;
|
||||||
|
int time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void create() {
|
||||||
|
DatabaseLocation = Util.GetProperties("server.properties", "DatabaseLocation", Util.PROPERTY_TYPE_STRING).stringValue;
|
||||||
|
if (DatabaseLocation == null | Objects.equals(DatabaseLocation, "")) {
|
||||||
|
Log.err("Database Location was Null/Empty!");
|
||||||
|
} else {
|
||||||
|
File database = new File(DatabaseLocation);
|
||||||
|
Log.info("Creating a New Database");
|
||||||
|
try {
|
||||||
|
if (!database.createNewFile()) {
|
||||||
|
Log.warn("Database Already Exists");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.err("Could Not Create Database!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void write(String data) {
|
||||||
|
try {
|
||||||
|
assert DatabaseLocation != null;
|
||||||
|
File database = new File(DatabaseLocation);
|
||||||
|
if (database.exists() && database.canWrite()) {
|
||||||
|
FileWriter databaseWriter = new FileWriter(DatabaseLocation, true);
|
||||||
|
databaseWriter.write(data);
|
||||||
|
databaseWriter.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.err("Database Could not be Written to!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void read() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,21 +1,7 @@
|
|||||||
package net.xircon.xenon.server.networking;
|
package net.xircon.xenon.server.networking;
|
||||||
|
|
||||||
import net.xircon.xenon.server.io.Log;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
|
|
||||||
public class Networking {
|
public class Networking {
|
||||||
@Nullable
|
public static String ServerName = "Xenon Test Server";
|
||||||
public static ServerSocket createTextServer(int Port) {
|
public static int PortVoIP = 49190;
|
||||||
try {
|
public static int PortText = 49180;
|
||||||
ServerSocket serverSocket = new ServerSocket(Port);
|
|
||||||
Log.info("Text Server Waiting For Clients");
|
|
||||||
return serverSocket;
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.info("Failed to Create Text Server!");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package net.xircon.xenon.server.networking.textserver;
|
package net.xircon.xenon.server.networking.textserver;
|
||||||
|
|
||||||
import net.xircon.xenon.server.io.Log;
|
import net.xircon.xenon.server.io.Log;
|
||||||
|
import net.xircon.xenon.server.io.database.Database;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -9,7 +10,7 @@ import java.io.PrintWriter;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class ClientHandler implements Runnable {
|
public class ClientHandler implements Runnable {
|
||||||
private Socket clientSocket;
|
private final Socket clientSocket;
|
||||||
|
|
||||||
public ClientHandler(Socket clientSocket) {
|
public ClientHandler(Socket clientSocket) {
|
||||||
this.clientSocket = clientSocket;
|
this.clientSocket = clientSocket;
|
||||||
@ -17,14 +18,16 @@ public class ClientHandler implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
while (clientSocket.isConnected()) try {
|
||||||
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||||
PrintWriter output = output = new PrintWriter(clientSocket.getOutputStream(), true);
|
PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||||
String message = input.readLine();
|
String message = input.readLine();
|
||||||
String namedMessage[] = message.split("\uffff", -1);
|
String[] namedMessage = message.split(Database.NAME_TERMINATOR, -1);
|
||||||
Log.info("<" + namedMessage[0] + "> " + namedMessage[1]);
|
Log.info("<" + namedMessage[0] + "> " + namedMessage[1]);
|
||||||
|
Database.write(message);
|
||||||
output.println("Server Received Message!");
|
output.println("Server Received Message!");
|
||||||
clientSocket.setKeepAlive(true);
|
clientSocket.setKeepAlive(true);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.warn("Could not Read Message From Client");
|
Log.warn("Could not Read Message From Client");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
package net.xircon.xenon.server.networking.textserver;
|
package net.xircon.xenon.server.networking.textserver;
|
||||||
|
|
||||||
import net.xircon.xenon.server.io.Log;
|
import net.xircon.xenon.server.io.Log;
|
||||||
|
import net.xircon.xenon.server.io.database.Database;
|
||||||
|
import net.xircon.xenon.server.networking.Networking;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
@ -9,23 +12,32 @@ import java.net.Socket;
|
|||||||
public class TextServer implements Runnable {
|
public class TextServer implements Runnable {
|
||||||
private ServerSocket mainServerSocket;
|
private ServerSocket mainServerSocket;
|
||||||
|
|
||||||
public TextServer(ServerSocket serverSocket) {
|
public TextServer() {}
|
||||||
this.mainServerSocket = serverSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
mainServerSocket = createTextServer(Networking.PortText);
|
||||||
try {
|
Database.create();
|
||||||
Socket clientSocket = mainServerSocket.accept();
|
while (true) try {
|
||||||
Log.info("Client Connected: " + clientSocket);
|
Socket clientSocket = mainServerSocket.accept();
|
||||||
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
Log.info("Client Connected: " + clientSocket);
|
||||||
Thread thread = new Thread(clientHandler);
|
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
||||||
thread.start();
|
Thread thread = new Thread(clientHandler);
|
||||||
} catch (IOException e) {
|
thread.start();
|
||||||
Log.err("Text Server Did not correctly Accept a Client!");
|
} catch (IOException e) {
|
||||||
}
|
Log.err("Text Server Did not correctly Accept a Client!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static ServerSocket createTextServer(int Port) {
|
||||||
|
try {
|
||||||
|
ServerSocket serverSocket = new ServerSocket(Port);
|
||||||
|
Log.info("Text Server Waiting For Clients");
|
||||||
|
return serverSocket;
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.info("Failed to Create Text Server!");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,3 +2,4 @@
|
|||||||
PortText=49180
|
PortText=49180
|
||||||
PortVoIP=49190
|
PortVoIP=49190
|
||||||
ServerName=Xircon's Test Server
|
ServerName=Xircon's Test Server
|
||||||
|
DatabaseLocation=server.db
|
||||||
Loading…
x
Reference in New Issue
Block a user