!7 static int chatPort = 9751; // just any number static File logFile; static L lines = synchroList(); p { installHelloMessage("#1000867 - Chat server"); setUpChat(); makeAndroid("This is the chat server."); sleep(); } static synchronized S answer(S s) { new Matches m; if (match3("how many lines are in the memory log", s)) return "" + lines.size(); if (match3("how many bytes are in the disk log", s)) return "" + logFile.length(); if (match3("where is the disk log", s)) return logFile.getAbsolutePath(); if (match3("say *", s, m)) { say("anonymous", unquote(m.m[0])); return "ok"; } return null; } static void setUpChat() { autoReportToChatOff(); if (logFile == null) logFile = new File(userHome(), "JavaX/Chat/log.txt"); mkdirsForFile(logFile); if (!startDialogServerIfPortAvailable(chatPort, dialogHandler { String dialogID = randomID(8); io.sendLine("Your ID: " + dialogID); while (io.isStillConnected()) { if (io.waitForLine()) { String line = io.readLineNoBlock(); say(dialogID, line); } } })) print("Chat port " + chatPort + " not available!"); else { print("Chat set up on port " + chatPort + "."); // Set up sucking port if (!startDialogServerIfPortAvailable(chatPort+1, dialogHandler { try { int i = 0; boolean present = false; while (io.isStillConnected()) { if (i < lines.size()) { do { io.sendLine(lines.get(i++)); } while (i < lines.size()); if (!present) { present = true; io.sendLine(""); // empty line indicates we have reached present time } } sleep(100); } } catch (Exception e) { // it's probably a disconnect, just handle it quietly } })) print("Chat port " + (chatPort+1) + " not available!"); else print("Chat suck channel set up on port " + (chatPort+1) + "."); } // Set up history-less sucking port if (!startDialogServerIfPortAvailable(chatPort+2, dialogHandler { try { int i = lines.size(); while (io.isStillConnected()) { while (i < lines.size()) io.sendLine(lines.get(i++)); sleep(100); } } catch (Exception e) { // it's probably a disconnect, just handle it quietly } })) print("Chat port " + (chatPort+2) + " not available!"); else print("Chat historyless suck channel set up on port " + (chatPort+2) + "."); } static void appendToLog(File logFile, String s) { appendToFile(logFile.getPath(), "\n" + s + "\n"); } static void say(S dialogID, S line) { S s = dialogID + " at " + now() + ": " + quote(line); print(s); lines.add(s); appendToLog(logFile, s); }