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

63
LINES

< > BotCompany Repo | #1001087 // answerQuestionsOnPort / answerQuestionsOnPortAndConsole

JavaX fragment (include)

1  
!636 // for includes
2  
!stringfunc {
3  
4  
!1000868 // dialogHandler shortcut
5  
6  
!include #1001065 // Dialog classes
7  
8  
static int answerQuestions_defaultPort = 5000;
9  
10  
static void answerQuestionsOnPort(final StringFunc f) {
11  
  answerQuestionsOnPort(answerQuestions_defaultPort, f);
12  
}
13  
14  
static void answerQuestionsOnPort(int port, final StringFunc f) {
15  
  startDialogServer(port, dialogHandler {
16  
    String dialogID = randomID(8);
17  
    
18  
    io.sendLine("Your ID: " + dialogID);
19  
    
20  
    while (io.isStillConnected()) {
21  
      if (io.waitForLine()) {
22  
        String line = io.readLineNoBlock();
23  
        S s = dialogID + " at " + now() + ": " + quote(line);
24  
        print(s);
25  
        if (line == "bye") {
26  
          io.sendLine("bye stranger");
27  
          return;
28  
        }
29  
        S answer = answerQuestions_getAnswer(line, f);
30  
        io.sendLine(answer == null ? "null" : answer);
31  
        //appendToLog(logFile, s);
32  
      }
33  
    }
34  
  });
35  
}
36  
37  
static S answerQuestions_getAnswer(S line, StringFunc f) {
38  
  S answer;
39  
  try {
40  
    answer = f.get(line);
41  
  } catch (Throwable e) {
42  
    e.printStackTrace();
43  
    answer = e.toString();
44  
  }
45  
  print("> " + answer);
46  
  return answer;
47  
}
48  
49  
static void answerQuestionsOnPortAndConsole(final StringFunc f) {
50  
  answerQuestionsOnPortAndConsole(answerQuestions_defaultPort, f);
51  
}
52  
53  
static void answerQuestionsOnPortAndConsole(int port, final StringFunc f) {
54  
  answerQuestionsOnPort(port, f);
55  
  print("You may also type on this console.");
56  
  S line;
57  
  while ((line = readLine()) != null) {
58  
    if (line == "bye")
59  
      print("! bye stranger");
60  
    else
61  
      answerQuestions_getAnswer(line, f); // prints answer on console too
62  
  }
63  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001087
Snippet name: answerQuestionsOnPort / answerQuestionsOnPortAndConsole
Eternal ID of this version: #1001087/1
Text MD5: eac1089be4042544fb3947bcf409f9b9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-20 21:47:25
Source code size: 1699 bytes / 63 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 726 / 974
Referenced in: [show references]