Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add (empty) ChangeLog entry for release 3.10.
[simgrid.git] / examples / mutualExclusion / centralized / Coordinator.java
1 /*
2  * 2012. The SimGrid Team. 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
8 package mutualExclusion.centralized;
9 import java.util.LinkedList;
10
11 import org.simgrid.msg.Host;
12 import org.simgrid.msg.Msg;
13 import org.simgrid.msg.MsgException;
14 import org.simgrid.msg.Task;
15 import org.simgrid.msg.Process;
16
17
18 public class Coordinator extends Process  {
19     public Coordinator(Host host, String name, String[]args) {
20                 super(host,name,args);
21     } 
22         LinkedList<RequestTask> waitingQueue=new LinkedList<RequestTask>();
23         int CsToServe;
24                 
25         public void main(String[] args) throws MsgException {
26                 CsToServe = Integer.parseInt(args[0]);
27                 Task task;
28                 while (CsToServe >0) {
29                         task = Task.receive("coordinator");
30                         if (task instanceof RequestTask) {
31                                 RequestTask t = (RequestTask) task;
32                                 if (waitingQueue.isEmpty()) {
33                                    Msg.info("Got a request from "+t.from+". Queue empty: grant it");
34                                         GrantTask tosend =  new GrantTask();
35                                         tosend.send(t.from);
36                                 } else {
37                                         waitingQueue.addFirst(t);
38                                 }
39                         } else if (task instanceof ReleaseTask) {
40                                 if (!waitingQueue.isEmpty()) {
41                                         RequestTask req = waitingQueue.removeLast();
42                                         GrantTask tosend = new GrantTask();
43                                         tosend.send(req.from);
44                                 }
45                                 CsToServe--;
46                                 if (waitingQueue.isEmpty() && CsToServe==0) {
47                                         Msg.info("we should shutdown the simulation now");
48                                 }
49                         }
50                 }
51         }
52 }