PageRenderTime 11ms CodeModel.GetById 7ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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