-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprocess.nim
More file actions
79 lines (61 loc) · 2 KB
/
process.nim
File metadata and controls
79 lines (61 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import std/[base64, options, json, logging, monotimes, net]
import ./ipc
import ./document
import ../../web/dom
import ../../shared/[nix, sugar]
import ../../../components/ipc/client/prelude
import jsony
proc htmlParse*(oparsingData: Option[ParseHTMLPacket]): HTMLParseResult =
if !oparsingData:
warn "Cannot reinterpret JSON data as `ParseHTMLPacket`!"
return
let parsingData = &oparsingData
info "Parsing HTML with source length: " & $parsingData.source.len & " chars"
let
startTime = getMonoTime()
document = parseHTML(newStringStream(decode(parsingData.source)))
endTime = getMonoTime()
info "Parsed HTML in " & $(endTime - startTime)
HTMLParseResult(document: some(document.parseHTMLDocument()))
type HTMLParserData* = object
running*: bool = true
documentsParsed*: uint64
proc talk(
client: var IPCClient, state: var HTMLParserData, process: FerusProcess
) {.inline.} =
var count: cint
discard nix.ioctl(client.socket.getFd().cint, nix.FIONREAD, addr(count))
if count < 1:
return
let
data = client.receive()
jdata = tryParseJson(data, JsonNode)
if not *jdata:
warn "Did not get any valid JSON data."
warn data
return
let kind = (&jdata).getOrDefault("kind").getStr().magicFromStr()
if not *kind:
warn "No `kind` field inside JSON data provided."
return
case &kind
of feParserParse:
client.setState(Processing)
let data = htmlParse(tryParseJson(data, ParseHTMLPacket))
client.send(data)
client.setState(Idling)
inc state.documentsParsed
of feGoodbye:
info "html: got goodbye packet, exiting."
info "html: we parsed " & $state.documentsParsed &
" documents throughout this process's lifetime"
state.running = false
else:
discard
proc htmlParserProcessLogic*(client: var IPCClient, process: FerusProcess) {.inline.} =
info "Entering HTML parser process logic."
var data = HTMLParserData()
client.setState(Idling)
client.poll()
while data.running:
client.talk(data, process)