1
2
3
4
5
6
7
8
9
10
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
26
27
28
29
30
31
32 public class MultiThreadServerSocketChannelService extends MultiThreadServerService {
33
34
35 protected int serverSocketTimeout = 1000;
36
37
38
39 protected int newSocketTimeout = 60000;
40
41
42 protected ServerSocketChannel serverSocketChannel;
43
44
45
46
47 public MultiThreadServerSocketChannelService() {
48 }
49
50
51
52
53
54 public int getServerSocketTimeout() {
55 return serverSocketTimeout;
56 }
57
58
59
60
61
62 public void setServerSocketTimeout(int socketTimeout) {
63 this.serverSocketTimeout = socketTimeout;
64 }
65
66
67
68
69
70 public int getNewSocketTimeout() {
71 return newSocketTimeout;
72 }
73
74
75
76
77
78 public void setNewSocketTimeout(int socketTimeout) {
79 this.newSocketTimeout = socketTimeout;
80 }
81
82
83
84
85
86 public void create() throws ServiceException {
87 super.create();
88 createServerSocket();
89 }
90
91
92
93
94
95 public void destroy() throws ServiceException {
96 super.destroy();
97 destroyServerSocket();
98 }
99
100
101
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
121
122
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
138
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
150
151
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
167
168
169
170
171
172 protected Connection createSocketConnection(SocketChannel socketChannel) throws IOException {
173 Connection serverConnection = new SocketChannelConnection(socketChannel);
174 return serverConnection;
175 }
176 }