def disconnect(sid): user = remove_session(sid) if user: trace_debug("Disconnected-->{}".format(user)) if USER_SESSION: user_list_response(sio, user.scope) else: trace_info("Disconnected with SID:: {}. No Info on DB!".format(sid))
def send_info(sid, scope, sender, receiver, info): user_session = get_session(scope, sender, False) receive_user_session = get_session(scope, receiver, False) if user_session and user_session.sid == sid and receive_user_session: send_info_response(sio, scope, sender, receiver, info, receive_user_session.sid) trace_debug("USER SEND INFO TRIGGERED for {} by {}".format( receiver, sender)) else: trace_debug("Sender {}/Receiver {} missing.".format(sender, receiver))
def get_session(scope, address, online=True): session_key = get_session_key(scope, address) user = USER_SESSION.get(session_key, None) if user: if online and user.is_online: return user return user elif not online: user = get_user_info(scope, address, online) return user trace_debug("No session found for {}".format(address))
def remove_session(sid): update_user_online_info(sid) try: result = SESSION_SID.get(sid, False) if result: info = USER_SESSION.get(result, False) del USER_SESSION[result] del SESSION_SID[sid] return info except KeyError as e: trace_debug( str(e) + " :: Session not found on USER_SESSION/SESSION_SID. SID:: " + sid) return False
def send_message(sid, scope, address, message): user_session = get_session(scope, address) if user_session.sid == sid: # trace_debug("Name={}. Message={}".format(address, message)) msg = get_dict(message) receiver = get_session(scope, msg['receiver'], False) if not receiver: reason = "User you tried is not registered! ({}, {})".format( scope, address) failed_response(sio, reason, address, sid) else: raw_send_read_msg = { "txn": msg["txn"], "text": msg['text'], "sender": address, "to": receiver.address } save_message = save_send_message(receiver, msg['txn'], set_json(raw_send_read_msg)) if save_message: if receiver and get_server_socket(sio, receiver.sid): new_message_response(sio, scope, msg['txn'], msg['text'], address, receiver.address, receiver.sid) receive_ack_response(sio, msg['txn'], scope, user_session.address, sid) trace_debug( "Message received by -->{}, SID: {}, TXN: {}, MSG: {}". format(receiver.address, receiver.sid, msg['txn'], msg['text'])) else: sent_ack_response(sio, msg['txn'], scope, user_session.address, sid) trace_debug( "Message sent to -->{}, SID: {}, TXN: {}, MSG: {}". format(receiver.address, receiver.sid, msg['txn'], msg['text'])) else: failed_response( sio, "DB STORE FAILED. MSG: {}, RAW MSG: {}".format( msg, raw_send_read_msg), address, sid) else: trace_info(">>>INVALID SESSION FOR {}".format(address)) sio.disconnect(sid)
def set_session(sid, scope, address): session_key = get_session_key(scope, address) session = USER_SESSION.get(session_key, None) if session and session.sid != sid: trace_debug("Duplicate Session for {}, SID:: {}. set_session".format( address, sid)) return None user = set_user_info(sid, scope, address) if user: user_data = get_user_info(scope, address) if user_data: USER_SESSION.update({session_key: user_data}) SESSION_SID.update({sid: session_key}) return USER_SESSION.get(SESSION_SID.get(sid, None)) else: trace_debug("Session set failed for {}, SID:: {}. set_session".format( address, sid))
def buyer_received(sid, c_address, scope, address, txn): ack_user_session = get_session(scope, address, False) current_user_session = get_session(scope, c_address, False) if ack_user_session and get_server_socket(sio, ack_user_session.sid) \ and current_user_session and get_server_socket(sio, current_user_session.sid) \ and current_user_session.sid == sid: if update_message_ack(txn, current_user_session): trace_debug("Receive Ack Done for both-->{}, {}".format( ack_user_session.address, ack_user_session.sid)) buyer_receive_ack_response(sio, scope, txn, ack_user_session.address, ack_user_session.sid) buyer_receive_ack_response(sio, scope, txn, current_user_session.address, current_user_session.sid) else: trace_debug(current_user_session) trace_debug("---**DB ERROR WHILE DELETE!**----") failed_response(sio, "DB ERROR WHILE DELETE", c_address, sid) elif current_user_session and current_user_session.sid == sid \ and get_server_socket(sio, sid) \ and ack_user_session and ack_user_session.sid != sid: if update_message_ack(txn, current_user_session, ack_user_session.id): # success_response(sio, "", current_user_session.address, sid) trace_debug( "Receiver {} missing. Receive ACK to sender {}. TXN: {}". format(address, c_address, txn)) else: failed_response( sio, "DB UPDATE FAILED FOR RECEIVER MISSING UPDATE. TXN {}".format( txn), c_address, sid) elif ack_user_session and get_server_socket(sio, ack_user_session.sid): reason = "Duplicate ACK USER {}".format(address) trace_debug(reason) failed_response(sio, reason, address, ack_user_session.sid) sio.disconnect(ack_user_session.sid) elif current_user_session: reason = "Duplicate CURRENT USER {}".format(c_address) trace_debug(reason) failed_response(sio, reason, current_user_session.address, current_user_session.sid) sio.disconnect(current_user_session.sid) else: trace_debug("ACK User not online. Details--> {}, {}, {}, {}".format( sid, scope, address, txn)) buyer_receive_ack_response(sio, scope, txn, c_address, sid)
def register(sid: str, scope: str, address: str): if (no_session(sid=sid) or no_session( scope=scope, address=address)) and sid and scope and address: sid = sid.strip() scope = scope.strip() address = address.strip() if scope not in SCOPE_WHITE_LIST: failed_response(sio, "Wrong APP/Scope", address, sid) else: user_session = get_session(scope, address) if user_session and user_session.is_online == 1: try: if get_server_socket(sio, user_session.sid): sio.disconnect(user_session.sid) except KeyError as e: trace_debug( str(e) + "-->No SID available on server as {}".format( user_session.sid)) remove_session(user_session.sid) user_session = set_session(sid, scope, address) if user_session: success_response( sio, "Session created for {}".format(user_session.address), user_session.address, sid) user_list_response(sio, scope) new_message = get_user_message(user_session) if new_message: for msg in new_message: msg_dict = get_dict(msg.message) if msg.status == MESSAGE_STATUS['buyer']: if update_message_ack(msg.key, user_session): trace_debug( "ACK Done and Removed for {}. ADDRESS: {}, SID:: {}. Key:: {}" .format(msg.message, user_session.address, sid, msg.key)) buyer_receive_ack_response( sio, scope, msg.key, user_session.address, sid) else: failed_response( sio, "DB ERROR FOR ACK {}. user {}. Message Key:: {}" .format(msg.message, user_session.address, msg.key), user_session.address, user_session.sid) else: new_message_response(sio, scope, msg.key, msg_dict.get('text', None), msg_dict.get('sender', None), user_session.address, sid) receiver = get_session(scope, msg_dict.get('sender', None)) # If Sender and receiver both are available if receiver and get_server_socket(sio, receiver.sid): # send receive ack for receive to receiver receive_ack_response(sio, msg_dict['txn'], scope, receiver.address, receiver.sid) if update_message_ack(msg_dict["txn"], receiver): # send receive ack for buyer/targeted user buyer_receive_ack_response( sio, scope, msg_dict['txn'], receiver.address, receiver.sid) trace_debug( "ACK DONE and removed {} with SID:: {}, TXN:: {}" .format(receiver.address, receiver.sid, msg_dict['txn'])) else: trace_info( "---------Receiver missing check sockets-------" ) trace_info(receiver) trace_debug( "Receiver {} not found. TXN:: {}".format( msg_dict['sender'], msg_dict['txn'])) trace_debug("SESSION: {}, SID: {}".format( USER_SESSION, SESSION_SID)) else: trace_debug("No message found for {}, SID:: {}".format( address, sid)) else: failed_response( sio, "User session establishment failed for {}. Try again.". format(address), address, sid) sio.disconnect(sid) else: trace_info("USER SESSION:: {}".format(USER_SESSION)) trace_info("USER SID:: {}".format(SESSION_SID)) reason = "Invalid Request. Address: {}, Session: {}, App:: {}".format( address, sid, scope) failed_response(sio, reason, address, sid) sio.disconnect(sid)