import msg

import sys
from msg import *
from simgridsimulationinterface import *


first = 1
PORT_22 = 0

FINALIZE = 221297

slave_count = 0

def slave(argv):
	global PORT_22
	global FINALIZE
	global slave_count
	
	print 'start Slave'
	g = PyMsgGlobal.instance()
	print 'After get instance'
	
	slave_count = slave_count + 1
	cur_slave = slave_count
	
	#if cur_slave == 5:
	#	return 0
	
	finalize = False
	
	while finalize == False:
		task = g.getTaskFromCurrentProcess(PORT_22)
		if task != None:
			print 'Slave ', cur_slave, 'Received \"', task.getName(), '\"', ' Data ', task.getData()
			
			if task.getData() == FINALIZE:
				print 'Slave ', cur_slave, 'Task Destroyed ', task.getName()
				task.destroy()
				finalize = True
				break
			else:
				print 'Slave ', cur_slave, 'Processing \"', task.getName(), '\"'
				task.execute()
				print 'Slave ', cur_slave, '\"', task.getName(), '\" done'
				task.destroy()
		else:
			print "Hey? What's up?"
			return -1
	print 'Slave ', cur_slave, 'DOne. See you!'
	return 0


def master(argv):
	global PORT_22
	global FINALIZE
	
	number_of_tasks = int(argv[1])
	task_comp_size = int(argv[2])
	task_comm_size = int(argv[3])
	
	todo = []
	
	g = PyMsgGlobal.instance()
	#g.currentProcessSleep(1000)
	
	print 'Args:\n'
	#for a in argv:
	#	print a
	
	print 'Tasks:', number_of_tasks
	#for i in range(9):
	#	print 'aaaaa'
	#return 0
	
	for i in range(number_of_tasks):
		taskStr = 'Task_' + str(i) + '\n'
		print taskStr
		todo.append(PyMsgTask('Task_' + str(i), task_comp_size, task_comm_size, 'Pepe ' + str(i)))
	
	slaves_count = len(argv) - 4
	slaves = []
	
	for i in range(slaves_count):
		slave = argv[4+i]
		slaves.append(g.getHostByName(slave));
		if slaves[i] == None:
			print "Unknown host ", slave, " Stopping Now! ", slave
			return -1
	
		print "Known host ", slave
	
	print "Got ", len(slaves), " slaves\n"
	
	for i in range(slaves_count):
		print slaves[i].getName()
	
	for i in range(number_of_tasks):
		sched_slave = i % slaves_count
		
		print "Sending ", todo[i].getName(), " to ", slaves[sched_slave].getName()
		#if g.getCurrentHost().getName() == slaves[sched_slave].getName():
		#	print "Hey! It's me! :)\n"
			
		slaves[sched_slave].putTask(todo[i], PORT_22)
		print "Send Completed\n"
	
	print "All tasks have been dispatched. Let's tell everybody the computation is over."
	for i in range(slaves_count):
		print 'Sending Finalize to ', slaves[i].getName()
		slaves[i].putTask(PyMsgTask("finalize", 0, 0, FINALIZE), PORT_22)
		
	print "Goodbye now!\n"
	
	return 0		

def forwarder(argv):
	return 0

#def main(deployFile):
#	g = msg.PyMsgGlobal.instance()
#	g.launchApplication(deployFile)
#	res = g.main()
#	print 'Simulation time ', str(g.getClock())


def main(platFile, deployFile):
	print deployFile
	print deployFile

	g = msg.PyMsgGlobal.instance()
	g.globalInit(['testMsg'])
	g.setChannelNumber(1)
	g.setPajeOutput("msg_test.trace")
	g.createEnvironment(platFile)
	g.functionRegister("master", master)
	g.functionRegister("slave", slave)
	g.functionRegister("forwarder", forwarder)
	
	g.launchApplication(deployFile)
	res = g.main()
	print 'Simulation time ', str(g.getClock())
	g.destroy()

#g.run(main, "platform_file.xml")

#def main(deployFile):
#	g = msg.PyMsgGlobal.instance()
#	g.globalInit(['testMsg'])
#	g.setChannelNumber(1)
#	g.setPajeOutput("msg_test.trace")
#	g.createEnvironment("platform_file.xml")
#	
#	g.functionRegister("master", master)
#	g.functionRegister("slave", slave)
#	g.functionRegister("forwarder", forwarder)
#	g.launchApplication(deployFile)
#	
#	res = g.main()
#	
#	print 'Simulation time ', str(g.getClock());



