Skip to content

Commit

Permalink
improve earthquakes synchronisation
Browse files Browse the repository at this point in the history
  • Loading branch information
xspanger3770 committed Oct 23, 2023
1 parent 3a37efe commit b99af8f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

public record EarthquakeInfo(UUID uuid, int revisionID) implements Serializable {

public static final int REMOVED = -1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gqserver.api.packets.earthquake;

import gqserver.api.Packet;

import java.util.UUID;

public class EarthquakeRequestPacket extends Packet {

private final UUID uuid;

public EarthquakeRequestPacket(UUID uuid){
this.uuid = uuid;
}

public UUID getUuid() {
return uuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gqserver.api.Packet;
import gqserver.api.ServerClient;
import gqserver.api.exception.UnknownPacketException;
import gqserver.core.GlobalQuakeServer;
import org.tinylog.Logger;

import java.io.IOException;
Expand All @@ -21,6 +22,7 @@ public void run() {
while (client.isConnected()) {
Packet packet = client.readPacket();
packet.onServerReceive(client);
GlobalQuakeServer.instance.getServerSocket().getDataService().processPacket(client, packet);
}
} catch (Exception | UnknownPacketException e) {
Logger.error("Client #%d experienced a crash while reading!".formatted(client.getID()));
Expand Down
46 changes: 38 additions & 8 deletions GlobalQuakeServerUI/src/main/java/gqserver/server/DataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import gqserver.api.ServerClient;
import gqserver.api.data.EarthquakeInfo;
import gqserver.api.data.HypocenterData;
import gqserver.api.packets.earthquake.EarthquakeCheckPacket;
import gqserver.api.packets.earthquake.EarthquakeRequestPacket;
import gqserver.api.packets.earthquake.EarthquakesRequestPacket;
import gqserver.api.packets.earthquake.HypocenterDataPacket;
import gqserver.core.GlobalQuakeServer;
import gqserver.core.earthquake.data.Earthquake;
Expand Down Expand Up @@ -45,7 +48,7 @@ public void onQuakeCreate(QuakeCreateEvent event) {
quakesWriteLock.unlock();
}

broadcast(getEartquakeReceivingClients(), createQuakePacket(earthquake));
broadcast(getEarthquakeReceivingClients(), createQuakePacket(earthquake));
}

@Override
Expand All @@ -64,7 +67,7 @@ public void onQuakeRemove(QuakeRemoveEvent event) {
quakesWriteLock.unlock();
}

broadcast(getEartquakeReceivingClients(), createQuakePacket(null));
broadcast(getEarthquakeReceivingClients(), new EarthquakeCheckPacket(new EarthquakeInfo(event.earthquake().getUuid(), EarthquakeInfo.REMOVED)));
}

@Override
Expand All @@ -86,16 +89,12 @@ public void onQuakeUpdate(QuakeUpdateEvent event) {
quakesWriteLock.unlock();
}

broadcast(getEartquakeReceivingClients(), createQuakePacket(earthquake));
broadcast(getEarthquakeReceivingClients(), createQuakePacket(earthquake));
}
});
}

private Packet createQuakePacket(Earthquake earthquake) {
if(earthquake == null){
return new HypocenterDataPacket(null);
}

return new HypocenterDataPacket(new HypocenterData(
earthquake.getUuid(),earthquake.getRevisionID(), earthquake.getLat(), earthquake.getLon(),
earthquake.getDepth(),earthquake.getOrigin(), earthquake.getMag())
Expand All @@ -112,12 +111,43 @@ private void broadcast(List<ServerClient> clients, Packet packet) {
});
}

private List<ServerClient> getEartquakeReceivingClients(){
private List<ServerClient> getEarthquakeReceivingClients(){
return getClients().stream().filter(serverClient -> serverClient.getClientConfig().earthquakeData()).toList();
}

private List<ServerClient> getClients() {
return GlobalQuakeServer.instance.getServerSocket().getClients();
}

public void processPacket(ServerClient client, Packet packet) {
try {
if (packet instanceof EarthquakesRequestPacket) {
processEarthquakesRequest(client);
} else if (packet instanceof EarthquakeRequestPacket earthquakeRequestPacket) {
processEarthquakeRequest(client, earthquakeRequestPacket);
}
}catch(IOException e){
Logger.error(e);
}
}

private void processEarthquakeRequest(ServerClient client, EarthquakeRequestPacket earthquakeRequestPacket) throws IOException {
for(Earthquake earthquake : GlobalQuakeServer.instance.getEarthquakeAnalysis().getEarthquakes()){
if(earthquake.getUuid().equals(earthquakeRequestPacket.getUuid())){
client.sendPacket(createQuakePacket(earthquake));
return;
}
}
}

private void processEarthquakesRequest(ServerClient client) throws IOException {
quakesReadLock.lock();
try {
for (EarthquakeInfo info : currentEarthquakes) {
client.sendPacket(new EarthquakeCheckPacket(info));
}
} finally {
quakesReadLock.unlock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ public int getClientCount() {
public List<ServerClient> getClients() {
return clients;
}

public DataService getDataService() {
return dataService;
}
}

0 comments on commit b99af8f

Please sign in to comment.