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.nio.channels.ServerSocketChannel;
21  import java.nio.channels.SocketChannel;
22  import java.util.concurrent.Executor;
23  
24  /**
25   * This is server service that is implemented using socket channels.
26   * This service works in blocked IO mode and accepts connections from a
27   * server socket channel, creates new {@link ConnectionHandlerThread} objects
28   * and executes them in a given thread by {@link Executor}.
29   *
30   * @author Daniel Sendula
31   */
32  public class MultiThreadServerSocketChannelService extends MultiThreadServerService {
33  
34      /** Initial socket timeout */
35      protected int serverSocketTimeout = 1000;
36  
37      // TODO implement this!!!
38      /** New socket timeout */
39      protected int newSocketTimeout = 60000;
40  
41      /** Reference to the server socket */
42      protected ServerSocketChannel serverSocketChannel;
43  
44      /**
45       * Default constructor
46       */
47      public MultiThreadServerSocketChannelService() {
48      }
49  
50      /**
51       * Returns server socket timeout
52       * @return server socket timeout
53       */
54      public int getServerSocketTimeout() {
55          return serverSocketTimeout;
56      }
57  
58      /**
59       * Sets initial socket timeout
60       * @param socketTimeout initial socket timeout
61       */
62      public void setServerSocketTimeout(int socketTimeout) {
63          this.serverSocketTimeout = socketTimeout;
64      }
65  
66      /**
67       * Returns new socket timeout
68       * @return new socket timeout
69       */
70      public int getNewSocketTimeout() {
71          return newSocketTimeout;
72      }
73  
74      /**
75       * Sets new socket timeout
76       * @param socketTimeout new socket timeout
77       */
78      public void setNewSocketTimeout(int socketTimeout) {
79          this.newSocketTimeout = socketTimeout;
80      }
81  
82      /**
83       * Creates the socket
84       * @throws ServiceException
85       */
86      public void create() throws ServiceException {
87          super.create();
88          createServerSocket();
89      }
90  
91      /**
92       * Closes the server socket
93       * @throws ServiceException
94       */
95      public void destroy() throws ServiceException {
96          super.destroy();
97          destroyServerSocket();
98      }
99  
100     /**
101      * This method processes connections
102      */
103     protected void processConnections() {
104         try {
105             SocketChannel socketChannel = serverSocketChannel.accept();
106             if (logger.isDebugEnabled()) { logger.debug("Accepted new connection;  " + socketChannel.socket()); }
107 
108             try {
109                 Connection serverConnection = createSocketConnection(socketChannel);
110                 ConnectionHandlerThread connectionHandlerThread = new ConnectionHandlerThread(serverConnection);
111                 connectionHandlerThread.start();
112             } catch (Exception e) {
113                 logger.error("Cannot process connection for socket; " + socketChannel.socket(), e);
114             }
115         } catch (IOException ignore) {
116         }
117     }
118 
119     /**
120      * Creates server socket
121      * @return server socket
122      * @throws ServiceException
123      */
124     protected void createServerSocket()throws ServiceException {
125         try {
126             serverSocketChannel = ServerSocketChannel.open();
127             serverSocketChannel.configureBlocking(true);
128             InetSocketAddress socketAddress = getSocketAddress();
129             serverSocketChannel.socket().bind(socketAddress);
130             serverSocketChannel.socket().setSoTimeout(getServerSocketTimeout());
131         } catch (IOException e) {
132             throw new ServiceException("Problem creating server socket", e);
133         }
134     }
135 
136     /**
137      * Closes server socket
138      * @throws ServiceException
139      */
140     protected void destroyServerSocket() throws ServiceException {
141         try {
142             serverSocketChannel.close();
143         } catch (IOException e) {
144             throw new ServiceException("Problem closing server socket", e);
145         }
146     }
147 
148     /**
149      * Creates socket connection and new instance of {@link ConnectionHandlerThread} to process
150      * socket under the given executor
151      * @param socketChannel socket channel
152      */
153     protected void processConnection(SocketChannel socketChannel) {
154         if (logger.isDebugEnabled()) { logger.debug("Accepted new connection;  " + socketChannel.socket()); }
155 
156         try {
157             Connection socketConnection = createSocketConnection(socketChannel);
158             ConnectionHandlerThread connectionHandlerThread = new ConnectionHandlerThread(socketConnection);
159             connectionHandlerThread.start();
160         } catch (Exception e) {
161             logger.error("Cannot process connection for socket; " + socketChannel.socket(), e);
162         }
163     }
164 
165     /**
166      * Creates new socket connection
167      * @param socketChannel socket channel
168      * @return server connection
169      * @throws IOException
170      * @throws Exception
171      */
172     protected Connection createSocketConnection(SocketChannel socketChannel) throws IOException {
173         Connection serverConnection = new SocketChannelConnection(socketChannel);
174         return serverConnection;
175     }
176 }