Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[homematic] Solves stability issues with HmIP devices #7902

Merged
merged 3 commits into from
Jun 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
Expand All @@ -32,6 +37,7 @@
import org.openhab.binding.homematic.internal.communicator.parser.RpcResponseParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

/**
* Client implementation for sending messages via XML-RPC to the Homematic server.
Expand Down Expand Up @@ -69,7 +75,7 @@ protected synchronized Object[] sendMessage(int port, RpcRequest<String> request
if (logger.isTraceEnabled()) {
logger.trace("Client XmlRpcRequest (port {}):\n{}", port, request);
}
Exception reason = null;
IOException reason = new IOException();
for (int rpcRetryCounter = 1; rpcRetryCounter <= MAX_RPC_RETRY; rpcRetryCounter++) {
try {
byte[] response = send(port, request);
Expand All @@ -78,57 +84,64 @@ protected synchronized Object[] sendMessage(int port, RpcRequest<String> request
return new RpcResponseParser(request).parse(data);
} catch (UnknownRpcFailureException | UnknownParameterSetException ex) {
throw ex;
} catch (Exception ex) {
} catch (SAXException | ParserConfigurationException ex) {
throw new IOException(ex);
} catch (IOException ex) {
reason = ex;
if ("init".equals(request.getMethodName())) { // no retries for "init" request
break;
}
logger.debug("XmlRpcMessage failure, sending message again {}/{}", rpcRetryCounter, MAX_RPC_RETRY);
logger.debug("XmlRpcMessage failed, sending message again {}/{}", rpcRetryCounter, MAX_RPC_RETRY);
}
}
throw new IOException(reason.getMessage(), reason); // can't be null here because logic in for loop
throw reason;
}

private byte[] send(int port, RpcRequest<String> request) throws Exception {
private byte[] send(int port, RpcRequest<String> request) throws IOException {
byte[] ret = new byte[0];
BytesContentProvider content = new BytesContentProvider(request.createMessage().getBytes(config.getEncoding()));
String url = String.format("http://%s:%s", config.getGatewayAddress(), port);
if (port == config.getGroupPort()) {
url += "/groups";
}
Request req = httpClient.POST(url).content(content).timeout(config.getTimeout(), TimeUnit.SECONDS)
.header(HttpHeader.CONTENT_TYPE, "text/xml;charset=" + config.getEncoding());
try {
ret = req.send().getContent();
} catch (IllegalArgumentException e) { // Returned buffer too large
logger.info("Blocking XmlRpcRequest failed: {}, trying non-blocking request", e.getMessage());
InputStreamResponseListener respListener = new InputStreamResponseListener();
req.send(respListener);
Response resp = respListener.get(config.getTimeout(), TimeUnit.SECONDS);
ByteArrayOutputStream respData = new ByteArrayOutputStream(RESP_BUFFER_SIZE);
BytesContentProvider content = new BytesContentProvider(
request.createMessage().getBytes(config.getEncoding()));
String url = String.format("http://%s:%s", config.getGatewayAddress(), port);
if (port == config.getGroupPort()) {
url += "/groups";
}
Request req = httpClient.POST(url).content(content).timeout(config.getTimeout(), TimeUnit.SECONDS)
.header(HttpHeader.CONTENT_TYPE, "text/xml;charset=" + config.getEncoding());
try {
ret = req.send().getContent();
} catch (IllegalArgumentException e) { // Returned buffer too large
logger.info("Blocking XmlRpcRequest failed: {}, trying non-blocking request", e.getMessage());
InputStreamResponseListener respListener = new InputStreamResponseListener();
req.send(respListener);
Response resp = respListener.get(config.getTimeout(), TimeUnit.SECONDS);
ByteArrayOutputStream respData = new ByteArrayOutputStream(RESP_BUFFER_SIZE);

int httpStatus = resp.getStatus();
if (httpStatus == HttpStatus.OK_200) {
byte[] recvBuffer = new byte[RESP_BUFFER_SIZE];
try (InputStream input = respListener.getInputStream()) {
while (true) {
int read = input.read(recvBuffer);
if (read == -1) {
break;
int httpStatus = resp.getStatus();
if (httpStatus == HttpStatus.OK_200) {
byte[] recvBuffer = new byte[RESP_BUFFER_SIZE];
try (InputStream input = respListener.getInputStream()) {
while (true) {
int read = input.read(recvBuffer);
if (read == -1) {
break;
}
respData.write(recvBuffer, 0, read);
}
respData.write(recvBuffer, 0, read);
ret = respData.toByteArray();
}
ret = respData.toByteArray();
} else {
logger.warn("Non-blocking XmlRpcRequest failed, status code: {} / request: {}", httpStatus,
request);
resp.abort(new Exception());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resp.abort(new Exception());
resp.abort(new IOException());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
} else {
logger.warn("Non-blocking XmlRpcRequest failed, status code: {} / request: {}", httpStatus, request);
resp.abort(new Exception());
}
}

if (logger.isTraceEnabled()) {
String result = new String(ret, config.getEncoding());
logger.trace("Client XmlRpcResponse (port {}):\n{}", port, result);
if (logger.isTraceEnabled()) {
String result = new String(ret, config.getEncoding());
logger.trace("Client XmlRpcResponse (port {}):\n{}", port, result);
}
} catch (UnsupportedEncodingException | ExecutionException | TimeoutException | InterruptedException e) {
throw new IOException(e);
}
return ret;
}
Expand Down