Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Nov 6, 2023
1 parent 02cdb4e commit 3d3db3c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
15 changes: 12 additions & 3 deletions src/main/java/de/malkusch/km200/KM200.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import de.malkusch.km200.http.Http;
import de.malkusch.km200.http.JdkHttp;
import de.malkusch.km200.http.RetryHttp;
import de.malkusch.km200.http.SerializedHttp;

/**
* This is an API for Bosch/Buderus/Junkers heaters with a KM200 gateway.
Expand Down Expand Up @@ -126,9 +127,17 @@ public KM200(String uri, int retries, Duration timeout, String gatewayPassword,
this.comm = new KM200Comm();

{
var jdkHttp = new JdkHttp(uri.replaceAll("/*$", ""), USER_AGENT, timeout);
queryHttp = new RetryHttp(jdkHttp, retries, IOException.class, ServerError.class);
updateHttp = new RetryHttp(jdkHttp, retries, ServerError.class);
Http http = new JdkHttp(uri.replaceAll("/*$", ""), USER_AGENT, timeout);

/*
* The KM200 itself is not thread safe. This proxy serializes all
* requests to protect users from a wrong concurrent usage of this
* API.
*/
http = new SerializedHttp(http);

queryHttp = new RetryHttp(http, retries, IOException.class, ServerError.class);
updateHttp = new RetryHttp(http, retries, ServerError.class);
}

query("/system");
Expand Down
34 changes: 13 additions & 21 deletions src/main/java/de/malkusch/km200/http/JdkHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,22 @@ public Response post(String path, byte[] body) throws IOException, InterruptedEx
return send(request);
}

private final Object serializedSendLock = new Object();

private Response send(HttpRequest request) throws IOException, InterruptedException, KM200Exception {
/*
* The KM200 itself is not thread safe. This lock serializes all
* requests to protect users from a wrong concurrent usage of this API.
*/
synchronized (serializedSendLock) {
var response = client.send(request, BodyHandlers.ofByteArray());
var status = response.statusCode();
var response = client.send(request, BodyHandlers.ofByteArray());
var status = response.statusCode();

if (status >= 200 && status <= 299) {
return new Response(status, response.body());
if (status >= 200 && status <= 299) {
return new Response(status, response.body());

} else {
throw switch (status) {
case 400 -> new KM200Exception.BadRequest("Bad request to " + request.uri());
case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden");
case 404 -> new KM200Exception.NotFound(request.uri() + " was not found");
case 423 -> new KM200Exception.Locked(request.uri() + " was locked");
case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error");
default -> new KM200Exception(request.uri() + " failed with response code " + status);
};
}
} else {
throw switch (status) {
case 400 -> new KM200Exception.BadRequest("Bad request to " + request.uri());
case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden");
case 404 -> new KM200Exception.NotFound(request.uri() + " was not found");
case 423 -> new KM200Exception.Locked(request.uri() + " was locked");
case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error");
default -> new KM200Exception(request.uri() + " failed with response code " + status);
};
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/de/malkusch/km200/http/SerializedHttp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.malkusch.km200.http;

import java.io.IOException;

import de.malkusch.km200.KM200Exception;

public final class SerializedHttp implements Http {

private final Http http;

public SerializedHttp(Http http) {
this.http = http;
}

private final Object lock = new Object();

@Override
public Response get(String path) throws IOException, InterruptedException, KM200Exception {
synchronized (lock) {
return http.get(path);
}
}

@Override
public Response post(String path, byte[] body) throws IOException, InterruptedException, KM200Exception {
synchronized (lock) {
return http.post(path, body);
}
}
}

0 comments on commit 3d3db3c

Please sign in to comment.