Exemple #1
0
def test_wait_for_message(support):
    address = nw0.core.address()
    message_sent = uuid.uuid4().hex
    support.queue.put(("wait_for_message_from", [address, message_sent]))
    message_received = nw0.wait_for_message_from(address, wait_for_s=5)
    assert message_received == message_sent
    nw0.send_reply_to(address, message_received)
Exemple #2
0
def test_wait_for_message(support):
    address = nw0.core.address()
    message_sent = uuid.uuid4().hex
    support.queue.put(("wait_for_message_from", [address, message_sent]))
    message_received = nw0.wait_for_message_from(address, wait_for_s=5)
    assert message_received == message_sent
    nw0.send_reply_to(address, message_received)
Exemple #3
0
def test_send_reply(support):
    address = nw0.core.address()
    reply_queue = queue.Queue()

    support.queue.put(("send_reply_to", [address, reply_queue]))
    message_received = nw0.wait_for_message_from(address, wait_for_s=5)
    nw0.send_reply_to(address, message_received)
    reply = reply_queue.get()
    assert reply == message_received
Exemple #4
0
def test_send_reply(support):
    address = nw0.core.address()
    reply_queue = queue.Queue()

    support.queue.put(("send_reply_to", [address, reply_queue]))
    message_received = nw0.wait_for_message_from(address, wait_for_s=5)
    nw0.send_reply_to(address, message_received)
    reply = reply_queue.get()
    assert reply == message_received
