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

107
LINES

< > BotCompany Repo | #1000867 // Chat server

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (3416L/23K/76K).

!7

static int chatPort = 9751; // just any number
static File logFile;
static L<S> lines = synchroList();

p {
  installHelloMessage("#1000867 - Chat server");
  setUpChat();
  makeAndroid("This is the chat server.");
  sleep();
}

static synchronized S answer(S s) {
  new Matches m;
  
  if (match3("how many lines are in the memory log", s))
    return "" + lines.size();
  if (match3("how many bytes are in the disk log", s))
    return "" + logFile.length();
  if (match3("where is the disk log", s))
    return logFile.getAbsolutePath();
  if (match3("say *", s, m)) {
    say("anonymous", unquote(m.m[0]));
    return "ok";
  }
  
  return null;
}

static void setUpChat() {
  autoReportToChatOff();
  if (logFile == null)
    logFile = new File(userHome(), "JavaX/Chat/log.txt");
  mkdirsForFile(logFile);
  
  if (!startDialogServerIfPortAvailable(chatPort, dialogHandler {
    String dialogID = randomID(8);
    
    io.sendLine("Your ID: " + dialogID);
    
    while (io.isStillConnected()) {
      if (io.waitForLine()) {
        String line = io.readLineNoBlock();
        say(dialogID, line);
      }
    }
  }))
    print("Chat port " + chatPort + " not available!");
  else {
    print("Chat set up on port " + chatPort + ".");
    
  // Set up sucking port
  if (!startDialogServerIfPortAvailable(chatPort+1, dialogHandler {
    try {
      int i = 0;
      boolean present = false;
      while (io.isStillConnected()) {
        if (i < lines.size()) {
          do {
            io.sendLine(lines.get(i++));
          } while (i < lines.size());
          if (!present) {
            present = true;
            io.sendLine(""); // empty line indicates we have reached present time
          }
        }

        sleep(100);
      }
    } catch (Exception e) {
      // it's probably a disconnect, just handle it quietly
    }
  }))
    print("Chat port " + (chatPort+1) + " not available!");
  else
    print("Chat suck channel set up on port " + (chatPort+1) + ".");
  }
  
  // Set up history-less sucking port
  if (!startDialogServerIfPortAvailable(chatPort+2, dialogHandler {
    try {
      int i = lines.size();
      while (io.isStillConnected()) {
        while (i < lines.size())
          io.sendLine(lines.get(i++));
        sleep(100);
      }
    } catch (Exception e) {
      // it's probably a disconnect, just handle it quietly
    }
  }))
    print("Chat port " + (chatPort+2) + " not available!");
  else
    print("Chat historyless suck channel set up on port " + (chatPort+2) + ".");
 }

static void appendToLog(File logFile, String s) {
  appendToFile(logFile.getPath(), "\n" + s + "\n");
}

static void say(S dialogID, S line) {
  S s = dialogID + " at " + now() + ": " + quote(line);
  print(s);
  lines.add(s);
  appendToLog(logFile, s);
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000867
Snippet name: Chat server
Eternal ID of this version: #1000867/3
Text MD5: 95a5d2bbd8587f318a2fbc607f667176
Transpilation MD5: f84d16315b2fb2bd63fb1f51ddff7661
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-06-20 17:52:36
Source code size: 2891 bytes / 107 lines
Pitched / IR pitched: No / No
Views / Downloads: 735 / 2214
Version history: 2 change(s)
Referenced in: #1000867 - Chat server
#1000928 - Chat client
#1001069 - hasInvocation bug test
#1001334 - Kill a program and wait until it is dead
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)