def send_chatrooms_to_client(payload, clientsocket): # load the address from the payload address = pickle.loads(payload) # list of chatroom ids chatroom_ids = [] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) # select all emails where sender = address data = db.select("*", "email_chatroom", "address = '{0}'".format(address)) # append them to the list for entry in data: chatroom_ids.append(entry[0]) db.close_connection() # our chat room list chatrooms = ChatroomList.ChatroomList() for id in chatroom_ids: list_of_users = get_users_in_chatroom(id) list_of_users = [User.User(user, "") for user in list_of_users] chatrooms.add_chatroom(id, list_of_users, get_chatroom_name(id)) # pickle the list of users into bytes pickled_chatrooms = pickle.dumps(chatrooms) # send them back to the client clientsocket.sendall(pickled_chatrooms)
def send_messages_to_client(payload, clientsocket): # load the request from the payload request = pickle.loads(payload) # unpack the chatroom id and the offset chatroom_id, offset = request[0], request[1] # list of messages messages = [] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) # prepare our select statement, where chatroom id = our chatroom id, and offset it by our offset data = db.select("*", "messages", "chatroom_id = '{0}'".format(chatroom_id, offset)) # append all the message objects for entry in data: message = Message.Message(entry[0], entry[2], entry[3], entry[4]) message.set_id(entry[1]) messages.append(message) db.close_connection() # pickle the messages pickled_messages = pickle.dumps(messages) # send the messags back to the client clientsocket.send(pickled_messages)
def get_chatroom_name(id): # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) name = db.select("*", "chatroom", "chatroom_id = '{0}'".format(id)) db.close_connection() return name[0][1]
def send_client_max_id(clientsocket): # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) id = db.select("max(chatroom_id)", "chatroom", "") pickled_id = pickle.dumps(id[0][0]) clientsocket.sendall(pickled_id) db.close_connection()
def request_messages(chatroomId, index): # make a socket s = new_socket(host, port) # prepare the request request = [chatroomId, index] # pickle the request into bytes pickled_request = pickle.dumps(request) # make a header header = create_header("getMsgs", pickled_request) # send the header s.sendall(header.encode('ascii')) # send the request s.sendall(pickled_request) # recieve data sent from server received_data = b"" data = s.recv(5000) while len(data) != 0: # append the recieved data to recieved_data received_data += data data = s.recv(5000) # unpickle the messages messages = pickle.loads(received_data) # prepare the value to be inserted insert_values = [(message.sender_address, message.id, message.chatroom_id, message.text, message.sent_date_time) for message in messages] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmailLocal", False) # calling db.insert() to insert data if len(insert_values) > 0: db.insert( "messages", "sender_address, message_id, chatroom_id, message, sent_date", insert_values) db.close_connection() # close the socket s.close() # return the messages return messages
def request_emails(bySender, address): # make a new socket s = new_socket(host, port) # make a list of data to send to the server request = [bySender, address] # pickle the request pickled_request = pickle.dumps(request) # make a new header for the request header = create_header("getEmail", pickled_request) # send the header so the server knows how much data to recieve s.sendall(header.encode('ascii')) # send the data s.sendall(pickled_request) # data the server will send back received_data = b"" # getting the data data = s.recv(5000) while len(data) != 0: # append it to our recieved_data string received_data += data data = s.recv(5000) # unpickle the emails from the recieved data emails = pickle.loads(received_data) # prepare the values to be inserted insert_values = [(email.id, email.sender, email.to, email.subject, email.body, email.time_sent) for email in emails] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmailLocal", False) # calling db.insert() to insert data if len(insert_values) > 0: db.insert("emails", "email_id, sender, receiver, subject, body, sent_date", insert_values) db.close_connection() # close the socket s.close() # return the emails return emails
def request_chatrooms(address): # make a socket s = new_socket(host, port) # prepare the request request = address # pickle the request into bytes pickled_request = pickle.dumps(request) # make a header header = create_header("getCR", pickled_request) # send the header s.sendall(header.encode('ascii')) # send the request s.sendall(pickled_request) # recieve data sent from server received_data = b"" data = s.recv(5000) while len(data) != 0: # append the recieved data to recieved_data received_data += data data = s.recv(5000) # unpickle the chatrooms chatrooms = pickle.loads(received_data) # ids in chatroom ids = chatrooms.get_all_ids() # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmailLocal", False) for id in ids: chatroom = chatrooms.get_chatroom(id) list_of_users = chatroom.list_of_users name = chatroom.name insert_values = [(id, user.email_address) for user in list_of_users] db.insert("email_chatroom", "chatroom_id, address", insert_values) insert_values = [(id, name)] db.insert("chatroom", "chatroom_id, chatroom_name", insert_values) db.close_connection() s.close() # return the messages return chatrooms
def add_user_to_chatroom(payload): address, chatroom_id = pickle.loads(payload) # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) count = db.select("count(*)", "chatroom", "chatroom_id = '{0}'".format(chatroom_id))[0][0] if count != 0: insert_values = [(chatroom_id, address)] # calling db.insert() to insert data db.insert("email_chatroom", "chatroom_id, address", insert_values) db.close_connection()
def insert_email(payload): # pickle the email into bytes email = pickle.loads(payload) # prepare the values to be inserted insert_values = [(email.sender, email.to, email.subject, email.body, email.time_sent)] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) # calling db.insert() to insert data db.insert("emails", "sender, receiver, subject, body, sent_date", insert_values) db.close_connection()
def get_users_in_chatroom(chatroom_id): # list of user's emails users = [] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) # select all emails where sender = address data = db.select("*", "email_chatroom", "chatroom_id = '{0}'".format(chatroom_id)) # append them to the list for entry in data: users.append(entry[1]) db.close_connection() return users
def insert_message(payload): # pickle the message into bytes message = pickle.loads(payload) # prepare the value to be inserted insert_values = [(message.sender_address, message.chatroom_id, message.text, time.strftime('%Y-%m-%d %H:%M:%S'))] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) # calling db.insert() to insert data db.insert("messages", "sender_address, chatroom_id, message, sent_date", insert_values) db.close_connection()
def make_chatroom(payload, clientsocket): # pickle the email into bytes id, name, user_address = pickle.loads(payload) # prepare the values to be inserted insert_values = [(id, name)] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) # calling db.insert() to insert data db.insert("chatroom", "chatroom_id, chatroom_name", insert_values) # prepare the values to be inserted insert_values = [(id, user_address)] # calling db.insert() to insert data db.insert("email_chatroom", "chatroom_id, address", insert_values) db.close_connection()
def send_emails_to_client(payload, clientsocket): # load the request from the payload request = pickle.loads(payload) # unpack the boolean and address queryBySender, address = request[0], request[1] # list of emails emails = [] # connect to db db = dbHelper.Connection("127.0.0.1", "root", "", "PrimoEmail", False) # if we are searching by sender if queryBySender: # select all emails where sender = address data = db.select("*", "emails", "sender = '{0}'".format(address)) # append them to the list for entry in data: emails.append( Email.Email(entry[1], entry[2], entry[3], entry[4], entry[5])) else: # if we are searching by the reciever data = db.select( "*", "emails", "receiver = '{0}' order by sent_date desc".format(address)) for entry in data: email = Email.Email(entry[1], entry[2], entry[3], entry[4], entry[5]) email.set_id(entry[0]) emails.append(email) db.close_connection() # pickle the emails into bytes pickled_emails = pickle.dumps(emails) # send them back to the client clientsocket.send(pickled_emails)
try: loadConfigFile() os.environ["PYTHONIOENCODING"] = "windows-1256" connection = None if __name__ == "__main__": logMain = Logger(filename="__init__", level=__level__, dirname="File-" + os.path.basename(__file__) + "-Func-" + sys._getframe().f_code.co_name, rootdir=__LOGDIR__) try: # create instances of the dbHelper connection and cursor connection = dbHelper.Connection(V_DB_USERNAME, V_DB_PASSWORD, V_DB_DSN, __LOGDIR__ + '/ORA') cursor = connection.cursor() # demonstrate that the dbHelper connection and cursor are being used try: # No commit as you don-t need to commit DDL. V_NLS_LANGUAGE, = cursor.execFetchOne( "SELECT VALUE AS NLS_LANGUAGE " "FROM V$NLS_PARAMETERS " "WHERE PARAMETER = ('NLS_LANGUAGE')", __LOGDIR__ + '/ORA') V_NLS_TERRITORY, = cursor.execFetchOne( "SELECT VALUE AS NLS_TERRITORY " "FROM V$NLS_PARAMETERS " "WHERE PARAMETER = ('NLS_TERRITORY')", __LOGDIR__ + '/ORA')