def handleRequest(self, interest): length = len(self.service_prefix) iname = str(interest.name) assert(iname.startswith(self.service_prefix)) args = iname[length:].split('/') if args[0] == 'handshake': return self.doHandshake(args, interest) elif args[0] == 'auth': log.debug("'auth' interest received {}".format(args)) return self.doAuth(args, interest) elif len(args) == 3: log.debug("interest received {}".format(args)) (sid, seq, data) = tuple(args) seq = int(seq) s = self.getSessionItem(sid) if not s: log.warn("session not found") return self.buildResponse(interest, seq, statuscode.STATUS_AUTH_ERROR, '') if s.seq+1 != seq: log.error("sequence number error, {} expected, but {} received".format(s.seq+1, seq)) return None else: s.seq += 1 try: result, status = self.OnDataRecv(s.session_decrypt_data(data)) return self.buildResponse(interest, seq, status, s.session_encrypt_data(result)) except Exception, e: log.error(e) return self.buildResponse(interest, seq, statuscode.STATUS_INTERNAL_ERROR, '')
def Connect(self, timeout): """Shake hand and authorize with server (may block)""" self.connect_timeout = timeout auth = security.Auth() if self.state != STATE_NOT_AUTH: raise ValueError log.debug('Connecting to %s' % self.name_prefix) self.auth_cond.acquire() self._auth_result = None self.ndn_interface.send(self.name_prefix + '/auth/{}'.format(hex(auth.getDHKey())), timeout, 0) self.auth_cond.wait(timeout) self.auth_cond.release() if not self._auth_result: raise RMSAuthException('Authorization timed out') try: data = json.loads(self._auth_result) auth.setOtherDHKey(long(data['randS'], 0)) auth.decryptPreMasterKey(unhexlify(data['preMaster']), self.pemFile) self.cipher = security.AESCipher(auth.genMasterKey()) self.session_id = data['session'] self.state = STATE_IDLE log.debug('Connected') except Exception, e: log.error(e) raise RMSAuthException('Illegal authorization response received')
def PublishFiles(self, hosts, name, flaskFileUp): key = 'something fun' filename = hashlib.md5(name).hexdigest()+'.upload' log.debug('args: {} {}'.format(name, filename)) flaskFileUp.save(os.path.join(self.tmp_dir, filename)) self.thread.publish(hosts, name, self.tmp_naming+'/'+filename, key)
def name2filepath(self, name): #TODO: restrict file in ndnflow_dir while name.startswith('/'): name = name[1:] p = os.path.join(self.ndnflow_dir, name) log.debug('name:{} path:{}'.format(name, p)) return p
def ReConnect(self, timeout): log.debug('Reconnecting to %s' % self.name_prefix) self.state = STATE_NOT_AUTH self.session_id = '' self.seq = 0 self.cipher = None self.Connect(timeout)
def Start(self): # self.thread = _thread() # self.thread.start() log.info('Starting...') log.debug('Trying to connect servers') for x in self.clients: self.doConnect(x) CmdLineBackend._monitor(self.clients).start()
def DeleteFile(self, name): obj = { 'op': 'delete', 'name': name } self.send_cmd(obj) status, content = self.Recv(self.cmd_timeout) log.debug('status:{} content:{}'.format(status, content)) return status == statuscode.STATUS_OK
def mark_uploading(self, name, b): self.uploading_mutex.acquire() try: if b: log.debug('set uploading mark %s' % name) self.uploading_set.add(name) else: log.debug('remove uploading mark %s' % name) self.uploading_set.remove(name) except Exception, e: log.error(e)
def SendFile(self, name, remotename, key): obj = { 'op': 'send', 'name': name, 'remotename': remotename, 'key': key } self.send_cmd(obj) status, content = self.Recv(self.cmd_timeout) log.debug('status:{} content:{}'.format(status, content)) return status == statuscode.STATUS_OK
def api_content_del(hosts, name): name = unhexlify(name) hosts = hosts.split(',') # ask hosts to delete file hosts = filter(lambda h:backend_content.DeleteFiles(h,name), hosts) log.debug("successfully deleted {}: {}".format(name,hosts)) with closing(connect_db()) as db: placeholders = ','.join('?'*len(hosts)) sql = 'DELETE FROM content WHERE id IN (SELECT content.id FROM content JOIN hosts ON (hosts.id = content.host_id) WHERE hosts.name IN (%s) AND content.name=?)' % placeholders hosts.append(name) db.execute(sql, hosts) db.commit() return '{"error": 0}'
def _recv_thread(self): while True: try: item = self.recv_queue.get() except Exception, e: log.error(e) continue if self.state == STATE_NOT_AUTH: #handle auth response if item.status == QueueItem.STATUS_TIME_OUT: self._auth_timed_out() else: self._auth_response(item.content) continue if item.status == QueueItem.STATUS_TIME_OUT: log.info("send timed out %s" % item.name) self.state = STATE_IDLE continue if self.state != STATE_WAIT_RECV: log.warn('content received in a wrong state %d'%self.state) continue if item.status in [QueueItem.STATUS_BAD, QueueItem.STATUS_UNVERIFIED]: log.warn('got bad content') self.state = STATE_IDLE elif item.status == QueueItem.STATUS_OK: log.debug('got content') try: (seq, status, content) = self._decode_response(item.content) if int(status) == common.statuscode.STATUS_AUTH_ERROR: log.warn("session expired") self.ReConnect(self.connect_timeout or 10.0) raise RMSAuthException #quit normal receiving procedure seq = int(seq) content = self._decrypt(content) log.debug("content: %s" %(content)) except RMSAuthException: pass except Exception, e: log.error("unable to decode content, %s" % e) except:
def CheckFileState(self, name): obj = { 'op': 'send', 'name': name } self.send_cmd(obj) status, content = self.Recv(self.cmd_timeout) log.debug('status:{} content:{}'.format(status, content)) if status != statuscode.STATUS_OK: return rmsContentClient.QUERY_FAILED if content == 'exist': return rmsContentClient.FILE_EXIST elif content == 'upload': return rmsContentClient.FILE_UPLOADING elif content == 'notfount': return rmsContentClient.FILE_NOT_FOUND else: log.debug('invalid response: {}'.format(content)) return rmsContentClient.QUERY_FAILED
self.daemon = True self.hostname = hostname def run(self): with open('common/testkey.pub') as f: s = service.SystemService(self.hostname, f.read()) s.start() class content_service_thread(threading.Thread): def __init__(self, hostname): super(content_service_thread, self).__init__() self.daemon = True self.hostname = hostname def run(self): with open('common/testkey.pub') as f: s = service.ContentService(self.hostname, f.read()) s.start() if __name__ == "__main__": signal.signal(signal.SIGHUP, signal.SIG_IGN) h = get_host() cmd_service_thread(h).start() sys_service_thread(h).start() content_service_thread(h).start() log.debug("RMS server started!") signal.signal(signal.SIGINT, signal_handler) import time while True: time.sleep(1)
def publish(self, target, name, remote, key): log.debug('publish %s from %s' % (name, remote)) for h in target: idx = self.hosts.index(h) self.clients[idx].SendFile(name, remote, key)
info['alive'], info['delay'] = x.Ping() except Exception, e: log.error(e) try: self.mutex.acquire() self.host_status[self.hosts[i]] = info except Exception, e: log.error(e) finally: try: self.mutex.release() except Exception, e: pass log.debug("Monitor result: {}".format(self.host_status)) time.sleep(webconfig.MOD_SYS_MONITOR_INTERVAL) def getHostStatus(self, h): s = None try: self.mutex.acquire() s = copy(self.host_status[h]) except Exception, e: log.error(e) finally: try: self.mutex.release() except Exception, e: pass return s