Uses 1626K of libraries. Click here for Pure Java version (13668L/76K).
1 | !7 |
2 | |
3 | concept AToken { S token; long lastSeen; } |
4 | concept ALine { S token; S line, language; } |
5 | concept ALineToSend > ALine {} |
6 | concept AIncomingLine > ALine { bool typed; } |
7 | concept AAction > ALine { bool verified; } |
8 | |
9 | cmodule AssistantWebServer > DynPrintLog { |
10 | int httpPort = 8083, interval = 200, longPollTimeout = 60000; |
11 | S token, line; |
12 | |
13 | start-thread { |
14 | importantDB(); |
15 | print("Tokens:"); |
16 | printIndentLines(collect token(sortByFieldDesc lastSeen(list(AToken)))); |
17 | dm_serveHttpFromFunction(httpPort, func(S uri, SS params) enter { |
18 | if (eq(uri, "/register")) |
19 | ret serveText((S) dm_call(dm_assistant_usersCRUD(), 'registerUser, params.get('email), params.get('pwHash))); |
20 | |
21 | if (eq(uri, "/checkPW")) { |
22 | S mail = params.get('email); |
23 | if (!isValidEmailAddress(mail)) ret serveText("Not a valid email address"); |
24 | S userID = cast dm_call(dm_assistant_usersCRUD(), 'userIDForEmail, mail); |
25 | if (userID == null) ret serveText("User not found"); |
26 | bool ok = cast dm_call(dm_assistant_passwordsCRUD(), 'checkPassword, userID, params.get('pwHash)); |
27 | ret serveText(ok ? "OK" : "Wrong password"); |
28 | } |
29 | |
30 | // token-based |
31 | |
32 | S token = params.get('token); |
33 | if (l(token) == 24) { |
34 | bumpToken(token); |
35 | if (eq(uri, "/poll")) { |
36 | ret serveLongPoll(longPollTimeout, interval, func { |
37 | temp enter(); |
38 | S text = withDBLock(func -> S { |
39 | L<ALineToSend> l = conceptsWhere(ALineToSend, +token); |
40 | if (empty(l)) null; |
41 | deleteConcepts(l); |
42 | S text = lines_rtrim(collect line(sortConceptsByID(l))); |
43 | ret text; |
44 | }); |
45 | if (text != null) |
46 | printWithTime("Served output to " + token + ": " + text); |
47 | ret text; |
48 | }); |
49 | } |
50 | |
51 | if (eq(uri, "/heard")) { |
52 | S line = params.get('line), language = params.get('language); |
53 | bool typed = eq("1", params.get('typed)); |
54 | if (nempty(line)) { |
55 | cnew(AIncomingLine, +token, +line, +typed, +language); |
56 | print("Heard from " + token + ": " + line); |
57 | vmBus_send phoneCatHeard(litmap(module := dm_moduleID(), +token, +line, +typed, +language)); |
58 | } |
59 | ret "OK"; |
60 | } |
61 | } |
62 | |
63 | ret serveText("hä"); |
64 | }); |
65 | } |
66 | |
67 | void bumpToken(S token) { cset(uniq_sync(AToken, +token), lastSeen := now()); } |
68 | |
69 | visual centerAndSouth(super, westCenterAndEastWithMargins( |
70 | withLabel("Token:", jMinWidth(150, dm_textField('token))), |
71 | withLabel("Text:", dm_textField('line)), |
72 | jbutton("Send", rThread guiSendLine))); |
73 | |
74 | void guiSendLine enter { |
75 | dm_trimFields('line, 'token); |
76 | if (l(token) < 24) { |
77 | L<AToken> tokens = filter(list(AToken), t -> startsWithIC(t.token, token)); |
78 | if (l(tokens) > 1) ret with infoBox("Multiple tokens found, please give more chars"); |
79 | if (l(tokens) == 1) setField(token := first(tokens).token); |
80 | } |
81 | if (l(token) != 24) ret with infoBox("Bad token"); |
82 | if (empty(line)) ret; |
83 | cnew(ALineToSend, +token, +line); |
84 | print("Line sent"); |
85 | } |
86 | |
87 | // API |
88 | |
89 | void sendToUser(S token, S line) { cnew(ALineToSend, +token, +line); } |
90 | } |
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: | 322 / 397 |
Referenced in: | [show references] |