generic signals
This commit is contained in:
@@ -8,11 +8,11 @@ public class ChatApp {
|
||||
Server server = new Server(6665);
|
||||
ClientConsole client = new ClientConsole(new InetSocketAddress("localhost", 6665));
|
||||
|
||||
client.onConnect.connect(() -> {
|
||||
client.onConnect.connect((arg) -> {
|
||||
client.getClientInterface().SendCreateRoom("101");
|
||||
});
|
||||
|
||||
client.onDisconnect.connect(() -> {
|
||||
client.onDisconnect.connect((arg) -> {
|
||||
System.out.println("Stopping server ...");
|
||||
|
||||
server.close();
|
||||
|
||||
@@ -20,7 +20,7 @@ public class ClientConnexion implements PacketVisitor, PacketHandler {
|
||||
this.serverAddress = serverAddress;
|
||||
this.socket = socket;
|
||||
this.callback = callback;
|
||||
this.socket.onClose.connect(this::onSocketClose);
|
||||
this.socket.onClose.connect((args) -> onSocketClose());
|
||||
this.socket.addHandler(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,15 @@ import java.util.Set;
|
||||
|
||||
public class Signal {
|
||||
|
||||
private final Set<Runnable> listeners;
|
||||
private final Class<?>[] types;
|
||||
private final Set<Slot> listeners;
|
||||
|
||||
public Signal() {
|
||||
public Signal(Class<?>... types) {
|
||||
this.types = types;
|
||||
this.listeners = new HashSet<>();
|
||||
}
|
||||
|
||||
public void connect(Runnable listener) {
|
||||
public void connect(Slot listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
@@ -19,9 +21,17 @@ public class Signal {
|
||||
this.listeners.clear();
|
||||
}
|
||||
|
||||
public void emit() {
|
||||
for (Runnable listener : this.listeners) {
|
||||
listener.run();
|
||||
public void emit(Object... values) {
|
||||
for (Slot listener : this.listeners) {
|
||||
if (values.length != types.length) {
|
||||
throw new UnsupportedOperationException("The number of provided arguments is not right");
|
||||
}
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (!types[i].isInstance(values[i])) {
|
||||
throw new UnsupportedOperationException("Incorrect value at index " + i);
|
||||
}
|
||||
}
|
||||
listener.run(values);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
5
ChatApp/app/src/main/java/utilities/Slot.java
Normal file
5
ChatApp/app/src/main/java/utilities/Slot.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package utilities;
|
||||
|
||||
public interface Slot {
|
||||
void run(Object ... args);
|
||||
}
|
||||
Reference in New Issue
Block a user