change listener<->observable designpattern

This commit is contained in:
sirjonasxx 2020-04-26 22:00:30 +02:00
parent eb8f6afecd
commit be2c8adecb
46 changed files with 274 additions and 235 deletions

View File

@ -1,8 +1,9 @@
package gearth.extensions;
import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import gearth.services.extensionhandler.extensions.network.NetworkExtensionInfo;
import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtensionInfo;
import java.io.*;
import java.net.Socket;
@ -139,7 +140,7 @@ public abstract class Extension implements IExtension{
int connectionPort = packet.readInteger();
String hotelVersion = packet.readString();
String harbleMessagesPath = packet.readString();
notifyConnectionListeners(host, connectionPort, hotelVersion, harbleMessagesPath);
onConnectionObservable.fireEvent(l -> l.onConnection(host, connectionPort, hotelVersion, harbleMessagesPath));
onStartConnection();
}
else if (packet.headerId() == NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.CONNECTIONEND) {
@ -367,17 +368,9 @@ public abstract class Extension implements IExtension{
}
public interface OnConnectionListener {
void act(String host, int port, String hotelversion, String harbleMessagesPath);
}
private List<OnConnectionListener> onConnectionListeners = new ArrayList<>();
private Observable<OnConnectionListener> onConnectionObservable = new Observable<>();
public void onConnect(OnConnectionListener listener){
onConnectionListeners.add(listener);
}
private void notifyConnectionListeners(String host, int port, String hotelversion, String harbleMessagesPath) {
for (OnConnectionListener listener : onConnectionListeners) {
listener.act(host, port, hotelversion, harbleMessagesPath);
}
onConnectionObservable.addListener(listener);
}
}

View File

