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