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");
byte[] outputBytes = sb.toString().getBytes(cs_utf8);
File f = new File(baseStandaloneDir, "config.js");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos.write(outputBytes);
} catch (IOException iox) {
Log.severe("Exception while writing " + f.getPath(), iox);
} finally {
if(fos != null) {
MapManager.scheduleDelayedJob(new Runnable() {
public void run() {
File f = new File(baseStandaloneDir, "config.js");
FileOutputStream fos = null;
try {
fos.close();
} catch (IOException x) {}
fos = null;
}
}
fos = new FileOutputStream(f);
fos.write(outputBytes);
} 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() {
@ -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() {
BufferInputStream bis = storage.getStandaloneFile("dynmap_webchat.json");
if (bis != null && lastTimestamp != 0) {
JSONArray jsonMsgs = null;
Reader inputFileReader = null;
try {
inputFileReader = new InputStreamReader(bis, cs_utf8);
jsonMsgs = (JSONArray) parser.parse(inputFileReader);
} catch (IOException ex) {
Log.severe("Exception while reading JSON-file.", ex);
} catch (ParseException ex) {
Log.severe("Exception while parsing JSON-file.", ex);
} finally {
if(inputFileReader != null) {
try {
inputFileReader.close();
} catch (IOException iox) {
}
inputFileReader = null;
}
}
MapManager.scheduleDelayedJob(new Runnable() {
public void run() {
BufferInputStream bis = storage.getStandaloneFile("dynmap_webchat.json");
if (bis != null && lastTimestamp != 0) {
JSONArray jsonMsgs = null;
Reader inputFileReader = null;
try {
inputFileReader = new InputStreamReader(bis, cs_utf8);
jsonMsgs = (JSONArray) parser.parse(inputFileReader);
} catch (IOException ex) {
Log.severe("Exception while reading JSON-file.", ex);
} catch (ParseException ex) {
Log.severe("Exception while parsing JSON-file.", ex);
} finally {
if(inputFileReader != null) {
try {
inputFileReader.close();
} catch (IOException iox) {
if (jsonMsgs != null) {
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);
}
}
}
}
}
}
inputFileReader = null;
}
}
if (jsonMsgs != null) {
final JSONArray json = jsonMsgs;
// Process content on server thread
core.getServer().scheduleServerTask(new Runnable() {
@Override
public void run() {
processWebChat(json);
}
}, 0);
}
}
}
}, 0);
}
protected void handleRegister() {
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());
}
}
/* Now publish marker files to the tiles directory */
for(MarkerIcon ico : api.getMarkerIcons()) {
api.publishMarkerIcon(ico);
}
/* Freshen files */
api.freshenMarkerFiles();
/* Add listener so we update marker files for other worlds as they become active */
core.events.addListener("worldactivated", api);
MapManager.scheduleDelayedJob(new Runnable() {
public void run() {
/* Now publish marker files to the tiles directory */
for(MarkerIcon ico : api.getMarkerIcons()) {
api.publishMarkerIcon(ico);
}
/* Freshen files */
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;
}

View File

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