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

63
LINES

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

JavaX fragment (include)

!636 // for includes
!stringfunc {


!1000868 // dialogHandler shortcut

!include #1001065 // Dialog classes

static int answerQuestions_defaultPort = 5000;

static void answerQuestionsOnPort(final StringFunc f) {
  answerQuestionsOnPort(answerQuestions_defaultPort, f);
}

static void answerQuestionsOnPort(int port, final StringFunc f) {
  startDialogServer(port, dialogHandler {
    String dialogID = randomID(8);
    
    io.sendLine("Your ID: " + dialogID);
    
    while (io.isStillConnected()) {
      if (io.waitForLine()) {
        String line = io.readLineNoBlock();
        S s = dialogID + " at " + now() + ": " + quote(line);
        print(s);
        if (line == "bye") {
          io.sendLine("bye stranger");
          return;
        }
        S answer = answerQuestions_getAnswer(line, f);
        io.sendLine(answer == null ? "null" : answer);
        //appendToLog(logFile, s);
      }
    }
  });
}

static S answerQuestions_getAnswer(S line, StringFunc f) {
  S answer;
  try {
    answer = f.get(line);
  } catch (Throwable e) {
    e.printStackTrace();
    answer = e.toString();
  }
  print("> " + answer);
  return answer;
}

static void answerQuestionsOnPortAndConsole(final StringFunc f) {
  answerQuestionsOnPortAndConsole(answerQuestions_defaultPort, f);
}

static void answerQuestionsOnPortAndConsole(int port, final StringFunc f) {
  answerQuestionsOnPort(port, f);
  print("You may also type on this console.");
  S line;
  while ((line = readLine()) != null) {
    if (line == "bye")
      print("! bye stranger");
    else
      answerQuestions_getAnswer(line, f); // prints answer on console too
  }
}

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: 725 / 972
Referenced in: #1001103 - makeAndroid
#1002427 - Accellerating 629 (SPIKE)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)