Esempio n. 1
0
def main():
    app = p2pConsole()
    while (True):
        console_input = str(raw_input('node$ ')).split()
        if str(console_input[0]) == 'connect':
            if len(console_input) == 3:
                app.host = str(console_input[1])
                app.port = int(console_input[2])
                app.start_connection()
            else:
                print("Staring connection with default values. Use " +
                      "following syntax for custom values: connect " +
                      "[host] [port]")
                app.start_connection()
        elif str(console_input[0]) == 'join':
            app.sock.send(
                createMessage(msg_type=MSG_JOIN,
                              payload="",
                              org_port=app.org_port,
                              org_ip=app.org_ip))
            app.receive_message()
        elif str(console_input[0]) == 'query':
            if len(console_input) == 2:
                message = createMessage(msg_type=MSG_QUERY,
                                        ttl=3,
                                        org_port=app.org_port,
                                        org_ip=app.org_ip,
                                        payload=str(console_input[1]))
                print(parseReceivedMessage(message))
                app.sock.send(message)
                app.receive_message()
            else:
                print("Use following syntax: query [payload]")
        elif str(console_input[0]) == 'ping':
            app.sock.send(
                createMessage(ttl=3, org_port=app.org_port, org_ip=app.org_ip))
            app.receive_message()
        elif str(console_input[0]) == 'bye':
            print("Closing connection")
            app.sock.send(
                createMessage(msg_type=MSG_BYE,
                              org_port=app.org_port,
                              org_ip=app.org_ip))
            app.close_connection()
            print("Closed")
        elif str(console_input[0]) == 'quit':
            app.close_connection()
            sys.exit(0)
        else:
            print('Wrong input!')
Esempio n. 2
0
def main():
    app = p2pConsole()
    while(True):
        console_input = str(raw_input('node$ ')).split()
        if str(console_input[0]) == 'connect':
            if len(console_input) == 3:
                app.host = str(console_input[1])
                app.port = int(console_input[2])
                app.start_connection()
            else:
                print("Staring connection with default values. Use " +
                      "following syntax for custom values: connect " +
                      "[host] [port]")
                app.start_connection()
        elif str(console_input[0]) == 'join':
            app.sock.send(createMessage(
                msg_type=MSG_JOIN, payload="", org_port=app.org_port,
                org_ip=app.org_ip))
            app.receive_message()
        elif str(console_input[0]) == 'query':
            if len(console_input) == 2:
                message = createMessage(
                    msg_type=MSG_QUERY, ttl=3, org_port=app.org_port,
                    org_ip=app.org_ip, payload=str(console_input[1]))
                print(parseReceivedMessage(message))
                app.sock.send(message)
                app.receive_message()
            else:
                print("Use following syntax: query [payload]")
        elif str(console_input[0]) == 'ping':
            app.sock.send(createMessage(ttl=3, org_port=app.org_port,
                                        org_ip=app.org_ip))
            app.receive_message()
        elif str(console_input[0]) == 'bye':
            print("Closing connection")
            app.sock.send(createMessage(msg_type=MSG_BYE,
                                        org_port=app.org_port,
                                        org_ip=app.org_ip))
            app.close_connection()
            print("Closed")
        elif str(console_input[0]) == 'quit':
            app.close_connection()
            sys.exit(0)
        else:
            print('Wrong input!')
Esempio n. 3
0
def handshake(socket):
    socket.send(createMessage(msg_type=MSG_JOIN,
                              org_ip=socket.getsockname()[0]))
    response = socket.recv(1024)
    header, payload = parseReceivedMessage(response)
    # Join was successful
    if payload == ('0200'):
        global neighbours
        neighbours.append((header[6], header[4]))
        # Send PING type b to expand neighbourhood
        socket.send(createMessage(ttl=5, org_port=socket.getsockname()[1],
                                  org_ip=socket.getsockname()[0]))
        response = socket.recv(1024)
        header, payload = parseReceivedMessage(response)
        if len(payload) > 0:
            # Iterate through payload
            for i in range(1, len(payload)):
                # Add to neighbours if not already there
                if not (payload[i][0], payload[i][1]) in neighbours:
                    neighbours.append((payload[i][0], payload[i][1]))
            print("Neighbours:")
            print(neighbours)
