PageRenderTime 29ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/actors/natives/NATLocalFarRef.java

http://ambienttalk.googlecode.com/
Java | 110 lines | 56 code | 10 blank | 44 comment | 4 complexity | e7144e16c990615990d7706fa800a369 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATLocalFarRef.java created on 22-dec-2006 at 11:34:15
  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 java.util.Iterator;
 31import java.util.Vector;
 32
 33import edu.vub.at.actors.ATAsyncMessage;
 34import edu.vub.at.actors.ATLetter;
 35import edu.vub.at.actors.id.ATObjectID;
 36import edu.vub.at.actors.natives.NATActorMirror.NATLetter;
 37import edu.vub.at.actors.natives.NATFarReference.NATOutboxLetter;
 38import edu.vub.at.eval.Evaluator;
 39import edu.vub.at.exceptions.InterpreterException;
 40import edu.vub.at.exceptions.XTypeMismatch;
 41import edu.vub.at.objects.ATObject;
 42import edu.vub.at.objects.ATTable;
 43import edu.vub.at.objects.ATTypeTag;
 44import edu.vub.at.objects.mirrors.NativeClosure;
 45import edu.vub.at.objects.natives.NATTable;
 46import edu.vub.at.util.logging.Logging;
 47
 48/**
 49 * Instances of NATLocalFarRef denote far references to objects 'local' to this address space.
 50 * That is, the far object is hosted by a local actor and hence message transmission is not subject
 51 * to network routing or partial failure and messages may be immediately scheduled in the inbox of
 52 * the recipient object's host actor.
 53 *
 54 * @author tvcutsem
 55 */
 56public class NATLocalFarRef extends NATFarReference {
 57
 58	/** when serializing a far reference, the event loop stays home */
 59	private transient final ELActor farObjectHost_;
 60
 61	public NATLocalFarRef(ELActor farObjectHost, ATObjectID objectId, ATTypeTag[] types, ELActor myHost, boolean isConnected) {
 62		super(objectId, types, myHost, isConnected);
 63		farObjectHost_ = farObjectHost;
 64	}
 65	
 66	protected void transmit(ATLetter letter) throws InterpreterException {
 67		synchronized (this) {
 68			if (!connected_) {
 69				outbox_.addLast(letter);
 70			} else{
 71				// the far reference itself is the receiver of the asynchronous message
 72				farObjectHost_.event_localAccept(this, letter.asNativeOutboxLetter().impl_getSerializedMessage());
 73			}
 74		}	
 75	}
 76	
 77	//called to notify connected/disconnected
 78	protected synchronized void notifyStateToSendLoop(boolean state){
 79		//if notifying reconnection, flush the outbox
 80		if (state) {
 81		  if (outbox_.size() > 0) {
 82			  for (Iterator Iter = outbox_.iterator(); Iter.hasNext();) {
 83				try {
 84					ATLetter next = (ATLetter)  Iter.next();
 85					NATOutboxLetter letter = next.asNativeOutboxLetter();
 86					 farObjectHost_.event_localAccept(this, letter.impl_getSerializedMessage());
 87				} catch (XTypeMismatch e) {
 88					Logging.RemoteRef_LOG.info(this + ": unexpected type mismatch: " + e.getMessage());
 89					e.printStackTrace();
 90				}
 91			  }
 92			  // empty the outbox
 93			  outbox_.clear();
 94		  }
 95		}
 96	}
 97	
 98	/**
 99	 * Note that the 'outbox' of a local far reference is normally empty, except if
100	 * the local far reference was disconnected by means of a 'soft disconnect'.
101	 */
102	public ATTable meta_retractUnsentMessages() throws InterpreterException {
103		return impl_retractUnsentMessages();
104	}
105	
106	public ELActor getFarHost() {
107		return farObjectHost_;
108	}
109	
110}