Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) SiteWhere, LLC. All rights reserved. http://www.sitewhere.com
   *
   * The software in this package is published under the terms of the CPAL v1.0
   * license, a copy of which has been included with this distribution in the
   * LICENSE.txt file.
   */
  package com.sitewhere.device.communication.socket;
  
 
 
Implementation of com.sitewhere.spi.device.communication.IInboundEventReceiver that creates a server socket and spawns threads to service requests.

Author(s):
Derek
 
 public class SocketInboundEventReceiver<T> extends LifecycleComponent implements IInboundEventReceiver<T> {

Static logger instance
 
 	private static Logger LOGGER = Logger.getLogger(SocketInboundEventReceiver.class);

Default number of threads used to service requests
 
 	private static final int DEFAULT_NUM_THREADS = 5;

Default ip binding for server socket
 
 	private static final String DEFAULT_BIND_ADDRESS = "localhost";

Default port for server socket
 
 	private static final int DEFAULT_PORT = 8484;

Number of threads used to service requests
 
 	private int numThreads = ;

Bind address used for server socket
 
Port used for server socket
 
 	private int port = ;

Parent event source
 
 
Pool of threads used to service requests
 
Pool of threads used to service requests
 
 	private ExecutorService pool;

Server socket that processes requests
 
 	private ServerSocket server;

Handles processing of server requests
 
 
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see com.sitewhere.spi.server.lifecycle.ILifecycleComponent#start()
 	 */
 	public void start() throws SiteWhereException {
 		try {
 			if (getHandlerFactory() == null) {
 				throw new SiteWhereException(
 						"No socket interaction handler factory configured for socket event source.");
 			}
 			.info("Receiver creating server socket on " + getBindAddress() + ":" + getPort() + ".");
 			this. = new ServerSocket(getPort());
 			this. = Executors.newFixedThreadPool(getNumThreads());
 			.info("Socket receiver creating processing pool of " + getNumThreads() + " threads.");
 			.info("Socket receiver processing started.");
 		} catch (IOException e) {
 			throw new SiteWhereException("Unable to bind server socket for event receiver."e);
 		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sitewhere.spi.server.lifecycle.ILifecycleComponent#getLogger()
	 */
	public Logger getLogger() {
		return ;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sitewhere.spi.device.communication.IInboundEventReceiver#getDisplayName()
	 */
	public String getDisplayName() {
		return getBindAddress() + ":" + getPort();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sitewhere.spi.server.lifecycle.ILifecycleComponent#stop()
	 */
	public void stop() throws SiteWhereException {
		if ( != null) {
		}
		if ( != null) {
		}
		if ( != null) {
		}
		if ( != null) {
			try {
catch (IOException e) {
				throw new SiteWhereException("Error shutting down server socket for event receiver."e);
			}
		}
		.info("Socket receiver processing stopped.");
	}

Handles loop that processes server requests.

Author(s):
Derek
	private class ServerProcessingThread implements Runnable {

Indicates if processing should continue
		private boolean terminate = false;
		public void run() {
			while (!) {
				try {
					Socket socket = .accept();
					.submit(processor);
catch (IOException e) {
					if (!) {
						.error("Exception while accepting request in event receiver server socket."e);
					}
				}
			}
		}
		public void setTerminate(boolean terminate) {
			this. = terminate;
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sitewhere.spi.device.communication.IInboundEventReceiver#onEventPayloadReceived
	 * (java.lang.Object)
	 */
	public void onEventPayloadReceived(T payload) {
	}

Handles processing for a single request.

Author(s):
Derek
	private class RequestProcessingThread implements Runnable {

Socket for processing
		private Socket socket;
		public RequestProcessingThread(Socket socket) {
			this. = socket;
		}
		public void run() {
			try {
				.debug("About to process request received on port " + getPort() + ".");
				.debug("Processing complete.");
catch (SiteWhereException e) {
				.error("Exception processing request in event receiver server socket."e);
			}
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sitewhere.spi.device.communication.IInboundEventReceiver#getEventSource()
	 */
		return ;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sitewhere.spi.device.communication.IInboundEventReceiver#setEventSource(com
	 * .sitewhere.spi.device.communication.IInboundEventSource)
	 */
	public void setEventSource(IInboundEventSource<T> eventSource) {
		this. = eventSource;
	}
	public int getNumThreads() {
		return ;
	}
	public void setNumThreads(int numThreads) {
		this. = numThreads;
	}
	public String getBindAddress() {
		return ;
	}
	public void setBindAddress(String bindAddress) {
		this. = bindAddress;
	}
	public int getPort() {
		return ;
	}
	public void setPort(int port) {
		this. = port;
	}
	}
	public void setHandlerFactory(ISocketInteractionHandlerFactory<T> handlerFactory) {
		this. = handlerFactory;
	}
New to GrepCode? Check out our FAQ X