コード例 #1
0
    def test_peer(self):
        # Creation peers
        peer1 = Peer(9999)
        peer2 = Peer(9998)

        # Send first message to peer2
        peer1.client.set_client(socket.gethostname(), 9998)
        message_peer1 = Message.create(Message.LIST, Message.REQUEST, 1234)
        peer1.produce_response(IP=socket.gethostname(),
                               port=9998,
                               close=False,
                               message=message_peer1)

        # Take back the message send by peer1
        IP, sock_peer2_peer1, mess = peer2.consume_receive()

        message_peer2 = Message.create(Message.LIST, Message.ERROR, None)
        peer2.produce_response(socket=sock_peer2_peer1,
                               close=True,
                               message=message_peer2)

        print(peer1.consume_receive())

        peer1.server.close()
        peer2.server.close()
コード例 #2
0
        def handle_thread():
            update = True
            while(update and not(self.event_halt.is_set())):
                # Get the member list
                while(not(self.event_member_list.is_set())
                      and not(self.event_halt.is_set())):
                    pass

                if(self.event_halt.is_set()):
                    return None

                member_list = self.member_list.ressource
                # If the member list is not empty then
                if(self.member_list.read(member_list.__len__) != 0):
                    ttl = self.ttl.ressource
                    cheese_stack = self.cheese_stack.ressource
                    # If the ttl is not dead,
                    while(not(self.ttl.read(ttl.is_zero))):
                        # We get the last cheese
                        last_cheese = self.cheese_stack.read(cheese_stack.last)
                        last_smell = last_cheese.smell

                        # We send the request every sleep seconds
                        time.sleep(sleep)

                        # and we will ask if there is a new cheese
                        message = Message.create(Message.CHEESE,
                                                 Message.REQUEST,
                                                 last_smell)
                        self.send(message)
                    update = False
                else:
                    time.sleep(sleep)
コード例 #3
0
 def process_cheese_request(self, message):
     """
     We process the message CHEESE REQUEST
     """
     # We get the parent smell
     parent_smell = message.get_data()
     # We get the cheese stak
     cheese_stack = self.cheese_stack.ressource
     # We get the requested cheese
     try:
         cheese = self.cheese_stack.read(cheese_stack.__getitem__,
                                         parent_smell)
     except KeyError:
         cheese = None
     # If we have the requested cheese,
     if(cheese is not None):
         # we return the response
         message = Message.create(Message.CHEESE, Message.RESPONSE, cheese)
     else:
         # otherwise, we get the cheese error
         message = Message.create(Message.CHEESE, Message.ERROR, None)
     return message
コード例 #4
0
        def handle_thread():
            while(not(self.event_halt.is_set())):

                while(not(self.event_mining.is_set())
                      and not(self.event_halt.is_set())):
                    pass

                if(self.event_halt.is_set()):
                    return None

                print("Debug: we are trying to mine ...")
                mining_cheese = self.mining_cheese.ressource

                if(self.mining_cheese.write(mining_cheese.mine,
                                            ntimes) is True):

                    # If we can add the cheese to the stack,
                    cheese_stack = self.cheese_stack.ressource
                    if(self.cheese_stack.write(cheese_stack.add,
                                               mining_cheese)):

                        # We remove the transactions from our list
                        trans_list = self.transaction_list.ressource
                        self.transaction_list.write(trans_list.remove_all,
                                                    mining_cheese.data)

                        print("Debug: We mined a cheese "
                              + str(self.mining_cheese.ressource.smell))

                        # We save the cheese stack
                        self.cheese_stack.write(cheese_stack.save)

                        # We add the money
                        self.money_list.add(mining_cheese)

                        # We broadcast the cheese
                        message = Message.create(Message.CHEESE,
                                                 Message.BROADCAST,
                                                 mining_cheese)
                        self.broadcast(message)

                # We create a new cheese to mine
                self.mining_cheese = Cheese()
                self.mining_cheese = Ressource(self.mining_cheese)
                self.create_mining_cheese()

                time.sleep(sleep)
コード例 #5
0
        def handle_thread():
            while(not(self.event_halt.is_set())):
                member_list = self.member_list.ressource
                size_member = self.member_list.read(member_list.__len__)
                if size_member < size:
                    self.event_member_list.clear()

                    # We create a message to send to the tracker
                    message = Message.create(Message.LIST,
                                             Message.REQUEST,
                                             self.port)
                    # and we send it
                    self.produce_response(IP=self.ip_tracker,
                                          port=self.port_tracker,
                                          message=message)

                # We ask test the list every sleep seconds
                time.sleep(sleep)
コード例 #6
0
    def tes_connection(self):
        # Start the sever
        s = Server('localhost', 9999)

        # Start the client
        c = Client('localhost', 9999)

        socket, _ = s.server_socket.accept()

        # Send a message and close the socket
        message_client = Message.create(Message.LIST, Message.REQUEST, 1234)
        c.send(message_client)

        message_server = s.read(socket)
        s.close()
        c.close()

        self.assertEqual(message_client.packet, message_server.packet)
        self.assertEqual(message_client.packet_type,
                         message_server.packet_type)
        self.assertEqual(message_client.data, message_server.data)
コード例 #7
0
 def handle_thread():
     while(not(self.event_halt.is_set())):
         # We get the response of a pong
         response = self.consume_pong()
         if(response is None):
             return None
         ip, port, pong = response
         if(not(pong)):
             # We get the member list
             member_list = self.member_list.ressource
             # We remove the member from our list
             self.member_list.write(member_list.remove_member,
                                    (ip, port))
             # and we report to the tracker that
             # a member is not connected
             message = Message.create(Message.MEMBER,
                                      Message.REPORT,
                                      (ip, port))
             self.produce_response(IP=self.ip_tracker,
                                   port=self.port_tracker,
                                   message=message)
コード例 #8
0
ファイル: peer.py プロジェクト: Sabrout/cantal_cryptocurrency
 def produce_ping(self, IP, port):
     """
     Ask for a ping
     """
     message = Message.create(Message.PING, Message.REQUEST, None)
     self.ping.queue_ping.put((IP, port, message))
コード例 #9
0
 def produce_pong(self, ip, port, pong):
     """
     We put in the queue the response of the ping
     """
     message = Message.create(Message.PING, Message.RESPONSE, pong)
     self.queue_pong.put((ip, port, message))