Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new example, Java implementation of the Token Ring
[simgrid.git] / examples / java / app / tokenring / RelayRunner.java
1 /* Copyright (c) 2016. 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.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;
14
15 public class RelayRunner extends Process {
16
17         private static final int TASK_COMM_SIZE = 1000000; /* The token is 1MB long*/
18
19         public RelayRunner(Host host, String name, String[]args) throws HostNotFoundException, NativeException{
20                 super(host,name,args);
21         }
22
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());
27
28                 if (rank == 0) {
29                         /* The root (rank 0) first sends the token then waits to receive it back */
30                         
31                         String mailbox = "1"; 
32                         Task token = new Task("Token", 0/* no computation associated*/ , TASK_COMM_SIZE );
33
34                         Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");
35                         token.send(mailbox);
36                         
37                         token = Task.receive(this.getName()); // Get a message from the mailbox having the same name as the current processor
38                         
39                         Msg.info("Host '"+rank+"' received '"+token.getName()+"'");
40
41                 } else {
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());
45                         
46                     Msg.info("Host '"+rank+"' received '"+token.getName()+"'");
47
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 */
51                         mailbox = "0";
52                     }
53                     
54                     Msg.info("Host '"+rank+"' send '"+token.getName()+"' to Host '"+mailbox+"'");
55                     token.send(mailbox);
56                 }
57         }
58 }