def __init__(self, req_dict: dict): super().__init__(req_dict) try: self.user_password = req_dict[KEY_USER_PASSWORD] except Exception as e: logger.e(str(e)) raise InvalidRequestDictError
def __init__(self, req_dict: dict): """Do not instantiate directly""" try: self.req_id = req_dict[KEY_REQ_ID] if type(self.req_id) != int: raise TypeError except Exception as e: logger.e(e) raise InvalidRequestDictError
def from_dict(req_dict: dict) -> 'ScrpRequest': """ Factory method to create an ScrpRequest object from a given dict. """ try: # Dispatch req_dict to the specific constructor return _get_request_class(req_dict[KEY_METHOD])(req_dict) except Exception as e: logger.e(str(e)) raise InvalidRequestDictError
def receive_str(self) -> str: """ Receive a string message. Raise MessageDecodeError if decoding failed. """ try: return self.__bytes_msg_sock.receive_bytes().decode() except UnicodeDecodeError as e: logger.e(str(e)) raise MessageDecodeError
def init(cls): if cls._instance is None: try: cls._instance = cls() logger.i("initialize gamepad is successfull.") except: logger.e("initialize gamepad failed.:" + str(sys.exc_info()[0])) logger.e(traceback.format_exc()) return cls._instance
def save(self): '''save a record to database''' try: self._init_db() sql = 'INSERT INTO record VALUES("{}", "{}", NOW())'.format(self.nickname, self.content) print(sql) self.cursor.execute(sql) self.conn.commit() self._close_db() except Exception as e: logger.e('failed to insert row; {}'.format(str(e)))
def __init__(self): initial_data = None try: initial_data = urllib2.urlopen( 'http://localhost:5601/api/gamepad').read() self.worker = ReadLineWorker(lambda: HwControllerManager.event(), json.loads(initial_data)) self.worker.start() except: logger.e("Unexpected error:" + str(sys.exc_info()[0])) logger.e(traceback.format_exc())
def upgrade_db(self, old_ver: int, new_ver: int): """Perform the upgrade of the database recursively""" assert old_ver <= new_ver c = self.__conn.cursor() if old_ver != new_ver: if old_ver == 0: # DB is empty # User c.execute(''' CREATE TABLE {USER} ( {ID} TEXT NOT NULL PRIMARY KEY, {HASHED_PASSWORD} TEXT NOT NULL, {SALT} TEXT NOT NULL, {NICKNAME} TEXT NOT NULL, {DESCRIPTION} TEXT NOT NULL, {SIGN_UP_TIME} INTEGER NOT NULL, {LAST_ACTIVITY_TIME} INTEGER NOT NULL ) '''.format( USER=TBL_USER, ID=COL_USER_ID, HASHED_PASSWORD=COL_USER_HASHED_PASSWORD, SALT=COL_USER_SALT, NICKNAME=COL_USER_NICKNAME, DESCRIPTION=COL_USER_DESCRIPTION, SIGN_UP_TIME=COL_USER_SIGN_UP_TIME, LAST_ACTIVITY_TIME=COL_USER_LAST_ACTIVITY_TIME )) # Room c.execute(''' CREATE TABLE {ROOM} ( {ID} TEXT NOT NULL PRIMARY KEY, {NICKNAME} TEXT NOT NULL, {DESCRIPTION} TEXT NOT NULL, {OWNER} TEXT NOT NULL, {PASSWORD} TEXT, {ACCESS_TYPE} INTEGER NOT NULL ) '''.format( ROOM=TBL_ROOM, ID=COL_ROOM_ID, NICKNAME=COL_ROOM_NICKNAME, DESCRIPTION=COL_ROOM_DESCRIPTION, OWNER=COL_ROOM_OWNER, PASSWORD=COL_ROOM_PASSWORD, ACCESS_TYPE=COL_ROOM_ACCESS_TYPE )) elif old_ver == 1: pass else: logger.e('Cannot upgrade DB from {} to {}!' .format(old_ver, old_ver + 1)) # Do the rest self.upgrade_db(old_ver + 1, new_ver)
def send_bytes(self, message: bytes): """ Send a bytes message. Raise SendBytesMessageError if connection is broken. """ length = len(message) assert 1 <= length <= BytesMessageSocketWrapper.LENGTH_LIMIT try: self.__sock.sendall(self._int_to_bytes(length) + message) except Exception as e: # Connection is broken logger.e(str(e)) raise BytesMessageSendError
def _init_db(self): '''init database whith create a table named record''' try: if self.conn is None: self.conn = MySQLdb.connect(settings.DB_HOST, settings.DB_USER, settings.DB_PASSWORD, settings.DB_NAME, charset='utf8') self.cursor = self.conn.cursor() self.cursor.execute("CREATE TABLE IF NOT EXISTS record \ (nickname VARCHAR(64),\ content VARCHAR(1024),\ pub_time DATETIME)") self.conn.commit() except Exception as e: logger.e('failed to create table on {}; {}'.format(settings.DB_NAME, str(e)))
def receive_json(self) -> dict: """ Receive JSON as a dict. Raise JsonParseError if parsing failed. """ s = self.__unicode_sock.receive_str() try: obj = json.loads(s) except Exception as e: logger.e(str(e)) raise JsonParseError if type(obj) != dict: raise JsonParseError return obj
def run(self): try: logger.i("flask server has started. host:{0}, port:{1}".format(self.host, self.port)) self.flask_app.run( debug=False, host=self.host, port=int(self.port) ) except Exception as e: logger.e("Unexpected error:" + str(sys.exc_info()[0])) logger.e(traceback.format_exc()) self.lock.acquire() self.errors.append(e) self.lock.release()
def upgrade_db(self, old_ver: int, new_ver: int): """Perform the upgrade of the database recursively""" assert old_ver <= new_ver c = self.__conn.cursor() if old_ver != new_ver: if old_ver == 0: # DB is empty # User c.execute(''' CREATE TABLE {USER} ( {ID} TEXT NOT NULL PRIMARY KEY, {HASHED_PASSWORD} TEXT NOT NULL, {SALT} TEXT NOT NULL, {NICKNAME} TEXT NOT NULL, {DESCRIPTION} TEXT NOT NULL, {SIGN_UP_TIME} INTEGER NOT NULL, {LAST_ACTIVITY_TIME} INTEGER NOT NULL ) '''.format(USER=TBL_USER, ID=COL_USER_ID, HASHED_PASSWORD=COL_USER_HASHED_PASSWORD, SALT=COL_USER_SALT, NICKNAME=COL_USER_NICKNAME, DESCRIPTION=COL_USER_DESCRIPTION, SIGN_UP_TIME=COL_USER_SIGN_UP_TIME, LAST_ACTIVITY_TIME=COL_USER_LAST_ACTIVITY_TIME)) # Room c.execute(''' CREATE TABLE {ROOM} ( {ID} TEXT NOT NULL PRIMARY KEY, {NICKNAME} TEXT NOT NULL, {DESCRIPTION} TEXT NOT NULL, {OWNER} TEXT NOT NULL, {PASSWORD} TEXT, {ACCESS_TYPE} INTEGER NOT NULL ) '''.format(ROOM=TBL_ROOM, ID=COL_ROOM_ID, NICKNAME=COL_ROOM_NICKNAME, DESCRIPTION=COL_ROOM_DESCRIPTION, OWNER=COL_ROOM_OWNER, PASSWORD=COL_ROOM_PASSWORD, ACCESS_TYPE=COL_ROOM_ACCESS_TYPE)) elif old_ver == 1: pass else: logger.e('Cannot upgrade DB from {} to {}!'.format( old_ver, old_ver + 1)) # Do the rest self.upgrade_db(old_ver + 1, new_ver)
def do_work(self, arg=None): if self.__work is None: return try: if arg is None: res = self.__work() else: res = self.__work(arg) self.do_complated(res) except Exception: logger.e('Unexpected error: {}'.format(str(sys.exc_info()[0]))) logger.e(traceback.format_exc()) except KeyboardInterrupt: logger.d('keyboard Ctrl+C in sound_pool.do_work()') self.abort = True raise
def _do_upgrade(): """ Upgrade the database if necessary. See CHANGELOG for changes made in each version. """ dao = Dao() existing_ver = dao.get_db_version() if existing_ver > DB_VERSION: logger.e('DB version ({}) is older than the existing version ({})!' .format(DB_VERSION, existing_ver)) elif existing_ver < DB_VERSION: logger.w('DB needs to upgrade from version {} to {}' .format(existing_ver, DB_VERSION)) input('Press enter to continue...') dao.upgrade_db(existing_ver, DB_VERSION) dao.set_db_version(DB_VERSION) dao.commit() logger.i('DB upgrade complete!')
def _do_upgrade(): """ Upgrade the database if necessary. See CHANGELOG for changes made in each version. """ dao = Dao() existing_ver = dao.get_db_version() if existing_ver > DB_VERSION: logger.e( 'DB version ({}) is older than the existing version ({})!'.format( DB_VERSION, existing_ver)) elif existing_ver < DB_VERSION: logger.w('DB needs to upgrade from version {} to {}'.format( existing_ver, DB_VERSION)) input('Press enter to continue...') dao.upgrade_db(existing_ver, DB_VERSION) dao.set_db_version(DB_VERSION) dao.commit() logger.i('DB upgrade complete!')
def init(cls): try: if cls._port is None: logger.i("searching controller....") cls._port = cls.__find_controller_port() if cls._instance is None and cls._port is not None: logger.i("find port: " + str(cls._port)) try: cls._instance = cls(cls._port) except serial.SerialException: pass else: logger.w("controller is not connected.") except: logger.e('HwControllerManager.init() failed.' + str(sys.exc_info()[0])) return cls._instance
def run(self): logger.i('I started') self.__st.cht_ready(self) while True: # Main loop for receiving requests try: req = self.__scrp_sock.receive_request() # Dispatch rht = RequestHandlerThread(self, req) logger.d('Starting RequestHandlerThread {}'.format(rht.name)) rht.start() except BytesMessageReceiveError as e: # Connection is broken break except (MessageDecodeError, JsonParseError, InvalidRequestDictError) as e: # Bad request raise BadRequestError except ScrpError as e: logger.e(e) logger.d('Thread terminate')
def rows(time_span='week'): '''select rows from record during a time span, the time span can be: day, week, month, year ''' conn = MySQLdb.connect(settings.DB_HOST, settings.DB_USER, settings.DB_PASSWORD, settings.DB_NAME, charset='utf8') cursor = conn.cursor() if time_span == 'day': sql = "SELECT * FROM record WHERE TO_DAYS(pub_time) = TO_DAYS(NOW())" elif time_span == 'week': sql = "SELECT * FROM record WHERE YEARWEEK(date_format(pub_time,'%Y-%m-%d')) = YEARWEEK(NOW())" elif time_span == 'month': sql = "SELECT * FROM record WHERE DATE_FORMAT(pub_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m')" else: sql = "SELECT * FROM record WHERE YEAR(pub_time)=YEAR(NOW())" try: cursor.execute(sql) results = cursor.fetchall() cursor.close() conn.close() return results except Exception as e: logger.e('failed to query data from database; {}'.format(str(e)))
def _receive(self, seg: Segment, addr: (str, int)) -> None: ''' Process a segment that was received. ''' with self._lock: logger.i('received {}'.format(seg)) events = ['rcv'] # Make sure the packet comes from the correct address if not self._addr: self._addr = addr elif self._addr != addr: logger.e('[{}] Unknown source! {} {}'.format( self._state, self._addr, addr)) return # If the segment has a bit error, log it and do nothing if seg.bit_error: logger.add_bit_error_received() events.append('corr') logger.log(events, seg) return # If this segment has been acked or buffered, log it as duplicate if seg.seq_num < self._ack_num or seg.seq_num in self._buffer: logger.add_duplicate_data_segment() if self._state == States.CLOSED: logger.w('[{}] Received message in closed state'.format( self._state)) return elif self._state == States.LISTEN: if seg.syn: # Syn received, respond with syn_ack and update state self._ack_num = seg.seq_num + 1 self._set_state(States.SYN_RECEIVED) logger.log(events, seg) self._send_segment(syn=True, ack=True) else: logger.w( '[{}] Received non-syn segment in listen state'.format( self._state)) return # If this segment is an ACK if seg.ack: if seg.ack_num == self._last_ack: # We have a duplicate ack self._dup_ack_count += 1 events.append('DA') elif seg.ack_num > self._last_ack: # This acks at least the oldest unacked segment self._dup_ack_count = 0 # Stop the timer if self._timer: self._timer.cancel() self._timer = None if self._rtt_seg: r = self._rtt_seg expected_ack = r.seq_num + len(r.data) + r.syn + r.fin if expected_ack == seg.ack_num: # Calculate RTT dt = seg.created_at - self._rtt_seg.created_at rtt = dt.total_seconds() * 1000 self.addRTT(rtt) logger.w('Received RTT for segment {}, {:0.4f}'.format( self._rtt_seg.seq_num, rtt)) self._rtt_seg = None elif expected_ack < seg.ack_num: logger.w('Received ack beyond RTT seg, clearing') self._rtt_seg = None # Update the last ack received self._last_ack = seg.ack_num # Clear the unacked list for i in range(len(self._unacked)): if self._unacked[i].seq_num >= seg.ack_num: self._unacked = self._unacked[i:] break else: self._unacked.clear() # Track the ack number when this segment arrived previous_ack_num = self._ack_num # Update the ack num if this segment arrived in order. if self._ack_num == seg.seq_num: self._ack_num += len(seg.data) + seg.syn + seg.fin # Log the segment before we send other segments logger.log(events, seg) # Resend on three duplicate acks if self._dup_ack_count == 3: self._fast_retransmit(seg.ack_num) self._dup_ack_count = 0 # Process the segment's data. Define as a function, since this is # done in many states def process_data(): if seg.seq_num == previous_ack_num: logger.add_file_size(len(seg.data)) self._data_proc(seg.data + self._clear_buffer()) elif seg.seq_num > previous_ack_num: self._buffer[seg.seq_num] = seg if self._state == States.SYN_SENT: if seg.ack and seg.syn: self._set_state(States.ESTABLISHED) self._send_segment(ack=True) elif seg.data: # If the segment has data, buffer it. self._buffer[seg.seq_num] = seg elif self._state == States.SYN_RECEIVED: if seg.ack: # Acking the syn/ack we sent self._set_state(States.ESTABLISHED) elif seg.data: # If the segment has data, buffer it. self._buffer[seg.seq_num] = seg elif self._state == States.ESTABLISHED: if seg.data: process_data() if seg.fin: self._send_segment(ack=True) self._set_state(States.CLOSE_WAIT) elif self._state == States.FIN_WAIT_1: if seg.data: process_data() if seg.ack: self._set_state(States.FIN_WAIT_2) elif seg.fin: # Assume two separate segments for FIN / ACK self._set_state(States.CLOSING) elif self._state == States.FIN_WAIT_2: if seg.data: process_data() if seg.fin: self._send_segment(ack=True) self._set_state(States.FINISHED) elif self._state == States.CLOSE_WAIT: # Wait for application to close connection if seg.data: process_data() elif self._state == States.LAST_ACK: if seg.ack: self._set_state(States.FINISHED) elif self._state == States.CLOSING: if seg.ack: self._set_state(States.FINISHED) # If this is a data segment, send an ack if seg.data: events = [] if self._ack_num == previous_ack_num: events.append('DA') logger.add_duplicate_ack_sent() self._send_segment(ack=True, events=events) # Notify threads waiting on receipt of a segment self._receive_condition.notify_all()
async def on_command_error(context, exception): logger.e( "❌ An error occured while restarting the submissions command to Discord.", exception) await context.send(str(exception))