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).

1  
!7
2  
3  
static int chatPort = 9751; // just any number
4  
static File logFile;
5  
static L<S> lines = synchroList();
6  
7  
p {
8  
  installHelloMessage("#1000867 - Chat server");
9  
  setUpChat();
10  
  makeAndroid("This is the chat server.");
11  
  sleep();
12  
}
13  
14  
static synchronized S answer(S s) {
15  
  new Matches m;
16  
  
17  
  if (match3("how many lines are in the memory log", s))
18  
    return "" + lines.size();
19  
  if (match3("how many bytes are in the disk log", s))
20  
    return "" + logFile.length();
21  
  if (match3("where is the disk log", s))
22  
    return logFile.getAbsolutePath();
23  
  if (match3("say *", s, m)) {
24  
    say("anonymous", unquote(m.m[0]));
25  
    return "ok";
26  
  }
27  
  
28  
  return null;
29  
}
30  
31  
static void setUpChat() {
32  
  autoReportToChatOff();
33  
  if (logFile == null)
34  
    logFile = new File(userHome(), "JavaX/Chat/log.txt");
35  
  mkdirsForFile(logFile);
36  
  
37  
  if (!startDialogServerIfPortAvailable(chatPort, dialogHandler {
38  
    String dialogID = randomID(8);
39  
    
40  
    io.sendLine("Your ID: " + dialogID);
41  
    
42  
    while (io.isStillConnected()) {
43  
      if (io.waitForLine()) {
44  
        String line = io.readLineNoBlock();
45  
        say(dialogID, line);
46  
      }
47  
    }
48  
  }))
49  
    print("Chat port " + chatPort + " not available!");
50  
  else {
51  
    print("Chat set up on port " + chatPort + ".");
52  
    
53  
  // Set up sucking port
54  
  if (!startDialogServerIfPortAvailable(chatPort+1, dialogHandler {
55  
    try {
56  
      int i = 0;
57  
      boolean present = false;
58  
      while (io.isStillConnected()) {
59  
        if (i < lines.size()) {
60  
          do {
61  
            io.sendLine(lines.get(i++));
62  
          } while (i < lines.size());
63  
          if (!present) {
64  
            present = true;
65  
            io.sendLine(""); // empty line indicates we have reached present time
66  
          }
67  
        }
68  
69  
        sleep(100);
70  
      }
71  
    } catch (Exception e) {
72  
      // it's probably a disconnect, just handle it quietly
73  
    }
74  
  }))
75  
    print("Chat port " + (chatPort+1) + " not available!");
76  
  else
77  
    print("Chat suck channel set up on port " + (chatPort+1) + ".");
78  
  }
79  
  
80  
  // Set up history-less sucking port
81  
  if (!startDialogServerIfPortAvailable(chatPort+2, dialogHandler {
82  
    try {
83  
      int i = lines.size();
84  
      while (io.isStillConnected()) {
85  
        while (i < lines.size())
86  
          io.sendLine(lines.get(i++));
87  
        sleep(100);
88  
      }
89  
    } catch (Exception e) {
90  
      // it's probably a disconnect, just handle it quietly
91  
    }
92  
  }))
93  
    print("Chat port " + (chatPort+2) + " not available!");
94  
  else
95  
    print("Chat historyless suck channel set up on port " + (chatPort+2) + ".");
96  
 }
97  
98  
static void appendToLog(File logFile, String s) {
99  
  appendToFile(logFile.getPath(), "\n" + s + "\n");
100  
}
101  
102  
static void say(S dialogID, S line) {
103  
  S s = dialogID + " at " + now() + ": " + quote(line);
104  
  print(s);
105  
  lines.add(s);
106  
  appendToLog(logFile, s);
107  
}

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: 747 / 2229
Version history: 2 change(s)
Referenced in: [show references]