Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / deprecated / java / app / tokenring / RelayRunner.java
1 /* Copyright (c) 2016-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 package app.tokenring;
7 import org.simgrid.msg.Host;
8 import org.simgrid.msg.Msg;
9 import org.simgrid.msg.MsgException;
10 import org.simgrid.msg.Process;
11 import org.simgrid.msg.Task;
12
13 public class RelayRunner extends Process {
14
15         private static final int TASK_COMM_SIZE = 1000000; /* The token is 1MB long*/
16
17         public RelayRunner(Host host, String name, String[]args) {
18                 super(host,name,args);
19         }
20
21         /* This is the function executed by this kind of processes */
22         @Override
23         public void main(String[] args) throws MsgException {
24                 // In this example, the processes are given numerical names: "0", "1", "2", and so on
25                 int rank = Integer.parseInt(this.getName());
26
27                 if (rank == 0) {
28                         /* The root (rank 0) first sends the token then waits to receive it back */
29
30                         String mailbox = "1";
31                         Task token = new Task("Token", 0/* no computation associated*/ , TASK_COMM_SIZE );
32
33                         Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");
34                         token.send(mailbox);
35
36                         token = Task.receive(this.getName()); // Get a message from the mailbox having the same name as the current processor
37
38                         Msg.info("Host '"+rank+"' received '"+token.getName()+"'");
39
40                 } else {
41                     /* The others processes receive on their name (coming from their left neighbor -- rank-1)
42                      * and send to their right neighbor (rank+1) */
43                         Task token = Task.receive(this.getName());
44
45                     Msg.info("Host '"+rank+"' received '"+token.getName()+"'");
46
47                     String mailbox = Integer.toString(rank+1);
48                     if (rank+1 == Host.getCount()) {
49                         /* The last process has no right neighbor, so it sends the token back to rank 0 */
50                         mailbox = "0";
51                     }
52
53                     Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");
54                     token.send(mailbox);
55                 }
56         }
57 }