More on Tornado/FIFOs etc.

This commit is contained in:
Raycho Mukelov 2023-09-09 19:56:54 +03:00
parent 5c10cf6ac4
commit 8f60c2381b
4 changed files with 69 additions and 0 deletions

0
Dockerfile Normal file → Executable file
View File

8
pipe.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
x=''
while [ t ];
do
$(`x < RAFTNODESENDMSGRESPPIPE`);
echo "$($x)" > RAFTNODERECEIVEMSGPIPE;
done

View File

@ -3,6 +3,7 @@ import basic_state_machine
import std/json
import uuids
import chronicles
import msgpack4nim
type
RaftPeerConf = object
@ -24,4 +25,5 @@ proc loadConfig() =
info "Conf", config=repr(conf)
if isMainModule:
loadConfig()
var node = RaftNode[SmCommand, SmState].new()

View File

@ -0,0 +1,59 @@
import asyncio
import tornado
import os
import sys
import errno
import msgpack
writeFIFO = '/home/raych/prg/nim-raft/RAFTNODESENDMSGRESPPIPE'
readFIFO = '/home/raych/prg/nim-raft/RAFTNODERECEIVEMSGPIPE'
def writeFifo(data):
with open(writeFIFO, "a") as fw:
fw.write(data)
def readPipe():
with open(readFIFO, "r") as fifo:
data = fifo.read()
if len(data) == 0:
pass
# print("Writer closed")
else:
print('Read: {0}'.format(data))
return data
class MainHandler(tornado.web.RequestHandler):
def post(self):
s = self.request.body.decode("utf-8")
print(s)
writeFifo(s)
self.write(msgpack.dumps(readPipe()))
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
async def main():
if len(sys.argv) < 2:
print("Usage: tornado_simple_raft_node_server.py <port>")
shutdown_event.set()
return
app = make_app()
app.listen(int(sys.argv[1]))
await asyncio.Event().wait()
if __name__ == "__main__":
try:
os.mkfifo(readFIFO)
os.mkfifo(writeFIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
asyncio.run(main())