View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.service.server;
14  
15  import org.abstracthorizon.danube.connection.Connection;
16  import org.abstracthorizon.danube.service.ServiceException;
17  
18  import java.io.IOException;
19  import java.net.InetSocketAddress;
20  import java.net.ServerSocket;
21  import java.net.Socket;
22  import java.util.concurrent.Executor;
23  
24  /**
25   * This is server service that is implemented using sockets. This service accepts connections from a
26   * server socket, creates new {@link ConnectionHandlerThread} objects
27   * and executes them in a given thread by {@link Executor}.
28   *
29   * @author Daniel Sendula
30   */
31  public class MultiThreadServerSocketService extends MultiThreadServerService {
32  
33      /** Initial socket timeout */
34      protected int serverSocketTimeout = 1000;
35  
36      /** New socket's timeout */
37      protected int newSocketTimeout = 60000;
38  
39      /** Reference to the server socket */
40      protected ServerSocket serverSocket;
41  
42      /**
43       * Default constructor
44       */
45      public MultiThreadServerSocketService() {
46      }
47  
48      /**
49       * Returns initial socket timeout
50       * @return initial socket timeout
51       */
52      public int getServerSocketTimeout() {
53          return serverSocketTimeout;
54      }
55  
56      /**
57       * Sets server socket timeout
58       * @param socketTimeout initial socket timeout
59       */
60      public void setServerSocketTimeout(int socketTimeout) {
61          this.serverSocketTimeout = socketTimeout;
62      }
63  
64      /**
65       * Returns new socket timeout
66       * @return new socket timeout
67       */
68      public int getNewSocketTimeout() {
69          return newSocketTimeout;
70      }
71  
72      /**
73       * Sets new socket timeout
74       * @param socketTimeout new socket timeout
75       */
76      public void setNewSocketTimeout(int socketTimeout) {
77          this.newSocketTimeout = socketTimeout;
78      }
79  
80      /**
81       * Creates the socket
82       * @throws ServiceException
83       */
84      public void create() throws ServiceException {
85          super.create();
86          createServerSocket();
87      }
88  
89      /**
90       * Closes the server socket
91       * @throws ServiceException
92       */
93      public void destroy() throws ServiceException {
94          super.destroy();
95          destroyServerSocket();
96      }
97  
98      /**
99       * This method processes connections
100      */
101     protected void processConnections() {
102         try {
103             Socket socket = serverSocket.accept();
104             if (logger.isDebugEnabled()) { logger.debug("Accepted new connection;  " + socket.toString()); }
105 
106             try {
107                 socket.setSoTimeout(getNewSocketTimeout());
108                 Connection serverConnection = createSocketConnection(socket);
109                 ConnectionHandlerThread connectionHandlerThread = new ConnectionHandlerThread(serverConnection);
110                 connectionHandlerThread.start();
111             } catch (Exception e) {
112                 logger.error("Cannot process connection for socket; " + socket, e);
113             }
114         } catch (IOException ignore) {
115         }
116     }
117 
118     /**
119      * Creates server socket
120      * @return server socket
121      * @throws ServiceException
122      */
123     protected void createServerSocket()throws ServiceException {
124         try {
125             int port = getPort();
126             if (port == -1) {
127                 serverSocket = new ServerSocket(0, 0, getSocketAddress().getAddress());
128                 InetSocketAddress socketAddress = (InetSocketAddress)serverSocket.getLocalSocketAddress();
129                 setSocketAddress(socketAddress);
130             } else {
131                 serverSocket = new ServerSocket(getPort(), 0, getSocketAddress().getAddress());
132             }
133             serverSocket.setSoTimeout(getServerSocketTimeout());
134         } catch (IOException e) {
135             throw new ServiceException("Problem creating server socket", e);
136         }
137     }
138 
139     /**
140      * Closes server socket
141      * @throws ServiceException
142      */
143     protected void destroyServerSocket() throws ServiceException {
144         try {
145             serverSocket.close();
146         } catch (IOException e) {
147             throw new ServiceException("Problem closing server socket", e);
148         }
149     }
150 
151     /**
152      * Creates server connection and new instance of {@link ConnectionHandlerThread} to process
153      * socket under the given executor
154      * @param socket socket
155      */
156     protected void processConnection(Socket socket) {
157         if (logger.isDebugEnabled()) { logger.debug("Accepted new connection;  " + socket.toString()); }
158 
159         try {
160             Connection serverConnection = createSocketConnection(socket);
161             ConnectionHandlerThread connectionHandlerThread = new ConnectionHandlerThread(serverConnection);
162             connectionHandlerThread.start();
163         } catch (Exception e) {
164             logger.error("Cannot process connection for socket; " + socket, e);
165         }
166     }
167 
168     /**
169      * Creates new socket connection
170      * @param socket socket
171      * @return socket connection
172      * @throws IOException
173      * @throws Exception
174      */
175     protected Connection createSocketConnection(Socket socket) throws IOException {
176         Connection serverConnection = new SocketConnection(socket);
177         return serverConnection;
178     }
179 }