Skip to content

Commit

Permalink
Merge pull request #1 from xspanger3770/refactor
Browse files Browse the repository at this point in the history
Refactor by splitting to Server UI and Core module
  • Loading branch information
xspanger3770 authored Oct 24, 2023
2 parents ba34442 + b3a22f4 commit f32e13a
Show file tree
Hide file tree
Showing 181 changed files with 704 additions and 854 deletions.
5 changes: 2 additions & 3 deletions GlobalQuakeAPI/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.morce.GlobalQuake</groupId>
<groupId>xspanger.GlobalQuake</groupId>
<artifactId>GlobalQuakeServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.example</groupId>
<artifactId>GlobalQuakeAPI</artifactId>

<properties>
Expand Down
74 changes: 74 additions & 0 deletions GlobalQuakeCore/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xspanger.GlobalQuake</groupId>
<artifactId>GlobalQuakeServer</artifactId>
<version>0.1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>GlobalQuakeCore</artifactId>
<version>0.10.0</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-api</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>edu.sc.seis</groupId>
<artifactId>seisFile</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson -->
<dependency>
<groupId>de.grundid.opendatalab</groupId>
<artifactId>geojson-jackson</artifactId>
<version>1.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/edu.sc.seis/seedCodec -->
<dependency>
<groupId>edu.sc.seis</groupId>
<artifactId>seedCodec</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/edu.sc.seis/TauP -->
<dependency>
<groupId>edu.sc.seis</groupId>
<artifactId>TauP</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>uk.me.berndporr</groupId>
<artifactId>iirj</artifactId>
<version>1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.wendykierp/JTransforms -->
<dependency>
<groupId>com.github.wendykierp</groupId>
<artifactId>JTransforms</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package gqserver.core;
package globalquake.core;

import gqserver.core.earthquake.ClusterAnalysis;
import gqserver.core.earthquake.EarthquakeAnalysis;
import gqserver.core.archive.EarthquakeArchive;
import gqserver.core.station.GlobalStationManager;
import gqserver.database.StationDatabaseManager;
import gqserver.events.GlobalQuakeEventHandler;
import gqserver.server.GQServerSocket;
import globalquake.core.archive.EarthquakeArchive;
import globalquake.core.database.StationDatabaseManager;
import globalquake.core.earthquake.ClusterAnalysis;
import globalquake.core.earthquake.EarthquakeAnalysis;
import globalquake.core.events.GlobalQuakeEventHandler;
import globalquake.core.exception.ApplicationErrorHandler;
import globalquake.core.station.GlobalStationManager;

