mirror of https://github.com/vacp2p/research.git
Remote log: Basic CAS mocking behavior
This commit is contained in:
parent
de378ae938
commit
a863e63641
|
@ -1,4 +1,4 @@
|
||||||
import net, os, nativesockets, strutils
|
import net, os, nativesockets, strutils, std/sha1, tables
|
||||||
|
|
||||||
type
|
type
|
||||||
Message = object
|
Message = object
|
||||||
|
@ -27,19 +27,24 @@ setControlCHook(handler)
|
||||||
|
|
||||||
server.setSockOpt(OptReuseAddr, true)
|
server.setSockOpt(OptReuseAddr, true)
|
||||||
server.getFd().setBlocking(false)
|
server.getFd().setBlocking(false)
|
||||||
server.bindAddr(Port(6001))
|
server.bindAddr(Port(6002))
|
||||||
server.listen()
|
server.listen()
|
||||||
|
|
||||||
# TODO: Implicit for Alice, can be part of startup args or so
|
# TODO: Implicit for Alice, can be part of startup args or so
|
||||||
stdout.writeLine("Name service starting, listening on 6001")
|
stdout.writeLine("Content addressable storage starting, listening on 6002")
|
||||||
|
|
||||||
var clients: seq[Socket] = @[]
|
var clients: seq[Socket] = @[]
|
||||||
|
|
||||||
#assert("POST :foo bar".split(':')[1] == "foo bar"
|
#assert("POST :foo bar".split(':')[1] == "foo bar"
|
||||||
|
|
||||||
# XXX: Single global mutating state, fish memory
|
# XXX: Single global mutating state, fish memory
|
||||||
# Could be a DB type and persist to disk
|
var contentStorage = initTable[string,string]()
|
||||||
var currentName = ""
|
|
||||||
|
proc contentHash(data: string): string =
|
||||||
|
# Prepend constant to highlight fact that hash fns can be different
|
||||||
|
let str = "storage-" & data
|
||||||
|
let sha1 = secureHash(str)
|
||||||
|
return $sha1
|
||||||
|
|
||||||
proc parseMessage(message: string): Message =
|
proc parseMessage(message: string): Message =
|
||||||
var msg = Message()
|
var msg = Message()
|
||||||
|
@ -52,18 +57,27 @@ proc parseMessage(message: string): Message =
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
proc store(data: string): string =
|
||||||
|
let hash = contentHash(data)
|
||||||
|
echo("store: ", hash)
|
||||||
|
contentStorage[hash] = data
|
||||||
|
echo("store content: ", $contentStorage)
|
||||||
|
return hash
|
||||||
|
|
||||||
proc handleMessage(message: Message): Response =
|
proc handleMessage(message: Message): Response =
|
||||||
let arg = message.arg
|
let arg = message.arg
|
||||||
let data = message.data
|
let data = message.data
|
||||||
|
|
||||||
if arg == "POST":
|
if arg == "POST":
|
||||||
echo("posting: ", data)
|
echo("posting: ", data)
|
||||||
currentName = data
|
let key = store(data)
|
||||||
return Response(code: OK, data: "success")
|
return Response(code: OK, data: key)
|
||||||
|
|
||||||
elif arg == "GET":
|
elif arg == "GET":
|
||||||
echo("getting: ", data)
|
echo("getting: ", data)
|
||||||
return Response(code: OK, data: currentName)
|
# XXX: No validation etc, nil?
|
||||||
|
let data = contentStorage[data]
|
||||||
|
return Response(code: OK, data: data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
echo("Unable to handle message: ", message)
|
echo("Unable to handle message: ", message)
|
||||||
|
|
Loading…
Reference in New Issue