Esempio n. 4
0
def forward(header, payload, sock):
    global connections
    data = struct.pack('>'+str(len(payload[0]))+'s', payload[0])
    message = createMessage(msg_type=MSG_QUERY, ttl=header[1]-1,
                            org_port=header[4], org_ip=header[6],
                            msg_id=header[7], payload=data)

    for connection in connections:
        # Don't send it back to searching node
        if sock is not connection[1]:
            print("Forwarded Message parsed:")
            print(parseReceivedMessage(message))
            connection[1].send(message)
Esempio n. 5
0
def handshake(socket):
    socket.send(
        createMessage(msg_type=MSG_JOIN, org_ip=socket.getsockname()[0]))
    response = socket.recv(1024)
    header, payload = parseReceivedMessage(response)
    # Join was successful
    if payload == ('0200'):
        global neighbours
        neighbours.append((header[6], header[4]))
        # Send PING type b to expand neighbourhood
        socket.send(
            createMessage(ttl=5,
                          org_port=socket.getsockname()[1],
                          org_ip=socket.getsockname()[0]))
        response = socket.recv(1024)
        header, payload = parseReceivedMessage(response)
        if len(payload) > 0:
            # Iterate through payload
            for i in range(1, len(payload)):
                # Add to neighbours if not already there
                if not (payload[i][0], payload[i][1]) in neighbours:
                    neighbours.append((payload[i][0], payload[i][1]))
            print("Neighbours:")
            print(neighbours)
Esempio n. 6
0
def forward(header, payload, sock):
    global connections
    data = struct.pack('>' + str(len(payload[0])) + 's', payload[0])
    message = createMessage(msg_type=MSG_QUERY,
                            ttl=header[1] - 1,
                            org_port=header[4],
                            org_ip=header[6],
                            msg_id=header[7],
                            payload=data)

    for connection in connections:
        # Don't send it back to searching node
        if sock is not connection[1]:
            print("Forwarded Message parsed:")
            print(parseReceivedMessage(message))
            connection[1].send(message)
Esempio n. 7
0
def process_message(header, payload, socket):
    # Discard messages with invalid version or ttl
    global q_searches
    global connections
    global neighbours
    if (header[0] == 1 or header[1] < 1 or header[1] > 5):
        if header[2] == MSG_PING:
            # Type a
            if header[1] == 1:
                print("Respond with PONG A")
                socket.send(createMessage(msg_type=MSG_PONG,
                                          org_port=socket.getsockname()[1],
                                          org_ip=socket.getsockname()[0]))
            # Type b
            else:
                tmp = list(neighbours)
                if (header[6], header[4]) in tmp:
                    tmp.remove((header[6], header[4]))
                response = tmp[:5]
                data = bytearray()
                if(len(response) > 0):
                    data.extend(struct.pack('>HH', len(response), 0))
                    for item in response:
                        data.extend(struct.pack('>IHH', ipToNum(item[0]),
                                                item[1], 0))
                print("Respond with PONG B")
                socket.send(createMessage(msg_type=MSG_PONG,
                                          org_port=socket.getsockname()[1],
                                          org_ip=socket.getsockname()[0],
                                          payload=data))
        elif header[2] == MSG_PONG:
            # Pong type b
            if len(payload) > 0:
                # Iterate through payload
                for i in range(1, len(payload)):
                    # Add to neighbours if not already there
                    if not (payload[i][0], payload[i][1]) in neighbours:
                        neighbours.append((payload[i][0], payload[i][1]))
        elif header[2] == MSG_BYE:
            if (header[6], header[4]) in neighbours:
                neighbours.remove((header[6], header[4]))
                print("Neighbours:")
                print(neighbours)
            if (header[6], socket) in connections:
                connections.remove((header[6], socket))

        elif header[2] == MSG_JOIN:
            if not (header[6], header[4]) in neighbours:
                neighbours.append((header[6], header[4]))
                print("Neighbours:")
                print(neighbours)
            data = bytearray()
            data.append(0x02)
            data.append(0x00)
            print("Respond with JOIN OK")
            socket.send(createMessage(msg_type=MSG_JOIN,
                                      org_port=socket.getsockname()[1],
                                      org_ip=socket.getsockname()[0],
                                      payload=data))
        elif header[2] == MSG_QUERY:
            # Check if key is known
            if payload[0] in keys:
                data = struct.pack('>HHHH4s', 1, 0, 1, 0,
                                   binascii.unhexlify(keys[payload[0]]))
                print("Respond with QUERY HIT")
                socket.send(createMessage(msg_type=MSG_QHIT,
                                          org_port=socket.getsockname()[1],
                                          org_ip=socket.getsockname()[0],
                                          payload=data, msg_id=header[7]))
            # Save search
            q_searches.append((header[6], header[7]))
            # Forward to other nodes
            forward(header, payload, socket)
        elif header[2] == MSG_QHIT:
            # Check if known message id
            for (x, y) in q_searches:
                if y == header[7]:
                    # Check if connection exists with given ip address
                    for (a, b) in connections:
                        if a == x:
                            data = bytearray()
                            data.extend(struct.pack('>HH', payload[0], 0))
                            for i in range(payload[0]):
                                data.extend(
                                    struct.pack('>HH4s', i+1, 0,
                                                binascii.unhexlify(
                                                    payload[1][i+1])))
                            b.send(createMessage(msg_type=MSG_QHIT,
                                                 org_port=header[4],
                                                 org_ip=header[6],
                                                 payload=data, msg_id=y))
