Exemplo n.º 1
0
    def GetMessages(self, request, context):
        key = self.CH.hash(request.username)
        print(f'GetMessages - Server {SERVER_ID} / Username {request.username} / Username key: {key}')

        node = self.CH.find_successor(key)
        if node.server_id != SERVER_ID:
            print(f'GetMessages - Redirected to server {node.server_id}')
            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    for message in stub.GetMessages(request):
                        yield message
            except Exception as e:
                print(e)
                yield API_pb2.Message()
        else:
            print(f'GetMessages - Server {SERVER_ID} executing the action')

            error, data = messages.get_messages(self.LSMT, request, context)
            for message in data['messages']:
                pb_message = API_pb2.Message()
                pb_message.id = message['id']
                pb_message.message = message['message']
                pb_message.user.username = message['username']
                pb_message.user.user_type = message['user_type']
                yield pb_message
Exemplo n.º 2
0
    def RegisterUser(self, request, context):
        key = self.CH.hash(request.username)
        print(f'RegisterUser - Server {SERVER_ID} / Username {request.username} / Username key: {key}')

        node = self.CH.find_successor(key)
        if node.server_id != SERVER_ID:
            print(f'RegisterUser - Redirected to server {node.server_id}')
            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    return stub.RegisterUser(request)
            except Exception as e:
                context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
                context.set_details('User Already exists')
                return API_pb2.AuthResponse()
        print(f'RegisterUser - Server {SERVER_ID} executing the action')

        error, data = users.register_user(self.LSMT, request, context)
        if error:
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(data['msg'])
            return API_pb2.AuthResponse()
        else:
            pb_auth_response = API_pb2.AuthResponse()
            pb_auth_response.user.id = data['user']['id']
            pb_auth_response.user.username = data['user']['username']
            pb_auth_response.user.user_type = data['user']['user_type']
            pb_auth_response.user.email = data['user']['email']
            pb_auth_response.user.token = data['token']
            return pb_auth_response
Exemplo n.º 3
0
    def GetMatches(self, request, context):
        key = self.CH.hash(request.username)
        print(f'GetMatches - Server {SERVER_ID} / Username {request.username} / Username key: {key}')

        node = self.CH.find_successor(key)
        if node.server_id != SERVER_ID:
            print(f'GetMatches - Redirected to server {node.server_id}')
            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    for match in stub.GetMatches(request):
                        yield match
            except Exception as e:
                print(e)
                yield API_pb2.Match()
        else:
            print(f'GetMatches - Server {SERVER_ID} executing the action')

            error, data = matches.get_matches(self.LSMT, request, context)
            for match in data['matches']:
                pb_match = API_pb2.Match()
                pb_match.id = match['id']
                pb_match.recruiter.username = match['recruiter']
                pb_match.employee.username = match['employee']
                pb_match.recruiter_match = match['recruiter_match']
                pb_match.employee_match = match['employee_match']
                yield pb_match
Exemplo n.º 4
0
    def RejectMatch(self, request, context):
        key = self.CH.hash(request.employee.username)
        print(f'RejectMatch - Employee Server {SERVER_ID} / Username {request.employee.username} / Username key: {key}')

        node = self.CH.find_successor(key)
        if node.server_id != SERVER_ID:
            print(f'RejectMatch - Redirected to server {node.server_id}')
            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    return stub.RejectMatch(request)
            except Exception as e:
                context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
                context.set_details('Wrong Credentials')
                return API_pb2.Match()

        key = self.CH.hash(request.recruiter.username)
        print(f'RejectMatch - Recruiter Server {SERVER_ID} / Username {request.recruiter.username} / Username key: {key}')

        node = self.CH.find_successor(key)
        if node.server_id != SERVER_ID:
            print(f'RejectMatch - Redirected to server {node.server_id}')
            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    stub.ReplicateRejectMatch(request)
            except Exception as e:
                print(e)

        print(f'RejectMatch - Server {SERVER_ID} executing the action')

        error, data = matches.reject_match(self.LSMT, request, context)
        if error:
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(data['msg'])
            return API_pb2.Match()
        else:
            pb_match = API_pb2.Match()
            pb_match.id = data['match']['id']
            pb_match.recruiter.username = data['match']['recruiter']
            pb_match.employee.username = data['match']['employee']
            return pb_match