@ -40,7 +40,7 @@ public abstract class ExtensionForm implements IExtension{
public boolean sendToClient(HPacket packet){
return extension.sendToClient(packet);
}
public void onConnect(Extension.OnConnectionListener listener) {
public void onConnect(OnConnectionListener listener) {
extension.onConnect(listener);
}

View File

@ -14,6 +14,6 @@ public interface IExtension {
void intercept(HMessage.Direction direction, Extension.MessageListener messageListener);
boolean requestFlags(Extension.FlagsCheckListener flagRequestCallback);
void writeToConsole(String s); // not implemented in g-earth yet
void onConnect(Extension.OnConnectionListener listener);
void onConnect(OnConnectionListener listener);
}

View File

@ -0,0 +1,5 @@
package gearth.extensions;
public interface OnConnectionListener {
void onConnection(String host, int port, String hotelversion, String harbleMessagesPath);
}

View File

@ -2,12 +2,11 @@ package gearth.extensions.extra.harble;
import gearth.extensions.ExtensionInfo;
import gearth.extensions.IExtension;
import gearth.extensions.OnConnectionListener;
import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Jonas on 3/12/2018.
*/
@ -23,6 +22,7 @@ public class ChatConsole {
private volatile String infoMessage;
private volatile boolean firstTime = true;
private volatile Observable<ChatInputListener> chatInputObservable = new Observable<>();
public ChatConsole(final HashSupport hashSupport, IExtension extension) {
@ -42,7 +42,6 @@ public class ChatConsole {
this.infoMessage = infoMessage;
final boolean[] doOncePerConnection = {false};
extension.onConnect((s, i, s1, h1) -> doOncePerConnection[0] = true);
extension.intercept(HMessage.Direction.TOSERVER, hMessage -> {
@ -81,7 +80,7 @@ public class ChatConsole {
writeOutput(infoMessage, false);
}
else {
notifyChatInputListeners(str);
chatInputObservable.fireEvent(l -> l.inputEntered(str));
}
}
});
@ -106,17 +105,8 @@ public class ChatConsole {
}
}
public interface ChatInputListener {
void inputEntered(String input);
}
private List<ChatInputListener> chatInputListenerList = new ArrayList<ChatInputListener>();
public void onInput(ChatInputListener listener) {
chatInputListenerList.add(listener);
}
private void notifyChatInputListeners (String s) {
for (ChatInputListener listener : chatInputListenerList) {
listener.inputEntered(s);
}
chatInputObservable.addListener(listener);
}

View File

@ -0,0 +1,5 @@
package gearth.extensions.extra.harble;
public interface ChatInputListener {
void inputEntered(String input);
}

View File

@ -3,6 +3,7 @@ package gearth.misc;
/**
* Created by Jonas on 14/06/18.
* This interface defines an object which can FULLY be represented as a String (all fields). An object must be able to be recreated having the String representation
* So this is basically "Serializable" but for Strings
*/
public interface StringifyAble {

View File

@ -0,0 +1,36 @@
package gearth.misc.listenerpattern;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Observable<Listener> {
private Consumer<Listener> defaultConsumer = null;
public Observable(Consumer<Listener> defaultConsumer) {
this.defaultConsumer = defaultConsumer;
}
public Observable() {
// no default consumer
this(listener -> {});
}
private List<Listener> listeners = new ArrayList<>();
public void addListener(Listener listener) {
listeners.add(listener);
}
public void removeListener(Listener listener) {
listeners.remove(listener);
}
public void fireEvent(Consumer<Listener> consumer) {
for (int i = listeners.size() - 1; i >= 0; i--) {
consumer.accept(listeners.get(i));
}
}
public void fireEvent() {
fireEvent(defaultConsumer);
}
}

View File

@ -0,0 +1,22 @@
package gearth.misc.listenerpattern;
import java.util.function.Consumer;
public class SynchronizedObservable<Listener> extends Observable<Listener> {
public SynchronizedObservable(Consumer<Listener> defaultConsumer) {
super(defaultConsumer);
}
public SynchronizedObservable() {
super();
}
@Override
public void fireEvent(Consumer<Listener> consumer) {
synchronized (this) {
super.fireEvent(consumer);
}
}
}

View File

@ -2,6 +2,7 @@ package gearth.protocol;
import gearth.misc.Cacher;
import gearth.misc.OSValidator;
import gearth.misc.listenerpattern.Observable;
import gearth.protocol.hostreplacer.hostsfile.HostReplacer;
import gearth.protocol.hostreplacer.hostsfile.HostReplacerFactory;
import gearth.protocol.hostreplacer.ipmapping.IpMapper;
@ -17,9 +18,6 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class HConnection {
@ -122,8 +120,9 @@ public class HConnection {
private static final HostReplacer hostsReplacer = HostReplacerFactory.get();
private volatile boolean hostRedirected = false;
private volatile Object[] trafficListeners = {new ArrayList<TrafficListener>(), new ArrayList<TrafficListener>(), new ArrayList<TrafficListener>()};
private volatile List<StateChangeListener> stateChangeListeners = new ArrayList<>();
private volatile Object[] trafficObservables = {new Observable<TrafficListener>(), new Observable<TrafficListener>(), new Observable<TrafficListener>()};
private volatile Observable<StateChangeListener> stateObservable = new Observable<>();
private volatile State state = State.NOT_CONNECTED;
public static class Proxy {
@ -416,8 +415,8 @@ public class HConnection {
final boolean[] aborted = new boolean[1];
Rc4Obtainer rc4Obtainer = new Rc4Obtainer(this);
OutgoingPacketHandler outgoingHandler = new OutgoingPacketHandler(habbo_server_out, trafficListeners);
IncomingPacketHandler incomingHandler = new IncomingPacketHandler(client_out, trafficListeners);
OutgoingPacketHandler outgoingHandler = new OutgoingPacketHandler(habbo_server_out, trafficObservables);
IncomingPacketHandler incomingHandler = new IncomingPacketHandler(client_out, trafficObservables);
rc4Obtainer.setPacketHandlers(outgoingHandler, incomingHandler);
outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> {
@ -593,13 +592,15 @@ public class HConnection {
State buffer = this.state;
this.state = state;
for (StateChangeListener listener : stateChangeListeners) {
listener.stateChanged(buffer, state);
}
stateObservable.fireEvent(l -> l.stateChanged(buffer, state));
}
}
public Observable<StateChangeListener> getStateObservable() {
return stateObservable;
}
/**
* 3 orders:
* 0 = before modification ¹
@ -609,19 +610,12 @@ public class HConnection {
* ¹don't edit the packet (block, replace)
*/
public void addTrafficListener(int order, TrafficListener listener) {
((List<TrafficListener>)trafficListeners[order]).add(listener);
((Observable<TrafficListener>) trafficObservables[order]).addListener(listener);
}
public void removeTrafficListener(TrafficListener listener) {
((List<TrafficListener>)trafficListeners[0]).remove(listener);
((List<TrafficListener>)trafficListeners[1]).remove(listener);
((List<TrafficListener>)trafficListeners[2]).remove(listener);
}
public void addStateChangeListener(StateChangeListener listener) {
stateChangeListeners.add(listener);
}
public void removeStateChangeListener(StateChangeListener listener) {
stateChangeListeners.remove(listener);
((Observable<TrafficListener>) trafficObservables[0]).removeListener(listener);
((Observable<TrafficListener>) trafficObservables[1]).removeListener(listener);
((Observable<TrafficListener>) trafficObservables[2]).removeListener(listener);
}
public int getServerPort() {

View File

@ -41,11 +41,11 @@ public class Rc4Obtainer {
public void act() {
if (handler.isEncryptedStream()) {
onSendFirstEncryptedMessage(handler);
handler.removeBufferChangeListener(this);
handler.getBufferChangeObservable().removeListener(this);
}
}
};
handler.onBufferChanged(bufferChangeListener);
handler.getBufferChangeObservable().addListener(bufferChangeListener);
}

View File

@ -1,5 +1,6 @@
package gearth.protocol.packethandler;
import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import gearth.protocol.TrafficListener;
@ -10,25 +11,25 @@ import java.util.List;
public class IncomingPacketHandler extends PacketHandler {
public IncomingPacketHandler(OutputStream outputStream, Object[] listeners) {
super(outputStream, listeners);
public IncomingPacketHandler(OutputStream outputStream, Object[] trafficObservables) {
super(outputStream, trafficObservables);
TrafficListener listener = new TrafficListener() {
@Override
public void onCapture(HMessage message) {
if (isDataStream && message.getPacket().structureEquals("s,b")) {
((List<TrafficListener>)listeners[0]).remove(this);
((Observable<TrafficListener>)trafficObservables[0]).removeListener(this);
HPacket packet = message.getPacket();
packet.readString();
isEncryptedStream = packet.readBoolean();
}
else if (message.getIndex() > 3) {
((List<TrafficListener>)listeners[0]).remove(this);
((Observable<TrafficListener>)trafficObservables[0]).removeListener(this);
}
}
};
((List<TrafficListener>)listeners[0]).add(listener);
((Observable<TrafficListener>)trafficObservables[0]).addListener(listener);
}
@Override

View File

@ -10,8 +10,8 @@ import java.util.List;
public class OutgoingPacketHandler extends PacketHandler {
public OutgoingPacketHandler(OutputStream outputStream, Object[] listeners) {
super(outputStream, listeners);
public OutgoingPacketHandler(OutputStream outputStream, Object[] trafficObservables) {
super(outputStream, trafficObservables);
}
private List<OnDatastreamConfirmedListener> onDatastreamConfirmedListeners = new ArrayList<>();

View File

@ -1,5 +1,6 @@
package gearth.protocol.packethandler;
import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
@ -17,7 +18,7 @@ public abstract class PacketHandler {
volatile PayloadBuffer payloadBuffer = new PayloadBuffer();
volatile OutputStream out;
volatile Object[] listeners = null; //get notified on packet send
volatile Object[] trafficObservables; //get notified on packet send
volatile boolean isTempBlocked = false;
volatile boolean isDataStream = false;
volatile int currentIndex = 0;
@ -31,8 +32,8 @@ public abstract class PacketHandler {
protected volatile boolean isEncryptedStream = false;
public PacketHandler(OutputStream outputStream, Object[] listeners) {
this.listeners = listeners;
public PacketHandler(OutputStream outputStream, Object[] trafficObservables) {
this.trafficObservables = trafficObservables;
out = outputStream;
}
@ -47,7 +48,7 @@ public abstract class PacketHandler {
public abstract void act(byte[] buffer) throws IOException;
protected void continuedAct(byte[] buffer) throws IOException {
notifyBufferListeners();
bufferChangeObservable.fireEvent();
if (!isEncryptedStream) {
payloadBuffer.push(buffer);
@ -111,10 +112,10 @@ public abstract class PacketHandler {
*/
void notifyListeners(HMessage message) {
for (int x = 0; x < 3; x++) {
for (int i = ((List<TrafficListener>)listeners[x]).size() - 1; i >= 0; i--) {
((Observable<TrafficListener>) trafficObservables[x]).fireEvent(trafficListener -> {
message.getPacket().resetReadIndex();
((List<TrafficListener>)listeners[x]).get(i).onCapture(message);
}
trafficListener.onCapture(message);
});
}
message.getPacket().resetReadIndex();
}
@ -164,17 +165,9 @@ public abstract class PacketHandler {
protected abstract void printForDebugging(byte[] bytes);
private List<BufferChangeListener> bufferChangeListeners = new ArrayList<>();
public void onBufferChanged(BufferChangeListener listener) {
bufferChangeListeners.add(listener);
}
public void removeBufferChangeListener(BufferChangeListener listener) {
bufferChangeListeners.remove(listener);
}
void notifyBufferListeners() {
for (int i = bufferChangeListeners.size() - 1; i >= 0; i -= 1) {
bufferChangeListeners.get(i).act();
}
private Observable<BufferChangeListener> bufferChangeObservable = new Observable<>(BufferChangeListener::act);
public Observable<BufferChangeListener> getBufferChangeObservable() {
return bufferChangeObservable;
}
public int getCurrentIndex() {

View File

@ -0,0 +1,7 @@
package gearth.services.extensionhandler;
import gearth.services.extensionhandler.extensions.GEarthExtension;
public interface ExtensionConnectedListener {
void onExtensionConnect(GEarthExtension e);
}

View File

@ -2,6 +2,7 @@ package gearth.services.extensionhandler;
import gearth.Main;
import gearth.misc.harble_api.HarbleAPIFetcher;
import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HConnection;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
@ -12,12 +13,22 @@ import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionP
import gearth.services.extensionhandler.extensions.GEarthExtension;
import java.util.*;
import java.util.function.Consumer;
public class ExtensionHandler {
private final List<GEarthExtension> gEarthExtensions = new ArrayList<>();
private final HConnection hConnection;
private List<ExtensionProducer> extensionProducers;
private Observable<ExtensionConnectedListener> observable = new Observable<>() {
@Override
public void addListener(ExtensionConnectedListener extensionConnectedListener) {
super.addListener(extensionConnectedListener);
for (GEarthExtension gEarthExtension : gEarthExtensions) {
extensionConnectedListener.onExtensionConnect(gEarthExtension);
}
}
};
public ExtensionHandler(HConnection hConnection) {
@ -27,7 +38,7 @@ public class ExtensionHandler {
private void initialize() {
hConnection.addStateChangeListener((oldState, newState) -> {
hConnection.getStateObservable().addListener((oldState, newState) -> {
if (newState == HConnection.State.CONNECTED) {
HarbleAPIFetcher.fetch(hConnection.getHotelVersion());
synchronized (gEarthExtensions) {
@ -76,12 +87,12 @@ public class ExtensionHandler {
collection.remove(extension);
}
extension.removeExtensionListener(this);
extension.getExtensionObservable().removeListener(this);
}
}
}
};
extension.registerExtensionListener(respondCallback);
extension.getExtensionObservable().addListener(respondCallback);
}
}
@ -134,7 +145,7 @@ public class ExtensionHandler {
private void initializeExtensionProducer(ExtensionProducer producer) {
producer.startProducing(new ExtensionProducerObserver() {
@Override
public void onExtensionConnect(GEarthExtension extension) {
public void onExtensionProduced(GEarthExtension extension) {
synchronized (gEarthExtensions) {
gEarthExtensions.add(extension);
}
@ -167,12 +178,12 @@ public class ExtensionHandler {
synchronized (gEarthExtensions) {
gEarthExtensions.remove(extension);
}
extension.removeExtensionListener(this);
extension.delete();
extension.getExtensionObservable().removeListener(this);
extension.getDeletedObservable().fireEvent();
}
};
extension.registerExtensionListener(listener);
extension.getExtensionObservable().addListener(listener);
extension.init();
if (hConnection.getState() == HConnection.State.CONNECTED) {
@ -184,10 +195,10 @@ public class ExtensionHandler {
);
}
extension.onRemoveClick(observable -> extension.close());
extension.onClick(observable -> extension.doubleclick());
extension.getRemoveClickObservable().addListener(extension::close);
extension.getClickedObservable().addListener(extension::doubleclick);
notifyExtensionConnectListeners(extension);
observable.fireEvent(l -> l.onExtensionConnect(extension));
}
});
}
@ -197,24 +208,9 @@ public class ExtensionHandler {
return extensionProducers;
}
public interface ExtensionConnectListener {
void extensionConnected(GEarthExtension e);
}
private List<ExtensionConnectListener> listeners = new ArrayList<>();
public void onExtensionConnected(ExtensionConnectListener extensionConnectListener) {
synchronized (gEarthExtensions) {
for (GEarthExtension gEarthExtension : gEarthExtensions) {
extensionConnectListener.extensionConnected(gEarthExtension);
}
}
listeners.add(extensionConnectListener);
}
public void removeExtensionConnectListener(ExtensionConnectListener extensionConnectListener) {
listeners.remove(extensionConnectListener);
}
private void notifyExtensionConnectListeners(GEarthExtension extension) {
for (int i = listeners.size() - 1; i >= 0; i--) {
listeners.get(i).extensionConnected(extension);
}
public Observable<ExtensionConnectedListener> getObservable() {
return observable;
}
}

View File

@ -1,12 +1,12 @@
package gearth.services.extensionhandler.extensions;
import gearth.misc.listenerpattern.Observable;
import gearth.misc.listenerpattern.SynchronizedObservable;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import javafx.beans.InvalidationListener;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import gearth.services.extensionhandler.extensions.listeners.OmRemoveClickListener;
import gearth.services.extensionhandler.extensions.listeners.OnClickListener;
import gearth.services.extensionhandler.extensions.listeners.OnDeleteListener;
public abstract class GEarthExtension {
@ -47,45 +47,36 @@ public abstract class GEarthExtension {
// ----------------- listen to the extension ---------------------
protected final List<ExtensionListener> extensionListeners = new ArrayList<>();
public void registerExtensionListener(ExtensionListener listener) {
this.extensionListeners.add(listener);
}
public void removeExtensionListener(ExtensionListener listener) {
this.extensionListeners.remove(listener);
}
private void notifyListeners(Consumer<ExtensionListener> consumer) {
for (int i = extensionListeners.size() - 1; i >= 0; i--) {
consumer.accept(extensionListeners.get(i));
}
extensionListeners.forEach(consumer);
protected final Observable<ExtensionListener> extensionObservable = new Observable<>();
public Observable<ExtensionListener> getExtensionObservable() {
return extensionObservable;
}
protected void sendManipulatedPacket(HMessage hMessage) {
int orgIndex = hMessage.getPacket().getReadIndex();
notifyListeners(listener -> {
extensionObservable.fireEvent(listener -> {
hMessage.getPacket().setReadIndex(6);
listener.manipulatedPacket(hMessage);
});
hMessage.getPacket().setReadIndex(orgIndex);
}
protected void requestFlags() {
notifyListeners(ExtensionListener::flagsRequest);
extensionObservable.fireEvent(ExtensionListener::flagsRequest);
}
protected void sendMessage(HMessage.Direction direction, HPacket packet) {
int orgIndex = packet.getReadIndex();
notifyListeners(listener -> {
extensionObservable.fireEvent(listener -> {
packet.setReadIndex(6);
listener.sendMessage(direction, packet);
});
packet.setReadIndex(orgIndex);
}
protected void log(String text) {
notifyListeners(listener -> listener.log(text));
extensionObservable.fireEvent(listener -> listener.log(text));
}
protected void hasClosed() {
notifyListeners(ExtensionListener::hasClosed);
extensionObservable.fireEvent(ExtensionListener::hasClosed);
}
// --------------------------------------------------------------------
@ -97,46 +88,19 @@ public abstract class GEarthExtension {
// ----------- methods for interaction with G-Earth UI, don't use/change them ----------------
private final List<InvalidationListener> onRemoveClickListener = new ArrayList<>();
public void onRemoveClick(InvalidationListener listener) {
synchronized (onRemoveClickListener) {
onRemoveClickListener.add(listener);
}
}
public void isRemoveClickTrigger() {
synchronized (onRemoveClickListener) {
for (int i = onRemoveClickListener.size() - 1; i >= 0; i--) {
onRemoveClickListener.get(i).invalidated(null);
}
}
private final Observable<OmRemoveClickListener> removeClickObservable = new SynchronizedObservable<>(OmRemoveClickListener::onRemove);
public Observable<OmRemoveClickListener> getRemoveClickObservable() {
return removeClickObservable;
}
private final List<InvalidationListener> onClickListener = new ArrayList<>();
public void onClick(InvalidationListener listener) {
synchronized (onClickListener) {
onClickListener.add(listener);
}
}
public void isClickTrigger() {
synchronized (onClickListener) {
for (int i = onClickListener.size() - 1; i >= 0; i--) {
onClickListener.get(i).invalidated(null);
}
}
private final Observable<OnClickListener> clickedObservable = new SynchronizedObservable<>(OnClickListener::onClick);
public Observable<OnClickListener> getClickedObservable() {
return clickedObservable;
}
private final List<InvalidationListener> onDeleteListeners = new ArrayList<>();
public void onDelete(InvalidationListener listener) {
synchronized (onDeleteListeners) {
onDeleteListeners.add(listener);
}
}
public void delete() {
synchronized (onDeleteListeners) {
for (int i = onDeleteListeners.size() - 1; i >= 0; i--) {
onDeleteListeners.get(i).invalidated(null);
}
}
private final Observable<OnDeleteListener> deletedObservable = new SynchronizedObservable<>(OnDeleteListener::onDelete);
public Observable<OnDeleteListener> getDeletedObservable() {
return deletedObservable;
}
// ----------------------------------------------------------------------------------------

View File

@ -1,7 +1,7 @@
package gearth.services.extensionhandler.extensions.extensionproducers;
import gearth.services.extensionhandler.extensions.network.NetworkExtensionsProducer;
import gearth.services.extensionhandler.extensions.simple.SimpleExtensionProducer;
import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtensionsProducer;
import gearth.services.extensionhandler.extensions.implementations.simple.SimpleExtensionProducer;
import java.util.Arrays;
import java.util.List;

View File

@ -3,5 +3,5 @@ package gearth.services.extensionhandler.extensions.extensionproducers;
import gearth.services.extensionhandler.extensions.GEarthExtension;
public interface ExtensionProducerObserver {
void onExtensionConnect(GEarthExtension extension);
void onExtensionProduced(GEarthExtension extension);
}

View File

@ -1,4 +1,4 @@
package gearth.services.extensionhandler.extensions.network;
package gearth.services.extensionhandler.extensions.implementations.network;
import gearth.protocol.HMessage;
import gearth.services.extensionhandler.extensions.GEarthExtension;
@ -63,7 +63,7 @@ public class NetworkExtension extends GEarthExtension {
HPacket message = new HPacket(headerandbody);
message.fixLength();
synchronized (selff.extensionListeners) {
synchronized (selff.extensionObservable) {
if (message.headerId() == NetworkExtensionInfo.INCOMING_MESSAGES_IDS.REQUESTFLAGS) {
requestFlags();
}
@ -96,7 +96,7 @@ public class NetworkExtension extends GEarthExtension {
} catch (IOException e) {
// An extension disconnected, which is OK
} finally {
synchronized (selff.extensionListeners) {
synchronized (selff.extensionObservable) {
hasClosed();
}
if (!connection.isClosed()) {

View File

@ -1,4 +1,4 @@
package gearth.services.extensionhandler.extensions.network;
package gearth.services.extensionhandler.extensions.implementations.network;
public class NetworkExtensionInfo {

View File

@ -1,9 +1,9 @@
package gearth.services.extensionhandler.extensions.network;
package gearth.services.extensionhandler.extensions.implementations.network;
import gearth.protocol.HPacket;
import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducer;
import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducerObserver;
import gearth.services.extensionhandler.extensions.network.authentication.Authenticator;
import gearth.services.extensionhandler.extensions.implementations.network.authentication.Authenticator;
import java.io.DataInputStream;
import java.io.IOException;
@ -63,7 +63,7 @@ public class NetworkExtensionsProducer implements ExtensionProducer {
);
if (Authenticator.evaluate(gEarthExtension)) {
observer.onExtensionConnect(gEarthExtension);
observer.onExtensionProduced(gEarthExtension);
}
else {
gEarthExtension.close(); //you shall not pass...

View File

@ -1,7 +1,7 @@
package gearth.services.extensionhandler.extensions.network.authentication;
package gearth.services.extensionhandler.extensions.implementations.network.authentication;
import gearth.misc.ConfirmationDialog;
import gearth.services.extensionhandler.extensions.network.NetworkExtension;
import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtension;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;

View File

@ -1,4 +1,4 @@
package gearth.services.extensionhandler.extensions.network.executer;
package gearth.services.extensionhandler.extensions.implementations.network.executer;
import java.util.ArrayList;
import java.util.HashMap;

View File

@ -1,4 +1,4 @@
package gearth.services.extensionhandler.extensions.network.executer;
package gearth.services.extensionhandler.extensions.implementations.network.executer;
/**
* Created by Jonas on 21/09/18.

View File

@ -1,4 +1,4 @@
package gearth.services.extensionhandler.extensions.network.executer;
package gearth.services.extensionhandler.extensions.implementations.network.executer;
/**
* Created by Jonas on 22/09/18.

View File

@ -1,7 +1,7 @@
package gearth.services.extensionhandler.extensions.network.executer;
package gearth.services.extensionhandler.extensions.implementations.network.executer;
import gearth.Main;
import gearth.services.extensionhandler.extensions.network.authentication.Authenticator;
import gearth.services.extensionhandler.extensions.implementations.network.authentication.Authenticator;
import java.io.BufferedReader;
import java.io.File;

View File

@ -1,4 +1,4 @@
package gearth.services.extensionhandler.extensions.simple;
package gearth.services.extensionhandler.extensions.implementations.simple;
import gearth.protocol.HMessage;
import gearth.services.extensionhandler.extensions.GEarthExtension;

View File

@ -1,4 +1,4 @@
package gearth.services.extensionhandler.extensions.simple;
package gearth.services.extensionhandler.extensions.implementations.simple;
import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducer;
import gearth.services.extensionhandler.extensions.extensionproducers.ExtensionProducerObserver;

View File

@ -0,0 +1,5 @@
package gearth.services.extensionhandler.extensions.listeners;
public interface OmRemoveClickListener {
void onRemove();
}

View File

@ -0,0 +1,5 @@
package gearth.services.extensionhandler.extensions.listeners;
public interface OnClickListener {
void onClick();
}

View File

@ -0,0 +1,5 @@
package gearth.services.extensionhandler.extensions.listeners;
public interface OnDeleteListener {
void onDelete();
}

View File

@ -73,7 +73,7 @@ public class Connection extends SubForm {
}
public void onParentSet(){
getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(() -> {
getHConnection().getStateObservable().addListener((oldState, newState) -> Platform.runLater(() -> {
txtfield_hotelversion.setText(getHConnection().getHotelVersion());
Platform.runLater(() -> {
if (newState == HConnection.State.NOT_CONNECTED) {

View File

@ -12,10 +12,10 @@ import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import gearth.misc.ConfirmationDialog;
import gearth.ui.buttons.*;
import gearth.services.extensionhandler.extensions.network.executer.ExecutionInfo;
import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunner;
import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunnerFactory;
import gearth.services.extensionhandler.extensions.network.executer.NormalExtensionRunner;
import gearth.services.extensionhandler.extensions.implementations.network.executer.ExecutionInfo;
import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunner;
import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunnerFactory;
import gearth.services.extensionhandler.extensions.implementations.network.executer.NormalExtensionRunner;
import java.nio.file.Paths;
@ -161,14 +161,14 @@ public class ExtensionItemContainer extends GridPane {
exitButton.removeEventHandler(MouseEvent.MOUSE_CLICKED, onExit);
clickButton.removeEventHandler(MouseEvent.MOUSE_CLICKED, onClick);
}
onExit = event -> item.isRemoveClickTrigger();
onClick = event -> item.isClickTrigger();
onExit = event -> item.getRemoveClickObservable().fireEvent();
onClick = event -> item.getClickedObservable().fireEvent();
exitButton.addEventHandler(MouseEvent.MOUSE_CLICKED, onExit);
clickButton.addEventHandler(MouseEvent.MOUSE_CLICKED, onClick);
ExtensionItemContainer this2 = this;
item.onDelete(observable -> Platform.runLater(() -> {
item.getDeletedObservable().addListener(() -> Platform.runLater(() -> {
if (item.isInstalledExtension()) {
setBackground(new Background(new BackgroundFill(Paint.valueOf("#cccccc"),null, null)));
getChildren().remove(buttonsBox);

View File

@ -1,10 +1,10 @@
package gearth.ui.extensions;
import gearth.services.extensionhandler.ExtensionHandler;
import gearth.services.extensionhandler.extensions.network.NetworkExtensionsProducer;
import gearth.services.extensionhandler.extensions.network.executer.ExecutionInfo;
import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunner;
import gearth.services.extensionhandler.extensions.network.executer.ExtensionRunnerFactory;
import gearth.services.extensionhandler.extensions.implementations.network.NetworkExtensionsProducer;
import gearth.services.extensionhandler.extensions.implementations.network.executer.ExecutionInfo;
import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunner;
import gearth.services.extensionhandler.extensions.implementations.network.executer.ExtensionRunnerFactory;
import gearth.ui.SubForm;
import javafx.application.Platform;
import javafx.event.ActionEvent;
@ -43,7 +43,7 @@ public class Extensions extends SubForm {
protected void onParentSet() {
ExtensionItemContainerProducer producer = new ExtensionItemContainerProducer(extensioncontainer, scroller);
extensionHandler = new ExtensionHandler(getHConnection());
extensionHandler.onExtensionConnected((e -> {
extensionHandler.getObservable().addListener((e -> {
Platform.runLater(() -> producer.extensionConnected(e));
}));

View File

@ -59,7 +59,7 @@ public class Extra extends SubForm implements ConnectionInfoOverrider {
cbx_alwaysOnTop.selectedProperty().addListener(observable -> parentController.getStage().setAlwaysOnTop(cbx_alwaysOnTop.isSelected()));
cbx_advanced.selectedProperty().addListener(observable -> updateAdvancedUI());
getHConnection().addStateChangeListener((oldState, newState) -> {
getHConnection().getStateObservable().addListener((oldState, newState) -> {
if (oldState == HConnection.State.NOT_CONNECTED || newState == HConnection.State.NOT_CONNECTED) {
updateAdvancedUI();
}

View File

@ -21,7 +21,7 @@ public class Injection extends SubForm {
public Button btn_sendToClient;
protected void onParentSet() {
getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(this::updateUI));
getHConnection().getStateObservable().addListener((oldState, newState) -> Platform.runLater(this::updateUI));
inputPacket.textProperty().addListener(event -> Platform.runLater(this::updateUI));
}

View File

@ -35,7 +35,7 @@ public class Logger extends SubForm {
private PacketLogger packetLogger = PacketLoggerFactory.get();
public void onParentSet(){
getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(() -> {
getHConnection().getStateObservable().addListener((oldState, newState) -> Platform.runLater(() -> {
if (newState == HConnection.State.PREPARING) {
miniLogText(Color.ORANGE, "Connecting to "+getHConnection().getDomain() + ":" + getHConnection().getServerPort());
}

View File

@ -1,5 +1,10 @@
package gearth.ui.scheduler;
import gearth.misc.listenerpattern.Observable;
import gearth.ui.scheduler.listeners.OnBeingUpdatedListener;
import gearth.ui.scheduler.listeners.OnDeleteListener;
import gearth.ui.scheduler.listeners.OnEditListener;
import gearth.ui.scheduler.listeners.OnUpdatedListener;
import javafx.beans.InvalidationListener;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
@ -16,11 +21,11 @@ import java.util.List;
*/
public class ScheduleItem implements StringifyAble {
private SimpleIntegerProperty indexProperty = null;
private SimpleBooleanProperty pausedProperty = null;
private SimpleObjectProperty<Interval> delayProperty = null;
private SimpleObjectProperty<HPacket> packetProperty = null;
private SimpleObjectProperty<HMessage.Direction> destinationProperty = null;
private SimpleIntegerProperty indexProperty;
private SimpleBooleanProperty pausedProperty;
private SimpleObjectProperty<Interval> delayProperty;
private SimpleObjectProperty<HPacket> packetProperty;
private SimpleObjectProperty<HMessage.Direction> destinationProperty;
ScheduleItem (int index, boolean paused, Interval delay, HPacket packet, HMessage.Direction destination) {
construct(index, paused, delay, packet, destination);
@ -59,44 +64,36 @@ public class ScheduleItem implements StringifyAble {
}
private List<InvalidationListener> onDeleteListeners = new ArrayList<>();
public void onDelete(InvalidationListener listener) {
onDeleteListeners.add(listener);
private Observable<OnDeleteListener> onDeleteObservable = new Observable<>(OnDeleteListener::onDelete);
public void onDelete(OnDeleteListener listener) {
onDeleteObservable.addListener(listener);
}
public void delete() {
for (int i = onDeleteListeners.size() - 1; i >= 0; i--) {
onDeleteListeners.get(i).invalidated(null);
}
onDeleteObservable.fireEvent();
}
private List<InvalidationListener> onEditListeners = new ArrayList<>();
public void onEdit(InvalidationListener listener) {
onEditListeners.add(listener);
private Observable<OnEditListener> onEditObservable = new Observable<>(OnEditListener::onEdit);
public void onEdit(OnEditListener listener) {
onEditObservable.addListener(listener);
}
public void edit() {
for (int i = onEditListeners.size() - 1; i >= 0; i--) {
onEditListeners.get(i).invalidated(null);
}
onEditObservable.fireEvent();
}
private List<InvalidationListener> onIsupdatedListeners = new ArrayList<>();
public void onIsupdated(InvalidationListener listener) {
onIsupdatedListeners.add(listener);
private Observable<OnUpdatedListener> onUpdatedObservable = new Observable<>(OnUpdatedListener::onUpdated);
public void onIsupdated(OnUpdatedListener listener) {
onUpdatedObservable.addListener(listener);
}
public void isUpdatedTrigger() {
for (int i = onIsupdatedListeners.size() - 1; i >= 0; i--) {
onIsupdatedListeners.get(i).invalidated(null);
}
onUpdatedObservable.fireEvent();
}
private List<InvalidationListener> OnIsBeingUpdatedListeners = new ArrayList<>();
public void onIsBeingUpdated(InvalidationListener listener) {
OnIsBeingUpdatedListeners.add(listener);
private Observable<OnBeingUpdatedListener> onBeingUpdatedObservable = new Observable<>(OnBeingUpdatedListener::onBeingUpdated);
public void onIsBeingUpdated(OnBeingUpdatedListener listener) {
onBeingUpdatedObservable.addListener(listener);
}
public void onIsBeingUpdatedTrigger() {
for (int i = OnIsBeingUpdatedListeners.size() - 1; i >= 0; i--) {
OnIsBeingUpdatedListeners.get(i).invalidated(null);
}
onBeingUpdatedObservable.fireEvent();
}
@Override

View File

@ -87,9 +87,9 @@ public class ScheduleItemContainer extends GridPane {
GridPane this2 = this;
item.onDelete(observable -> parent.getChildren().remove(this2));
item.onIsBeingUpdated(observable -> setStyle("-fx-background-color: #faebcc;"));
item.onIsupdated(observable -> setStyle("-fx-background-color: #ffffff;"));
item.onDelete(() -> parent.getChildren().remove(this2));
item.onIsBeingUpdated(() -> setStyle("-fx-background-color: #faebcc;"));
item.onIsupdated(() -> setStyle("-fx-background-color: #ffffff;"));
}
private Label initNewLabelColumn(String text) {

View File

@ -173,7 +173,7 @@ public class Scheduler extends SubForm {
scheduleItemList.add(newItem);
newItem.onDelete(observable -> {
newItem.onDelete(() -> {
if (isBeingEdited == newItem) {
setInputDefault();
isBeingEdited = null;
@ -183,7 +183,7 @@ public class Scheduler extends SubForm {
scheduleItemList.get(i).getIndexProperty().set(i);
}
});
newItem.onEdit(observable -> {
newItem.onEdit(() -> {
if (isBeingEdited != null) {
isBeingEdited.isUpdatedTrigger();
}
@ -195,7 +195,7 @@ public class Scheduler extends SubForm {
rb_outgoing.setSelected(newItem.getDestinationProperty().get() == HMessage.Direction.TOSERVER);
isBeingEdited = newItem;
btn_addoredit.setText("Edit schedule item"); //Add to scheduler
btn_addoredit.setText("Edit");
updateUI();
newItem.onIsBeingUpdatedTrigger();
}
@ -213,7 +213,7 @@ public class Scheduler extends SubForm {
rb_incoming.setSelected(true);
rb_outgoing.setSelected(false);
btn_addoredit.setText("Add to scheduler");
btn_addoredit.setText("Add");
updateUI();
}

View File

@ -0,0 +1,5 @@
package gearth.ui.scheduler.listeners;
public interface OnBeingUpdatedListener {
void onBeingUpdated();
}

View File

@ -0,0 +1,5 @@
package gearth.ui.scheduler.listeners;
public interface OnDeleteListener {
void onDelete();
}

View File

@ -0,0 +1,5 @@
package gearth.ui.scheduler.listeners;
public interface OnEditListener {
void onEdit();
}

View File

@ -0,0 +1,5 @@
package gearth.ui.scheduler.listeners;
public interface OnUpdatedListener {
void onUpdated();
}