/** * Sends a request which is not answered by the server. This method can be used for time-critical * data */ public void sendRequest(HTTPMessageObject request) { if (request == null) { logger.error("no message to be sent"); return; } try { if (socket == null) { socket = new DatagramSocket(); } SocketHelper.sendBinaryMessage(request.toBinaryMessage(), socket); } catch (Exception e) { } finally { tryClose(); } }
/* * (non-Javadoc) * * @see de.fhg.fokus.magic.http.IHTTPClient#sendRequestAndWaitForResponse(de.fhg.fokus.magic.http.HTTPMessageObject) */ public void sendRequestAndWaitForResponse(HTTPMessageObject request) { if (request == null) { logger.error("no message to be sent"); return; } // cancel request if too many active clients if (activeClients > HTTPDefaults.MAX_ACTIVE_CLIENTS) { return; } activeClients++; int numberOfRetries = 0; boolean success = false; while (!success && !terminateClient && numberOfRetries < HTTPDefaults.MAX_CLIENT_FAILURES) { try { if (socket == null) { socket = new DatagramSocket(); } SocketHelper.sendBinaryMessage(request.toBinaryMessage(), socket); // try to receive response for 2 seconds BinaryMessageObject binaryResponse = SocketHelper.readBinaryMessage(null, socket, 2000); if (binaryResponse != null) { responseMessage = binaryResponse.toHTTPMessageObject(); responseMessage.setDestinationAddress(binaryResponse.getSourceAddress()); responseMessage.setSourceAddress(request.getDestinationAddress()); } success = true; } catch (Exception e) { numberOfRetries++; } } tryClose(); activeClients--; if (debug) { System.out.println("HTTPClient response header:" + responseMessage.getHeader()); System.out.println( "HTTPClient response body:" + StringHelper.byteArrayToUTF8String(responseMessage.getBody())); } }