Shift remaining storage access call off of server thread

This commit is contained in:
Mike Primm 2020-05-26 22:34:00 -05:00
parent 366d3e5a5d
commit a67f50350a
3 changed files with 154 additions and 132 deletions

View File

@ -265,21 +265,25 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
sb.append("'\n }\n};\n"); sb.append("'\n }\n};\n");
byte[] outputBytes = sb.toString().getBytes(cs_utf8); byte[] outputBytes = sb.toString().getBytes(cs_utf8);
File f = new File(baseStandaloneDir, "config.js"); MapManager.scheduleDelayedJob(new Runnable() {
FileOutputStream fos = null; public void run() {
try { File f = new File(baseStandaloneDir, "config.js");
fos = new FileOutputStream(f); FileOutputStream fos = null;
fos.write(outputBytes);
} catch (IOException iox) {
Log.severe("Exception while writing " + f.getPath(), iox);
} finally {
if(fos != null) {
try { try {
fos.close(); fos = new FileOutputStream(f);
} catch (IOException x) {} fos.write(outputBytes);
fos = null; } catch (IOException iox) {
} Log.severe("Exception while writing " + f.getPath(), iox);
} } finally {
if(fos != null) {
try {
fos.close();
} catch (IOException x) {}
fos = null;
}
}
}
}, 0);
} }
protected void writeConfiguration() { protected void writeConfiguration() {
@ -369,116 +373,130 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
} }
} }
private void processWebChat(JSONArray jsonMsgs) {
Iterator<?> iter = jsonMsgs.iterator();
boolean init_skip = (lastChatTimestamp == 0);
while (iter.hasNext()) {
boolean ok = true;
JSONObject o = (JSONObject) iter.next();
String ts = String.valueOf(o.get("timestamp"));
if(ts.equals("null")) ts = "0";
long cts;
try {
cts = Long.parseLong(ts);
} catch (NumberFormatException nfx) {
try {
cts = (long) Double.parseDouble(ts);
} catch (NumberFormatException nfx2) {
cts = 0;
}
}
if (cts > lastChatTimestamp) {
String name = String.valueOf(o.get("name"));
String ip = String.valueOf(o.get("ip"));
String uid = null;
Object usr = o.get("userid");
if(usr != null) {
uid = String.valueOf(usr);
}
boolean isip = true;
lastChatTimestamp = cts;
if(init_skip)
continue;
if(uid == null) {
if((!trust_client_name) || (name == null) || (name.equals(""))) {
if(ip != null)
name = ip;
}
if(useplayerloginip) { /* Try to match using IPs of player logins */
List<String> ids = core.getIDsForIP(name);
if(ids != null && !ids.isEmpty()) {
name = ids.get(0);
isip = false;
if(checkuserban) {
if(core.getServer().isPlayerBanned(name)) {
Log.info("Ignore message from '" + ip + "' - banned player (" + name + ")");
ok = false;
}
}
if(chat_perms && !core.getServer().checkPlayerPermission(name, "webchat")) {
Log.info("Rejected web chat from " + ip + ": not permitted (" + name + ")");
ok = false;
}
}
else if(requireplayerloginip) {
Log.info("Ignore message from '" + name + "' - no matching player login recorded");
ok = false;
}
}
if(hidewebchatip && isip) {
String n = useralias.get(name);
if(n == null) { /* Make ID */
n = String.format("web-%03d", aliasindex);
aliasindex++;
useralias.put(name, n);
}
name = n;
}
}
else {
if(core.getServer().isPlayerBanned(uid)) {
Log.info("Ignore message from '" + uid + "' - banned user");
ok = false;
}
if(chat_perms && !core.getServer().checkPlayerPermission(uid, "webchat")) {
Log.info("Rejected web chat from " + uid + ": not permitted");
ok = false;
}
name = uid;
}
if(ok) {
String message = String.valueOf(o.get("message"));
if((lengthlimit > 0) && (message.length() > lengthlimit))
message = message.substring(0, lengthlimit);
core.webChat(name, message);
}
}
}
}
protected void handleWebChat() { protected void handleWebChat() {
BufferInputStream bis = storage.getStandaloneFile("dynmap_webchat.json"); MapManager.scheduleDelayedJob(new Runnable() {
if (bis != null && lastTimestamp != 0) { public void run() {
JSONArray jsonMsgs = null; BufferInputStream bis = storage.getStandaloneFile("dynmap_webchat.json");
Reader inputFileReader = null; if (bis != null && lastTimestamp != 0) {
try { JSONArray jsonMsgs = null;
inputFileReader = new InputStreamReader(bis, cs_utf8); Reader inputFileReader = null;
jsonMsgs = (JSONArray) parser.parse(inputFileReader); try {
} catch (IOException ex) { inputFileReader = new InputStreamReader(bis, cs_utf8);
Log.severe("Exception while reading JSON-file.", ex); jsonMsgs = (JSONArray) parser.parse(inputFileReader);
} catch (ParseException ex) { } catch (IOException ex) {
Log.severe("Exception while parsing JSON-file.", ex); Log.severe("Exception while reading JSON-file.", ex);
} finally { } catch (ParseException ex) {
if(inputFileReader != null) { Log.severe("Exception while parsing JSON-file.", ex);
try { } finally {
inputFileReader.close(); if(inputFileReader != null) {
} catch (IOException iox) { try {
inputFileReader.close();
} } catch (IOException iox) {
inputFileReader = null;
}
}
if (jsonMsgs != null) { }
Iterator<?> iter = jsonMsgs.iterator(); inputFileReader = null;
boolean init_skip = (lastChatTimestamp == 0); }
while (iter.hasNext()) { }
boolean ok = true; if (jsonMsgs != null) {
JSONObject o = (JSONObject) iter.next(); final JSONArray json = jsonMsgs;
String ts = String.valueOf(o.get("timestamp")); // Process content on server thread
if(ts.equals("null")) ts = "0"; core.getServer().scheduleServerTask(new Runnable() {
long cts; @Override
try { public void run() {
cts = Long.parseLong(ts); processWebChat(json);
} catch (NumberFormatException nfx) { }
try { }, 0);
cts = (long) Double.parseDouble(ts); }
} catch (NumberFormatException nfx2) { }
cts = 0; }
} }, 0);
}
if (cts > lastChatTimestamp) {
String name = String.valueOf(o.get("name"));
String ip = String.valueOf(o.get("ip"));
String uid = null;
Object usr = o.get("userid");
if(usr != null) {
uid = String.valueOf(usr);
}
boolean isip = true;
lastChatTimestamp = cts;
if(init_skip)
continue;
if(uid == null) {
if((!trust_client_name) || (name == null) || (name.equals(""))) {
if(ip != null)
name = ip;
}
if(useplayerloginip) { /* Try to match using IPs of player logins */
List<String> ids = core.getIDsForIP(name);
if(ids != null && !ids.isEmpty()) {
name = ids.get(0);
isip = false;
if(checkuserban) {
if(core.getServer().isPlayerBanned(name)) {
Log.info("Ignore message from '" + ip + "' - banned player (" + name + ")");
ok = false;
}
}
if(chat_perms && !core.getServer().checkPlayerPermission(name, "webchat")) {
Log.info("Rejected web chat from " + ip + ": not permitted (" + name + ")");
ok = false;
}
}
else if(requireplayerloginip) {
Log.info("Ignore message from '" + name + "' - no matching player login recorded");
ok = false;
}
}
if(hidewebchatip && isip) {
String n = useralias.get(name);
if(n == null) { /* Make ID */
n = String.format("web-%03d", aliasindex);
aliasindex++;
useralias.put(name, n);
}
name = n;
}
}
else {
if(core.getServer().isPlayerBanned(uid)) {
Log.info("Ignore message from '" + uid + "' - banned user");
ok = false;
}
if(chat_perms && !core.getServer().checkPlayerPermission(uid, "webchat")) {
Log.info("Rejected web chat from " + uid + ": not permitted");
ok = false;
}
name = uid;
}
if(ok) {
String message = String.valueOf(o.get("message"));
if((lengthlimit > 0) && (message.length() > lengthlimit))
message = message.substring(0, lengthlimit);
core.webChat(name, message);
}
}
}
}
}
} }
protected void handleRegister() { protected void handleRegister() {
if(core.pendingRegisters() == false) if(core.pendingRegisters() == false)

View File

@ -395,16 +395,20 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
Log.severe("Error creating markers directory - " + api.markerdir.getPath()); Log.severe("Error creating markers directory - " + api.markerdir.getPath());
} }
} }
/* Now publish marker files to the tiles directory */ MapManager.scheduleDelayedJob(new Runnable() {
for(MarkerIcon ico : api.getMarkerIcons()) { public void run() {
api.publishMarkerIcon(ico); /* Now publish marker files to the tiles directory */
} for(MarkerIcon ico : api.getMarkerIcons()) {
/* Freshen files */ api.publishMarkerIcon(ico);
api.freshenMarkerFiles(); }
/* Add listener so we update marker files for other worlds as they become active */ /* Freshen files */
core.events.addListener("worldactivated", api); api.freshenMarkerFiles();
/* Add listener so we update marker files for other worlds as they become active */
core.events.addListener("worldactivated", api);
api.scheduleWriteJob(); /* Start write job */ api.scheduleWriteJob(); /* Start write job */
}
}, 0);
return api; return api;
} }

View File

@ -2,7 +2,7 @@ arguments=
auto.sync=false auto.sync=false
build.scans.enabled=false build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3)) connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3))
connection.project.dir= connection.project.dir=..
eclipse.preferences.version=1 eclipse.preferences.version=1
gradle.user.home= gradle.user.home=
java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home