KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > philosophers > Table


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.philosophers;
32
33 import org.objectweb.proactive.ObjectForSynchronousCall;
34
35 /**
36  * This class implements the behaviors of the table and the storage of the forks
37  */

38 public class Table implements org.objectweb.proactive.RunActive {
39
40   /**
41    * The array containing the forks
42    */

43   protected boolean[] forks;
44   /**
45    * The reference to the UI layout
46    */

47   protected DinnerLayout layout;
48
49
50   /**
51    * The no arg constructor as commanded by PAPDC
52    */

53   public Table() {
54   }
55
56
57   /**
58    * The real constructor
59    * @param layout the reference to the Active layout stub
60    */

61   public Table(DinnerLayout layout) {
62     this.layout = layout;
63     forks = new boolean[5];
64   }
65    
66  
67   /**
68    * putForks
69    * Realeases both forks
70    * @param id the id of the philosopher [same as the seat index]
71    */

72   public void putForks(int id) {
73     // Sets the specified fork to be released.
74
int nextId = (id+1) % forks.length;
75     forks[id] = forks[nextId] = false;
76     layout.updateFork(id,3);
77     layout.updateFork(nextId,3);
78     layout.update(id, 0);
79   }
80
81
82   /**
83    * getForks
84    * Try to take the forks
85    * <b>Note:</b> If both forks aren't free, no forks are taken
86    * @param id the id of the philosopher [same as the seat index]
87    */

88   public ObjectForSynchronousCall getForks(int id) {
89     int nextId = (id+1) % forks.length;
90     forks[id] = forks[nextId] = true;
91     layout.update(id, 2);
92     layout.updateFork(id,4);
93     layout.updateFork(nextId,4);
94     return new ObjectForSynchronousCall();
95   }
96
97
98   /**
99    * mayEat
100    */

101   private boolean mayEat(int id) {
102     int nextId = (id+1) % forks.length;
103     return (forks[id] == false && forks[nextId] == false); // if false both forks aren't free
104
}
105
106
107   /**
108    * The live method
109    * @param body The active object's body
110    */

111   public void runActivity(org.objectweb.proactive.Body body) {
112     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
113     GetForkRequestFilter getForkRequestFilter = new GetForkRequestFilter();
114     while (body.isActive()) {
115       service.serveAll(getForkRequestFilter);
116       service.serveOldest("putForks");
117       try {
118         Thread.sleep(600);
119       } catch (InterruptedException JavaDoc e) {}
120     }
121   }
122
123   
124   //
125
// -- INNER CLASSES -----------------------------------------------
126
//
127

128   private class GetForkRequestFilter implements org.objectweb.proactive.core.body.request.RequestFilter {
129   
130     public GetForkRequestFilter() {
131     }
132     
133     public boolean acceptRequest(org.objectweb.proactive.core.body.request.Request request) {
134       if (! request.getMethodName().equals("getForks")) return false;
135       int place = ((Integer JavaDoc)request.getParameter(0)).intValue();
136       if (mayEat(place)) {
137         // Notify the user interface
138
return true;
139       } else {
140         return false;
141       }
142     }
143   } // end inner class MyRequestFilter
144

145 }
Popular Tags