Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[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 static final double commSizeLat = 1;
19   private static final double commSizeBw = 100000000;
20   private static final String PM_STATE = "PM_STATE";
21
22   public Sender(String hostname, String name, String[] args) throws HostNotFoundException, NativeException {
23     super(hostname,name,args);
24   }
25
26   public void main(String[] args) throws MsgException {
27     Msg.info("hello !"); 
28     Trace.hostPushState (getHost().getName(), PM_STATE, "sendingPing");
29
30     int hostCount = args.length;
31     Msg.info("host count: " + hostCount);
32     String[] mailboxes = new String[hostCount]; 
33     double time;
34     double computeDuration = 0;
35     PingPongTask ping;
36
37     for(int pos = 0; pos < args.length ; pos++) {
38       mailboxes[pos] = Host.getByName(args[pos]).getName();
39     }
40
41     for (int pos = 0; pos < hostCount; pos++) { 
42       time = Msg.getClock(); 
43       Msg.info("sender time: " + time);
44       ping = new PingPongTask("no name",computeDuration,commSizeLat);
45       ping.setTime(time);
46       ping.send(mailboxes[pos]);
47
48       Trace.hostPushState (getHost().getName(), PM_STATE, "waitingPong");
49       Task.receive(getHost().getName());
50       double timeGot = Msg.getClock();
51       double timeSent = ping.getTime();
52       double communicationTime;
53
54       Msg.info("Got at time "+ timeGot);
55       Msg.info("Was sent at time "+timeSent);
56       time = timeSent;
57
58       communicationTime=timeGot - time;
59       Msg.info("Communication time : " + communicationTime);
60
61       Msg.info(" --- bw "+ commSizeBw/communicationTime + " ----");
62
63       /* Pop the last state (going back to sending ping) */  
64       Trace.hostPopState (getHost().getName(), PM_STATE);
65     }
66
67     /* Pop the sendingPong state */  
68     Trace.hostPopState (getHost().getName(), PM_STATE);
69     Msg.info("goodbye!");
70   }
71 }