mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-27 02:40:51 +01:00
more advanced caching and onConnect(host,port,hotelversion) in extensions
This commit is contained in:
parent
3dc77492c0
commit
ce71cf114a
@ -41,9 +41,11 @@ public class AdminOnConnect extends Extension {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
intercept(HMessage.Side.TOSERVER, 4000, message -> done = false);
|
||||
}
|
||||
|
||||
protected void onStartConnection() {
|
||||
done = false;
|
||||
}
|
||||
// protected void onStartConnection() {
|
||||
// done = false;
|
||||
// }
|
||||
}
|
||||
|
@ -134,6 +134,10 @@ public abstract class Extension {
|
||||
writeToStream(response.toBytes());
|
||||
}
|
||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONSTART) {
|
||||
String host = packet.readString();
|
||||
int connectionPort = packet.readInteger();
|
||||
String hotelVersion = packet.readString();
|
||||
notifyConnectionListeners(host, connectionPort, hotelVersion);
|
||||
onStartConnection();
|
||||
}
|
||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONEND) {
|
||||
@ -359,8 +363,22 @@ public abstract class Extension {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
ExtensionInfo getInfoAnnotations() {
|
||||
return getClass().getAnnotation(ExtensionInfo.class);
|
||||
}
|
||||
|
||||
|
||||
protected interface OnConnectionListener {
|
||||
void act(String host, int port, String hotelversion);
|
||||
}
|
||||
private List<OnConnectionListener> onConnectionListeners = new ArrayList<>();
|
||||
protected void onConnect(OnConnectionListener listener){
|
||||
onConnectionListeners.add(listener);
|
||||
}
|
||||
private void notifyConnectionListeners(String host, int port, String hotelversion) {
|
||||
for (OnConnectionListener listener : onConnectionListeners) {
|
||||
listener.act(host, port, hotelversion);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -43,6 +43,9 @@ public abstract class ExtensionForm {
|
||||
protected boolean sendToClient(HPacket packet){
|
||||
return extension.sendToClient(packet);
|
||||
}
|
||||
protected void onConnect(Extension.OnConnectionListener listener) {
|
||||
extension.onConnect(listener);
|
||||
}
|
||||
|
||||
protected void onShow(){};
|
||||
protected void onHide(){};
|
||||
|
@ -16,25 +16,29 @@ import java.util.List;
|
||||
*/
|
||||
public class Cacher {
|
||||
|
||||
private static final String CACHE_FILENAME = "cache.json";
|
||||
private static final String DEFAULT_CACHE_FILENAME = "cache.json";
|
||||
|
||||
private static String getCacheDir() {
|
||||
try {
|
||||
return new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
|
||||
return new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent()
|
||||
+ File.separator
|
||||
+ "Cache";
|
||||
} catch (URISyntaxException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean cacheFileExists() {
|
||||
File f = new File(getCacheDir(), CACHE_FILENAME);
|
||||
|
||||
|
||||
private static boolean cacheFileExists(String cache_filename) {
|
||||
File f = new File(getCacheDir(), cache_filename);
|
||||
return (f.exists() && !f.isDirectory());
|
||||
}
|
||||
|
||||
private static JSONObject getCacheContents() {
|
||||
if (cacheFileExists()) {
|
||||
public static JSONObject getCacheContents(String cache_filename) {
|
||||
if (cacheFileExists(cache_filename)) {
|
||||
try {
|
||||
File f = new File(getCacheDir(), CACHE_FILENAME);
|
||||
File f = new File(getCacheDir(), cache_filename);
|
||||
String contents = String.join("\n", Files.readAllLines(f.toPath()));
|
||||
|
||||
return new JSONObject(contents);
|
||||
@ -44,8 +48,11 @@ public class Cacher {
|
||||
}
|
||||
return new JSONObject();
|
||||
}
|
||||
private static void updateCache(JSONObject contents) {
|
||||
try (FileWriter file = new FileWriter(new File(getCacheDir(), CACHE_FILENAME))) {
|
||||
public static void updateCache(JSONObject contents, String cache_filename) {
|
||||
File parent_dir = new File(getCacheDir());
|
||||
parent_dir.mkdirs();
|
||||
|
||||
try (FileWriter file = new FileWriter(new File(getCacheDir(), cache_filename))) {
|
||||
|
||||
file.write(contents.toString());
|
||||
file.flush();
|
||||
@ -54,28 +61,48 @@ public class Cacher {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void put(String key, Object val) {
|
||||
JSONObject object = getCacheContents();
|
||||
public static void put(String key, Object val, String cache_filename) {
|
||||
JSONObject object = getCacheContents(cache_filename);
|
||||
if (object.has(key)) object.remove(key);
|
||||
|
||||
object.put(key, val);
|
||||
updateCache(object);
|
||||
updateCache(object, cache_filename);
|
||||
}
|
||||
|
||||
public static Object get(String key) {
|
||||
JSONObject object = getCacheContents();
|
||||
public static Object get(String key, String cache_filename) {
|
||||
JSONObject object = getCacheContents(cache_filename);
|
||||
if (object.has(key)) return object.get(key);
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static List<Object> getList(String key) {
|
||||
JSONObject object = getCacheContents();
|
||||
public static List<Object> getList(String key, String cache_filename) {
|
||||
JSONObject object = getCacheContents(cache_filename);
|
||||
if (object.has(key)) return ((JSONArray)object.get(key)).toList();
|
||||
else return null;
|
||||
}
|
||||
public static void clear(String cache_filename) {
|
||||
updateCache(new JSONObject(), cache_filename);
|
||||
}
|
||||
|
||||
|
||||
private static boolean cacheFileExists() {
|
||||
return cacheFileExists(DEFAULT_CACHE_FILENAME);
|
||||
}
|
||||
|
||||
public static JSONObject getCacheContents() {
|
||||
return getCacheContents(DEFAULT_CACHE_FILENAME);
|
||||
}
|
||||
public static void updateCache(JSONObject contents) {
|
||||
updateCache(contents, DEFAULT_CACHE_FILENAME);
|
||||
}
|
||||
public static void put(String key, Object val) {
|
||||
put(key, val, DEFAULT_CACHE_FILENAME);
|
||||
}
|
||||
public static Object get(String key) {
|
||||
return get(key, DEFAULT_CACHE_FILENAME);
|
||||
}
|
||||
public static List<Object> getList(String key) {
|
||||
return getList(key, DEFAULT_CACHE_FILENAME);
|
||||
}
|
||||
public static void clear() {
|
||||
updateCache(new JSONObject());
|
||||
clear(DEFAULT_CACHE_FILENAME);
|
||||
}
|
||||
}
|
||||
|
10
G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java
Normal file
10
G-Earth/src/main/java/gearth/misc/harble_api/HarbleAPI.java
Normal file
@ -0,0 +1,10 @@
|
||||
package gearth.misc.harble_api;
|
||||
|
||||
/**
|
||||
* Created by Jonas on 10/11/2018.
|
||||
*/
|
||||
public class HarbleAPI {
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package gearth.misc.harble_api;
|
||||
|
||||
/**
|
||||
* Created by Jeunez on 10/11/2018.
|
||||
*/
|
||||
public class HarbleAPIFetcher {
|
||||
|
||||
}
|
@ -259,12 +259,13 @@ public class HConnection {
|
||||
rc4Obtainer.setIncomingHandler(incomingHandler);
|
||||
|
||||
outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> {
|
||||
this.hotelVersion = hotelVersion;
|
||||
incomingHandler.setAsDataStream();
|
||||
this.hotelVersion = hotelVersion;
|
||||
clientHostAndPort = client.getLocalAddress().getHostAddress() + ":" + client.getPort();
|
||||
onConnect();
|
||||
|
||||
if (DEBUG) System.out.println(clientHostAndPort);
|
||||
setState(State.CONNECTED);
|
||||
onConnect();
|
||||
outHandler = outgoingHandler;
|
||||
inHandler = incomingHandler;
|
||||
});
|
||||
|
@ -69,8 +69,9 @@ import java.util.*;
|
||||
* -----------------------------------------------------------------------------------------------------
|
||||
* | 4 | FLAGS-CHECK** | Body: String with G-Earth's boot flags (args from static gearth method) |
|
||||
* -----------------------------------------------------------------------------------------------------
|
||||
* | 5 | CONNECTION START | Empty body, just a note that a new connection has been made, |
|
||||
* | 5 | CONNECTION START | just a note that a new connection has been made, |
|
||||
* | | | you could check this yourself as well (listen to out:4000 packet) |
|
||||
* | | | host/port, hotel version |
|
||||
* -----------------------------------------------------------------------------------------------------
|
||||
* | 6 | CONNECTION END | Empty body, just a note that a connection has ended |
|
||||
* -----------------------------------------------------------------------------------------------------
|
||||
@ -150,7 +151,12 @@ public class Extensions extends SubForm {
|
||||
if (newState == HConnection.State.CONNECTED) {
|
||||
synchronized (gEarthExtensions) {
|
||||
for (GEarthExtension extension : gEarthExtensions) {
|
||||
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART));
|
||||
extension.sendMessage(
|
||||
new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART)
|
||||
.appendString(getHConnection().getDomain())
|
||||
.appendInt(getHConnection().getPort())
|
||||
.appendString(getHConnection().getHotelVersion())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -281,7 +287,12 @@ public class Extensions extends SubForm {
|
||||
|
||||
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.INIT));
|
||||
if (getHConnection().getState() == HConnection.State.CONNECTED) {
|
||||
extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART));
|
||||
extension.sendMessage(
|
||||
new HPacket(OUTGOING_MESSAGES_IDS.CONNECTIONSTART)
|
||||
.appendString(getHConnection().getDomain())
|
||||
.appendInt(getHConnection().getPort())
|
||||
.appendString(getHConnection().getHotelVersion())
|
||||
);
|
||||
}
|
||||
|
||||
extension.onRemoveClick(observable -> {
|
||||
|
Loading…
Reference in New Issue
Block a user