mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2025-01-19 00:26:27 +01:00
fix concurrency errors and another bug
This commit is contained in:
parent
5b11349133
commit
df87eb339d
@ -43,4 +43,13 @@ public class Main extends Application {
|
||||
Main.args = args;
|
||||
launch(args);
|
||||
}
|
||||
|
||||
public static boolean hasFlag(String flag) {
|
||||
for(String s : args) {
|
||||
if (s.equals(flag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,10 @@ public class HMessage implements StringifyAble {
|
||||
constructFromString(fromString);
|
||||
}
|
||||
|
||||
public HMessage(HMessage message) {
|
||||
constructFromHMessage(message);
|
||||
}
|
||||
|
||||
public HMessage(HPacket packet, Side side, int index) {
|
||||
this.side = side;
|
||||
this.hPacket = packet;
|
||||
@ -66,6 +70,13 @@ public class HMessage implements StringifyAble {
|
||||
this.hPacket = p;
|
||||
}
|
||||
|
||||
public void constructFromHMessage(HMessage message) {
|
||||
this.isBlocked = message.isBlocked();
|
||||
this.index = message.getIndex();
|
||||
this.side = message.getDestination();
|
||||
this.hPacket = new HPacket(message.getPacket());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof HMessage)) return false;
|
||||
|
@ -17,6 +17,10 @@ public class HPacket implements StringifyAble {
|
||||
public HPacket(byte[] packet) {
|
||||
packetInBytes = packet.clone();
|
||||
}
|
||||
public HPacket(HPacket packet) {
|
||||
packetInBytes = packet.packetInBytes.clone();
|
||||
isEdited = packet.isEdited;
|
||||
}
|
||||
public HPacket(String packet) {
|
||||
packetInBytes = fromStringToBytes(fromExpressionToString(packet));
|
||||
}
|
||||
|
@ -169,48 +169,43 @@ public class Extensions extends SubForm {
|
||||
collection = new HashSet<>(gEarthExtensions);
|
||||
}
|
||||
|
||||
|
||||
String stringified = message.stringify();
|
||||
HPacket manipulatePacketRequest = new HPacket(OUTGOING_MESSAGES_IDS.PACKETINTERCEPT);
|
||||
manipulatePacketRequest.appendLongString(stringified);
|
||||
|
||||
HMessage result = new HMessage(message);
|
||||
|
||||
boolean[] isblock = new boolean[1];
|
||||
|
||||
Iterator<GEarthExtension> it;
|
||||
synchronized (collection) {
|
||||
it = collection.iterator();
|
||||
}
|
||||
while (true) {
|
||||
GEarthExtension extension;
|
||||
synchronized (collection) {
|
||||
if (!it.hasNext()) break;
|
||||
extension = it.next();
|
||||
}
|
||||
for (GEarthExtension extension : collection) {
|
||||
GEarthExtension.ReceiveMessageListener respondCallback = new GEarthExtension.ReceiveMessageListener() {
|
||||
@Override
|
||||
public void act(HPacket packet) {
|
||||
if (packet.headerId() == INCOMING_MESSAGES_IDS.MANIPULATEDPACKET) {
|
||||
String stringifiedresponse = packet.readLongString(6);
|
||||
HMessage responseMessage = new HMessage(stringifiedresponse);
|
||||
if (responseMessage.getDestination() == message.getDestination() && responseMessage.getIndex() == message.getIndex()) {
|
||||
synchronized (result) {
|
||||
if (!message.equals(responseMessage)) {
|
||||
result.constructFromString(stringifiedresponse);
|
||||
}
|
||||
if (responseMessage.isBlocked()) {
|
||||
isblock[0] = true;
|
||||
}
|
||||
synchronized (collection) {
|
||||
collection.remove(extension);
|
||||
}
|
||||
|
||||
GEarthExtension.ReceiveMessageListener respondCallback = new GEarthExtension.ReceiveMessageListener() {
|
||||
@Override
|
||||
public void act(HPacket packet) {
|
||||
if (packet.headerId() == INCOMING_MESSAGES_IDS.MANIPULATEDPACKET) {
|
||||
String stringifiedresponse = packet.readLongString(6);
|
||||
HMessage responseMessage = new HMessage(stringifiedresponse);
|
||||
if (responseMessage.getDestination() == message.getDestination() && responseMessage.getIndex() == message.getIndex()) {
|
||||
if (!message.equals(responseMessage)) {
|
||||
message.constructFromString(stringifiedresponse);
|
||||
extension.removeOnReceiveMessageListener(this);
|
||||
}
|
||||
}
|
||||
if (responseMessage.isBlocked()) {
|
||||
isblock[0] = true;
|
||||
}
|
||||
synchronized (collection) {
|
||||
collection.remove(extension);
|
||||
}
|
||||
|
||||
extension.removeOnReceiveMessageListener(this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
extension.addOnReceiveMessageListener(respondCallback);
|
||||
extension.sendMessage(manipulatePacketRequest);
|
||||
};
|
||||
extension.addOnReceiveMessageListener(respondCallback);
|
||||
extension.sendMessage(manipulatePacketRequest);
|
||||
}
|
||||
}
|
||||
|
||||
//block untill all extensions have responded
|
||||
@ -221,22 +216,24 @@ public class Extensions extends SubForm {
|
||||
break;
|
||||
}
|
||||
|
||||
for (GEarthExtension extension : collection) {
|
||||
synchronized (gEarthExtensions) {
|
||||
synchronized (gEarthExtensions) {
|
||||
for (GEarthExtension extension : collection) {
|
||||
if (!gEarthExtensions.contains(extension)) willdelete.add(extension);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = willdelete.size() - 1; i >= 0; i--) {
|
||||
synchronized (collection) {
|
||||
collection.remove(willdelete.get(i));
|
||||
}
|
||||
collection.remove(willdelete.get(i));
|
||||
willdelete.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
|
||||
}
|
||||
|
||||
message.constructFromHMessage(result);
|
||||
|
||||
if (isblock[0]) {
|
||||
message.setBlocked(true);
|
||||
}
|
||||
@ -286,7 +283,7 @@ public class Extensions extends SubForm {
|
||||
if (getHConnection().getState() == HConnection.State.CONNECTED) {
|
||||
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART));
|
||||
}
|
||||
Platform.runLater(() -> producer.extensionConnected(extension));
|
||||
|
||||
extension.onRemoveClick(observable -> {
|
||||
try {
|
||||
extension.getConnection().close();
|
||||
@ -295,6 +292,8 @@ public class Extensions extends SubForm {
|
||||
}
|
||||
});
|
||||
extension.onClick(observable -> extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.ONDOUBLECLICK)));
|
||||
|
||||
Platform.runLater(() -> producer.extensionConnected(extension));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -103,9 +103,11 @@ public class GEarthExtension {
|
||||
HPacket packet = new HPacket(headerandbody);
|
||||
packet.fixLength();
|
||||
|
||||
for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) {
|
||||
receiveMessageListeners.get(i).act(packet);
|
||||
packet.setReadIndex(6);
|
||||
synchronized (receiveMessageListeners) {
|
||||
for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) {
|
||||
receiveMessageListeners.get(i).act(packet);
|
||||
packet.setReadIndex(6);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -181,12 +183,16 @@ public class GEarthExtension {
|
||||
}
|
||||
|
||||
|
||||
private List<ReceiveMessageListener> receiveMessageListeners = new ArrayList<>();
|
||||
private final List<ReceiveMessageListener> receiveMessageListeners = new ArrayList<>();
|
||||
public void addOnReceiveMessageListener(ReceiveMessageListener receiveMessageListener) {
|
||||
receiveMessageListeners.add(receiveMessageListener);
|
||||
synchronized (receiveMessageListeners) {
|
||||
receiveMessageListeners.add(receiveMessageListener);
|
||||
}
|
||||
}
|
||||
public void removeOnReceiveMessageListener(ReceiveMessageListener receiveMessageListener) {
|
||||
receiveMessageListeners.remove(receiveMessageListener);
|
||||
synchronized (receiveMessageListeners) {
|
||||
receiveMessageListeners.remove(receiveMessageListener);
|
||||
}
|
||||
}
|
||||
|
||||
public interface ReceiveMessageListener {
|
||||
@ -200,33 +206,45 @@ public class GEarthExtension {
|
||||
}
|
||||
|
||||
|
||||
private List<InvalidationListener> onRemoveClickListener = new ArrayList<>();
|
||||
private final List<InvalidationListener> onRemoveClickListener = new ArrayList<>();
|
||||
public void onRemoveClick(InvalidationListener listener) {
|
||||
onRemoveClickListener.add(listener);
|
||||
synchronized (onRemoveClickListener) {
|
||||
onRemoveClickListener.add(listener);
|
||||
}
|
||||
}
|
||||
public void isRemoveClickTrigger() {
|
||||
for (int i = onRemoveClickListener.size() - 1; i >= 0; i--) {
|
||||
onRemoveClickListener.get(i).invalidated(null);
|
||||
synchronized (onRemoveClickListener) {
|
||||
for (int i = onRemoveClickListener.size() - 1; i >= 0; i--) {
|
||||
onRemoveClickListener.get(i).invalidated(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<InvalidationListener> onClickListener = new ArrayList<>();
|
||||
private final List<InvalidationListener> onClickListener = new ArrayList<>();
|
||||
public void onClick(InvalidationListener listener) {
|
||||
onClickListener.add(listener);
|
||||
synchronized (onClickListener) {
|
||||
onClickListener.add(listener);
|
||||
}
|
||||
}
|
||||
public void isClickTrigger() {
|
||||
for (int i = onClickListener.size() - 1; i >= 0; i--) {
|
||||
onClickListener.get(i).invalidated(null);
|
||||
synchronized (onClickListener) {
|
||||
for (int i = onClickListener.size() - 1; i >= 0; i--) {
|
||||
onClickListener.get(i).invalidated(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<InvalidationListener> onDeleteListeners = new ArrayList<>();
|
||||
private final List<InvalidationListener> onDeleteListeners = new ArrayList<>();
|
||||
public void onDelete(InvalidationListener listener) {
|
||||
onDeleteListeners.add(listener);
|
||||
synchronized (onDeleteListeners) {
|
||||
onDeleteListeners.add(listener);
|
||||
}
|
||||
}
|
||||
public void delete() {
|
||||
for (int i = onDeleteListeners.size() - 1; i >= 0; i--) {
|
||||
onDeleteListeners.get(i).invalidated(null);
|
||||
synchronized (onDeleteListeners) {
|
||||
for (int i = onDeleteListeners.size() - 1; i >= 0; i--) {
|
||||
onDeleteListeners.get(i).invalidated(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package gearth.ui.logger.loggerdisplays;
|
||||
|
||||
import gearth.Main;
|
||||
import gearth.misc.OSValidator;
|
||||
|
||||
/**
|
||||
@ -8,13 +9,9 @@ import gearth.misc.OSValidator;
|
||||
public class PacketLoggerFactory {
|
||||
|
||||
public static PacketLogger get() {
|
||||
// if (OSValidator.isUnix()) {
|
||||
// return new LinuxTerminalLogger();
|
||||
// }
|
||||
// if (System.getenv("XDG_CURRENT_DESKTOP") != null && System.getenv("XDG_CURRENT_DESKTOP").toLowerCase().contains("gnome")) {
|
||||
// return new GnomeTerminalLogger();
|
||||
// }
|
||||
// return new SimpleTerminalLogger();
|
||||
if (OSValidator.isUnix() && Main.hasFlag("-t")) {
|
||||
return new LinuxTerminalLogger();
|
||||
}
|
||||
|
||||
return new UiLogger();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user