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