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

http://ambienttalk.googlecode.com/ · 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. */
  28. package edu.vub.at.actors.natives;
  29. import edu.vub.at.actors.ATLetter;
  30. import edu.vub.at.actors.eventloops.BlockingFuture;
  31. import edu.vub.at.actors.id.ATObjectID;
  32. import edu.vub.at.eval.Evaluator;
  33. import edu.vub.at.exceptions.InterpreterException;
  34. import edu.vub.at.exceptions.XTypeMismatch;
  35. import edu.vub.at.objects.ATObject;
  36. import edu.vub.at.objects.ATTable;
  37. import edu.vub.at.objects.ATTypeTag;
  38. /**
  39. * Instances of NATRemoteFarRef represent far references to physically remote actors.
  40. * By 'physically remote', we mean in a separate address space.
  41. *
  42. * @author tvcutsem
  43. */
  44. public class NATRemoteFarRef extends NATFarReference {
  45. /**
  46. * When a remote far reference is passed on to another virtual machine, the sendLoop
  47. * is not taken with it. At the remote end, the sendLoop is bound to the
  48. * FarReferencesThreadPool of the receiving actor.
  49. */
  50. private transient final FarReferencesThreadPool sendLoop_;
  51. /** boolean that keeps track if there is a thread of the FarReferencesThreadPool
  52. * currently transmitting a message. It is used to make meta_retract() wait
  53. * for the success/failure of the message currently being sent.
  54. */
  55. private transient boolean transmitting_;
  56. public NATRemoteFarRef(ATObjectID objectId, ELActor hostActor, ATTypeTag[] types, boolean isConnected) {
  57. super(objectId, types, hostActor, isConnected);
  58. sendLoop_ = hostActor.getHost().farReferencesThreadPool_;
  59. transmitting_ = false;
  60. }
  61. /**
  62. * Inserts an AmbientTalk message into this far reference's outbox
  63. * and signals a transmit event to the corresponding FarReferencesThreadPool
  64. * to schedule its transmission.
  65. */
  66. protected void transmit(ATLetter letter) throws InterpreterException {
  67. outbox_.addLast(letter);
  68. impl_transmit();
  69. }
  70. public ATTable meta_retractUnsentMessages() throws InterpreterException {
  71. return sendLoop_.sync_event_retractUnsentMessages(this);
  72. }
  73. protected synchronized void notifyStateToSendLoop(boolean state){
  74. //if notifying reconnection, start flushing the outbox serially
  75. if (state) { impl_transmit(); }
  76. }
  77. public NATRemoteFarRef asNativeRemoteFarReference() throws XTypeMismatch { return this;}
  78. /* Following methods are called by a thread within FarReferencesThreadPool */
  79. public ATObject impl_serve() throws InterpreterException{
  80. synchronized(this) {
  81. if (outbox_.size() > 0 && connected_) {
  82. NATOutboxLetter next = (NATOutboxLetter) outbox_.removeLast();
  83. setTransmitting(true);
  84. return next;
  85. }
  86. }
  87. return Evaluator.getNil();
  88. }
  89. // called from a FarReferencesThreadPool#TransmissionEvent
  90. // after successfully sending a message to
  91. // cause the transmission of next message if any.
  92. // Also called from ELActor after adding a message in this outbox.
  93. public void impl_transmit() {
  94. sendLoop_.event_serve(this);
  95. }
  96. // called from a FarReferencesThreadPool#TransmissionEvent
  97. // after the message being transmitted failed.
  98. public void impl_transmitFailed(ATLetter letter) {
  99. disconnected();
  100. // add the message back to the outbox.
  101. // it cannot happen that this event_transmit is followed by an event_transmit for another message,
  102. // so the order will be preserved.
  103. synchronized(this){
  104. outbox_.addFirst(letter);
  105. }
  106. setTransmitting(false);
  107. }
  108. public synchronized void setTransmitting(boolean status) {
  109. transmitting_ = status;
  110. }
  111. public synchronized boolean getTransmitting(){
  112. return transmitting_;
  113. }
  114. }