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

64
LINES

< > BotCompany Repo | #1001178 // Networking module (developing)

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

Libraryless. Click here for Pure Java version (1156L/9K/29K).

1  
!747
2  
3  
m {
4  
  static Set<S> peers = synchroTreeSet(); // only those with a running web server are listed
5  
  
6  
  p {
7  
    makeAndroid("Networking module!");
8  
    startAWebServer(8888);
9  
    for (S ip : getMyIPs()) discover(ip); // You are your own peer! :)
10  
    connectToGateway();
11  
  }
12  
  
13  
  static synchronized S answer(S s) {
14  
    if (match3("how many peers", s))
15  
      return "" + peers.size();
16  
    if (match3("what is a peer", s))
17  
      return "an ip address that had a port 8888 web server at some point";
18  
    if (match3("list peers", s))
19  
      return structure(peers);
20  
    if (match3("ok", s))
21  
      return "ok!";
22  
    return null;
23  
  }
24  
  
25  
  static void startAWebServer(int port) {
26  
    Class webserver = hotwire("#1000842");
27  
    set(webserver, "port", port);
28  
    set(webserver, "onClient", new Object() {
29  
      public Object run(Object... args) {
30  
        /* args are: final String uri, final Method method,
31  
            final Map<String, String> header, final Map<String, String> parms,
32  
            final Map<String, String> files */
33  
            
34  
        Map<S, S> header = (Map) args[2];
35  
        print("header: " + structure(header));
36  
        S ip = header.get("remote-addr");
37  
        discover(ip);
38  
        return null;
39  
      }
40  
    });
41  
    call(webserver, "start");
42  
  }
43  
  
44  
  static void connectToGateway() {
45  
    L<S> ips = detectGateways();
46  
    if (ips.isEmpty()) print("No gateways found.");
47  
    for (String ip : ips) 
48  
      discover(ip);
49  
  }
50  
  
51  
  static void discover(S ip) {
52  
    if (peers.contains(ip))
53  
      return;
54  
      
55  
    print("Possible peer found: " + ip + " - connecting");
56  
    try {
57  
      String page = loadPage("http://" + ip + ":8888"); // TODO: HEAD is enough
58  
      peers.add(ip);
59  
      print("OK, peer found: " + ip + " - peers now: " + structure(peers));
60  
    } catch (Throwable e) {
61  
      print("Could not load home page of " + ip + ", not adding as peer. Peers: " + structure(peers));
62  
    }
63  
  }
64  
}

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: #1001178
Snippet name: Networking module (developing)
Eternal ID of this version: #1001178/1
Text MD5: c3274e29deafb72c731722d64047f7e0
Transpilation MD5: 243568b271c38478f589184d03783241
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-29 14:40:50
Source code size: 1971 bytes / 64 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 640 / 768
Referenced in: [show references]