def main(args):
	#args = number of nodes.
	print "Starting up..."
		
	num_nodes = int(args[0])
		
	node_names = ['node{}_{}'.format(i+1,key) for i, key in enumerate(sample(["".join(item) for item in permutations('ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(log(num_nodes, 26))+1)], num_nodes))]
	#make a list of node names, in the form 'node1_CZY' where the bit after the underscore is a randomized tag, unique within this run. As many letters after the underscore will be used as necessary for all combinations to have a unique tag.
	
	#write node list
	with open('multibyz_kingsaia_nodenames','w') as thisFile:
		for name in node_names:
			thisFile.write(name+'\n')
	
	#make logs dir
	try:
		mkdir('logs/')
	except OSError:
		pass #dir already exists
	
	### IMPORTANT: The server needs to be launched manually, with command 'rabbitmq-server &'. If you don't you will get a number of amusing errors.
	
	#launch nodes
	for node in node_names:
		print "Starting {}.".format(node)
		Popen(['python', 'multibyz_kingsaia_node.py', node, 'multibyz_kingsaia_nodenames'], stdout=open('logs/log_'+node+'.txt','a',0) ) #stderror resolves on to this stdout. the '0' at the end makes all writes to log files immediate.
		
	#launch client
	#maybe this gets done manually for UI?
	try:
		call(['python', 'multibyz_kingsaia_client.py', 'multibyz_kingsaia_nodenames'])
	except (KeyboardInterrupt, SystemExit):
		pass #swallow Ctrl-C and graceful halt
		#'call' instead of 'Popen' means this will WAIT for the process to return.
	
		#after this point, we're done, and are doing shutdown.
	
		#halt all
	MessageHandler.init("HaltHandler","halt") #anonymous client
	MessageHandler.sendAll(None,None) #send empty halt message
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?