Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class DaemonHttpServlet extends HttpServlet {
- private static final long serialVersionUID = 5915272369604402570L;
- /**
- * The default listening port (8888)
- */
- protected int DEFAULT_PORT = 8888;
- private Thread daemonThread;
- /**
- * Begins a thread listening for socket connections. Subclasses that
- * override this method must be sure to first call
- * <tt>super.init(config)</tt>.
- *
- * @param config
- * the servlet config
- * @exception ServletException
- * if a servlet exception occurs
- */
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- try {
- log("Entering init...");
- daemonThread = new Daemon(this);
- daemonThread.start();
- } catch (Exception e) {
- log("Problem starting socket server daemon thread"
- + e.getClass().getName() + ": " + e.getMessage());
- }
- }
- /**
- * Returns the socket port on which the servlet will listen. A servlet can
- * change the port in three ways: by using the <tt>socketPort</tt> init
- * parameter, by setting the <tt>DEFAULT_PORT</tt> variable before calling
- * <tt>super.init()</tt>, or by overriding this method's implementation.
- *
- * @return the port number on which to listen
- */
- protected int getSocketPort() {
- try {
- return Integer.parseInt(getInitParameter("socketPort"));
- } catch (NumberFormatException e) {
- return DEFAULT_PORT;
- }
- }
- /**
- * Handles a new socket connection. Subclasses must define this method.
- *
- * @param client
- * the client socket
- */
- abstract public void handleClient(Socket client);
- /**
- * Halts the thread listening for socket connections. Subclasses that
- * override this method must be sure to first call <tt>super.destroy()</tt>.
- */
- public void destroy() {
- log("destroy called for servlet...");
- try {
- daemonThread.stop();
- daemonThread = null;
- } catch (Exception e) {
- log("Problem stopping server socket daemon thread: "
- + e.getClass().getName() + ": " + e.getMessage());
- }
- }
- }
- // This work is broken into a helper class so that subclasses of
- // DaemonHttpServlet can define their own run() method without problems.
- class Daemon extends Thread {
- private ServerSocket serverSocket;
- private DaemonHttpServlet servlet;
- public Daemon(DaemonHttpServlet servlet) {
- this.servlet = servlet;
- }
- public void run() {
- try {
- // Create a server socket to accept connections
- servlet.log("Creating server socket...");
- serverSocket = new ServerSocket(servlet.getSocketPort());
- } catch (Exception e) {
- servlet.log("Problem establishing server socket: "
- + e.getClass().getName() + ": " + e.getMessage());
- return;
- }
- try {
- while (true) {
- // As each connection comes in, call the servlet's
- // handleClient().
- // Note this method is blocking. It's the servlet's
- // responsibility
- // to spawn a handler thread for long-running connections.
- try {
- servlet.log("Calling handleClient");
- servlet.handleClient(serverSocket.accept());
- } catch (IOException ioe) {
- servlet.log("Problem accepting client's socket connection: "
- + ioe.getClass().getName()
- + ": "
- + ioe.getMessage());
- }
- }
- } catch (ThreadDeath e) {
- // When the thread is killed, close the server socket
- try {
- serverSocket.close();
- } catch (IOException ioe) {
- servlet.log("Problem closing server socket: "
- + ioe.getClass().getName() + ": " + ioe.getMessage());
- }
- }
- }
- }
- public class ProtoBuffServlet extends DaemonHttpServlet{
- private static final long serialVersionUID = -6951891715383576742L;
- private final Logger logger = LoggerFactory.getLogger(ProtoBuffServlet.class);
- private DataOutputStream mDataOutputStream;
- @Override
- public void handleClient(Socket client) {
- log("handleClient called (servlet.log)...");
- logger.debug("handleClient called.");
- try {
- mDataOutputStream = new DataOutputStream(client.getOutputStream());
- logger.debug("Creating new device connection handler");
- DeviceConnectionRegistry.createDeviceConnectionHandler(client);
- } catch (IOException ioe) {
- log("Exception caught:");
- System.out.print(ioe);
- ioe.printStackTrace();
- } catch (Exception e) {
- log("Exception caught:");
- e.printStackTrace();
- }
- }
- private void sendReply(ControllerToDaemon message) throws IOException {
- ...
- }
- private byte[] receiveMessage(Socket socket) throws IOException {
- ...
- }
- }
- public class DeviceConnectionRegistry {
- public static final Logger logger = LoggerFactory.getLogger(DeviceConnectionRegistry.class);
- public static Map<UUID, DeviceConnectionHandler> DEVICE_CONNECTIONS = new HashMap<UUID, DeviceConnectionHandler>();
- public static List<DeviceConnectionHandler> ANONYMOUS_CONNECTIONS = new ArrayList<DeviceConnectionHandler>();
- public static DeviceConnectionHandler getConnection(Device device){
- return DEVICE_CONNECTIONS.get(device.getUuid());
- }
- public static void createDeviceConnectionHandler(Socket socket){
- DeviceConnectionHandler handler = new DeviceConnectionHandler(socket);
- try {
- handler.open();
- Thread t = new Thread(handler);
- t.run();
- ANONYMOUS_CONNECTIONS.add(handler);
- } catch (IOException ioe) {
- logger.debug("Exception caught:D");
- System.out.print(ioe);
- ioe.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement