Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

59
LINES

< > BotCompany Repo | #1001117 // Online bot!

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (503L/5K/14K).

// 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.";
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001117
Snippet name: Online bot!
Eternal ID of this version: #1001117/1
Text MD5: 345af20f18ef9a29714afcf60f9ebcdb
Transpilation MD5: 6ed9e505e8bd101cef2e87e00c42966f
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-22 19:33:40
Source code size: 1697 bytes / 59 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 703 / 761
Referenced in: [show references]