Esempio n. 8
0
def process_message(header, payload, socket):
    # Discard messages with invalid version or ttl
    global q_searches
    global connections
    global neighbours
    if (header[0] == 1 or header[1] < 1 or header[1] > 5):
        if header[2] == MSG_PING:
            # Type a
            if header[1] == 1:
                print("Respond with PONG A")
                socket.send(
                    createMessage(msg_type=MSG_PONG,
                                  org_port=socket.getsockname()[1],
                                  org_ip=socket.getsockname()[0]))
            # Type b
            else:
                tmp = list(neighbours)
                if (header[6], header[4]) in tmp:
                    tmp.remove((header[6], header[4]))
                response = tmp[:5]
                data = bytearray()
                if (len(response) > 0):
                    data.extend(struct.pack('>HH', len(response), 0))
                    for item in response:
                        data.extend(
                            struct.pack('>IHH', ipToNum(item[0]), item[1], 0))
                print("Respond with PONG B")
                socket.send(
                    createMessage(msg_type=MSG_PONG,
                                  org_port=socket.getsockname()[1],
                                  org_ip=socket.getsockname()[0],
                                  payload=data))
        elif header[2] == MSG_PONG:
            # Pong type b
            if len(payload) > 0:
                # Iterate through payload
                for i in range(1, len(payload)):
                    # Add to neighbours if not already there
                    if not (payload[i][0], payload[i][1]) in neighbours:
                        neighbours.append((payload[i][0], payload[i][1]))
        elif header[2] == MSG_BYE:
            if (header[6], header[4]) in neighbours:
                neighbours.remove((header[6], header[4]))
                print("Neighbours:")
                print(neighbours)
            if (header[6], socket) in connections:
                connections.remove((header[6], socket))

        elif header[2] == MSG_JOIN:
            if not (header[6], header[4]) in neighbours:
                neighbours.append((header[6], header[4]))
                print("Neighbours:")
                print(neighbours)
            data = bytearray()
            data.append(0x02)
            data.append(0x00)
            print("Respond with JOIN OK")
            socket.send(
                createMessage(msg_type=MSG_JOIN,
                              org_port=socket.getsockname()[1],
                              org_ip=socket.getsockname()[0],
                              payload=data))
        elif header[2] == MSG_QUERY:
            # Check if key is known
            if payload[0] in keys:
                data = struct.pack('>HHHH4s', 1, 0, 1, 0,
                                   binascii.unhexlify(keys[payload[0]]))
                print("Respond with QUERY HIT")
                socket.send(
                    createMessage(msg_type=MSG_QHIT,
                                  org_port=socket.getsockname()[1],
                                  org_ip=socket.getsockname()[0],
                                  payload=data,
                                  msg_id=header[7]))
            # Save search
            q_searches.append((header[6], header[7]))
            # Forward to other nodes
            forward(header, payload, socket)
        elif header[2] == MSG_QHIT:
            # Check if known message id
            for (x, y) in q_searches:
                if y == header[7]:
                    # Check if connection exists with given ip address
                    for (a, b) in connections:
                        if a == x:
                            data = bytearray()
                            data.extend(struct.pack('>HH', payload[0], 0))
                            for i in range(payload[0]):
                                data.extend(
                                    struct.pack(
                                        '>HH4s', i + 1, 0,
                                        binascii.unhexlify(payload[1][i + 1])))
                            b.send(
                                createMessage(msg_type=MSG_QHIT,
                                              org_port=header[4],
                                              org_ip=header[6],
                                              payload=data,
                                              msg_id=y))