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

65
LINES

< > BotCompany Repo | #1000776 // Kill Switch!

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

Libraryless. Click here for Pure Java version (206L/2K/6K).

!747

!721 // thread {

!include #1000774 // CircularFifoBuffer
!include #1000782 // Flag

m {
  static int port = 1234;
  static new Flag killFlag;

  p {
    installKillSwitch();
    print("Hello world. Sleeping. Port=" + port);
    killFlag.waitUntilUp();
  }

  static void installKillSwitch() ctex {
    final ServerSocket serverSocket = new ServerSocket(1234);
    //serverSocket.setSoTimeout(1000);
    
    daemon { // todo: higher priority
      while (true) {
        try {
          final Socket s = serverSocket.accept();
          print("connect");
          thread {
            InputStream in = s.getInputStream();
            CircularFifoBuffer<Character> b = new CircularFifoBuffer<Character>(4);
            while (true) {
              int i = in.read();
              if (i < 0) break;
              print(i);
              b.add((char) i);
              S s = getLast(b, 4);
              //print("> " + s);
              if (s == "kill")
                killMe();
            }
            print("disconnect");
          }
        } catch (SocketTimeoutException e) {
        }
      }
    }
  }
  
  static void killMe() {
    print("killing");
    
    // keepWakingUp(); // see sleep() above. Does this make sense?
    
    System.exit(0); // This works.

    // killFlag.raise(); // And this is a very safe way to do it too.
  }

  static S getLast(CircularFifoBuffer<Character> b, int n) {
    n = Math.min(n, b.size());
    new StringBuilder buf;
    Iterator<Character> it = b.getBackingStore().descendingIterator();
    for (int i = 0; i < n; i++)
      buf.append(it.next());
    return buf.reverse().toString();
  }
}

Author comment

Began life as a copy of #1000775

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000776
Snippet name: Kill Switch!
Eternal ID of this version: #1000776/1
Text MD5: be05b2dcc0789c1ad3003ec2ee8c92eb
Transpilation MD5: 9f9b363f0a44c3e5b427d179785f6243
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-27 02:01:33
Source code size: 1712 bytes / 65 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 594 / 566
Referenced in: [show references]