/**
* Receives the response from the server. The method is processed in separate thread
*/
public void run() {
try {
ResponseHeader response;
boolean run = true;
while(!this.isInterrupted()) {
String headerBuffer = "";
String line = "";
while( dataIn.available() == 0 ) {
if(System.currentTimeMillis() -
lastTime > keepalive ) {
clientBroker.close();
run = false;
break;
}
Thread.sleep(100);
}
if( !run ) break;
while((line = readLine()).length() > 0) {
headerBuffer += line + ConstData.EMPTY_LINE;
}
response = new ResponseHeader(headerBuffer);
clientBroker.send(response.getBytes());
if(response.getContentLength() > 0) {
readBytes(response.getContentLength(), null);
} else if(response.getTransfEncoding().
equalsIgnoreCase("chunked")) {
int numberBytes = 0;
while((line = readLine()).length() ==0 ) {}
while(!((numberBytes =
Integer.parseInt(line, 16)) == 0)) {
readBytes(numberBytes, line);
while((line = readLine()).length() ==0 ) {}
}
}
String connection = response.getConnection();
if(connection.equalsIgnoreCase("close")) {
clientBroker.close();
} else if(connection.equalsIgnoreCase("keep-alive")) {
this.keepalive = response.getKeepAliveTimeout();
this.lastTime = System.currentTimeMillis();
socket.setKeepAlive(true);
clientBroker.setKeepAlive(true);
}
}