import time


def wait_for_new_message(app, node, component, channel, timeout=2):
    """Waiting for a new message on a specific channel, returns none if no new message was received within the timeout."""
    wait_time = 0.0
    cur_time = app.clock.time * 1000000000 # Turn into nano seconds
    msg = None
    while wait_time < timeout:
        msg = app.receive(node, component, channel)
        if msg is not None:
            if cur_time < msg.pubtime:
                break
        time.sleep(0.1)
        wait_time += 0.1

    return msg