Remote log: Node and ns mocking start

Ad hoc post/get message handling
This commit is contained in:
Oskar Thoren 2019-08-05 12:18:59 +08:00
parent 5b581aeeff
commit 9341a74858
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
2 changed files with 106 additions and 0 deletions

23
remote_log/node.nim Normal file
View File

@ -0,0 +1,23 @@
import net, os
let client = newSocket()
proc handler() {.noconv.} =
stdout.writeLine("Shutting down connection.")
client.close()
quit 0
setControlCHook(handler)
client.connect("127.0.0.1", Port(6001))
stdout.writeLine("Client connected to name service on 127.0.0.1:6001")
while true:
stdout.write("> ")
let message: string = stdin.readLine()
client.send(message & "\r\L")
client.close()
# 1) Node wants to post data to ns, and ns stores it
# 2) Node can recevive

83
remote_log/ns.nim Normal file
View File

@ -0,0 +1,83 @@
import net, os, nativesockets, strutils
var server: Socket = newSocket()
proc handler() {.noconv.} =
stdout.write("Shutting down server.")
# Unclear if this matters with ReuseAddr
server.close()
quit 0
setControlCHook(handler)
server.setSockOpt(OptReuseAddr, true)
server.getFd().setBlocking(false)
server.bindAddr(Port(6001))
server.listen()
# TODO: Implicit for Alice, can be part of startup args or so
stdout.writeLine("Name service starting, listening on 6001")
var clients: seq[Socket] = @[]
#assert("POST :foo bar".split(':')[1] == "foo bar"
type
Message = object
arg: string
data: string
proc parseMessage(message: string): Message =
var msg = Message()
stdout.writeLine("Server: received from client: ", message)
try:
msg.arg = message.split(' ')[0]
msg.data = message.split(':')[1]
except:
echo("Unable to parse message: ", message)
return msg
proc handleMessage(message: Message) =
let arg = message.arg
let data = message.data
if arg == "POST":
echo("posting: ", data)
elif arg == "GET":
echo("getting: ", data)
else:
echo("Unable to handle message: ", message)
while true:
try:
var client: Socket = new(Socket)
server.accept(client)
clients.add(client)
stdout.writeLine("Server: client connected")
except OSError:
discard
var clientsToRemove: seq[int] = @[]
for index, client in clients:
try:
let message: string = client.recvLine(timeout = 1)
if message == "":
clientsToRemove.add(index)
handleMessage(parseMessage(message))
except TimeoutError:
discard
for index in clientsToRemove:
clients.del(index)
stdout.writeLine("Server: client disconnected")
server.close()