WIP misc parsing/creation

still some issues with .id field now
This commit is contained in:
Oskar Thoren 2019-08-12 02:18:37 +08:00
parent 8a7042741e
commit fe09be631e
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
2 changed files with 19 additions and 12 deletions

View File

@ -48,6 +48,7 @@ proc contentHash(data: string): string =
let sha1 = secureHash(str)
return $sha1
# TODO: Remove me
proc parseMessage(message: string): Message =
var msg = Message()
stdout.writeLine("Server: received from client: ", message)
@ -59,6 +60,12 @@ proc parseMessage(message: string): Message =
return msg
proc stringifyProtobuf[T](protobuf: T): string =
var stream = newStringStream()
stream.write(protobuf)
var stringified = $stream.data
return stringified
proc store(data: string): string =
let hash = contentHash(data)
contentStorage[hash] = data
@ -108,6 +115,7 @@ proc handleMessage2(msg: string): CASResponse =
var resp = new CASResponse
resp.id = key
resp.data = data
echo("resp: ", resp)
return resp
elif readMsg.has(operation) and readMsg.operation == CASRequest_Op.GET:
# TODO: Handle GET op
@ -135,20 +143,15 @@ while true:
if message == "":
clientsToRemove.add(index)
let resp = handleMessage2(message)
#let resp = handleMessage(parseMessage(message))
# TODO: Respond to client
# XXX: Client is currently non-interactive so sending to other client
# That is, node is split into node_receiving and node_sending
# XXX: Getting field id not init, er
let casResp = handleMessage2(message)
let resp = stringifyProtobuf(casResp)
# Sending to all clients:
echo("RESPONSE: ", resp)
# TODO XXX: This seems off? Just send to replier here
# XXX: Wrong, need to serialize here right
# TODO: Fixme
# TODO: Should send to actually sending client, not all
for c in clients:
c.send($resp & "\r\L")
let payload = $resp & "\r\L"
c.send(payload)
except TimeoutError:
discard

View File

@ -12,7 +12,7 @@ proc handler() {.noconv.} =
setControlCHook(handler)
# TODO: Move to protocol yeah
proc parseCASResponse(msg: string): CASResponse =
var stream = newStringStream()
stream.write(msg)
@ -43,9 +43,13 @@ proc connect(socket: AsyncSocket, serverAddr: string, portInt: int) {.async.} =
try:
var readMsg = parseCASResponse(line)
echo("readMsg2: ", readMsg)
# XXX: This should be hit, wy not?
if readMsg.has(id):
# XXX: for data, need to map it back
echo("readMsg id: ", readMsg.id)
echo("readMsg id regardless:", readMsg.id)
if readMsg.has(data):
echo("readMsg data: ", readMsg.data)
except:
echo("Ignoring incoming message: " & getCurrentExceptionMsg())