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

69
LINES

< > BotCompany Repo | #1001065 // Dialog classes (DialogIO/DialogHandler)

JavaX fragment (include)

static abstract class DialogIO implements AutoCloseable {
  String line;
  boolean eos, loud, noClose;
  Lock lock = lock();
  
  abstract String readLineImpl();
  abstract boolean isStillConnected();
  abstract void sendLine(String line);
  abstract boolean isLocalConnection();
  abstract Socket getSocket();

  int getPort() { Socket s = getSocket(); ret s == null ? 0 : s.getPort(); }
  
  boolean helloRead;
  int shortenOutputTo = 500;
  
  String readLineNoBlock() {
    String l = line;
    line = null;
    return l;
  }
  
  boolean waitForLine() ctex {
    ping();
    if (line != null) return true;
    //print("Readline");
    line = readLineImpl();
    //print("Readline done: " + line);
    if (line == null) eos = true;
    return line != null;
  }
  
  String readLine() {
    waitForLine();
    helloRead = true;
    return readLineNoBlock();
  }
  
  String ask(String s, Object... args) {
    if (loud) ret askLoudly(s, args);
    if (!helloRead) readLine();
    if (args.length != 0) s = format3(s, args);
    sendLine(s);
    return readLine();
  }
  
  String askLoudly(String s, Object... args) {
    if (!helloRead) readLine();
    if (args.length != 0) s = format3(s, args);
    print("> " + shorten(s, shortenOutputTo));
    sendLine(s);
    String answer = readLine();
    print("< " + shorten(answer, shortenOutputTo));
    return answer;
  }
  
  void pushback(String l) {
    if (line != null)
      fail();
    line = l;
    helloRead = false;
  }
}

static abstract class DialogHandler is IDialogHandler {}

interface IDialogHandler {
  void run(DialogIO io);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 33 computer(s): aoiabmzegqzx, ayivmpnvhhik, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, fehiwqupcyrn, gwrvuhgaqvyk, imzmzdywqqli, irmadwmeruwu, ishqpsrjomds, jozkyjcghlvl, jtubtzbbkimh, lhdilzshxjzv, lpdgvwnxivlt, lqnftawlhpir, lulzaavyztxj, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wtqryiryparv, xprdwmaupziu, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001065
Snippet name: Dialog classes (DialogIO/DialogHandler)
Eternal ID of this version: #1001065/7
Text MD5: cf7c831ae1d1ebe7e4e429713d3d2412
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-28 23:10:45
Source code size: 1662 bytes / 69 lines
Pitched / IR pitched: No / No
Views / Downloads: 840 / 20907
Version history: 6 change(s)
Referenced in: [show references]