Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
done reformating the doc of MSG examples. Many examples remain undocumented
[simgrid.git] / examples / java / trace / pingpong / Sender.java
1 /* Copyright (c) 2006-2014, 2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package trace.pingpong;
8 import org.simgrid.msg.Msg;
9 import org.simgrid.msg.Host;
10 import org.simgrid.msg.Task;
11 import org.simgrid.msg.Process;
12 import org.simgrid.msg.MsgException;
13 import org.simgrid.msg.NativeException;
14 import org.simgrid.msg.HostNotFoundException;
15 import org.simgrid.trace.Trace;
16
17 public class Sender extends Process {
18   private final double commSizeLat = 1;
19   private final double commSizeBw = 100000000;
20
21   public Sender(String hostname, String name, String[] args) throws HostNotFoundException, NativeException {
22     super(hostname,name,args);
23   }
24
25   public void main(String[] args) throws MsgException {
26     Msg.info("hello !"); 
27     Trace.hostPushState (getHost().getName(), "PM_STATE", "sendingPing");
28
29     int hostCount = args.length;
30     Msg.info("host count: " + hostCount);
31     String mailboxes[] = new String[hostCount]; 
32     double time;
33     double computeDuration = 0;
34     PingPongTask ping, pong;
35
36     for(int pos = 0; pos < args.length ; pos++) {
37       try {
38         mailboxes[pos] = Host.getByName(args[pos]).getName();
39       } catch (HostNotFoundException e) {
40         Msg.info("Invalid deployment file: " + e.toString());
41         System.exit(1);
42       }
43     }
44
45     for (int pos = 0; pos < hostCount; pos++) { 
46       time = Msg.getClock(); 
47       Msg.info("sender time: " + time);
48       ping = new PingPongTask("no name",computeDuration,commSizeLat);
49       ping.setTime(time);
50       ping.send(mailboxes[pos]);
51
52       Trace.hostPushState (getHost().getName(), "PM_STATE", "waitingPong");
53       pong = (PingPongTask)Task.receive(getHost().getName());
54       double timeGot = Msg.getClock();
55       double timeSent = ping.getTime();
56       double communicationTime=0;
57
58       Msg.info("Got at time "+ timeGot);
59       Msg.info("Was sent at time "+timeSent);
60       time=timeSent;
61
62       communicationTime=timeGot - time;
63       Msg.info("Communication time : " + communicationTime);
64
65       Msg.info(" --- bw "+ commSizeBw/communicationTime + " ----");
66
67       /* Pop the last state (going back to sending ping) */  
68       Trace.hostPopState (getHost().getName(), "PM_STATE");
69     }
70
71     /* Pop the sendingPong state */  
72     Trace.hostPopState (getHost().getName(), "PM_STATE");
73     Msg.info("goodbye!");
74   }
75 }