Exemplo n.º 5
0
 def _initialize_finger_table(self):
     for i in range(self.m):
         id = (self.node.id + (2 ** i)) % (2 ** self.m)
         try:
             with grpc.insecure_channel(f'{self.connect_host}:{self.connect_port}') as channel:
                 stub = API_pb2_grpc.APIStub(channel)
                 pb_request_find_successor = API_pb2.RequestFindSuccessor(key=str(id))
                 pb_node = stub.FindSuccessor(pb_request_find_successor)
                 node = Node(pb=pb_node)
                 self.finger_table[i] = {
                     'id': id,
                     'succ': node
                 }
         except Exception as e:
             print(e)
Exemplo n.º 6
0
    def _join(self):
        print(f'Joining: {self.connect_host} {self.connect_port}')
        while True:
            try:
                with grpc.insecure_channel(f'{self.connect_host}:{self.connect_port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    pb_request_find_successor = API_pb2.RequestFindSuccessor(key=str(self.node.id))
                    pb_node_succ = stub.FindSuccessor(pb_request_find_successor)
                    node_succ = Node(pb=pb_node_succ)
                    self.finger_table[0]['succ'] = node_succ
                    break
            except Exception as e:
                pass

        self._initialize_stabilize()
        self._initialize_fix_fingers()
Exemplo n.º 7
0
    def ReplicateRejectMatch(self, request, context):
        key = self.CH.hash(request.recruiter.username)
        print(f'ReplicateRejectMatch - Recruiter Server {SERVER_ID} / Username {request.recruiter.username} / Username key: {key}')

        node = self.CH.find_successor(key)
        if node.server_id != SERVER_ID:
            print(f'ReplicateRejectMatch - Redirected to server {node.server_id}')
            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    return stub.ReplicateRejectMatch(request)
            except Exception as e:
                print(e)
        else:
            print(f'ReplicateRejectMatch - Server {SERVER_ID} executing the action')
            empty = API_pb2.Empty()
            matches.reject_match(self.LSMT, request, context)
            return empty
Exemplo n.º 8
0
 def _find_successor(self, key):
     if self.node.predecessor is not None and self._in_interval(key, self.node.predecessor.id, self.node.id, inclusive_right=True):
         return self.node
     elif self._in_interval(key, self.node.id, self.finger_table[0]['succ'].id, inclusive_right=True):
         return self.finger_table[0]['succ']
     else:
         preceding_node = self._closest_preceding_node(key)
         try:
             with grpc.insecure_channel(f'{preceding_node.host}:{preceding_node.port}') as channel:
                 stub = API_pb2_grpc.APIStub(channel)
                 pb_request_find_successor = API_pb2.RequestFindSuccessor(key=str(preceding_node.id))
                 pb_node = stub.FindSuccessor(pb_request_find_successor)
                 node = Node(pb=pb_node)
                 return node
         except Exception as e:
             print('Node not working')
             print(e)
             return None
Exemplo n.º 9
0
    def SendMessage(self, request, context):
        """The messages need to be replicated across the chord"""
        print(f'SendMessage - Server {SERVER_ID} / Username {request.user.username}')

        replicated_message = API_pb2.ReplicatedMessage()
        replicated_message.message.user.username = request.user.username
        replicated_message.message.user.user_type = request.user.user_type
        replicated_message.message.user.token = request.user.token
        replicated_message.message.message = request.message

        nodes = self.CH.get_reachable_nodes()
        for node in nodes:
            pb_node = replicated_message.nodes.add()
            pb_node.id = str(node.id)
            pb_node.server_id = node.server_id

        pb_node = replicated_message.nodes.add()
        pb_node.id = str(self.CH.node.id)
        pb_node.server_id = self.CH.node.server_id

        for node in nodes:
            if node.server_id == SERVER_ID:
                continue

            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    stub.ReplicateMessage(replicated_message)
            except Exception as e:
                print(e)
                continue

        error, data = messages.send_message(self.LSMT, request, context)
        if error:
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(data['msg'])
            return API_pb2.Message()
        else:
            pb_message = API_pb2.Message()
            pb_message.id = data['message']['id']
            pb_message.user.username = data['message']['username']
            pb_message.user.user_type = data['message']['user_type']
            pb_message.message = data['message']['message']
            return pb_message
Exemplo n.º 10
0
    def Authenticate(self, request, context):
        key = self.CH.hash(request.username)
        print(f'Authenticate - Server {SERVER_ID} / Username {request.username} / Username key: {key}')

        node = self.CH.find_successor(key)
        if node.server_id != SERVER_ID:
            print(f'Authenticate - Redirected to server {node.server_id}')
            try:
                with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                    stub = API_pb2_grpc.APIStub(channel)
                    return stub.Authenticate(request)
            except Exception as e:
                context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
                context.set_details('Wrong Credentials')
                return API_pb2.AuthResponse()
        print(f'Authenticate - Server {SERVER_ID} executing the action')

        error, data = authentication.authenticate(self.LSMT, request, context)
        if error:
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(data['msg'])
            return API_pb2.AuthResponse()
        else:
            pb_auth_response = API_pb2.AuthResponse()
            pb_auth_response.user.id = data['user']['id']
            pb_auth_response.user.username = data['user']['username']
            pb_auth_response.user.user_type = data['user']['user_type']
            pb_auth_response.user.email = data['user']['email']

            for match in data['matches']:
                pb_match = pb_auth_response.matches.add()
                pb_match.id = match['id']
                pb_match.recruiter.username = match['recruiter']
                pb_match.employee.username = match['employee']
                pb_match.employee.email = match['employee_email']
                pb_match.recruiter_match = match['recruiter_match']
                pb_match.employee_match = match['employee_match']

            return pb_auth_response
Exemplo n.º 11
0
    def ReplicateMessage(self, request, context):
        print(f'ReplicateMessage - Server {SERVER_ID} executing the action')
        messages.send_message(self.LSMT, request.message, context)

        replicated_message = request

        nodes = self.CH.get_reachable_nodes()
        for node in nodes:
            exists = next((x for x in replicated_message.nodes if x.id == str(node.id)), None)
            if not exists:
                pb_node = replicated_message.nodes.add()
                pb_node.id = str(node.id)
                pb_node.server_id = node.server_id
                try:
                    with grpc.insecure_channel(f'{node.host}:{node.port}') as channel:
                        stub = API_pb2_grpc.APIStub(channel)
                        stub.ReplicateMessage(replicated_message)
                except Exception as e:
                    print(e)
                    continue

        empty = API_pb2.Empty()
        return empty
Exemplo n.º 12
0
    def _stabilize(self):
        skip = False

        successor = self.finger_table[0]['succ']

        if not skip:
            with grpc.insecure_channel(f'{successor.host}:{successor.port}') as channel:
                stub = API_pb2_grpc.APIStub(channel)
                pb_node = stub.GetPredecessor(API_pb2.Empty())
                x = Node(pb=pb_node)

                # print(f"In stabilize receive predecessor {x.server_id} from successor {successor.server_id}")

                if x.id == -1:
                    skip = True

                if not skip:
                    if x is not None and self._in_interval(x.id, self.node.id, successor.id):
                        self.finger_table[0]['succ'] = x

        self._notify(self.finger_table[0]['succ'])

        t = Timer(ConsistentHashing.STABILIZE_TIME, self._stabilize)
        t.start()
Exemplo n.º 13
0
 def _notify(self, notify_node):
     with grpc.insecure_channel(f'{notify_node.host}:{notify_node.port}') as channel:
         stub = API_pb2_grpc.APIStub(channel)
         stub.Notify(self.node.to_pb())