1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package ca.uhn.hl7v2.app;
28
29 import java.io.File;
30 import java.util.concurrent.BlockingQueue;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.LinkedBlockingQueue;
33 import java.util.concurrent.TimeUnit;
34
35 import ca.uhn.hl7v2.util.StandardSocketFactory;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import ca.uhn.hl7v2.DefaultHapiContext;
40 import ca.uhn.hl7v2.HapiContext;
41 import ca.uhn.hl7v2.app.AcceptorThread.AcceptedSocket;
42 import ca.uhn.hl7v2.concurrent.DefaultExecutorService;
43 import ca.uhn.hl7v2.llp.LowerLayerProtocol;
44 import ca.uhn.hl7v2.llp.MinLowerLayerProtocol;
45 import ca.uhn.hl7v2.parser.Parser;
46 import ca.uhn.hl7v2.parser.PipeParser;
47 import ca.uhn.hl7v2.util.SocketFactory;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class SimpleServer extends HL7Service {
76
77
78
79
80 public static final int SO_TIMEOUT = StandardSocketFactory.DEFAULT_ACCEPTED_SOCKET_TIMEOUT;
81
82 private static final Logger log = LoggerFactory.getLogger(SimpleServer.class);
83
84 private final int port;
85 private final boolean tls;
86 private final BlockingQueue<AcceptedSocket> queue;
87 private AcceptorThread acceptor;
88 private final HapiContext hapiContext;
89 private boolean acceptAllMsg = false;
90
91
92
93
94
95 public SimpleServer(int port) {
96 this(port, new MinLowerLayerProtocol(), new PipeParser(), false);
97 }
98
99
100
101
102
103 public SimpleServer(int port, boolean tls) {
104 this(port, new MinLowerLayerProtocol(), new PipeParser(), tls);
105 }
106
107
108
109
110 public SimpleServer(int port, LowerLayerProtocol llp, Parser parser) {
111 this(port, llp, parser, false);
112 }
113
114
115
116
117 public SimpleServer(int port, LowerLayerProtocol llp, Parser parser, boolean tls) {
118 this(port, llp, parser, tls, DefaultExecutorService.getDefaultService());
119 }
120
121
122
123
124
125
126 public SimpleServer(int port, LowerLayerProtocol llp, Parser parser, boolean tls,
127 ExecutorService executorService) {
128 super(parser, llp, executorService);
129 this.port = port;
130 this.tls = tls;
131 this.hapiContext = new DefaultHapiContext();
132 this.queue = new LinkedBlockingQueue<>(100);
133 }
134
135
136
137
138
139
140
141
142
143 public SimpleServer(HapiContext hapiContext, int port, boolean tls) {
144 super(hapiContext);
145 this.hapiContext = hapiContext;
146 this.port = port;
147 this.tls = tls;
148 this.queue = new LinkedBlockingQueue<>(100);
149 }
150
151
152
153
154
155
156
157
158
159
160
161 public SimpleServer(HapiContext hapiContext, int port, boolean tls, boolean acceptAll) {
162 this(hapiContext, port, tls);
163 acceptAllMsg = acceptAll;
164 }
165
166
167
168
169
170
171 @Override
172 protected void afterStartup() {
173 try {
174 super.afterStartup();
175 log.info("Starting SimpleServer running on port {}", port);
176 SocketFactory ss = this.hapiContext.getSocketFactory();
177 acceptor = new AcceptorThread(port, tls, getExecutorService(), queue, ss);
178 acceptor.start();
179 } catch (Exception e) {
180 log.error("Failed starting SimpleServer on port {}", port);
181 throw new RuntimeException(e);
182 }
183 }
184
185
186
187
188
189 @Override
190 protected void handle() {
191 if (acceptor.getServiceExitedWithException() != null) {
192 setServiceExitedWithException(acceptor.getServiceExitedWithException());
193 }
194
195 try {
196
197 AcceptedSocket newSocket = queue.poll(500, TimeUnit.MILLISECONDS);
198 if (newSocket != null) {
199 log.info("Accepted connection from {}:{} on local port {}",
200 newSocket.socket.getInetAddress().getHostAddress(), newSocket.socket.getPort(), port);
201 ActiveConnection c = new ActiveConnection(getParser(), getLlp(), newSocket.socket,
202 getExecutorService(), acceptAllMsg);
203 newConnection(c);
204 }
205 } catch (InterruptedException ie) {
206
207 } catch (Exception e) {
208 log.error("Error while accepting connections: ", e);
209 }
210 }
211
212
213
214
215 @Override
216 protected void afterTermination() {
217 super.afterTermination();
218
219
220
221 acceptor.stopAndWait();
222 }
223
224
225
226
227
228
229
230
231 public static void main(String[] args) {
232 if (args.length < 1 || args.length > 2) {
233 System.out
234 .println("Usage: ca.uhn.hl7v2.app.SimpleServer port_num [application_spec_file_name]");
235 System.exit(1);
236 }
237
238 int port = 0;
239 try {
240 port = Integer.parseInt(args[0]);
241 } catch (NumberFormatException e) {
242 System.err.println("The given port (" + args[0]
243 + ") is not an integer.");
244 System.exit(1);
245 }
246
247 File appFile = null;
248 if (args.length == 2) {
249 appFile = new File(args[1]);
250 }
251
252 try {
253 SimpleServer server = new SimpleServer(port);
254 if (appFile != null)
255 server.loadApplicationsFromFile(appFile);
256 server.start();
257 } catch (Exception e) {
258 e.printStackTrace();
259 }
260
261 }
262
263 }