!636 // for includes !stringfunc { !1000868 // dialogHandler shortcut !include #1001065 // Dialog classes static int answerQuestions_defaultPort = 5000; static void answerQuestionsOnPort(final StringFunc f) { answerQuestionsOnPort(answerQuestions_defaultPort, f); } static void answerQuestionsOnPort(int port, final StringFunc f) { startDialogServer(port, dialogHandler { String dialogID = randomID(8); io.sendLine("Your ID: " + dialogID); while (io.isStillConnected()) { if (io.waitForLine()) { String line = io.readLineNoBlock(); S s = dialogID + " at " + now() + ": " + quote(line); print(s); if (line == "bye") { io.sendLine("bye stranger"); return; } S answer = answerQuestions_getAnswer(line, f); io.sendLine(answer == null ? "null" : answer); //appendToLog(logFile, s); } } }); } static S answerQuestions_getAnswer(S line, StringFunc f) { S answer; try { answer = f.get(line); } catch (Throwable e) { e.printStackTrace(); answer = e.toString(); } print("> " + answer); return answer; } static void answerQuestionsOnPortAndConsole(final StringFunc f) { answerQuestionsOnPortAndConsole(answerQuestions_defaultPort, f); } static void answerQuestionsOnPortAndConsole(int port, final StringFunc f) { answerQuestionsOnPort(port, f); print("You may also type on this console."); S line; while ((line = readLine()) != null) { if (line == "bye") print("! bye stranger"); else answerQuestions_getAnswer(line, f); // prints answer on console too } }