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).

1  
sclass StefansOS_ConnectToServer implements AutoCloseable {
2  
  DialogIO io;
3  
  volatile bool on;
4  
  Set<S> subs = synchroLinkedHashSet();
5  
  VF1<S> onLine;
6  
  S server = "botcompany.de";
7  
  L<Runnable> onConnected = syncList();
8  
9  
  void start {
10  
    if (on) ret;
11  
    on = true;
12  
    _connect();
13  
    doEvery_daemon_highPriority(30000, r { if (io != null) io.sendLine("") });
14  
  }
15  
  
16  
  void cleanMeUp { on = false; closeIO(); }
17  
  public void close { cleanMeUp(); }
18  
  
19  
  void _connect {
20  
    thread "Connect" {
21  
      while (licensed() && on) {
22  
        if (!tryToConnect()) continue with sleepSeconds(1);
23  
        try {
24  
          io = talkTo(server, 6000);
25  
          io.getSocket().setSoTimeout(30000+10000);
26  
          print("Connected.");
27  
          pcallFAll(onConnected);
28  
          temp tempAfterwards(r { print("Disconnected.") });
29  
          io.sendLine(format("computerID=*", computerID()));
30  
          for (S channel : cloneList(subs)) io.sendLine(format("sub *", channel));
31  
          print("Sent computer ID: " + computerID());
32  
          S line;
33  
          while ((line = io.readLine()) != null)
34  
            pcallF(onLine, line);
35  
        } catch e { // pcall would also do an annoying message box
36  
          printShortException(e);
37  
        }
38  
        closeIO();
39  
        sleepSeconds(5);
40  
      }
41  
    }
42  
  }
43  
  
44  
  void closeIO {
45  
    if (io != null) { pcall { io.close(); } io = null; }
46  
  }
47  
  
48  
  bool connected() { ret io != null; }
49  
  
50  
  void sub(S channel) {
51  
    if (subs.add(channel) && io != null)
52  
      pcall { io.sendLine(format("sub *", channel); }
53  
  }
54  
  
55  
  void startWithSubs(S... channels) {
56  
    for (S channel : channels) sub(channel);
57  
    start();
58  
  }
59  
  
60  
  void sendLine(S line) {
61  
    if (io != null)
62  
      io.sendLine(line);
63  
    else print("Can't send, not connected: " + line);
64  
  }
65  
  
66  
  swappable bool tryToConnect() { true; }
67  
}

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: 271 / 705
Version history: 19 change(s)
Referenced in: [show references]

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