package de.dwerth.gntpserver.gntp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import de.dwerth.gntpserver.gntp.messages.GNTPMessage;
import de.dwerth.gntpserver.helpers.MessageHelper;
public class InputThread extends Thread {
private Socket socket;
public InputThread(Socket s) {
socket = s;
}
@Override
public void run() {
OutputStream os = null;
InputStream is = null;
try {
os = socket.getOutputStream();
is = socket.getInputStream();
byte[] rawData = MessageHelper.readInput(is);
GNTPMessage message = MessageFactory.parseRawData(rawData);
try {
while (MessageHelper.processMessage(message, is, os)) {
rawData = MessageHelper.readInput(is);
message = MessageFactory.parseRawData(rawData);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("closing connection");
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}