|
| 1 | +from ctypes import * |
| 2 | +from queue import Queue |
| 3 | +from threading import Thread |
| 4 | +import socket |
| 5 | +import json |
| 6 | + |
| 7 | +# note: these files must first be built. see the make file |
| 8 | +hashing = CDLL('./lib/hashing/hashing.so') |
| 9 | +hexutil = CDLL('./lib/hexutil/hexutil.so') |
| 10 | +config = CDLL('./lib/config/config.so') |
| 11 | +stringutil = CDLL('./lib/stringutil/stringutil.so') |
| 12 | + |
| 13 | +class BytesResponse(Structure): |
| 14 | + _fields_ = [ |
| 15 | + ("r0", c_void_p), |
| 16 | + ("r1", c_int), |
| 17 | + ] |
| 18 | + |
| 19 | +stringutil.CompactJSON.restype = BytesResponse |
| 20 | +hexutil.DecodeString.restype = BytesResponse |
| 21 | + |
| 22 | +ErrMethodAlreadyRegistered = Exception("method already registered") |
| 23 | +ErrMethodNotExists = Exception("method does not exist") |
| 24 | +ErrIncorrectNumberOfArgs = Exception("method requires two arguments") |
| 25 | + |
| 26 | +class C3(): |
| 27 | + def __init__(self, statefile): |
| 28 | + self.methods = {} |
| 29 | + self.state = {} |
| 30 | + self.q = Queue(maxsize=0) |
| 31 | + self.statefile = statefile |
| 32 | + |
| 33 | + def registerMethod(self, methodName, ifn): |
| 34 | + b = bytearray() |
| 35 | + b.extend(map(ord, methodName)) |
| 36 | + arr = (c_byte * len(b))(*b) |
| 37 | + |
| 38 | + methodNameHash = c_char_p(hashing.HashToHexString(arr, len(arr))).value.decode('utf-8') |
| 39 | + if methodNameHash in self.methods: |
| 40 | + raise ErrMethodAlreadyExists |
| 41 | + |
| 42 | + def newMethod(*args): |
| 43 | + if len(args) != 2: |
| 44 | + raise ErrIncorrectNumberOfArgs |
| 45 | + |
| 46 | + key = args[0] |
| 47 | + res = hexutil.DecodeString(c_char_p(key.encode('utf-8'))) |
| 48 | + ArrayType = c_ubyte*(c_int(res.r1).value) |
| 49 | + pa = cast(c_void_p(res.r0), POINTER(ArrayType)) |
| 50 | + key = "".join(map(chr, pa.contents[:])) |
| 51 | + |
| 52 | + val = args[1] |
| 53 | + res = hexutil.DecodeString(c_char_p(val.encode('utf-8'))) |
| 54 | + ArrayType = c_ubyte*(c_int(res.r1).value) |
| 55 | + pa = cast(c_void_p(res.r0), POINTER(ArrayType)) |
| 56 | + val = "".join(map(chr, pa.contents[:])) |
| 57 | + |
| 58 | + try: |
| 59 | + res = ifn(key, val) |
| 60 | + print("[c3] result", res) |
| 61 | + except Exception as inst: |
| 62 | + print("[c3] invokation failed", inst) |
| 63 | + |
| 64 | + self.methods[methodNameHash] = newMethod |
| 65 | + |
| 66 | + def setInitialState(self): |
| 67 | + currState = "" |
| 68 | + |
| 69 | + file = open(self.statefile, "r") |
| 70 | + currState = file.read() |
| 71 | + |
| 72 | + if len(currState) == 0: |
| 73 | + print("no current state") |
| 74 | + return |
| 75 | + |
| 76 | + b = bytearray() |
| 77 | + b.extend(map(ord, currState)) |
| 78 | + arr = (c_byte * len(b))(*b) |
| 79 | + |
| 80 | + res = stringutil.CompactJSON(arr, len(arr)) |
| 81 | + ArrayType = c_ubyte*(c_int(res.r1).value) |
| 82 | + pa = cast(c_void_p(res.r0), POINTER(ArrayType)) |
| 83 | + |
| 84 | + self.state = json.loads("".join(map(chr, pa.contents[:]))) |
| 85 | + print("initial state loaded") |
| 86 | + |
| 87 | + def process(self, payloadBytes): |
| 88 | + payload = json.loads(payloadBytes) |
| 89 | + |
| 90 | + if len(payload) <= 1: |
| 91 | + return |
| 92 | + |
| 93 | + # ifc format is [a, b, c] |
| 94 | + if isinstance(payload[0], str): |
| 95 | + self.invoke(payload[0], payload[1:]) |
| 96 | + |
| 97 | + # ifc format is [[a, b, c], [a, b, c]] |
| 98 | + for ifc in payload: |
| 99 | + self.invoke(ifc[0], *ifc[1:]) |
| 100 | + |
| 101 | + def invoke(self, methodName, *params): |
| 102 | + b = bytearray() |
| 103 | + b.extend(map(ord, methodName)) |
| 104 | + arr = (c_byte * len(b))(*b) |
| 105 | + |
| 106 | + methodNameHash = c_char_p(hashing.HashToHexString(arr, len(arr))).value.decode('utf-8') |
| 107 | + if methodNameHash not in self.methods: |
| 108 | + raise ErrMethodNotExists |
| 109 | + |
| 110 | + fn = self.methods[methodNameHash] |
| 111 | + try: |
| 112 | + fn(*params) |
| 113 | + except Exception as inst: |
| 114 | + print("[c3] err invoking method", inst) |
| 115 | + return |
| 116 | + |
| 117 | + def listen(self): |
| 118 | + while True: |
| 119 | + self.process(self.q.get()) |
| 120 | + q.task_done() |
| 121 | + |
| 122 | + def serve(): |
| 123 | + host = c_char_p(config.ServerHost()).value |
| 124 | + port = c_int(config.ServerPort()).value |
| 125 | + |
| 126 | + server = Server(host, port, self.q) |
| 127 | + |
| 128 | + worker = Thread(target=server.run) |
| 129 | + # worker.setDaemon(True) |
| 130 | + worker.start() |
| 131 | + |
| 132 | +def NewC3(stateFilePath = c_char_p(config.TempContainerStateFilePath()).value.decode('utf-8')): |
| 133 | + c3 = C3(stateFilePath) |
| 134 | + |
| 135 | + c3.setInitialState() |
| 136 | + |
| 137 | + worker = Thread(target=c3.listen) |
| 138 | + # worker.setDaemon(True) |
| 139 | + worker.start() |
| 140 | + |
| 141 | + return c3 |
| 142 | + |
| 143 | +class Server(): |
| 144 | + def __init__(self, host, port, q): |
| 145 | + self.host = host |
| 146 | + self.port = port |
| 147 | + self.q = q |
| 148 | + |
| 149 | + def run(): |
| 150 | + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 151 | + server.bind((self.host, self.port)) |
| 152 | + server.listen(1) # max backlog of connections |
| 153 | + |
| 154 | + print("Listening on {0}:{1}".format(bind_ip, bind_port)) |
| 155 | + |
| 156 | + |
| 157 | + def handle_conn(conn): |
| 158 | + data = [] |
| 159 | + while 1: |
| 160 | + tmp = conn.recv(1024) |
| 161 | + if not tmp: break |
| 162 | + data.append(tmp) |
| 163 | + |
| 164 | + self.q.put(''.join(data)) |
| 165 | + conn.close() |
| 166 | + |
| 167 | + while True: |
| 168 | + client_sock, address = server.accept() |
| 169 | + print('Accepted connection from {0}:{1}'.format(address[0], address[1])) |
| 170 | + client_handler = threading.Thread( |
| 171 | + target=handle_conn, |
| 172 | + args=(client_sock,) # note: comment required! |
| 173 | + ) |
| 174 | + |
| 175 | + client_handler.start() |
0 commit comments