/interpreter/tags/at2dist030708/src/edu/vub/at/actors/net/comm/CommandProcessor.java

http://ambienttalk.googlecode.com/ · Java · 107 lines · 36 code · 8 blank · 63 comment · 1 complexity · e54c0acfe0547e108f88ef5dd1c1356b MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * CommandProcessor.java created on 3-apr-2007 at 15:02:51
  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.net.comm;
  29. import edu.vub.at.actors.net.cmd.VMCommand;
  30. import edu.vub.at.util.logging.Logging;
  31. import java.io.IOException;
  32. import java.io.ObjectInputStream;
  33. import java.net.Socket;
  34. /**
  35. * A command processor is responsible for:
  36. * <ul>
  37. * <li>Reading and executing incoming VM Command objects from an incoming socket connection
  38. * <li>Signalling to the communication bus that the connection has died upon a network exception
  39. * </ul>
  40. *
  41. * Command processors are spawned as threads by the {@link MasterConnectionThread} when a
  42. * slave VM connects to the master VM. The command processor is responsible for accepting
  43. * and executing incoming command objects sent by that slave.
  44. *
  45. * From the moment the incoming connection raises any kind of exception, the command processor
  46. * is stopped and the communication bus is notified that the connected slave should be removed
  47. * from the connections table. At that point, the thread's body will stop executing, which
  48. * should cause the command processor to become subject to garbage collection (because the
  49. * {@link MasterConnectionThread} does not store any reference to the created processor).
  50. *
  51. * @author tvcutsem
  52. */
  53. public class CommandProcessor extends Thread {
  54. private final Address remoteVM_;
  55. private final Socket connection_;
  56. private final ObjectInputStream inputStream_;
  57. private final CommunicationBus communicationBus_;
  58. /**
  59. * Create a new command object where:
  60. * @param remoteVM is the incoming slave VM for which to process incoming {@link VMCommand} objects.
  61. * @param connection is the socket connection on which to receive the command objects.
  62. * @param inputStream is the wrapped input stream of the socket.
  63. * @param bus is the communication bus to notify when communication with the slave becomes disrupted
  64. * @throws IOException if the socket connection could not be wrapped in an ObjectInputStream.
  65. */
  66. public CommandProcessor(Address remoteVM, Socket connection, ObjectInputStream inputStream, CommunicationBus bus) throws IOException {
  67. super("CommandProcessor for " + remoteVM);
  68. remoteVM_ = remoteVM;
  69. connection_ = connection;
  70. inputStream_ = inputStream;
  71. communicationBus_ = bus;
  72. }
  73. /**
  74. * Reads {@link VMCommand} objects from the incoming socket connection and executes them.
  75. * The thread does this eternally until network connection is disrupted, or the socket is
  76. * closed by the communication bus.
  77. *
  78. * Note: it is the responsibility of this thread to properly unregister and remove the
  79. * connection from the communication bus when an I/O error of any kind has occured.
  80. */
  81. public void run() {
  82. try {
  83. while (true) {
  84. VMCommand cmd = (VMCommand) inputStream_.readObject();
  85. Logging.VirtualMachine_LOG.info("CommandProcessor for " + remoteVM_ + " handling incoming command: " + cmd);
  86. // allow the command to execute itself
  87. cmd.uponReceiptBy(communicationBus_.host_, remoteVM_);
  88. }
  89. } catch(Exception e) {
  90. Logging.Network_LOG.debug(toString() + ": stopping processing because of:", e);
  91. } finally {
  92. communicationBus_.removeConnection(remoteVM_, connection_);
  93. Logging.Network_LOG.debug(toString() + " stopped.");
  94. }
  95. }
  96. public String toString() {
  97. return super.getName();
  98. }
  99. }