Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. public abstract class DaemonHttpServlet extends HttpServlet {
  2.  
  3. private static final long serialVersionUID = 5915272369604402570L;
  4.  
  5.  
  6. /**
  7. * The default listening port (8888)
  8. */
  9. protected int DEFAULT_PORT = 8888;
  10. private Thread daemonThread;
  11.  
  12. /**
  13. * Begins a thread listening for socket connections. Subclasses that
  14. * override this method must be sure to first call
  15. * <tt>super.init(config)</tt>.
  16. *
  17. * @param config
  18. * the servlet config
  19. * @exception ServletException
  20. * if a servlet exception occurs
  21. */
  22. public void init(ServletConfig config) throws ServletException {
  23. super.init(config);
  24.  
  25. try {
  26. log("Entering init...");
  27. daemonThread = new Daemon(this);
  28. daemonThread.start();
  29. } catch (Exception e) {
  30. log("Problem starting socket server daemon thread"
  31. + e.getClass().getName() + ": " + e.getMessage());
  32. }
  33. }
  34.  
  35. /**
  36. * Returns the socket port on which the servlet will listen. A servlet can
  37. * change the port in three ways: by using the <tt>socketPort</tt> init
  38. * parameter, by setting the <tt>DEFAULT_PORT</tt> variable before calling
  39. * <tt>super.init()</tt>, or by overriding this method's implementation.
  40. *
  41. * @return the port number on which to listen
  42. */
  43. protected int getSocketPort() {
  44. try {
  45. return Integer.parseInt(getInitParameter("socketPort"));
  46. } catch (NumberFormatException e) {
  47. return DEFAULT_PORT;
  48. }
  49. }
  50.  
  51. /**
  52. * Handles a new socket connection. Subclasses must define this method.
  53. *
  54. * @param client
  55. * the client socket
  56. */
  57. abstract public void handleClient(Socket client);
  58.  
  59. /**
  60. * Halts the thread listening for socket connections. Subclasses that
  61. * override this method must be sure to first call <tt>super.destroy()</tt>.
  62. */
  63. public void destroy() {
  64. log("destroy called for servlet...");
  65. try {
  66. daemonThread.stop();
  67. daemonThread = null;
  68. } catch (Exception e) {
  69. log("Problem stopping server socket daemon thread: "
  70. + e.getClass().getName() + ": " + e.getMessage());
  71. }
  72. }
  73. }
  74.  
  75. // This work is broken into a helper class so that subclasses of
  76. // DaemonHttpServlet can define their own run() method without problems.
  77. class Daemon extends Thread {
  78.  
  79. private ServerSocket serverSocket;
  80. private DaemonHttpServlet servlet;
  81.  
  82. public Daemon(DaemonHttpServlet servlet) {
  83. this.servlet = servlet;
  84. }
  85.  
  86. public void run() {
  87. try {
  88. // Create a server socket to accept connections
  89. servlet.log("Creating server socket...");
  90. serverSocket = new ServerSocket(servlet.getSocketPort());
  91. } catch (Exception e) {
  92. servlet.log("Problem establishing server socket: "
  93. + e.getClass().getName() + ": " + e.getMessage());
  94. return;
  95. }
  96.  
  97. try {
  98. while (true) {
  99. // As each connection comes in, call the servlet's
  100. // handleClient().
  101. // Note this method is blocking. It's the servlet's
  102. // responsibility
  103. // to spawn a handler thread for long-running connections.
  104. try {
  105. servlet.log("Calling handleClient");
  106. servlet.handleClient(serverSocket.accept());
  107. } catch (IOException ioe) {
  108. servlet.log("Problem accepting client's socket connection: "
  109. + ioe.getClass().getName()
  110. + ": "
  111. + ioe.getMessage());
  112. }
  113. }
  114. } catch (ThreadDeath e) {
  115. // When the thread is killed, close the server socket
  116. try {
  117. serverSocket.close();
  118. } catch (IOException ioe) {
  119. servlet.log("Problem closing server socket: "
  120. + ioe.getClass().getName() + ": " + ioe.getMessage());
  121. }
  122. }
  123. }
  124. }
  125.  
  126. public class ProtoBuffServlet extends DaemonHttpServlet{
  127.  
  128. private static final long serialVersionUID = -6951891715383576742L;
  129. private final Logger logger = LoggerFactory.getLogger(ProtoBuffServlet.class);
  130.  
  131. private DataOutputStream mDataOutputStream;
  132.  
  133. @Override
  134. public void handleClient(Socket client) {
  135. log("handleClient called (servlet.log)...");
  136. logger.debug("handleClient called.");
  137. try {
  138. mDataOutputStream = new DataOutputStream(client.getOutputStream());
  139. logger.debug("Creating new device connection handler");
  140. DeviceConnectionRegistry.createDeviceConnectionHandler(client);
  141. } catch (IOException ioe) {
  142. log("Exception caught:");
  143. System.out.print(ioe);
  144. ioe.printStackTrace();
  145. } catch (Exception e) {
  146. log("Exception caught:");
  147. e.printStackTrace();
  148. }
  149. }
  150.  
  151. private void sendReply(ControllerToDaemon message) throws IOException {
  152. ...
  153. }
  154.  
  155. private byte[] receiveMessage(Socket socket) throws IOException {
  156. ...
  157. }
  158.  
  159. }
  160.  
  161. public class DeviceConnectionRegistry {
  162.  
  163. public static final Logger logger = LoggerFactory.getLogger(DeviceConnectionRegistry.class);
  164.  
  165. public static Map<UUID, DeviceConnectionHandler> DEVICE_CONNECTIONS = new HashMap<UUID, DeviceConnectionHandler>();
  166.  
  167. public static List<DeviceConnectionHandler> ANONYMOUS_CONNECTIONS = new ArrayList<DeviceConnectionHandler>();
  168.  
  169. public static DeviceConnectionHandler getConnection(Device device){
  170. return DEVICE_CONNECTIONS.get(device.getUuid());
  171. }
  172.  
  173. public static void createDeviceConnectionHandler(Socket socket){
  174. DeviceConnectionHandler handler = new DeviceConnectionHandler(socket);
  175. try {
  176. handler.open();
  177. Thread t = new Thread(handler);
  178. t.run();
  179. ANONYMOUS_CONNECTIONS.add(handler);
  180. } catch (IOException ioe) {
  181. logger.debug("Exception caught:D");
  182. System.out.print(ioe);
  183. ioe.printStackTrace();
  184. }
  185. }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement