forked from SaravananSubramanian/hl7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLLPBasedHL7ThreadedServer.java
208 lines (169 loc) · 6.88 KB
/
MLLPBasedHL7ThreadedServer.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.saravanansubramanian;
import java.io.*;
import java.net.*;
import java.util.*;
public class MLLPBasedHL7ThreadedServer {
private int maxConnections;
private int listenPort;
public MLLPBasedHL7ThreadedServer(int aListenPort, int maxConnections) {
listenPort = aListenPort;
this.maxConnections = maxConnections;
}
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listenPort, 5);
Socket clientSocket = null;
while (true) {
clientSocket = server.accept();
handleConnection(clientSocket);
}
} catch (BindException e) {
System.out.println("Unable to bind to port " + listenPort);
} catch (IOException e) {
System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
}
}
protected void handleConnection(Socket connectionToHandle) {
ConnectionHandler.processRequest(connectionToHandle);
}
public static void main(String[] args) {
MLLPBasedHL7ThreadedServer server = new MLLPBasedHL7ThreadedServer(1080, 3);
server.setUpConnectionHandlers();
server.acceptConnections();
}
public void setUpConnectionHandlers() {
for (int i = 0; i < maxConnections; i++) {
ConnectionHandler currentHandler =
new ConnectionHandler();
Thread handlerThread = new Thread(currentHandler, "Handler " + i);
handlerThread.setDaemon(true);
handlerThread.start();
}
}
private static class ConnectionHandler implements Runnable {
private static final int MESSAGE_CONTROL_ID_LOCATION = 9;
private static final String FIELD_DELIMITER = "|";
private Socket connection;
private static List pool = new LinkedList();
static final char END_OF_BLOCK = '\u001c';
static final char START_OF_BLOCK = '\u000b';
static final char CARRIAGE_RETURN = 13;
private static final int END_OF_TRANSMISSION = -1;
public ConnectionHandler() {
}
public void handleConnection() {
try {
System.out.println("Handling client at "
+ connection.getInetAddress().getHostAddress()
+ " on port " + connection.getPort());
InputStream in = connection.getInputStream();
OutputStream out = connection.getOutputStream();
String parsedHL7Message = getMessage(in);
String buildAcknowledgmentMessage = getSimpleAcknowledgementMessage(parsedHL7Message);
out.write(buildAcknowledgmentMessage.getBytes(), 0, buildAcknowledgmentMessage.length());
} catch (IOException e) {
String errorMessage = "Error whiling reading and writing to connection " + e.getMessage();
System.out.println(errorMessage);
throw new RuntimeException(errorMessage);
}
finally {
try {
connection.close();
}
catch (IOException e) {
String errorMessage = "Error whiling attempting to close to connection " + e.getMessage();
System.out.println(errorMessage);
throw new RuntimeException(errorMessage);
}
}
}
public static void processRequest(Socket requestToHandle) {
synchronized (pool) {
pool.add(pool.size(), requestToHandle);
pool.notifyAll();
}
}
public void run() {
while (true) {
synchronized (pool) {
while (pool.isEmpty()) {
try {
pool.wait();
} catch (InterruptedException e) {
return;
}
}
connection = (Socket) pool.remove(0);
}
handleConnection();
}
}
public String getMessage(InputStream anInputStream) throws IOException {
boolean end_of_message = false;
StringBuffer parsedMessage = new StringBuffer();
int characterReceived = 0;
try {
characterReceived = anInputStream.read();
} catch (SocketException e) {
System.out
.println("Unable to read from socket stream. "
+ "Connection may have been closed: " + e.getMessage());
return null;
}
if (characterReceived == END_OF_TRANSMISSION) {
return null;
}
if (characterReceived != START_OF_BLOCK) {
throw new RuntimeException(
"Start of block character has not been received");
}
while (!end_of_message) {
characterReceived = anInputStream.read();
if (characterReceived == END_OF_TRANSMISSION) {
throw new RuntimeException(
"Message terminated without end of message character");
}
if (characterReceived == END_OF_BLOCK) {
characterReceived = anInputStream.read();
if (characterReceived != CARRIAGE_RETURN) {
throw new RuntimeException(
"End of message character must be followed by a carriage return character");
}
end_of_message = true;
} else {
parsedMessage.append((char) characterReceived);
}
}
return parsedMessage.toString();
}
private String getSimpleAcknowledgementMessage(String aParsedHL7Message) {
if (aParsedHL7Message == null)
throw new RuntimeException("Invalid HL7 message for parsing operation" +
". Please check your inputs");
String messageControlID = getMessageControlID(aParsedHL7Message);
StringBuffer ackMessage = new StringBuffer();
ackMessage = ackMessage.append(START_OF_BLOCK)
.append("MSH|^~\\&|||||||ACK||P|2.2")
.append(CARRIAGE_RETURN)
.append("MSA|AA|")
.append(messageControlID)
.append(CARRIAGE_RETURN)
.append(END_OF_BLOCK)
.append(CARRIAGE_RETURN);
return ackMessage.toString();
}
private String getMessageControlID(String aParsedHL7Message) {
int fieldCount = 0;
StringTokenizer tokenizer = new StringTokenizer(aParsedHL7Message, FIELD_DELIMITER);
while (tokenizer.hasMoreElements())
{
String token = tokenizer.nextToken();
fieldCount++;
if (fieldCount == MESSAGE_CONTROL_ID_LOCATION){
return token;
}
}
return "";
}
}
}