def main(args):
    #args = [[my user ID, the number of nodes]]. For the time being, we're not passing around node IDs but eventually we WILL need everyone to know all the node ids.
    print "Starting up..."
    global username, num_nodes, fault_bound
    username = args[0]
    MessageHandler.init(username, "node")
    num_nodes = int(args[1])
    fault_bound = num_nodes // 3 - 1 if num_nodes % 3 == 0 else num_nodes // 3  #t < n/3. Not <=.
    print "Maximum adversarial nodes: {}/{}.".format(fault_bound, num_nodes)
    print "I'm an adversary! Class: POTATO (does nothing)"
    weSaidNoMessages = False
    while True:
        #print "Checking for messages."
        message_counter = 0
        message = MessageHandler.receive_next()
        while message is not None:
            message_counter += 1
            message = MessageHandler.receive_next()

        if message_counter > 0:
            print "{} messages received, but I am merely a potato.".format(
                message_counter)
            message_counter = 0
        sleep(1)
def main(args):
    #args = [[my user ID, the number of nodes]]. For the time being, we're not passing around node IDs but eventually we WILL need everyone to know all the node ids.
    print "Starting up..."
    global username, num_nodes, fault_bound
    username = args[0]
    MessageHandler.init(username, "node")
    num_nodes = int(args[1])
    fault_bound = num_nodes // 3 - 1 if num_nodes % 3 == 0 else num_nodes // 3  #t < n/3. Not <=.
    print "Maximum adversarial nodes: {}/{}.".format(fault_bound, num_nodes)
    print "I'm an adversary! Class: GOLDFISH (immediately forgets about received echo/ready messages)"
    weSaidNoMessages = False
    while True:
        #print "Checking for messages."
        message = MessageHandler.receive_next()
        if message is None:
            if not weSaidNoMessages:  #only say 'nobody home' once until we receive messages again.
                print "No messages."
                weSaidNoMessages = True
            sleep(1)  #wait a second before we check again.
        else:
            weSaidNoMessages = False
            #print message.headers
            #print message.body

            type = message.headers['type']

            msgBody = message.decode()

            if type == "client":
                #TODO - client messages.
                code = msgBody[0]
                if code == "broadcast":
                    print "Client message received: broadcast {}.".format(
                        repr(msgBody[1]))
                    ReliableBroadcast.broadcast(msgBody[1])
                else:
                    print "Unknown client message received - code: {}.".format(
                        msgBody[0])
                    print message.headers
                    print message.body
                    pass  #no other types of client messages implemented yet.

            elif type == "node":
                msgType = msgBody[0]
                if msgType == "rBroadcast":
                    result = ReliableBroadcast.handleRBroadcast(message)
                    if result is not None:
                        #result from Accept. Do stuff.
                        print "Accepted message: " + repr(result)
                else:
                    print "Unknown node message received."
                    print message.headers
                    print message.body
                    pass  #TODO: throw error on junk message. Or just drop it.
            elif type == "announce":
                #announce to client - IGNORE
                pass
            else:
                print "Unknown message received."
                print message.headers
                print message.body
                pass  #malformed headers! Throw an error? Drop? Request resend?