Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reorganize java sources
[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 import java.util.LinkedList;
11
12 import simgrid.msg.Msg;
13 import simgrid.msg.MsgException;
14 import simgrid.msg.Task;
15
16 public class Coordinator extends simgrid.msg.Process  {
17
18         LinkedList<RequestTask> waitingQueue=new LinkedList<RequestTask>();
19         int CsToServe;
20                 
21         public void main(String[] args) throws MsgException {
22                 CsToServe = Integer.parseInt(args[0]);
23                 Task task;
24                 while (CsToServe >0) {
25                         task = Task.receive("coordinator");
26                         if (task instanceof RequestTask) {
27                                 RequestTask t = (RequestTask) task;
28                                 if (waitingQueue.isEmpty()) {
29                                    Msg.info("Got a request from "+t.from+". Queue empty: grant it");
30                                         GrantTask tosend =  new GrantTask();
31                                         tosend.send(t.from);
32                                 } else {
33                                         waitingQueue.push(t);
34                                 }
35                         } else if (task instanceof ReleaseTask) {
36                                 if (!waitingQueue.isEmpty()) {
37                                         RequestTask req = waitingQueue.removeLast();
38                                         GrantTask tosend = new GrantTask();
39                                         tosend.send(req.from);
40                                 }
41                                 CsToServe--;
42                                 if (waitingQueue.isEmpty() && CsToServe==0) {
43                                         Msg.info("we should shutdown the simulation now");
44                                 }
45                         }
46                 }
47         }
48 }