diff --git a/remote_log/README.md b/remote_log/README.md index 53505ec..5842a33 100644 --- a/remote_log/README.md +++ b/remote_log/README.md @@ -49,3 +49,24 @@ Supports two operations: - `Upload data => id` - `Fetch id => data` + + +## Sketching interface + +NS POST <> +NS GET <> + +CAS POST <> +CAS GET <> + +What's in <>? + +- hash string +- blob +- [[h1_3, h2_3], [h1_2, h2_2]], next_page_chunk] + +- How do we want to encode this? +- Or we can use protobuf or json +- This seems mostly relevant for ns_head + +- lets just start with json thingy and NS/CAS sep diff --git a/remote_log/src/node.nim b/remote_log/src/node.nim index d683cc5..e8b362f 100644 --- a/remote_log/src/node.nim +++ b/remote_log/src/node.nim @@ -1,35 +1,79 @@ -import net, os, threadpool, asyncdispatch, asyncnet +import net, os, threadpool, asyncdispatch, asyncnet, strutils -var socket = newAsyncSocket() +var socket1 = newAsyncSocket() +var socket2 = newAsyncSocket() proc handler() {.noconv.} = stdout.writeLine("Shutting down connection.") - socket.close() + socket1.close() + socket2.close() quit 0 setControlCHook(handler) -proc connect(socket: AsyncSocket, serverAddr: string) {.async.} = - echo("Connecting to ", serverAddr) - await socket.connect(serverAddr, 6001.Port) - echo("Connected!") +proc isNSMessage(message: string): bool = + return message.split(' ')[0] == "NS" + +proc isCASMessage(message: string): bool = + return message.split(' ')[0] == "CAS" + +# XXX: drop first part? or maybe you know, use proper encoding +# XXX: Crashes if bad too, obv +proc prepareMessage(message: string): string = + var s = "" + try: + s = message.split(' ')[1] & " :" & message.split(':')[1] + except: + echo("prepareMessage error ", message) + s = "bad msg" + return s + +assert("NS POST :hi".split(' ')[0] == "NS") +var test = "NS POST :foo bar" +assert(test.split(' ')[1] & " :" & test.split(':')[1] == "POST :foo bar") +assert isNSMessage("NS POST :hi") +assert prepareMessage("NS POST :foo bar") == "POST :foo bar" + +proc connect(socket: AsyncSocket, serverAddr: string, portInt: int) {.async.} = + echo("Connecting to ", serverAddr, ":", portInt) + await socket.connect(serverAddr, portInt.Port) + echo(portInt, ": Connected!") while true: let line = await socket.recvLine() # TODO: parse message - echo("Incoming: ", line) + # TODO: Differentiate between NS and CAS + echo(portInt, ": Incoming: ", line) echo("Node started") # TODO: paramCount and paramStr parsing args let serverAddr = "localhost" -asyncCheck connect(socket, serverAddr) + +# Connect to NS +asyncCheck connect(socket1, serverAddr, 6001) + +# Connecting to CAS +asyncCheck connect(socket2, serverAddr, 6002) + var messageFlowVar = spawn stdin.readLine() while true: if messageFlowVar.isReady(): # TODO: create message - #echo("Sending \"", ^messageFlowVar, "\"") - let message = ^messageFlowVar & "\r\L" - asyncCheck socket.send(message) + # TODO: Differentiate between CAS and NS messages + echo("Sending \"", ^messageFlowVar, "\"") + + let prepared = prepareMessage(^messageFlowVar) + let message = prepared & "\r\L" + + if isNSMessage(^messageFlowVar): + echo("Send NS: ", prepared) + asyncCheck socket1.send(message) + elif isCASMessage(^messageFlowVar): + echo("Send CAS: ", prepared) + asyncCheck socket2.send(message) + else: + echo("Unknown message type ", ^messageFlowVar) + messageFlowVar = spawn stdin.readLine() asyncdispatch.poll() @@ -45,3 +89,13 @@ while true: # stringify if that's a thing, or JSON # Consider separating out interactive parts into client.nim + +# Send message triggers: +# Upload to cas => get id +# Upload to NS +# So other node can fetch + +# Interface here? +# Two connections + +# CAS and NS different how?