async def value(req, res): global log if req.body is not None: #js = json.loads(req.body) #log.log('S: value request received:\n{}'.format(js)) #key = js['body'] key = req.wildcards['key'] log.log(f'S: value request received: {key}') if not fi.chunk_exists(key): res.body = json.dumps(csdm.make_status_response('NO_FILE')) return res chunk = fi.read_chunk(key) rdata = csdm.make_status_response('OK') rdata['body'] = base64.b64encode(chunk).decode() res.body = json.dumps(rdata) log.log('S: sending value...') return res else: res.body = json.dumps(csdm.make_status_response('BAD')) return res
async def manifest_list(req, res): global manifests global log resjs = csdm.make_status_response('OK') resjs['body'] = manifests res.body = json.dumps(resjs) log.log('S: sending manifest list...') return res
async def manifest(req, res): global log if req.body is not None: #js = json.loads(req.body) #log.log('S: manifest request received:\n{}'.format(js)) #key = js['body'] key = req.wildcards['key'] log.log(f'S: manifest request received: {key}') if not fi.manifest_exists(key): res.body = json.dumps(csdm.make_status_response('NO_FILE')) return res manifest = fi.read_manifest_file(key) resjs = csdm.make_status_response('OK') resjs['body'] = manifest res.body = json.dumps(resjs) return res else: res.body = json.dumps(csdm.make_status_response('BAD')) return res
def process(self, js): if 'manifest' == js['header']['cmd']: key = js['body'] if not self.fi.manifest_exists(key): #dont have manifest, need to search network for manifest return json.dumps(csdm.make_status_response('NO_FILE')) manifest = self.fi.read_manifest_file(key) response = csdm.make_status_response('OK') response['body'] = manifest return json.dumps(response) elif 'value' == js['header']['cmd']: key = js['body'] if not self.fi.chunk_exists(key): #dont have chunk, need to search network for chunk return json.dumps(csdm.make_status_response('NO_FILE')) chunk = self.fi.read_chunk(key) response = csdm.make_status_response('OK') response['body'] = base64.b64encode(chunk).decode() return json.dumps(response) elif 'manifest-list' == js['header']['cmd']: response = csdm.make_status_response('OK') #print('manifests {}'.format(self.manifests)) response['body'] = self.manifests return json.dumps(response) elif 'ping' == js['header']['cmd']: response = csdm.make_status_response('OK') response['body'] = 'pong' return json.dumps(response) elif 'friend-request' == js['header']['cmd']: response = csdm.make_status_response('OK') common = self.process_friend_reqest(js) response['body'] = { 'status': 'received', 'stage': 0, 'common': common } return json.dumps(response) else: return json.dumps(csdm.make_status_response('OK'))
def run(self): """ Run ZMQ server and handle connections """ self.setup() self.context = zmq.Context.instance() self.router = self.context.socket(zmq.ROUTER) #self.router.bind('tcp://127.0.0.1:8080') self.router.bind(self.PROTOCOL + self.ADDRESS + ':' + self.PORT) self.poller = zmq.Poller() self.poller.register(self.router, zmq.POLLIN) self.running = True while self.running: try: #poll and timeout every 60th of a second to do other processing socks = dict(self.poller.poll(timeout=1000)) except Exception as e: if e is zmq.error.Again or zmq.error.ZMQError: self.cleanup() break print('S: something else: ' + str(Exception)) self.cleanup() break if socks.get(self.router) == zmq.POLLIN: id, null, data = self.router.recv_multipart() if data is not None: print("S: received: " + data.decode()) j = json.loads(data.decode()) response = self.process(j) else: response = json.dumps(csdm.make_status_response('BAD')) self.router.send_multipart([id, null, response.encode()]) else: #print('S: doing other...') self.do_other() print('S: Exiting...')