Exemple #5
0
def wait_for_ip(address):
    print("waiting for ip")
    content = nw0.wait_for_message_from(address)
    pat = re.compile(".*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
    test = pat.match(content)
    if not test:
        nw0.send_reply_to(address, "please input ip address")
        print("please input ip address")
        wait_for_ip(address)
    else:
        print("connected!")
        nw0.send_reply_to(address, "connected!")
        return content
def play_second(choose_move):
    moves = []
    # advertise
    my_address = nw0.advertise("RPS")
    # comm 1a
    # recv their hash
    their_hash = nw0.wait_for_message_from(my_address)

    # we don't know their move yet!
    my_move = decide_move(choose_move, their_move=None)

    my_salt = make_salt()
    my_hash = make_hash(my_move, my_salt)

    # comm 1b
    # reply with hash
    nw0.send_reply_to(my_address, my_hash)
    print('I send a sealed {}'.format(my_move))

    # comm 2a
    # receive their move + salt
    their_move_char, their_salt = nw0.wait_for_message_from(my_address)
    their_move = RPS(their_move_char)

    check_hash(their_move, their_salt, their_hash)

    # now we know their move, let's choose again!
    my_move = decide_move(choose_move, their_move=their_move)

    # comm 2b
    # reply with my move + salt
    nw0.send_reply_to(my_address, (my_move.char, my_salt))

    # my_move = RPS(my_move.char)
    moves.append(their_move)
    moves.append(my_move)

    print('They play', their_move)
    print('I play', my_move)
    return decide_winner(moves)
def play_second(choose_move):
    moves = []
    # advertise
    my_address = nw0.advertise("RPS")
    # receive their move
    their_move = RPS(
        nw0.wait_for_message_from(my_address)
    )
    # reply with my move
    if isinstance(choose_move, type('')):
        my_move = RPS(choose_move)
    else:
        my_move = choose_move(their_move)
    nw0.send_reply_to(my_address, my_move.char)

    my_move = RPS(my_move.char)
    moves.append(their_move)
    moves.append(my_move)

    print('I play', my_move)
    print('They play', their_move)
    return decide_winner(moves)
# Search for messages from participants until roster is full
while len(players) < MAX_PLAYERS:
    # Poll the server checking for requests from participants
    connections = nwz.discover_all()
    #print(connections)
    # If a message has been recieved
    if len(connections) > 0:
        for c in connections:
            # Capture the address for the connection to the quiz client
            if "Quiz Participant" in c[0] and c[1] not in player_addresses:
                print(connections)
                nwz.send_news_to(quiz_server, "Information", "Player acknowledged" + c[1])
                conn = nwz.discover(c[0])
                response = nwz.send_message_to(conn, player_ID)
                nwz.send_reply_to(conn)
                players.append(Player(player_ID, c[1], conn))
                player_addresses.append(c[1])
                player_ID += 1
    else:
        sleep(1)
        if len(players) > 0:
            nwz.send_news_to(quiz_server, "Information", "Waiting for... " + str(MAX_PLAYERS -len(players)) + " players")
sleep (5)
questions = []

with open("questions.csv", 'r') as file:
    for line in file.readlines():
        line = line.split(',')
        if len(line) > 4:
            new_q = Question(line[0], line[1:5], line[5])
    else:
        reply = nwz.wait_for_message_from(connection,
                                          wait_for_s=0,
                                          autoreply=True)
        if reply is not None:
            print(reply)
            if reply == "Quiz Starting":
                quiz_starting = True
        sleep(1)
print("Quiz loop starting")
quiz_over = False
while not quiz_over:
    reply = nwz.wait_for_message_from(connection, wait_for_s=0, autoreply=True)
    if reply is not None:
        #print(reply)
        if reply == "Next Question":
            question = nwz.wait_for_message_from(connection, autoreply=True)
            print(question)
            reply = None
            answers = nwz.wait_for_message_from(connection)
            for i in range(len(answers)):
                print(str(i) + ". " + answers[i])
            print('Enter the number of your answer')
            my_answer = int(input(">"))
            nwz.send_reply_to(connection, my_answer)
            correct = nwz.wait_for_message_from(connection)
            print(correct)
            nwz.send_reply_to(connection, None)
    else:
        sleep(1)
Exemple #10
0
quiz_server = nwz.advertise('Quiz')
player_ID = 0

# Search for messages from participants until roster is full
while len(players) < MAX_PLAYERS:
    # Poll the server checking for rquests from participants
    message = nwz.wait_for_message_from(quiz_server, wait_for_s=0)
    # If a message has been recieved
    if message is not None:
        # Capture the address for the connection to the quiz client
        address = message
        print(address)
        conn = nwz.discover(address)
        players.append(Player(player_ID, address, conn))
        player_ID += 1
        nwz.send_reply_to(quiz_server, "Player acknowledged")
        reply = nwz.send_message_to(conn, "Welcome to the quiz")
        message = None
    else:
        sleep(1)
        if len(players) > 0:
            reply = nwz.send_message_to(
                players[0].connection,
                "Waiting for... " + str(len(players) - MAX_PLAYERS))

questions = []
with open("../questions.csv", 'r') as file:
    for line in file.readlines():
        line = line.split(',')
        if len(line) > 4:
            new_q = Question(line[0], line[1:5], line[5])
Exemple #11
0
    n2 = random.randint(1, MAX_QUESTNUM)
    o, op = random.choice(list(operations.items()))
    return "{} {} {}".format(n1,o,n2), str(op(n1,n2))


num_rounds = 0
score = 0

while True:
    greeting = nw0.wait_for_message_from(a)
    print("-> {}".format(greeting))

    question, expected_answer = questionator3000()

    reply1 = "Round #{}:\nQuestion: {}".format(num_rounds, question)
    nw0.send_reply_to(a, reply1)
    print("<- " + reply1)

    answer = nw0.wait_for_message_from(a)
    print("-> {}".format(answer))


    if answer == expected_answer:
        outcome = "correct!"
        score += 1
    else:
        outcome = "incorrect, the correct answer was {}!!!!!!!".format(expected_answer)

    reply = "{}\nthe current score is: {}".format(outcome, score)
    nw0.send_reply_to(a, reply)
    print("<-" + reply)
Exemple #12
0
address = networkzero.advertise("foo")

STORE = {}


def my_func(input_data):
    try:
        command = json.loads(input_data)
        if command["cmd"] == "SET":
            STORE[command["key"]] = command["data"]
            response = {"status": "ok"}
        elif command["cmd"] == "GET":
            response = {
                "status": "ok",
                "info": STORE[command["key"]]
            }
        else:
            raise NotImplementedError(command)
    except:
        response = {
            "status": "error",
            "info": traceback.format_exc()
        }
    return json.dumps(response)


while True:
    input_data = networkzero.wait_for_message_from(address)
    print(input_data)
    networkzero.send_reply_to(address, my_func(input_data))
Exemple #13
0
import networkzero as nw0

address = nw0.advertise("reverso")
while True:
    message = nw0.wait_for_message_from(address)
    nw0.send_reply_to(address, message[::-1])
Exemple #14
0
import time
import networkzero as nw0

address = nw0.advertise("poller1")

while True:
    message = nw0.wait_for_message_from(address, wait_for_s=0)
    if message is not None:
        print("Received:", message)
        nw0.send_reply_to(address, "Received: %s" % message)
        break
    else:
        print("Doing other useful things ...")
        time.sleep(1)
Exemple #15
0
    #
    # Receive an anagram for which we have to find a match
    #
    anagram = nw0.wait_for_message_from(address).lower().strip()
    print("Looking for %s" % anagram)
    
    #
    # To compare two words we'll sort each and compare
    # the result. We need only sort the word we're searching
    # for once.
    #
    letters = "".join(sorted(anagram))
    
    #
    # Compare our incoming anagram, sorted, against each word
    # in turn, sorted. If we get a match, send the matching 
    # word and stop searching
    #
    for word in words:
        if "".join(sorted(word)) == letters:
            print("Found %s" % word)
            nw0.send_reply_to(address, word)
            break
    
    #
    # If we can't find a match, return None
    #
    else:
        print("No match found")
        nw0.send_reply_to(address, None)