Logo AND Algorithmique Numérique Distribuée

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