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

88
LINES

< > BotCompany Repo | #1023335 // Assistant Web Server [token based, LIVE]

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

Uses 911K of libraries. Click here for Pure Java version (13632L/75K).

1  
!7
2  
3  
concept AToken { S token; long lastSeen; }
4  
concept ALine  { S token; S line, language; }
5  
concept AEvent { S token; S what; }
6  
concept ALineToSend   > ALine {}
7  
concept AIncomingLine > ALine { bool typed; }
8  
concept AAction       > ALine { bool verified; }
9  
10  
cmodule AssistantWebServer > DynPrintLog {
11  
  int httpPort = 8083, interval = 200, longPollTimeout = 60000;
12  
  S token, line;
13  
  
14  
  start-thread {
15  
    importantDB();
16  
    print("Tokens:");
17  
    printIndentLines(collect token(sortByFieldDesc lastSeen(list(AToken))));
18  
    dm_serveHttpFromFunction(httpPort, func(S uri, SS params) enter {
19  
      S token = params.get('token);
20  
      if (l(token) == 24) {
21  
        bumpToken(token);
22  
        S shortenedToken = assistant_shortenToken(token);
23  
        if (eq(uri, "/poll")) {
24  
          ret serveLongPoll(longPollTimeout, interval, func {
25  
            temp enter();
26  
            S text = withDBLock(func -> S {
27  
              L<ALineToSend> l = conceptsWhere(ALineToSend, +token);
28  
              if (empty(l)) null;
29  
              deleteConcepts(l);
30  
              S text = lines_rtrim(collect line(sortConceptsByID(l)));
31  
              ret text;
32  
            });
33  
            if (text != null)
34  
              printWithTime("Served output to " + token + ": " + text);
35  
            ret text;
36  
          });
37  
        }
38  
        
39  
        if (eq(uri, "/heard")) {
40  
          S line = params.get('line), language = params.get('language);
41  
          bool typed = eq("1", params.get('typed));
42  
          if (nempty(line)) {
43  
            cnew(AIncomingLine, +token, +line, +typed, +language);
44  
            print("Heard from " + token + ": " + line);
45  
            vmBus_send phoneCatHeard(litmap(module := dm_moduleID(), +token, +line, +typed, +language));
46  
          }
47  
          ret "OK";
48  
        }
49  
        
50  
        if (eq(uri, "/event")) {
51  
          S what = params.get('what);
52  
          if (nempty(what)) {
53  
            cnew(AEvent, +token, +what);
54  
            print("Event from " + shortenedToken + ": " + what);
55  
            vmBus_send phoneCatEvent(litmap(module := dm_moduleID(), +token, +what));
56  
            ret "OK";
57  
          }
58  
        }
59  
      } // end of have-token block
60  
61  
      ret serveText("hä");
62  
    });
63  
  }
64  
  
65  
  void bumpToken(S token) { cset(uniq_sync(AToken, +token), lastSeen := now()); }
66  
  
67  
  visual centerAndSouth(super, westCenterAndEastWithMargins(
68  
    withLabel("Token:", jMinWidth(150, dm_textField('token))),
69  
    withLabel("Text:", dm_textField('line)),
70  
    jbutton("Send", rThread guiSendLine)));
71  
      
72  
  void guiSendLine enter {
73  
    dm_trimFields('line, 'token);
74  
    if (l(token) < 24) {
75  
      L<AToken> tokens = filter(list(AToken), t -> startsWithIC(t.token, token));
76  
      if (l(tokens) > 1) ret with infoBox("Multiple tokens found, please give more chars");
77  
      if (l(tokens) == 1) setField(token := first(tokens).token);
78  
    }
79  
    if (l(token) != 24) ret with infoBox("Bad token");
80  
    if (empty(line)) ret;
81  
    cnew(ALineToSend, +token, +line);
82  
    print("Line sent");
83  
  }
84  
  
85  
  // API
86  
  
87  
  void sendToUser(S token, S line) { cnew(ALineToSend, +token, +line); }
88  
}

Author comment

Began life as a copy of #1023280

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: #1023335
Snippet name: Assistant Web Server [token based, LIVE]
Eternal ID of this version: #1023335/43
Text MD5: 413c6605f4ca4f17c840d45bd15a1e9e
Transpilation MD5: fa4810e9d9e5c82e27389d390ea3c563
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-13 12:53:57
Source code size: 3148 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 300 / 9804
Version history: 42 change(s)
Referenced in: [show references]