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

!747

m {
  static Set<S> peers = synchroTreeSet(); // only those with a running web server are listed
  
  p {
    makeAndroid("Networking module!");
    startAWebServer(8888);
    for (S ip : getMyIPs()) discover(ip); // You are your own peer! :)
    connectToGateway();
  }
  
  static synchronized S answer(S s) {
    if (match3("how many peers", s))
      return "" + peers.size();
    if (match3("what is a peer", s))
      return "an ip address that had a port 8888 web server at some point";
    if (match3("list peers", s))
      return structure(peers);
    if (match3("ok", s))
      return "ok!";
    return null;
  }
  
  static void startAWebServer(int port) {
    Class webserver = hotwire("#1000842");
    set(webserver, "port", port);
    set(webserver, "onClient", new Object() {
      public Object run(Object... args) {
        /* args are: final String uri, final Method method,
            final Map<String, String> header, final Map<String, String> parms,
            final Map<String, String> files */
            
        Map<S, S> header = (Map) args[2];
        print("header: " + structure(header));
        S ip = header.get("remote-addr");
        discover(ip);
        return null;
      }
    });
    call(webserver, "start");
  }
  
  static void connectToGateway() {
    L<S> ips = detectGateways();
    if (ips.isEmpty()) print("No gateways found.");
    for (String ip : ips) 
      discover(ip);
  }
  
  static void discover(S ip) {
    if (peers.contains(ip))
      return;
      
    print("Possible peer found: " + ip + " - connecting");
    try {
      String page = loadPage("http://" + ip + ":8888"); // TODO: HEAD is enough
      peers.add(ip);
      print("OK, peer found: " + ip + " - peers now: " + structure(peers));
    } catch (Throwable e) {
      print("Could not load home page of " + ip + ", not adding as peer. Peers: " + structure(peers));
    }
  }
}

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: 638 / 764
Referenced in: [show references]