예제 #1
0
def handle_broadcast(json: dict, address: tuple, *args, **kwargs):
    from UI import openDialog
    if not Data.has_broadcast_received(json.get('id')):
        Data.add_broadcast_received(json.get('id'))
        openDialog(json['content'].get('text'), title=f'Broadcast: {json["content"]["src"]}')
        send_flood(json.get('content'), json.get('type'), json.get('status'), json.get('id'),
                   black_list=[address[0], json['content'].get('src')])
    else:
        print('hbroadcast else')
예제 #2
0
def handle_query(json: dict, address: tuple, *args, **kwargs):
    if not Data.is_greater_query_received(json.get('id'), json['content']['TTL']):
        Data.add_query_received(json.get('id'), json['content']['TTL'])
        if Data.is_waiting_answered(json['id']):
            print(f'{json["id"]} has been answered')
            return

        res = search_text_db(json['content'].get('text'))
        content = json['content']
        if res:
            res_content = dict(data=res)
            res_content['src'] = settings.MY_IP_ADDRESS
            res_content['uuid'] = json.get('id')
            rcv_from = address[0]  # Data.get_waiting(json['id'])['received_from']
            Data.add_waiting(json['id'], address[0], [])
            send_query_answer(res_content, rcv_from)
        # elif content['TTL'] == 0:
        #     is_successful = False
        #     res_content = dict()

        else:
            content['TTL'] -= 1
            sent_to = send_flood(content, json.get('type'), json.get('status'), json.get('id'),
                                 black_list=[address[0], json['content'].get('src')])
            Data.add_waiting(json['id'], address[0], sent_to)
            return

    else:
        print('hquery else')
예제 #3
0
def send_query(message: str):
    content = {
        'text': message,
        'timestamp': '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()),
        'src': MY_IP_ADDRESS,
        'TTL': TTL
    }
    uuid_str = str(uuid.uuid4())
    sent_to = send_flood(content, MessageType.QUERY, 270, uuid_str)
    Data.add_waiting(uuid_str, None, sent_to)
예제 #4
0
def send_leave():
    send_flood({}, MessageType.LEAVE, 999, str(uuid.uuid4()))
예제 #5
0
def send_broadcast(message: str):
    time = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
    content = {'text': message, 'timestamp': time, 'src': MY_IP_ADDRESS}
    uuid_str = str(uuid.uuid4())
    add_uuid_to_db(uuid_str, message, time)
    send_flood(content, MessageType.BROADCAST, 250, uuid_str)