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

67
LINES

< > BotCompany Repo | #1016875 - StefansOS_ConnectToServer

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4802L/32K).

sclass StefansOS_ConnectToServer implements AutoCloseable {
  DialogIO io;
  volatile bool on;
  Set<S> subs = synchroLinkedHashSet();
  VF1<S> onLine;
  S server = "botcompany.de";
  L<Runnable> onConnected = syncList();

  void start {
    if (on) ret;
    on = true;
    _connect();
    doEvery_daemon_highPriority(30000, r { if (io != null) io.sendLine("") });
  }
  
  void cleanMeUp { on = false; closeIO(); }
  public void close { cleanMeUp(); }
  
  void _connect {
    thread "Connect" {
      while (licensed() && on) {
        if (!tryToConnect()) continue with sleepSeconds(1);
        try {
          io = talkTo(server, 6000);
          io.getSocket().setSoTimeout(30000+10000);
          print("Connected.");
          pcallFAll(onConnected);
          temp tempAfterwards(r { print("Disconnected.") });
          io.sendLine(format("computerID=*", computerID()));
          for (S channel : cloneList(subs)) io.sendLine(format("sub *", channel));
          print("Sent computer ID: " + computerID());
          S line;
          while ((line = io.readLine()) != null)
            pcallF(onLine, line);
        } catch e { // pcall would also do an annoying message box
          printShortException(e);
        }
        closeIO();
        sleepSeconds(5);
      }
    }
  }
  
  void closeIO {
    if (io != null) { pcall { io.close(); } io = null; }
  }
  
  bool connected() { ret io != null; }
  
  void sub(S channel) {
    if (subs.add(channel) && io != null)
      pcall { io.sendLine(format("sub *", channel); }
  }
  
  void startWithSubs(S... channels) {
    for (S channel : channels) sub(channel);
    start();
  }
  
  void sendLine(S line) {
    if (io != null)
      io.sendLine(line);
    else print("Can't send, not connected: " + line);
  }
  
  swappable bool tryToConnect() { true; }
}

Author comment

Began life as a copy of #1016578

download  show line numbers  debug dex   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1016875
Snippet name: StefansOS_ConnectToServer
Eternal ID of this version: #1016875/20
Text MD5: c15bbc487a810d8c091e5baf57d91342
Transpilation MD5: 38e79c3e88fccfbda1967bf34f1581a4
Author: stefan
Category: javax / stefan's os
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-16 17:14:25
Source code size: 1887 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 251 / 664
Version history: 19 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1016875 & http://1016875.tinybrain.de