mirror of https://github.com/vacp2p/research.git
Node works with ad hoc protocol for both CAS and NS naive
This commit is contained in:
parent
812e6e3ec4
commit
2255b78a80
|
@ -49,3 +49,24 @@ Supports two operations:
|
||||||
|
|
||||||
- `Upload data => id`
|
- `Upload data => id`
|
||||||
- `Fetch id => data`
|
- `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
|
||||||
|
|
|
@ -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.} =
|
proc handler() {.noconv.} =
|
||||||
stdout.writeLine("Shutting down connection.")
|
stdout.writeLine("Shutting down connection.")
|
||||||
socket.close()
|
socket1.close()
|
||||||
|
socket2.close()
|
||||||
quit 0
|
quit 0
|
||||||
|
|
||||||
setControlCHook(handler)
|
setControlCHook(handler)
|
||||||
|
|
||||||
proc connect(socket: AsyncSocket, serverAddr: string) {.async.} =
|
proc isNSMessage(message: string): bool =
|
||||||
echo("Connecting to ", serverAddr)
|
return message.split(' ')[0] == "NS"
|
||||||
await socket.connect(serverAddr, 6001.Port)
|
|
||||||
echo("Connected!")
|
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:
|
while true:
|
||||||
let line = await socket.recvLine()
|
let line = await socket.recvLine()
|
||||||
# TODO: parse message
|
# TODO: parse message
|
||||||
echo("Incoming: ", line)
|
# TODO: Differentiate between NS and CAS
|
||||||
|
echo(portInt, ": Incoming: ", line)
|
||||||
|
|
||||||
echo("Node started")
|
echo("Node started")
|
||||||
# TODO: paramCount and paramStr parsing args
|
# TODO: paramCount and paramStr parsing args
|
||||||
let serverAddr = "localhost"
|
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()
|
var messageFlowVar = spawn stdin.readLine()
|
||||||
while true:
|
while true:
|
||||||
if messageFlowVar.isReady():
|
if messageFlowVar.isReady():
|
||||||
# TODO: create message
|
# TODO: create message
|
||||||
#echo("Sending \"", ^messageFlowVar, "\"")
|
# TODO: Differentiate between CAS and NS messages
|
||||||
let message = ^messageFlowVar & "\r\L"
|
echo("Sending \"", ^messageFlowVar, "\"")
|
||||||
asyncCheck socket.send(message)
|
|
||||||
|
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()
|
messageFlowVar = spawn stdin.readLine()
|
||||||
|
|
||||||
asyncdispatch.poll()
|
asyncdispatch.poll()
|
||||||
|
@ -45,3 +89,13 @@ while true:
|
||||||
# stringify if that's a thing, or JSON
|
# stringify if that's a thing, or JSON
|
||||||
|
|
||||||
# Consider separating out interactive parts into client.nim
|
# 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?
|
||||||
|
|
Loading…
Reference in New Issue