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

74
LINES

< > BotCompany Repo | #1016875 // StefansOS_ConnectToServer

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

Transpiled version (8872L) is out of date.

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

Author comment

Began life as a copy of #1016578

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1016875
Snippet name: StefansOS_ConnectToServer
Eternal ID of this version: #1016875/28
Text MD5: bfa23e4f3b793b8c9afe51b9d8e94b63
Author: stefan
Category: javax / stefan's os
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-21 20:08:09
Source code size: 2159 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 573 / 1272
Version history: 27 change(s)
Referenced in: [show references]