more advanced caching and onConnect(host,port,hotelversion) in extensions

This commit is contained in:
sirjonasxx 2018-11-10 16:35:43 +01:00
parent 3dc77492c0
commit ce71cf114a
8 changed files with 109 additions and 29 deletions

View File

@ -41,9 +41,11 @@ public class AdminOnConnect extends Extension {
} }
} }
}); });
intercept(HMessage.Side.TOSERVER, 4000, message -> done = false);
} }
protected void onStartConnection() { // protected void onStartConnection() {
done = false; // done = false;
} // }
} }

View File

@ -134,6 +134,10 @@ public abstract class Extension {
writeToStream(response.toBytes()); writeToStream(response.toBytes());
} }
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONSTART) { 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(); onStartConnection();
} }
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONEND) { else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONEND) {
@ -359,8 +363,22 @@ public abstract class Extension {
return true; return true;
} }
ExtensionInfo getInfoAnnotations() { ExtensionInfo getInfoAnnotations() {
return getClass().getAnnotation(ExtensionInfo.class); 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);
}
}
} }

View File

@ -43,6 +43,9 @@ public abstract class ExtensionForm {
protected boolean sendToClient(HPacket packet){ protected boolean sendToClient(HPacket packet){
return extension.sendToClient(packet); return extension.sendToClient(packet);
} }
protected void onConnect(Extension.OnConnectionListener listener) {
extension.onConnect(listener);
}
protected void onShow(){}; protected void onShow(){};
protected void onHide(){}; protected void onHide(){};

View File

@ -16,25 +16,29 @@ import java.util.List;
*/ */
public class Cacher { public class Cacher {
private static final String CACHE_FILENAME = "cache.json"; private static final String DEFAULT_CACHE_FILENAME = "cache.json";
private static String getCacheDir() { private static String getCacheDir() {
try { 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) { } catch (URISyntaxException e) {
return null; 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()); return (f.exists() && !f.isDirectory());
} }
private static JSONObject getCacheContents() { public static JSONObject getCacheContents(String cache_filename) {
if (cacheFileExists()) { if (cacheFileExists(cache_filename)) {
try { try {
File f = new File(getCacheDir(), CACHE_FILENAME); File f = new File(getCacheDir(), cache_filename);
String contents = String.join("\n", Files.readAllLines(f.toPath())); String contents = String.join("\n", Files.readAllLines(f.toPath()));
return new JSONObject(contents); return new JSONObject(contents);
@ -44,8 +48,11 @@ public class Cacher {
} }
return new JSONObject(); return new JSONObject();
} }
private static void updateCache(JSONObject contents) { public static void updateCache(JSONObject contents, String cache_filename) {
try (FileWriter file = new FileWriter(new File(getCacheDir(), 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.write(contents.toString());
file.flush(); file.flush();
@ -54,28 +61,48 @@ public class Cacher {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void put(String key, Object val, String cache_filename) {
public static void put(String key, Object val) { JSONObject object = getCacheContents(cache_filename);
JSONObject object = getCacheContents();
if (object.has(key)) object.remove(key); if (object.has(key)) object.remove(key);
object.put(key, val); object.put(key, val);
updateCache(object); updateCache(object, cache_filename);
} }
public static Object get(String key, String cache_filename) {
public static Object get(String key) { JSONObject object = getCacheContents(cache_filename);
JSONObject object = getCacheContents();
if (object.has(key)) return object.get(key); if (object.has(key)) return object.get(key);
else return null; else return null;
} }
public static List<Object> getList(String key, String cache_filename) {
public static List<Object> getList(String key) { JSONObject object = getCacheContents(cache_filename);
JSONObject object = getCacheContents();
if (object.has(key)) return ((JSONArray)object.get(key)).toList(); if (object.has(key)) return ((JSONArray)object.get(key)).toList();
else return null; 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() { public static void clear() {
updateCache(new JSONObject()); clear(DEFAULT_CACHE_FILENAME);
} }
} }

View File

@ -0,0 +1,10 @@
package gearth.misc.harble_api;
/**
* Created by Jonas on 10/11/2018.
*/
public class HarbleAPI {
}

View File

@ -0,0 +1,8 @@
package gearth.misc.harble_api;
/**
* Created by Jeunez on 10/11/2018.
*/
public class HarbleAPIFetcher {
}

View File

@ -259,12 +259,13 @@ public class HConnection {
rc4Obtainer.setIncomingHandler(incomingHandler); rc4Obtainer.setIncomingHandler(incomingHandler);
outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> { outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> {
this.hotelVersion = hotelVersion;
incomingHandler.setAsDataStream(); incomingHandler.setAsDataStream();
this.hotelVersion = hotelVersion;
clientHostAndPort = client.getLocalAddress().getHostAddress() + ":" + client.getPort(); clientHostAndPort = client.getLocalAddress().getHostAddress() + ":" + client.getPort();
onConnect();
if (DEBUG) System.out.println(clientHostAndPort); if (DEBUG) System.out.println(clientHostAndPort);
setState(State.CONNECTED); setState(State.CONNECTED);
onConnect();
outHandler = outgoingHandler; outHandler = outgoingHandler;
inHandler = incomingHandler; inHandler = incomingHandler;
}); });

View File

@ -69,8 +69,9 @@ import java.util.*;
* ----------------------------------------------------------------------------------------------------- * -----------------------------------------------------------------------------------------------------
* | 4 | FLAGS-CHECK** | Body: String with G-Earth's boot flags (args from static gearth method) | * | 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) | * | | | 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 | * | 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) { if (newState == HConnection.State.CONNECTED) {
synchronized (gEarthExtensions) { synchronized (gEarthExtensions) {
for (GEarthExtension extension : 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)); extension.sendMessage(new HPacket(OUTGOING_MESSAGES_IDS.INIT));
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)
.appendString(getHConnection().getDomain())
.appendInt(getHConnection().getPort())
.appendString(getHConnection().getHotelVersion())
);
} }
extension.onRemoveClick(observable -> { extension.onRemoveClick(observable -> {