/interpreter/tags/at2dist220411/src/edu/vub/at/actors/natives/NATRemoteFarRef.java
Java | 128 lines | 56 code | 14 blank | 58 comment | 3 complexity | 73cf4aba94ebecc08e9aa3c8c6cb7a96 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * NATRemoteFarRef.java created on 22-dec-2006 at 11:35:01 4 * (c) Programming Technology Lab, 2006 - 2007 5 * Authors: Tom Van Cutsem & Stijn Mostinckx 6 * 7 * Permission is hereby granted, free of charge, to any person 8 * obtaining a copy of this software and associated documentation 9 * files (the "Software"), to deal in the Software without 10 * restriction, including without limitation the rights to use, 11 * copy, modify, merge, publish, distribute, sublicense, and/or 12 * sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following 14 * conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 * OTHER DEALINGS IN THE SOFTWARE. 27 */ 28package edu.vub.at.actors.natives; 29 30import edu.vub.at.actors.ATLetter; 31import edu.vub.at.actors.eventloops.BlockingFuture; 32import edu.vub.at.actors.id.ATObjectID; 33import edu.vub.at.eval.Evaluator; 34import edu.vub.at.exceptions.InterpreterException; 35import edu.vub.at.exceptions.XTypeMismatch; 36import edu.vub.at.objects.ATObject; 37import edu.vub.at.objects.ATTable; 38import edu.vub.at.objects.ATTypeTag; 39 40/** 41 * Instances of NATRemoteFarRef represent far references to physically remote actors. 42 * By 'physically remote', we mean in a separate address space. 43 * 44 * @author tvcutsem 45 */ 46public class NATRemoteFarRef extends NATFarReference { 47 48 /** 49 * When a remote far reference is passed on to another virtual machine, the sendLoop 50 * is not taken with it. At the remote end, the sendLoop is bound to the 51 * FarReferencesThreadPool of the receiving actor. 52 */ 53 private transient final FarReferencesThreadPool sendLoop_; 54 55 /** boolean that keeps track if there is a thread of the FarReferencesThreadPool 56 * currently transmitting a message. It is used to make meta_retract() wait 57 * for the success/failure of the message currently being sent. 58 */ 59 private transient boolean transmitting_; 60 61 public NATRemoteFarRef(ATObjectID objectId, ELActor hostActor, ATTypeTag[] types, boolean isConnected) { 62 super(objectId, types, hostActor, isConnected); 63 sendLoop_ = hostActor.getHost().farReferencesThreadPool_; 64 transmitting_ = false; 65 } 66 67 /** 68 * Inserts an AmbientTalk message into this far reference's outbox 69 * and signals a transmit event to the corresponding FarReferencesThreadPool 70 * to schedule its transmission. 71 */ 72 protected void transmit(ATLetter letter) throws InterpreterException { 73 outbox_.addLast(letter); 74 impl_transmit(); 75 } 76 77 public ATTable meta_retractUnsentMessages() throws InterpreterException { 78 return sendLoop_.sync_event_retractUnsentMessages(this); 79 } 80 81 protected synchronized void notifyStateToSendLoop(boolean state){ 82 //if notifying reconnection, start flushing the outbox serially 83 if (state) { impl_transmit(); } 84 } 85 86 public NATRemoteFarRef asNativeRemoteFarReference() throws XTypeMismatch { return this;} 87 88 /* Following methods are called by a thread within FarReferencesThreadPool */ 89 90 public ATObject impl_serve() throws InterpreterException{ 91 synchronized(this) { 92 if (outbox_.size() > 0 && connected_) { 93 NATOutboxLetter next = (NATOutboxLetter) outbox_.removeLast(); 94 setTransmitting(true); 95 return next; 96 } 97 } 98 return Evaluator.getNil(); 99 } 100 101 // called from a FarReferencesThreadPool#TransmissionEvent 102 // after successfully sending a message to 103 // cause the transmission of next message if any. 104 // Also called from ELActor after adding a message in this outbox. 105 public void impl_transmit() { 106 sendLoop_.event_serve(this); 107 } 108 // called from a FarReferencesThreadPool#TransmissionEvent 109 // after the message being transmitted failed. 110 public void impl_transmitFailed(ATLetter letter) { 111 disconnected(); 112 // add the message back to the outbox. 113 // it cannot happen that this event_transmit is followed by an event_transmit for another message, 114 // so the order will be preserved. 115 synchronized(this){ 116 outbox_.addFirst(letter); 117 } 118 setTransmitting(false); 119 } 120 121 public synchronized void setTransmitting(boolean status) { 122 transmitting_ = status; 123 } 124 125 public synchronized boolean getTransmitting(){ 126 return transmitting_; 127 } 128}