示例#1
0
 def waitForAckMsg(self):
     if self.username is not None:
         db.user_logout(self.username)
         if self.username in tcpThreads:
             del tcpThreads[self.username]
     self.tcpClientSocket.close()
     print("Removed " + self.username + " from online peers")
示例#2
0
    def waitHelloMessage(self):
        if self.username is not None:
            db.user_logout(self.username)
            if self.username in tcpThreads:
                del tcpThreads[self.username]

            self.tcpClientSoccket.close()
            self.timer.cancel()
            logging.info("Removed " + self.username + " from online peers")
            print("Removed " + self.username + " from online peers")
示例#3
0
    def run(self):
        #locks for thread which will be used for thread synchronization,break the lock
        #when curent peer(which send request to server) succesfully login or logout
        self.lock = threading.Lock()
        print("Connection from : " + self.ip + " : " + str(self.port))
        print("IP Connected: " + self.ip)

        #handle request from another peer to the server thread
        while True:
            try:
                #waits for incoming message from  peers to registry
                #message form: list with 3 element, first is request type,
                #second is user name, thirst is password
                message = self.tcpClientSocket.recv(1024).decode().split()
                logging.info("Received from " + self.ip + " : " +
                             str(self.port) + " -> " + " ".join(message))
                #Register Message
                if message[0] == 'JOIN':
                    #If the username is exist
                    if db.is_account_exists(message[1]):
                        response = "account already exist"
                        print("From-> " + self.ip + " : " + str(self.port) +
                              " " + response)
                        logging.info("Send to " + self.ip + " : " +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    else:
                        #If not, create an account
                        db.register(message[1], message[2])
                        response = 'successfully register'
                        logging.info("Send to " + self.ip + " : " +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                #Login Message
                elif message[0] == 'LOGIN':
                    #Handle login using non-exist account
                    if not db.is_account_exists(message[1]):
                        response = "account not exist"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    #If account exist, but already online
                    elif db.is_account_online(message[1]):
                        response = 'account is online'
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    #account exist and not online, handle password
                    else:
                        truePass = db.get_password(message[1])
                        #password correct
                        if truePass == message[2]:
                            self.username = message[1]
                            self.lock.acquire()
                            try:
                                tcpThreads[self.username] = self
                            finally:
                                self.lock.release()

                            db.user_login(message[1], self.ip, self.port)
                            #login succes, so create server thread for this peer,
                            # also set timer for this thread
                            response = 'succesfully login'
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                            self.ServerThread = ServerThread(
                                self.username, self.tcpClientSocket)
                            self.ServerThread.start()
                            self.ServerThread.timer.start()
                        else:
                            response = "incorect password"
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())

                elif message[0] == 'LOGOUT':
                    #user is online =>removes from online_peers list

                    if len(message) > 1 and message[
                            1] is not None and db.is_account_online(
                                message[1]):
                        db.user_logout(message[1])
                        self.lock.acquire()
                        try:
                            if message[1] in tcpThreads:
                                del tcpThreads[message[1]]
                        finally:
                            self.lock.release()
                        print(self.ip + ":" + str(self.port) +
                              " is logged out")
                        self.tcpClientSocket.close()
                        self.ServerThread.timer.cancel()
                        break

                    else:
                        self.tcpClientSocket.close()
                        break

                #check if an arbitary user is online or not
                elif message[0] == 'CHECKONL':
                    if db.is_account_exists(message[1]):
                        if db.is_account_online(message[1]):
                            peer_info = db.get_peer_ip_port(message[1])
                            response = "search successfull " + peer_info[
                                0] + ":" + peer_info[1]
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())

                        else:
                            response = 'user does not online'
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())

                    else:
                        response = "search-user-not-found"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())

            except OSError as oErr:
                logging.error("OSError: {0}".format(oErr))