public class GlobalQuakeServer {
import java.io.File;

public class GlobalQuake {

private final GlobalQuakeRuntime globalQuakeRuntime;
private final SeedlinkNetworksReader seedlinkNetworksReader;
Expand All @@ -19,14 +21,20 @@ public class GlobalQuakeServer {

private final GlobalQuakeEventHandler eventHandler;

public static GlobalQuakeServer instance;
public static GlobalQuake instance;

private final GlobalStationManager globalStationManager;

private final GQServerSocket serverSocket;
private final ApplicationErrorHandler errorHandler;
private final File mainFolder;


public GlobalQuakeServer(StationDatabaseManager stationDatabaseManager) {
public GlobalQuake(StationDatabaseManager stationDatabaseManager,
ApplicationErrorHandler errorHandler,
File mainFolder) {
instance = this;
this.mainFolder = mainFolder;
this.errorHandler = errorHandler;
this.stationDatabaseManager = stationDatabaseManager;

eventHandler = new GlobalQuakeEventHandler().runHandler();
Expand All @@ -40,8 +48,6 @@ public GlobalQuakeServer(StationDatabaseManager stationDatabaseManager) {

globalQuakeRuntime = new GlobalQuakeRuntime();
seedlinkNetworksReader = new SeedlinkNetworksReader();

serverSocket = new GQServerSocket();
}

public void startRuntime(){
Expand Down Expand Up @@ -91,7 +97,11 @@ public GlobalQuakeEventHandler getEventHandler() {
return eventHandler;
}

public GQServerSocket getServerSocket() {
return serverSocket;
public ApplicationErrorHandler getErrorHandler() {
return errorHandler;
}

public File getMainFolder() {
return mainFolder;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package gqserver.core;
package globalquake.core;

import gqserver.core.station.AbstractStation;
import gqserver.main.Main;
import gqserver.utils.NamedThreadFactory;
import globalquake.core.station.AbstractStation;
import globalquake.utils.NamedThreadFactory;
import org.tinylog.Logger;

import java.util.concurrent.Executors;
Expand Down Expand Up @@ -35,47 +34,47 @@ public void runThreads() {
execAnalysis.scheduleAtFixedRate(() -> {
try {
long a = System.currentTimeMillis();
GlobalQuakeServer.instance.getStationManager().getStations().parallelStream().forEach(AbstractStation::analyse);
GlobalQuake.instance.getStationManager().getStations().parallelStream().forEach(AbstractStation::analyse);
lastAnalysis = System.currentTimeMillis() - a;
} catch (Exception e) {
Logger.error("Exception occurred in station analysis");
Main.getErrorHandler().handleException(e);
GlobalQuake.instance.getErrorHandler().handleException(e);
}
}, 0, 100, TimeUnit.MILLISECONDS);

exec1Sec.scheduleAtFixedRate(() -> {
try {
long a = System.currentTimeMillis();
GlobalQuakeServer.instance.getStationManager().getStations().parallelStream().forEach(station -> station.second(a));
if (GlobalQuakeServer.instance.getEarthquakeAnalysis() != null) {
GlobalQuakeServer.instance.getEarthquakeAnalysis().second();
GlobalQuake.instance.getStationManager().getStations().parallelStream().forEach(station -> station.second(a));
if (GlobalQuake.instance.getEarthquakeAnalysis() != null) {
GlobalQuake.instance.getEarthquakeAnalysis().second();
}
lastSecond = System.currentTimeMillis() - a;
} catch (Exception e) {
Logger.error("Exception occurred in 1-second loop");
Main.getErrorHandler().handleException(e);
GlobalQuake.instance.getErrorHandler().handleException(e);
}
}, 0, 1, TimeUnit.SECONDS);

execClusters.scheduleAtFixedRate(() -> {
try {
long a = System.currentTimeMillis();
GlobalQuakeServer.instance.getClusterAnalysis().run();
GlobalQuake.instance.getClusterAnalysis().run();
clusterAnalysisT = System.currentTimeMillis() - a;
} catch (Exception e) {
Logger.error("Exception occurred in cluster analysis loop");
Main.getErrorHandler().handleException(e);
GlobalQuake.instance.getErrorHandler().handleException(e);
}
}, 0, 500, TimeUnit.MILLISECONDS);

execQuake.scheduleAtFixedRate(() -> {
try {
long a = System.currentTimeMillis();
GlobalQuakeServer.instance.getEarthquakeAnalysis().run();
GlobalQuake.instance.getEarthquakeAnalysis().run();
lastQuakesT = System.currentTimeMillis() - a;
} catch (Exception e) {
Logger.error("Exception occurred in hypocenter location loop");
Main.getErrorHandler().handleException(e);
GlobalQuake.instance.getErrorHandler().handleException(e);
}
}, 0, 1, TimeUnit.SECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package gqserver.core;
package globalquake.core;

import globalquake.core.database.SeedlinkNetwork;
import globalquake.core.database.SeedlinkStatus;
import globalquake.core.station.AbstractStation;
import globalquake.core.station.GlobalStation;
import edu.sc.seis.seisFile.mseed.DataRecord;
import edu.sc.seis.seisFile.seedlink.SeedlinkPacket;
import edu.sc.seis.seisFile.seedlink.SeedlinkReader;
import gqserver.core.station.AbstractStation;
import gqserver.core.station.GlobalStation;
import gqserver.database.SeedlinkNetwork;
import gqserver.database.SeedlinkStatus;
import org.tinylog.Logger;

import java.time.Instant;
Expand Down Expand Up @@ -54,19 +54,19 @@ public static void main(String[] args) throws Exception{
public void run() {
createCache();
seedlinkReaderService = Executors.newCachedThreadPool();
GlobalQuakeServer.instance.getStationDatabaseManager().getStationDatabase().getDatabaseReadLock().lock();
GlobalQuake.instance.getStationDatabaseManager().getStationDatabase().getDatabaseReadLock().lock();
try{
GlobalQuakeServer.instance.getStationDatabaseManager().getStationDatabase().getSeedlinkNetworks().forEach(
GlobalQuake.instance.getStationDatabaseManager().getStationDatabase().getSeedlinkNetworks().forEach(
seedlinkServer -> seedlinkReaderService.submit(() -> runSeedlinkThread(seedlinkServer, RECONNECT_DELAY)));
} finally {
GlobalQuakeServer.instance.getStationDatabaseManager().getStationDatabase().getDatabaseReadLock().unlock();
GlobalQuake.instance.getStationDatabaseManager().getStationDatabase().getDatabaseReadLock().unlock();
}
}

private final Map<String, GlobalStation> stationCache = new HashMap<>();

private void createCache() {
for (AbstractStation s : GlobalQuakeServer.instance.getStationManager().getStations()) {
for (AbstractStation s : GlobalQuake.instance.getStationManager().getStations()) {
if (s instanceof GlobalStation) {
stationCache.put("%s %s".formatted(s.getNetworkCode(), s.getStationCode()), (GlobalStation) s);
}
Expand All @@ -87,7 +87,7 @@ private void runSeedlinkThread(SeedlinkNetwork seedlinkNetwork, int reconnectDel
reconnectDelay = RECONNECT_DELAY; // if connect succeeded then reset the delay
boolean first = true;

for (AbstractStation s : GlobalQuakeServer.instance.getStationManager().getStations()) {
for (AbstractStation s : GlobalQuake.instance.getStationManager().getStations()) {
if (s.getSeedlinkNetwork() != null && s.getSeedlinkNetwork().equals(seedlinkNetwork)) {
Logger.trace("Connecting to %s %s %s %s [%s]".formatted(s.getStationCode(), s.getNetworkCode(), s.getChannelName(), s.getLocationCode(), seedlinkNetwork.getName()));
if(!first) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package gqserver.ui.settings;
package globalquake.core;

import gqserver.exception.RuntimeApplicationException;
import gqserver.geo.DistanceUnit;
import gqserver.intensity.IntensityScales;
import gqserver.main.Main;
import globalquake.core.exception.RuntimeApplicationException;
import globalquake.core.geo.DistanceUnit;
import globalquake.core.intensity.IntensityScales;
import org.tinylog.Logger;

import java.io.File;
Expand All @@ -20,7 +19,7 @@

public final class Settings {

private static final File optionsFile = new File(Main.MAIN_FOLDER, "globalQuake.properties");
private static final File optionsFile = new File(GlobalQuake.instance.getMainFolder(), "globalQuake.properties");
private static final Properties properties = new Properties();

public static Double homeLat;
Expand Down Expand Up @@ -332,7 +331,7 @@ public static void save() {

properties.store(new FileOutputStream(optionsFile), "Fun fact: I've never felt an earthquake in my life");
} catch (IOException e) {
Main.getErrorHandler().handleException(e);
GlobalQuake.instance.getErrorHandler().handleException(e);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gqserver.ui.action;
package globalquake.core.action;

import org.tinylog.Logger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gqserver.core.alert;
package globalquake.core.alert;

public interface Warnable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gqserver.core.analysis;
package globalquake.core.analysis;

import globalquake.core.station.AbstractStation;
import edu.sc.seis.seisFile.mseed.DataRecord;
import gqserver.core.station.AbstractStation;
import org.tinylog.Logger;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gqserver.core.analysis;
package globalquake.core.analysis;

public enum AnalysisStatus {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package gqserver.core.analysis;
package globalquake.core.analysis;

import globalquake.core.Settings;
import globalquake.core.station.AbstractStation;
import globalquake.core.station.StationState;
import edu.sc.seis.seisFile.mseed.DataRecord;
import gqserver.core.station.AbstractStation;
import gqserver.core.station.StationState;
import gqserver.ui.settings.Settings;
import org.tinylog.Logger;
import uk.me.berndporr.iirj.Butterworth;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gqserver.core.analysis;
package globalquake.core.analysis;

import gqserver.core.earthquake.data.Cluster;
import gqserver.core.report.StationReport;
import globalquake.core.earthquake.data.Cluster;
import globalquake.core.report.StationReport;

import java.io.Serial;
import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gqserver.core.analysis;
package globalquake.core.analysis;

import java.io.Serial;
import java.io.Serializable;
Expand Down
Loading

0 comments on commit f32e13a

Please sign in to comment.