// The online bot LOVES to be online. !747 m { // -1, 0, +1 static new AtomicInteger online; static new AtomicLong nextCheck; static S lastProblem; p { makeAndroid("Online bot! " + getStatus(), stringfunc { answer(s) }); while (true) { check(); long seconds = online.get() > 0 ? 60 : 2; nextCheck.set(now() + seconds*1000); sleep(Math.max(nextCheck.get()-now(), 0)); } } // We should add a timeout I guess? Although, if actually offline, // it doesn't block, so it's probably ok unless the net or the // server is currently very slow. static synchronized void check() { int oldValue = online.get(); Throwable problem = checkIfHttpServerIsOnline("tinybrain.de"); int value = problem == null ? 1 : -1; if (problem == null) lastProblem = null; else if (!problem.toString().equals(lastProblem)) { print(problem); lastProblem = problem.toString(); } online.set(value); if (value != oldValue) print(getStatus()); } static synchronized S answer(S s) { if (match3("are we online?", s)) return getStatus(); if (match3("check again", s)) { print("Checking..."); check(); return getStatus(); } return null; } static S getStatus() { int i = online.get(); long checkSeconds = (nextCheck.get()-now())/1000; S checkTime = checkSeconds >= 0 ? "(Next check in " + checkSeconds + " seconds.)" : ""; if (i < 0) return "We are: OFFLINE. " + checkTime; else if (i > 0) return "We are: ONLINE. " + checkTime; else return "Online status check in progress."; } }