示例#4
0
    def run(self):
        #locks for thread which will be used for thread synchronization,break the lock
        #when curent peer(which send request to server) succesfully login or logout
        self.lock = threading.Lock()
        print("Connection from : " + self.ip + " : " + str(self.port))
        print("IP Connected: " + self.ip)

        #handle request from another peer to the server thread
        while True:
            try:
                #waits for incoming message from  peers to registry
                #message form: list with 3 element, first is request type,
                #second is user name, thirst is password
                message = self.tcpClientSocket.recv(1024).decode().split()

                logging.info("Received from " + self.ip + " : " +
                             str(self.port) + " -> " + " ".join(message))
                if len(message) == 0:
                    break
                #Register Message
                if message[0] == 'JOIN':
                    #If the username is exist
                    if db.is_account_exists(message[1]):
                        response = "account already exist"
                        print("From-> " + self.ip + " : " + str(self.port) +
                              " " + response)
                        logging.info("Send to " + self.ip + " : " +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    else:
                        #If not, create an account
                        db.register(message[1], message[2])
                        response = 'successfully register'
                        logging.info("Send to " + self.ip + " : " +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                #Login Message
                elif message[0] == 'LOGIN':
                    #Handle login using non-exist account
                    if not db.is_account_exists(message[1]):
                        response = "account not exist"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    #If account exist, but already online
                    elif db.is_account_online(message[1]):
                        response = 'account is online'
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    #account exist and not online, handle password
                    else:
                        truePass = db.get_password(message[1])
                        #password correct
                        if truePass == message[2]:
                            self.username = message[1]
                            self.lock.acquire()
                            try:
                                tcpThreads[self.username] = self
                            finally:
                                self.lock.release()

                            db.user_login(message[1], self.ip, str(message[3]))
                            #login succes, so create server thread for this peer,
                            # also set timer for this thread
                            response = 'succesfully login'
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                            self.ServerThread = ServerThread(
                                self.username, self.tcpClientSocket)
                            self.ServerThread.start()
                            self.ServerThread.timer.start()
                        else:
                            response = "incorect password"
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())

                #param :type. username of logout account
                elif message[0] == 'LOGOUT':
                    #user is online =>removes from online_peers list

                    if len(message) > 1 and message[
                            1] is not None and db.is_account_online(
                                message[1]):
                        db.user_logout(message[1])
                        self.lock.acquire()
                        try:
                            if message[1] in tcpThreads:
                                del tcpThreads[message[1]]
                        finally:
                            self.lock.release()
                        print(self.ip + ":" + str(self.port) +
                              " is logged out")
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " ->  is logout")
                        self.tcpClientSocket.close()
                        self.ServerThread.timer.cancel()
                        break

                    else:
                        self.tcpClientSocket.close()
                        break

            #find online friend
                elif message[0] == 'CHECKONL':
                    if db.is_account_exists(
                            message[1]) and db.is_account_online(message[1]):
                        ListOnlineFriends = db.get_online_friends(message[1])
                        if len(ListOnlineFriends) == 0:
                            response = 'No firend of you are online now'
                        else:
                            response = "list of online friends are "
                            for i in ListOnlineFriends:
                                response += i + " "
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())

                #param: type,username(peer want to add),username(of peer being added)
                elif message[0] == 'ADDFRIEND':
                    if len(message) > 1 and message[1] and message[
                            2] and db.is_account_exists(
                                message[2]) and db.is_account_online(
                                    message[1]):
                        response = db.insert_friend_request(
                            message[2], message[1])
                        if response == 'AlreadyFriend':
                            response = 'You and ' + message[
                                1] + " are already friend"
                        elif response == 'AlreadyAskFriendRequest':
                            response = 'You have sent a request for ' + message[
                                2]
                        else:
                            response = "your request is successfully sending to " + message[
                                2]
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    else:
                        response = 'user does not exist'
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                # param: type,username (peer THAT ACCEPT),username(peer being ACCEPTED)
                elif message[0] == 'ACCEPTFRIEND':
                    if len(message) > 1 and message[1] and message[
                            2] and db.is_account_exists(
                                message[2]) and db.is_account_online(
                                    message[1]):
                        if db.accept_friend_request(
                                message[1], message[2]) == 'NotInRequest':
                            response = message[2] + "not in your request list"

                        else:
                            db.make_friend(message[1], message[2])
                            response = "accept successfull you and " + message[
                                2] + " are friend"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    else:
                        response = 'user does not exist'
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())

                elif message[0] == 'VIEWREQUESTFRIEND':
                    if len(message) > 1 and db.is_account_online(message[1]):
                        ListReFriend = db.get_request_friend(message[1])
                        if len(ListReFriend) == 0:
                            response = "You have no request"
                        else:
                            response = "list of request friends are "
                            for i in ListReFriend:
                                response += i + " "
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    else:
                        response = 'NotLoginYet'
                        self.tcpClientSocket.send(response.encode())

                elif message[0] == "SEARCH":
                    # checks if an account with the username exists
                    if db.is_account_exists(message[1]):
                        # checks if the account is online
                        # and sends the related response to peer
                        if db.is_account_online(message[1]):
                            peer_info = db.get_peer_ip_port(message[1])
                            response = "search-success " + peer_info[
                                0] + ":" + peer_info[1]
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                        else:
                            response = "search-user-not-online"
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                    # enters if username does not exist
                    else:
                        response = "search-user-not-found"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())

                elif message[0] == 'CHECKFRIEND':
                    if db.is_account_exists(
                            message[1]) and db.is_account_exists(message[2]):
                        if db.is_account_online(
                                message[1]) and db.is_account_online(
                                    message[2]):
                            if db.Is_Friend(message[1], message[2]):
                                response = 'YES'
                            else:
                                response = 'NO'
                            self.tcpClientSocket.send(response.encode())
                        else:
                            response = 'NOTONL'
                            self.tcpClientSocket.send(response.encode())
                    else:
                        response = 'NOTEX'
                        self.tcpClientSocket.send(response.encode())

            except OSError as oErr:
                pass
