Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace push by addFirst, compatible with older versions of Java
[simgrid.git] / examples / mutualExclusion / centralized / Coordinator.java
1 /*
2  * $Id$
3  *
4  * Copyright 2010. The SimGrid Team. All rights reserved. 
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. 
8  */
9
10 package mutualExclusion.centralized;
11 import java.util.LinkedList;
12
13 import org.simgrid.msg.Msg;
14 import org.simgrid.msg.MsgException;
15 import org.simgrid.msg.Task;
16 import org.simgrid.msg.Process;
17
18
19 public class Coordinator extends Process  {
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 }