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)

1  
static abstract class DialogIO implements AutoCloseable {
2  
  String line;
3  
  boolean eos, loud, noClose;
4  
  Lock lock = lock();
5  
  
6  
  abstract String readLineImpl();
7  
  abstract boolean isStillConnected();
8  
  abstract void sendLine(String line);
9  
  abstract boolean isLocalConnection();
10  
  abstract Socket getSocket();
11  
12  
  int getPort() { Socket s = getSocket(); ret s == null ? 0 : s.getPort(); }
13  
  
14  
  boolean helloRead;
15  
  int shortenOutputTo = 500;
16  
  
17  
  String readLineNoBlock() {
18  
    String l = line;
19  
    line = null;
20  
    return l;
21  
  }
22  
  
23  
  boolean waitForLine() ctex {
24  
    ping();
25  
    if (line != null) return true;
26  
    //print("Readline");
27  
    line = readLineImpl();
28  
    //print("Readline done: " + line);
29  
    if (line == null) eos = true;
30  
    return line != null;
31  
  }
32  
  
33  
  String readLine() {
34  
    waitForLine();
35  
    helloRead = true;
36  
    return readLineNoBlock();
37  
  }
38  
  
39  
  String ask(String s, Object... args) {
40  
    if (loud) ret askLoudly(s, args);
41  
    if (!helloRead) readLine();
42  
    if (args.length != 0) s = format3(s, args);
43  
    sendLine(s);
44  
    return readLine();
45  
  }
46  
  
47  
  String askLoudly(String s, Object... args) {
48  
    if (!helloRead) readLine();
49  
    if (args.length != 0) s = format3(s, args);
50  
    print("> " + shorten(s, shortenOutputTo));
51  
    sendLine(s);
52  
    String answer = readLine();
53  
    print("< " + shorten(answer, shortenOutputTo));
54  
    return answer;
55  
  }
56  
  
57  
  void pushback(String l) {
58  
    if (line != null)
59  
      fail();
60  
    line = l;
61  
    helloRead = false;
62  
  }
63  
}
64  
65  
static abstract class DialogHandler is IDialogHandler {}
66  
67  
interface IDialogHandler {
68  
  void run(DialogIO io);
69  
}

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: 923 / 21003
Version history: 6 change(s)
Referenced in: [show references]