示例#5
0
            logging.info("Removed " + self.username + " from online peers")
            print("Removed " + self.username + " from online peers")

    # resets timer for serverThread
    def resetTimer(self):
        self.timer.cancel()
        self.timer = threading.Timer(5, self.waitHelloMessage)
        self.timer.start()


print("Registry started...")
port = 15600
checkOnlServerport = 15500
db = db.DB()
for online_user in db.db.online_peers.find():
    db.user_logout(online_user["username"])
tcpThreads = {}
hostname = gethostname()
host = gethostbyname(hostname)
#host= gethostbyname( '0.0.0.0' )
#print("Host ip: "+str(host)+ " port "+str(port))
logging.basicConfig(filename="registry.log", level=logging.INFO)
Mainserver = socket(AF_INET, SOCK_STREAM)
checkOnlServer = socket(AF_INET, SOCK_DGRAM)
Mainserver.bind(('', port))
checkOnlServer.bind(('', checkOnlServerport))
Mainserver.listen(5)

sock_list = [Mainserver, checkOnlServer]
while sock_list:
    print("Listening for incoming connections...")
示例#6
0
    def run(self):
        # locking the thread 
        self.lock = threading.Lock()
        print("Connection from: " + self.IPAddr + ":" + str(self.portAddr))
        print("IP Connected: " + self.IPAddr)
        
        while True:
            try:
                # message incoming from the peer
                message = self.tcpClientSocket.recv(1024).decode().split()
                # if JOIN recieved  #
                if message[0] == "JOIN":
                    # if account exists 
                    # then account-exists is send as reponse
                    if db.is_account_exist(message[1]):
                        response = "account-exist"
                        self.tcpClientSocket.send(response.encode())
                    
                    # if account doesn't exists
                    # account created and account success message is sent
                    else:
                        db.register(message[1], message[2], " ".join(message[3:])) #messagep[3] status
                        response = "account-success"
                        self.tcpClientSocket.send(response.encode())
                # if CHANGE recieved  #
                elif message[0] == "CHANGE":
                    # if account exist 
                    # status changed mssg is sent 
                    if db.is_account_exist(message[1]):
                        db.update_status(message[1]," ".join(message[2:]))
                        response = "status-changed"
                        self.tcpClientSocket.send(response.encode())
                # if GET recieved  #
                elif message[0] == "GET":
                    # if account exists
                    # current status is sent as mssg
                    if db.is_account_exist(message[1]):
                        status = db.get_status(message[1])
                        self.tcpClientSocket.send(status.encode())
                # if LOGIN recieved  #
                elif message[0] == "LOGIN":
                    # if no account exist with the given username 
                    # flag is sent to the peer
                    if not db.is_account_exist(message[1]):
                        response = "Account-not-exist"
                        self.tcpClientSocket.send(response.encode())
                    # if an account is already online with given username
                    # flag is sent
                    elif db.is_account_online(message[1]):
                        response = "Account-online"
                        self.tcpClientSocket.send(response.encode())
                    # if an account is log in successfully 
                    # extracts the password from the string
                    else:
                        # checks the password from looking into database
                        retrievedPass = db.get_password(message[1])
                        if retrievedPass == message[2]:
                            self.username = message[1]
                            self.lock.acquire()
                            try:
                                tcpThreads[self.username] = self
                            finally:
                                self.lock.release()

                            db.user_login(message[1], self.IPAddr, message[3])
                            # login-success is sent to the peer and udp thread is started to 
                            # watch the time
                            response = "Account-success"
                            self.tcpClientSocket.send(response.encode())
                            # udp server created
                            self.udpServer = UDPServer(self.username, self.tcpClientSocket)
                            # udp server started 
                            self.udpServer.start()
                            # start the timer of udp
                            self.udpServer.timer.start()
                        # if wrong password then flag is sent
                        else:
                            response = "Wrong-password" 
                            self.tcpClientSocket.send(response.encode())
                #   if LOGOUT recieved  #
                elif message[0] == "LOGOUT":
                    # if user logouts removes usre from the online list of peer
                    # thread removed from the list and socket is closed
                    # timer udp thread cancels 
                    if len(message) > 1 and message[1] is not None and db.is_account_online(message[1]):
                        db.user_logout(message[1])
                        # lock acquired 
                        self.lock.acquire()
                        try:
                            if message[1] in tcpThreads:
                                # removes thread form the list
                                del tcpThreads[message[1]]
                        finally:
                            # lock released
                            self.lock.release()
                        self.tcpClientSocket.close()
                        self.udpServer.timer.cancel()
                        break
                    else:
                        self.tcpClientSocket.close()
                        break
                #   if SEARCH recieved  #
                elif message[0] == "SEARCH":
                    # checks for accounts existsence
                    if db.is_account_exist(message[1]):
                        # if account is online search success is send
                        if db.is_account_online(message[1]):
                            peer_info = db.get_peer_ip_port(message[1])
                            response = "Success " + peer_info[0] + ":" + peer_info[1]
                            self.tcpClientSocket.send(response.encode())
                        # else user not online is send
                        else:
                            response = "User-not-online"
                            self.tcpClientSocket.send(response.encode())
                    # if no user with given name is found serach user not found is sent
                    else:
                        response = "User-not-found"
                        self.tcpClientSocket.send(response.encode())
            except OSError as oErr:
                pass 
