1 /* Copyright (c) 2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 import org.simgrid.msg.Msg;
8 import org.simgrid.msg.Task;
9 import org.simgrid.msg.Process;
10 import org.simgrid.msg.MsgException;
11 import org.simgrid.msg.NativeException;
12 import org.simgrid.msg.Host;
13 import org.simgrid.msg.HostNotFoundException;
15 public class RelayRunner extends Process {
17 private static final int TASK_COMM_SIZE = 1000000; /* The token is 1MB long*/
19 public RelayRunner(Host host, String name, String[]args) throws HostNotFoundException, NativeException{
20 super(host,name,args);
23 /* This is the function executed by this kind of processes */
24 public void main(String[] args) throws MsgException {
25 // In this example, the processes are given numerical names: "0", "1", "2", and so on
26 int rank = Integer.parseInt(this.getName());
29 /* The root (rank 0) first sends the token then waits to receive it back */
32 Task token = new Task("Token", 0/* no computation associated*/ , TASK_COMM_SIZE );
34 Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");
37 token = Task.receive(this.getName()); // Get a message from the mailbox having the same name as the current processor
39 Msg.info("Host '"+rank+"' received '"+token.getName()+"'");
42 /* The others processes receive on their name (coming from their left neighbor -- rank-1)
43 * and send to their right neighbor (rank+1) */
44 Task token = Task.receive(this.getName());
46 Msg.info("Host '"+rank+"' received '"+token.getName()+"'");
48 String mailbox = ""+(rank+1); // Java idiomatic to get the String version of rank+1
49 if (rank+1 == Host.getCount()) {
50 /* The last process has no right neighbor, so it sends the token back to rank 0 */
54 Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");