Basic globaleventbus

This commit is contained in:
Bas de Jong
2025-09-09 21:11:32 +02:00
parent 4842f38a50
commit 76ddb0d6b8
12 changed files with 386 additions and 32 deletions

124
.idea/uiDesigner.xml generated Normal file
View File

@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@@ -20,6 +20,12 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.4.8-jre</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package org.toop;
import com.google.common.eventbus.EventBus;
public enum GlobalEventBus {
INSTANCE;
private final EventBus eventBus = new EventBus("global-bus");
public EventBus get() {
return eventBus;
}
}

View File

@@ -0,0 +1,14 @@
package org.toop;
import com.google.common.eventbus.Subscribe;
import org.toop.server.*;
public class LoggerListener {
@Subscribe
public void onCommand(CommandEvent event) {
System.out.printf("LOG: %s args=%s -> %s%n",
event.command(),
String.join(",", event.args()),
event.result());
}
}

View File

@@ -1,7 +1,20 @@
package org.toop;
import org.toop.server.Server;
public class Main {
public static void main(String[] args) {
Server server = new Server("0.0.0.0", "5000");
Server server = new Server(Server.ServerBackend.LOCAL, "127.0.0.1", "5000");
server.setBackend(Server.ServerBackend.REMOTE);
GlobalEventBus.INSTANCE.get().register(new LoggerListener());
Server.Message msg = server.sendCommand(Server.Command.LOGIN, "move");
server.sendCommand(Server.Command.LOGIN, "test");
System.out.println(msg);
System.out.println(server);
}
}

View File

@@ -1,29 +0,0 @@
package org.toop;
public class Server {
String ip;
String port;
public Server(String set_ip, String set_port) {
ip = set_ip;
port = set_port;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}

View File

@@ -0,0 +1,3 @@
package org.toop.server;
public record CommandEvent(Server.Command command, String[] args, Server.Message result) {}

View File

@@ -0,0 +1,148 @@
package org.toop.server;
import org.toop.GlobalEventBus;
import org.toop.server.backend.*;
import java.util.EnumSet;
public class Server {
public enum ServerBackend {
LOCAL,
REMOTE,
}
public enum Command {
/**
* Login, "username"
*/
LOGIN,
/**
* Logout, "username"
*/
LOGOUT,
EXIT,
QUIT,
DISCONNECT,
BYE,
GET,
SUBSCRIBE,
MOVE,
CHALLENGE,
FORFEIT,
MESSAGE,
HELP,
}
private static final EnumSet<Command> VALID_COMMANDS = EnumSet.of(
Command.LOGIN, Command.LOGOUT, Command.EXIT, Command.QUIT,
Command.DISCONNECT, Command.BYE, Command.GET, Command.SUBSCRIBE,
Command.MOVE, Command.CHALLENGE, Command.FORFEIT,
Command.MESSAGE, Command.HELP
);
public enum Message {
OK,
ERR,
SVR,
}
String ip;
String port;
Backend backend;
public Server(ServerBackend set_backend, String set_ip, String set_port) {
ip = set_ip;
port = set_port;
setBackend(set_backend);
}
public Backend getBackend() {
return backend;
}
public void setBackend(ServerBackend backend) {
if (backend == ServerBackend.LOCAL) { this.backend = new Local(); }
else { this.backend = new Remote(); }
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
private Message sendCommandString(String sentence) {
return Message.OK;
}
private boolean isCommandValid(Command command) {
return VALID_COMMANDS.contains(command);
}
/**
* Sends a command to the server.
*
* @param command the command to execute
* @return a Message indicating success or error
*/
public Message sendCommand(Command command) {
if (!isCommandValid(command)) {
throw new IllegalArgumentException("Invalid command: " + command);
}
Message result = sendCommandString(command.toString());
GlobalEventBus.INSTANCE.get().post(new CommandEvent(command, new String[0], result));
return sendCommandString(command.toString());
}
/**
* Sends a command to the server.
*
* @param command the command to execute
* @param args command arguments.
* @return a Message indicating success or error
*/
public Message sendCommand(Command command, String... args) {
if (!isCommandValid(command)) {
throw new IllegalArgumentException("Invalid command: " + command);
}
for (int i = 0; i < args.length; i++) {
args[i] = args[i].trim();
if (args[i].isEmpty()) {
throw new IllegalArgumentException("Empty argument");
}
}
String[] fullCommand = new String[args.length + 1];
fullCommand[0] = command.toString();
System.arraycopy(args, 0, fullCommand, 1, args.length);
Message result = sendCommandString(String.join(" ", fullCommand));
GlobalEventBus.INSTANCE.get().post(new CommandEvent(command, args, result));
return result;
}
@Override
public String toString() {
return String.format(
"Server {ip: \"%s\", port: \"%s\", backend: \"%s\"}",
ip, port, backend
);
}
}

View File

@@ -0,0 +1,8 @@
package org.toop.server.backend;
import org.toop.server.Server;
public interface Backend {
Server.Message login(String username);
}

View File

@@ -0,0 +1,21 @@
package org.toop.server.backend;
import org.toop.server.Server;
public class Local implements Backend {
@Override
public Server.Message login(String username) {
return null;
}
@Override
public boolean equals(Object o) {
return o instanceof Local; // TODO
}
@Override
public String toString() {
return "Local";
}
}

View File

@@ -0,0 +1,21 @@
package org.toop.server.backend;
import org.toop.server.Server;
public class Remote implements Backend {
@Override
public Server.Message login(String username) {
return null;
}
@Override
public boolean equals(Object o) {
return o instanceof Remote; // TODO
}
@Override
public String toString() {
return "Remote";
}
}

View File

@@ -1,7 +1,8 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.toop.Server;
import org.toop.server.Server;
import org.toop.server.backend.*;
public class ServerTest {
@@ -9,7 +10,7 @@ public class ServerTest {
@Before
public void setUp() {
server = new Server("127.0.0.1", "8080");
server = new Server(Server.ServerBackend.LOCAL, "127.0.0.1", "8080");
}
@Test
@@ -30,6 +31,17 @@ public class ServerTest {
Assert.assertEquals("9090", server.getPort());
}
@Test
public void testSetLocalBackend() {
Assert.assertEquals(new Local(), server.getBackend());
}
@Test
public void testSetRemoteBackend() {
server.setBackend(Server.ServerBackend.REMOTE);
Assert.assertEquals(new Remote(), server.getBackend());
}
@Test
public void testNotNullAfterConstruction() {
Assert.assertNotNull(server);