Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a straightforward implementation of start_time and kill_time. This change the...
[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.Host;
14 import org.simgrid.msg.Msg;
15 import org.simgrid.msg.MsgException;
16 import org.simgrid.msg.Task;
17 import org.simgrid.msg.Process;
18
19
20 public class Coordinator extends Process  {
21         public Coordinator(Host host, String name, String[]args, double startTime, double killTime) {
22                 super(host,name,args, startTime, killTime);
23         }
24         LinkedList<RequestTask> waitingQueue=new LinkedList<RequestTask>();
25         int CsToServe;
26                 
27         public void main(String[] args) throws MsgException {
28                 CsToServe = Integer.parseInt(args[0]);
29                 Task task;
30                 while (CsToServe >0) {
31                         task = Task.receive("coordinator");
32                         if (task instanceof RequestTask) {
33                                 RequestTask t = (RequestTask) task;
34                                 if (waitingQueue.isEmpty()) {
35                                    Msg.info("Got a request from "+t.from+". Queue empty: grant it");
36                                         GrantTask tosend =  new GrantTask();
37                                         tosend.send(t.from);
38                                 } else {
39                                         waitingQueue.addFirst(t);
40                                 }
41                         } else if (task instanceof ReleaseTask) {
42                                 if (!waitingQueue.isEmpty()) {
43                                         RequestTask req = waitingQueue.removeLast();
44                                         GrantTask tosend = new GrantTask();
45                                         tosend.send(req.from);
46                                 }
47                                 CsToServe--;
48                                 if (waitingQueue.isEmpty() && CsToServe==0) {
49                                         Msg.info("we should shutdown the simulation now");
50                                 }
51                         }
52                 }
53         }
54 }