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

67
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 {
66  
  abstract void run(DialogIO io);
67  
}

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1001065
Snippet name: Dialog classes (DialogIO/DialogHandler)
Eternal ID of this version: #1001065/6
Text MD5: 558ffd9dacae188e1f9afa5dc2a756ac
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-13 21:35:25
Source code size: 1622 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 585 / 19570
Version history: 5 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1001065 & http://1001065.tinybrain.de