1 31 package org.objectweb.proactive.examples.readers; 32 33 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 55 public ReaderWriter() { 56 } 57 58 59 62 public ReaderWriter(ReaderDisplay display, int policy) { 63 this.display = display; 64 this.policy = policy; 65 } 66 67 68 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 110 public void evenPolicy(org.objectweb.proactive.Service service) { 111 if (writeCount == 0) { 113 service.serveOldest(new MyRequestFilter("startRead", "startWrite")); 114 } 115 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 (writeCount == 0) { 125 service.serveOldest("startRead"); 127 } 128 if (readCount == 0 && writeCount == 0) { 129 service.serveOldest("startWrite"); 131 } 132 } 133 134 135 public void writerPolicy(org.objectweb.proactive.Service service) { 136 if (readCount == 0 && writeCount == 0) { 138 service.serveOldest("startWrite"); 140 } 141 if (writeCount == 0) { 142 service.serveOldest("startRead"); 144 } 145 } 146 147 148 151 public void runActivity(org.objectweb.proactive.Body body) { 152 org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body); 153 while (!done) { 155 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: evenPolicy(service); 170 break; 171 172 } 173 service.serveOldest("endRead"); 175 service.serveOldest("endWrite"); 176 service.waitForRequest(); } 178 } 179 180 181 182 186 190 private class MyRequestFilter implements org.objectweb.proactive.core.body.request.RequestFilter { 191 192 private String methodNameA; 193 private String methodNameB; 194 private boolean foundMethodB; 195 196 public MyRequestFilter(String methodNameA, String 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 methodName = request.getMethodName(); 204 if (methodName.equals(methodNameA)) return true; 205 foundMethodB = methodName.equals(methodNameB); 206 return false; 207 } 208 } 209 210 } | Popular Tags |