Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Let our examples compile with java 1.6, or almost
[simgrid.git] / examples / java / app / centralizedmutex / Coordinator.java
1 /* Copyright (c) 2012-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 app.centralizedmutex;
8 import java.util.LinkedList;
9
10 import org.simgrid.msg.Msg;
11 import org.simgrid.msg.Host;
12 import org.simgrid.msg.Task;
13 import org.simgrid.msg.Process;
14 import org.simgrid.msg.MsgException;
15
16 public class Coordinator extends Process {
17   LinkedList<RequestTask> waitingQueue=new LinkedList<RequestTask>();
18   int csToServe;
19
20   public Coordinator(Host host, String name, String[]args) {
21     super(host,name,args);
22   }
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 }