Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / psgsim / PSGProcessEvent.java
1 package psgsim;
2
3 import org.simgrid.msg.Host;
4 import org.simgrid.msg.MsgException;
5
6 import peersim.core.Node;
7 import peersim.edsim.EDProtocol;
8
9 /**
10  * This class extends {@link org.simgrid.msg.Process} which creates a process
11  * for each host (corresponding to node in peersim) in the system.
12  * <p>
13  * The main method of this class is to handle events received, by calling the
14  * processEvent method on the corresponding node and pid.
15  * <p>
16  * See {@link peersim.edsim.EDProtocol#processEvent}
17  * 
18  * @author Khaled Baati 28/10/2014
19  * @version version 1.1
20  */
21 public class PSGProcessEvent extends org.simgrid.msg.Process {
22         /** The delivered event **/
23         private PSGTask task;
24         /** The current protocol **/
25         private EDProtocol prot;
26         /** The identifier of the current protocol **/
27         private int pid;
28
29         /**
30          * Constructs a new process from the name of a host.
31          * 
32          * @param host
33          *            the local host to create according to the active node in
34          *            peersim
35          * @param name
36          *            the host's name
37          * @param args
38          *            The arguments of main method of the process.
39          */
40         public PSGProcessEvent(Host host, String name, String[] args) {
41                 super(host, name, args);
42         }
43
44         @Override
45         public void main(String[] args) throws MsgException {
46                 Node node = NodeHost.getNode(getHost());
47                 Host.setAsyncMailbox(getHost().getName());
48                 while (PSGPlatform.getTime() < PSGPlatform.getDuration()) {
49                         task = null;
50                         task = (PSGTask) PSGTask.receive(Host.currentHost().getName(),
51                                         PSGPlatform.psToSgTime(PSGPlatform.getDuration() - PSGPlatform.getTime()-1));
52                         if (task != null && PSGPlatform.getTime() < PSGPlatform.getDuration()) {
53                                 pid = task.getPid();
54                                 prot = (EDProtocol) node.getProtocol(pid);
55                                 prot.processEvent(node, pid, task.getEvent());
56                                 PSGTransport.flush();
57                         } else
58                                 break;
59                 }
60         }
61
62 }