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.net.ServerSocket;
21 import java.net.Socket;
22 import java.util.concurrent.Executor;
23
24
25
26
27
28
29
30
31 public class MultiThreadServerSocketService extends MultiThreadServerService {
32
33
34 protected int serverSocketTimeout = 1000;
35
36
37 protected int newSocketTimeout = 60000;
38
39
40 protected ServerSocket serverSocket;
41
42
43
44
45 public MultiThreadServerSocketService() {
46 }
47
48
49
50
51
52 public int getServerSocketTimeout() {
53 return serverSocketTimeout;
54 }
55
56
57
58
59
60 public void setServerSocketTimeout(int socketTimeout) {
61 this.serverSocketTimeout = socketTimeout;
62 }
63
64
65
66
67
68 public int getNewSocketTimeout() {
69 return newSocketTimeout;
70 }
71
72
73
74
75
76 public void setNewSocketTimeout(int socketTimeout) {
77 this.newSocketTimeout = socketTimeout;
78 }
79
80
81
82
83
84 public void create() throws ServiceException {
85 super.create();
86 createServerSocket();
87 }
88
89
90
91
92
93 public void destroy() throws ServiceException {
94 super.destroy();
95 destroyServerSocket();
96 }
97
98
99
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
120
121
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
141
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
153
154
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
170
171
172
173
174
175 protected Connection createSocketConnection(Socket socket) throws IOException {
176 Connection serverConnection = new SocketConnection(socket);
177 return serverConnection;
178 }
179 }