示例#7
0
    def run(self):
        # locks for thread which will be used for thread synchronization
        self.lock = threading.Lock()
        print("Connection from: " + self.ip + ":" + str(port))
        print("IP Connected: " + self.ip)

        while True:
            try:
                # waits for incoming messages from peers
                message = self.tcpClientSocket.recv(1024).decode().split()
                logging.info("Received from " + self.ip + ":" +
                             str(self.port) + " -> " + " ".join(message))
                #   JOIN    #
                if message[0] == "JOIN":
                    # join-exist is sent to peer,
                    # if an account with this username already exists
                    if db.is_account_exist(message[1]):
                        response = "join-exist"
                        print("From-> " + self.ip + ":" + str(self.port) +
                              " " + response)
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    # join-success is sent to peer,
                    # if an account with this username is not exist, and the account is created
                    else:
                        db.register(message[1], message[2])
                        response = "join-success"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                #   LOGIN    #
                elif message[0] == "LOGIN":
                    # login-account-not-exist is sent to peer,
                    # if an account with the username does not exist
                    if not db.is_account_exist(message[1]):
                        response = "login-account-not-exist"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    # login-online is sent to peer,
                    # if an account with the username already online
                    elif db.is_account_online(message[1]):
                        response = "login-online"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
                    # login-success is sent to peer,
                    # if an account with the username exists and not online
                    else:
                        # retrieves the account's password, and checks if the one entered by the user is correct
                        retrievedPass = db.get_password(message[1])
                        # if password is correct, then peer's thread is added to threads list
                        # peer is added to db with its username, port number, and ip address
                        if retrievedPass == message[2]:
                            self.username = message[1]
                            self.lock.acquire()
                            try:
                                tcpThreads[self.username] = self
                            finally:
                                self.lock.release()

                            db.user_login(message[1], self.ip, message[3])
                            # login-success is sent to peer,
                            # and a udp server thread is created for this peer, and thread is started
                            # timer thread of the udp server is started
                            response = "login-success"
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                            self.udpServer = UDPServer(self.username,
                                                       self.tcpClientSocket)
                            self.udpServer.start()
                            self.udpServer.timer.start()
                        # if password not matches and then login-wrong-password response is sent
                        else:
                            response = "login-wrong-password"
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                #   LOGOUT  #
                elif message[0] == "LOGOUT":
                    # if user is online,
                    # removes the user from onlinePeers list
                    # and removes the thread for this user from tcpThreads
                    # socket is closed and timer thread of the udp for this
                    # user is cancelled
                    if len(message) > 1 and message[
                            1] is not None and db.is_account_online(
                                message[1]):
                        db.user_logout(message[1])
                        self.lock.acquire()
                        try:
                            if message[1] in tcpThreads:
                                del tcpThreads[message[1]]
                        finally:
                            self.lock.release()
                        print(self.ip + ":" + str(self.port) +
                              " is logged out")
                        self.tcpClientSocket.close()
                        self.udpServer.timer.cancel()
                        break
                    else:
                        self.tcpClientSocket.close()
                        break
                #   SEARCH  #
                elif message[0] == "SEARCH":
                    # checks if an account with the username exists
                    if db.is_account_exist(message[1]):
                        # checks if the account is online
                        # and sends the related response to peer
                        if db.is_account_online(message[1]):
                            peer_info = db.get_peer_ip_port(message[1])
                            response = "search-success " + peer_info[
                                0] + ":" + peer_info[1]
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                        else:
                            response = "search-user-not-online"
                            logging.info("Send to " + self.ip + ":" +
                                         str(self.port) + " -> " + response)
                            self.tcpClientSocket.send(response.encode())
                    # enters if username does not exist
                    else:
                        response = "search-user-not-found"
                        logging.info("Send to " + self.ip + ":" +
                                     str(self.port) + " -> " + response)
                        self.tcpClientSocket.send(response.encode())
            except OSError as oErr:
                logging.error("OSError: {0}".format(oErr))