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

90
LINES

< > BotCompany Repo | #1023382 // Assistant Web Server [alternative version with registration]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1626K of libraries. Click here for Pure Java version (13668L/76K).

!7

concept AToken { S token; long lastSeen; }
concept ALine  { S token; S line, language; }
concept ALineToSend   > ALine {}
concept AIncomingLine > ALine { bool typed; }
concept AAction       > ALine { bool verified; }

cmodule AssistantWebServer > DynPrintLog {
  int httpPort = 8083, interval = 200, longPollTimeout = 60000;
  S token, line;
  
  start-thread {
    importantDB();
    print("Tokens:");
    printIndentLines(collect token(sortByFieldDesc lastSeen(list(AToken))));
    dm_serveHttpFromFunction(httpPort, func(S uri, SS params) enter {
      if (eq(uri, "/register"))
        ret serveText((S) dm_call(dm_assistant_usersCRUD(), 'registerUser, params.get('email), params.get('pwHash)));

      if (eq(uri, "/checkPW")) {
        S mail = params.get('email);
        if (!isValidEmailAddress(mail)) ret serveText("Not a valid email address");
        S userID = cast dm_call(dm_assistant_usersCRUD(), 'userIDForEmail, mail);
        if (userID == null) ret serveText("User not found");
        bool ok = cast dm_call(dm_assistant_passwordsCRUD(), 'checkPassword, userID, params.get('pwHash));
        ret serveText(ok ? "OK" : "Wrong password");
      }

      // token-based
      
      S token = params.get('token);
      if (l(token) == 24) {
        bumpToken(token);
        if (eq(uri, "/poll")) {
          ret serveLongPoll(longPollTimeout, interval, func {
            temp enter();
            S text = withDBLock(func -> S {
              L<ALineToSend> l = conceptsWhere(ALineToSend, +token);
              if (empty(l)) null;
              deleteConcepts(l);
              S text = lines_rtrim(collect line(sortConceptsByID(l)));
              ret text;
            });
            if (text != null)
              printWithTime("Served output to " + token + ": " + text);
            ret text;
          });
        }
        
        if (eq(uri, "/heard")) {
          S line = params.get('line), language = params.get('language);
          bool typed = eq("1", params.get('typed));
          if (nempty(line)) {
            cnew(AIncomingLine, +token, +line, +typed, +language);
            print("Heard from " + token + ": " + line);
            vmBus_send phoneCatHeard(litmap(module := dm_moduleID(), +token, +line, +typed, +language));
          }
          ret "OK";
        }
      }

      ret serveText("hä");
    });
  }
  
  void bumpToken(S token) { cset(uniq_sync(AToken, +token), lastSeen := now()); }
  
  visual centerAndSouth(super, westCenterAndEastWithMargins(
    withLabel("Token:", jMinWidth(150, dm_textField('token))),
    withLabel("Text:", dm_textField('line)),
    jbutton("Send", rThread guiSendLine)));
      
  void guiSendLine enter {
    dm_trimFields('line, 'token);
    if (l(token) < 24) {
      L<AToken> tokens = filter(list(AToken), t -> startsWithIC(t.token, token));
      if (l(tokens) > 1) ret with infoBox("Multiple tokens found, please give more chars");
      if (l(tokens) == 1) setField(token := first(tokens).token);
    }
    if (l(token) != 24) ret with infoBox("Bad token");
    if (empty(line)) ret;
    cnew(ALineToSend, +token, +line);
    print("Line sent");
  }
  
  // API
  
  void sendToUser(S token, S line) { cnew(ALineToSend, +token, +line); }
}

Author comment

Began life as a copy of #1023335

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1023382
Snippet name: Assistant Web Server [alternative version with registration]
Eternal ID of this version: #1023382/1
Text MD5: da61488f9d6fa5f5a4276b50f5e583a1
Transpilation MD5: c0d87b8e3622f278f5f1708b7249e0c0
Author: stefan
Category: javax / web / a.i. / android
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-06-12 03:05:49
Source code size: 3328 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 321 / 396
Referenced in: [show references]