import java.util.Vector;

import org.simgrid.msg.Host;
import org.simgrid.msg.Msg;
import org.simgrid.msg.MsgException;
import org.simgrid.msg.Process;

public class Merge extends Process {
	private String mailbox;
//	private String closeSEName;

	private void setMailbox(){
		this.mailbox = Integer.toString(this.getPID()) + "@" +
				getHost().getName();
	}

	public String getMailbox(){
		return this.mailbox;
	}

	public Merge(Host host, String name, String[]args) {
		super(host,name,args);
//		this.closeSEName = host.getProperty("closeSE");
	}

	public void main(String[] args) throws MsgException {
		boolean stop = false;
		// Build the mailbox name from the PID and the host name. This might be 
		// useful to distinguish different Gate processes running on a same host
		setMailbox();

		if (args.length < 1) {
			Msg.info("Slave needs 1 argument (its number)");
			System.exit(1);
		}
		Msg.info("Register Merge on '" + mailbox + "'");
		// Use of some simulation magic here, every worker knows the mailbox of 
		// the VIP server
		GateMessage.sendTo("VIPServer",GateMessage.Type.MERGE_CONNECT);

		while (!stop){
			GateMessage message = GateMessage.getFrom(mailbox);

			switch(message.getType()){
			case MERGE_START:
				Msg.info("Processing Merge");

				// Ask the LFC for the list of files to merge
				Message.sendTo(VIPSimulator.getDefaultLFC(), 
						Message.Type.ASK_MERGE_LIST);
				Msg.info("asked for list of files to merge. waiting for reply from " + VIPSimulator.getDefaultLFC());
				Message getFileList = 
						Message.getFrom(
								VIPSimulator.getDefaultLFC()+getMailbox());
				Vector<LogicalFile> logicalfilesToMerge = 
						getFileList.getFileList();

				Msg.info("Files to merge:" + logicalfilesToMerge.toString());
				Process.sleep(5000);
				Msg.verb("Goodbye!");
				stop = true;
				break;
			default:
				break;
			}
		}
	}
}

@@ -1271,425 +1271,99 @@
 %09%09%09%09
-// Ask the LFC for the list of files to merge%0A%09%09%09%09Message.sendTo(VIPSimulator.getDefaultLFC(), %0A%09%09%09%09%09%09Message.Type.ASK_MERGE_LIST);%0A%09%09%09%09Msg.info(%22asked for list of files to merge. waiting for reply from %22 + VIPSimulator.getDefaultLFC());%0A%09%09%09%09Message getFileList = %0A%09%09%09%09%09%09Message.getFrom(%0A%09%09%09%09%09%09%09%09VIPSimulator.getDefaultLFC()+getMailbox());%0A%09%09%09%09Vector%3CLogicalFile%3E logicalfilesToMerge = %0A%09%09%09%09%09%09getFileList.getFileList(
+Vector%3CString%3E fileNameList = %0A%09%09%09%09%09%09LCG.ls(mailbox,%22results/%22,VIPSimulator.getDefaultLFC()
 );%0A%0A
@@ -1399,27 +1399,20 @@
 %22 + 
-logicalfilesToMerge
+fileNameList
 .toS

call LCG.ls