00001 /* 00002 * ################################################################ 00003 * 00004 * ProActive: The Java(TM) library for Parallel, Distributed, 00005 * Concurrent computing with Security and Mobility 00006 * 00007 * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis 00008 * Contact: proactive@objectweb.org 00009 * 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 * USA 00024 * 00025 * Initial developer(s): The ProActive Team 00026 * http://www.inria.fr/oasis/ProActive/contacts.html 00027 * Contributor(s): 00028 * 00029 * ################################################################ 00030 */ 00031 package org.objectweb.proactive.core.body.request; 00032 00033 import java.io.IOException; 00034 00035 import org.apache.log4j.Logger; 00036 import org.objectweb.proactive.Body; 00037 import org.objectweb.proactive.ProActive; 00038 import org.objectweb.proactive.core.UniqueID; 00039 import org.objectweb.proactive.core.body.UniversalBody; 00040 import org.objectweb.proactive.core.body.ft.exception.ProtocolErrorException; 00041 import org.objectweb.proactive.core.body.ft.message.MessageInfo; 00042 import org.objectweb.proactive.core.body.ft.protocols.FTManager; 00043 import org.objectweb.proactive.core.body.reply.Reply; 00044 import org.objectweb.proactive.core.exceptions.proxy.ProxyNonFunctionalException; 00045 import org.objectweb.proactive.core.mop.MethodCall; 00046 import org.objectweb.proactive.core.util.log.Loggers; 00047 import org.objectweb.proactive.core.util.log.ProActiveLogger; 00048 import org.objectweb.proactive.ext.security.ProActiveSecurityManager; 00049 import org.objectweb.proactive.ext.security.exceptions.RenegotiateSessionException; 00050 00051 00064 public class AwaitedRequest implements Request, java.io.Serializable { 00065 00067 public static Logger logger = ProActiveLogger.getLogger(Loggers.REQUESTS); 00068 00069 // awaited sender 00070 private UniqueID awaitedSender; 00071 00072 // wrapped Request 00073 private Request wrappedRequest; 00074 private boolean isArrived; 00075 00076 //Non Functional Requests 00077 protected boolean isNFRequest = false; 00078 protected int nfRequestPriority; 00079 00084 public AwaitedRequest(UniqueID awaitedSender) { 00085 this.awaitedSender = awaitedSender; 00086 this.isArrived = false; 00087 } 00088 00093 public synchronized void setAwaitedRequest(Request r) { 00094 //System.err.println("[AWAITED] Request is updated by " + this.awaitedSender + " with " + r); 00095 //if(!(r.getSourceBodyID().equals(this.awaitedSender))){ 00096 // logger.error(" **ERROR** : update request is not from the awaited sender !"); 00097 //} 00098 this.wrappedRequest = r; 00099 this.isArrived = true; 00100 notifyAll(); 00101 } 00102 00107 public UniqueID getAwaitedSender() { 00108 return this.awaitedSender; 00109 } 00110 00115 public Reply serve(Body targetBody) throws ServeException { 00116 waitForRequest(); 00117 return wrappedRequest.serve(targetBody); 00118 } 00119 00120 /* 00121 * Wait for a request from the awaited sender. 00122 */ 00123 private synchronized void waitForRequest() { 00124 while (!isArrived) { 00125 try { 00126 // //if (logger.isDebugEnabled()) { 00127 // UniqueID waiter = ProActive.getBodyOnThis().getID(); 00128 // logger.info("[WAIT] " + waiter + " is waiting for a request from " + 00129 // this.awaitedSender); 00130 // //} 00131 this.wait(3000); 00132 00133 if (!isArrived) { 00134 UniqueID waiter = ProActive.getBodyOnThis().getID(); 00135 logger.info("[WAIT] " + waiter + 00136 " is waiting for a request from " + this.awaitedSender); 00137 } 00138 } catch (InterruptedException e) { 00139 e.printStackTrace(); 00140 } 00141 } 00142 } 00143 00145 public MethodCall getMethodCall() { 00146 if (this.isArrived) { 00147 return this.wrappedRequest.getMethodCall(); 00148 } else { 00149 return null; 00150 } 00151 } 00152 00153 public Object getParameter(int index) { 00154 return wrappedRequest.getParameter(index); 00155 } 00156 00157 public UniversalBody getSender() { 00158 return wrappedRequest.getSender(); 00159 } 00160 00161 public boolean hasBeenForwarded() { 00162 return wrappedRequest.hasBeenForwarded(); 00163 } 00164 00165 public void resetSendCounter() { 00166 throw new ProtocolErrorException( 00167 "An active object is trying to send an awaited request"); 00168 } 00169 00170 public void notifyReception(UniversalBody bodyReceiver) 00171 throws IOException { 00172 wrappedRequest.notifyReception(bodyReceiver); 00173 } 00174 00175 public int send(UniversalBody destinationBody) 00176 throws IOException, RenegotiateSessionException { 00177 throw new ProtocolErrorException( 00178 "An active object is trying to send an awaited request"); 00179 } 00180 00181 public String getMethodName() { 00182 if (!this.isArrived) { 00183 return "Awaited Request from " + this.awaitedSender; 00184 } else { 00185 return wrappedRequest.getMethodName(); 00186 } 00187 } 00188 00189 public UniqueID getSourceBodyID() { 00190 return this.awaitedSender; 00191 } 00192 00193 public long getTimeStamp() { 00194 return wrappedRequest.getTimeStamp(); 00195 } 00196 00197 public boolean isOneWay() { 00198 return wrappedRequest.isOneWay(); 00199 } 00200 00201 public long getSequenceNumber() { 00202 if (!this.isArrived) { 00203 return 0; 00204 } else { 00205 return wrappedRequest.getSequenceNumber(); 00206 } 00207 } 00208 00209 public void setMessageInfo(MessageInfo mi) { 00210 wrappedRequest.setMessageInfo(mi); 00211 } 00212 00213 public MessageInfo getMessageInfo() { 00214 if (!this.isArrived) { 00215 return null; 00216 } else { 00217 return wrappedRequest.getMessageInfo(); 00218 } 00219 } 00220 00221 public void setIgnoreIt(boolean ignore) { 00222 wrappedRequest.setIgnoreIt(ignore); 00223 } 00224 00225 public boolean ignoreIt() { 00226 return wrappedRequest.ignoreIt(); 00227 } 00228 00229 public void setFTManager(FTManager ft) { 00230 } 00231 00232 public FTManager getFTManager() { 00233 return null; 00234 } 00235 00236 public Reply serveAlternate(Body targetBody, ProxyNonFunctionalException nfe) { 00237 return this.wrappedRequest.serveAlternate(targetBody, nfe); 00238 } 00239 00240 public boolean isCiphered() { 00241 return this.wrappedRequest.isCiphered(); 00242 } 00243 00244 public long getSessionId() { 00245 return this.wrappedRequest.getSessionId(); 00246 } 00247 00248 public boolean decrypt(ProActiveSecurityManager psm) 00249 throws RenegotiateSessionException { 00250 return this.wrappedRequest.decrypt(psm); 00251 } 00252 00253 public boolean crypt(ProActiveSecurityManager psm, 00254 UniversalBody destinationBody) throws RenegotiateSessionException { 00255 return this.wrappedRequest.crypt(psm, destinationBody); 00256 } 00257 00258 // 00259 // -- Methods dealing with Non Functional Requests 00260 // 00261 00262 public boolean isFunctionalRequest() { 00263 return isNFRequest; 00264 } 00265 00266 public void setFunctionalRequest(boolean isFunctionalRequest) { 00267 this.isNFRequest = isFunctionalRequest; 00268 } 00269 00270 public void setNFRequestPriority(int nfReqPriority) { 00271 this.nfRequestPriority = nfReqPriority; 00272 } 00273 00274 public int getNFRequestPriority() { 00275 return nfRequestPriority; 00276 } 00277 }