KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > readers > ReaderWriter


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.readers;
32
33 /*
34   ajouter un futur a la place de tout ca.
35   Dans l interface ajouter orange pour attente.
36 */

37
38 import org.objectweb.proactive.ObjectForSynchronousCall;
39
40 public class ReaderWriter implements org.objectweb.proactive.RunActive {
41
42   private int readCount;
43   private int writeCount;
44   private ReaderDisplay display;
45   private boolean done;
46   private int policy;
47   public static final int DEFAULT_POLICY = 0;
48   public static final int WRITER_POLICY = 1;
49   public static final int READER_POLICY = 2;
50
51
52   /**
53    * The no arg constructor commanded by Papdc
54    */

55   public ReaderWriter() {
56   }
57
58
59   /**
60    * The real constructor
61    */

62   public ReaderWriter(ReaderDisplay display, int policy) {
63     this.display = display;
64     this.policy = policy;
65   }
66
67
68   /**
69    * setPolicy
70    * Changes the synchronization policy
71    * @param policy the id of the new policy
72    */

73   public void setPolicy(int policy) {
74     if (policy == DEFAULT_POLICY || policy == WRITER_POLICY || policy == READER_POLICY)
75       this.policy = policy;
76   }
77
78
79   public ObjectForSynchronousCall startRead() {
80     readCount++;
81     return new ObjectForSynchronousCall();
82   }
83
84
85   public int endRead() {
86     return --readCount;
87   }
88
89
90   public ObjectForSynchronousCall startWrite() {
91     writeCount++;
92     return new ObjectForSynchronousCall();
93   }
94
95
96   public int endWrite() {
97     return --writeCount;
98   }
99
100
101   /**
102    * evenPolicy
103    * <ul>This handles the default sync policy :
104    * <li> When a new <code>startWrite</code> request arrives,
105    * no <b>new</b> <code>startRead</code> request are served</li>
106    * <li>If there is no reader or writer, just serve the oldest request.
107    * </ul><br>Note that here, we <b>explicitly</b> allow specifics
108    * requests to be carried out.
109    */

110   public void evenPolicy(org.objectweb.proactive.Service service) {
111     // if there is no writer and no write requests are before the first Read rq.
112
if (writeCount == 0) {
113       service.serveOldest(new MyRequestFilter("startRead", "startWrite"));
114     }
115     // If ther is no activity and the first request is startWrite
116
if (readCount == 0 && writeCount == 0) {
117       service.serveOldest(new MyRequestFilter("startWrite", "startRead"));
118     }
119   }
120
121
122   public void readerPolicy(org.objectweb.proactive.Service service) {
123     // If there is no activity, serve the first READER
124
if (writeCount == 0) {
125       // If there is a waiting reader, let him come im
126
service.serveOldest("startRead");
127     }
128     if (readCount == 0 && writeCount == 0) {
129       // Serve the writers in priority
130
service.serveOldest("startWrite");
131     }
132   }
133
134
135   public void writerPolicy(org.objectweb.proactive.Service service) {
136     // If there is no activity, serve the first READER
137
if (readCount == 0 && writeCount == 0) {
138       // Serve the writers in priority
139
service.serveOldest("startWrite");
140     }
141     if (writeCount == 0) {
142       // If there is a waiting reader, let him come im
143
service.serveOldest("startRead");
144     }
145   }
146
147
148   /**
149    * The main synchronization method
150    */

151   public void runActivity(org.objectweb.proactive.Body body) {
152     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
153     // Loops over lifetime...
154
while (!done) {
155       // Allows policy changes
156
service.serveOldest("setPolicy");
157       switch (policy) {
158       
159         case WRITER_POLICY:
160           writerPolicy(service);
161           break;
162
163         case READER_POLICY:
164           readerPolicy(service);
165           break;
166           
167         case DEFAULT_POLICY: // Default policy
168
default: // We never know..
169
evenPolicy(service);
170           break;
171           
172       }
173       // Allow endXX requests
174
service.serveOldest("endRead");
175       service.serveOldest("endWrite");
176       service.waitForRequest(); // Passive wait.
177
}
178   }
179   
180   
181   
182   //
183
// -- INNER CLASSES -----------------------------------------------
184
//
185

186   /**
187    * Filter that will accept the first Request for methodA only if there is no Request for method B
188    * before it.
189    */

190   private class MyRequestFilter implements org.objectweb.proactive.core.body.request.RequestFilter {
191   
192     private String JavaDoc methodNameA;
193     private String JavaDoc methodNameB;
194     private boolean foundMethodB;
195     
196     public MyRequestFilter(String JavaDoc methodNameA, String JavaDoc methodNameB) {
197       this.methodNameA = methodNameA;
198       this.methodNameB = methodNameB;
199     }
200     
201     public boolean acceptRequest(org.objectweb.proactive.core.body.request.Request request) {
202       if (foundMethodB) return false;
203       String JavaDoc methodName = request.getMethodName();
204       if (methodName.equals(methodNameA)) return true;
205       foundMethodB = methodName.equals(methodNameB);
206       return false;
207     }
208   }
209   
210 }
Popular Tags