Skip to content

batch notifications when possible #66

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

Merged
merged 1 commit into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,15 @@
package com.beowulfe.hap.impl.connections;

import com.beowulfe.hap.characteristics.EventableCharacteristic;

public class PendingNotification {
public int aid;
public int iid;
public EventableCharacteristic characteristic;

public PendingNotification(int aid, int iid, EventableCharacteristic characteristic) {
this.aid = aid;
this.iid = iid;
this.characteristic = characteristic;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.beowulfe.hap.impl.http.HomekitClientConnection;
import com.beowulfe.hap.impl.http.HttpResponse;
import com.beowulfe.hap.impl.json.EventController;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
Expand All @@ -20,6 +21,9 @@ public class SubscriptionManager {
new ConcurrentHashMap<>();
private final ConcurrentMap<HomekitClientConnection, Set<EventableCharacteristic>> reverse =
new ConcurrentHashMap<>();
private final ConcurrentMap<HomekitClientConnection, ArrayList<PendingNotification>>
pendingNotifications = new ConcurrentHashMap<>();
private int nestedBatches = 0;

public synchronized void addSubscription(
int aid,
Expand Down Expand Up @@ -72,6 +76,7 @@ public synchronized void removeSubscription(

public synchronized void removeConnection(HomekitClientConnection connection) {
Set<EventableCharacteristic> characteristics = reverse.remove(connection);
pendingNotifications.remove(connection);
if (characteristics != null) {
for (EventableCharacteristic characteristic : characteristics) {
Set<HomekitClientConnection> characteristicSubscriptions =
Expand All @@ -91,10 +96,42 @@ private <T> Set<T> newSet() {
return Collections.newSetFromMap(new ConcurrentHashMap<T, Boolean>());
}

public void publish(int accessoryId, int iid, EventableCharacteristic changed) {
public synchronized void batchUpdate() {
++this.nestedBatches;
}

public synchronized void completeUpdateBatch() {
if (--this.nestedBatches == 0 && !pendingNotifications.isEmpty()) {
LOGGER.info("Publishing batched changes");
for (ConcurrentMap.Entry<HomekitClientConnection, ArrayList<PendingNotification>> entry :
pendingNotifications.entrySet()) {
try {
HttpResponse message = new EventController().getMessage(entry.getValue());
entry.getKey().outOfBand(message);
} catch (Exception e) {
LOGGER.error("Faled to create new event message", e);
}
}
pendingNotifications.clear();
}
}

public synchronized void publish(int accessoryId, int iid, EventableCharacteristic changed) {
if (nestedBatches != 0) {
LOGGER.info("Batching change for " + accessoryId);
PendingNotification notification = new PendingNotification(accessoryId, iid, changed);
for (HomekitClientConnection connection : subscriptions.get(changed)) {
if (!pendingNotifications.containsKey(connection)) {
pendingNotifications.put(connection, new ArrayList<PendingNotification>());
}
pendingNotifications.get(connection).add(notification);
}
return;
}

try {
HttpResponse message = new EventController().getMessage(accessoryId, iid, changed);
LOGGER.info("Publishing changes for " + accessoryId);
LOGGER.info("Publishing change for " + accessoryId);
for (HomekitClientConnection connection : subscriptions.get(changed)) {
connection.outOfBand(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,34 @@ public HttpResponse get(HttpRequest request) throws Exception {

public HttpResponse put(HttpRequest request, HomekitClientConnection connection)
throws Exception {
try (ByteArrayInputStream bais = new ByteArrayInputStream(request.getBody())) {
JsonArray jsonCharacteristics =
Json.createReader(bais).readObject().getJsonArray("characteristics");
for (JsonValue value : jsonCharacteristics) {
JsonObject jsonCharacteristic = (JsonObject) value;
int aid = jsonCharacteristic.getInt("aid");
int iid = jsonCharacteristic.getInt("iid");
Characteristic characteristic = registry.getCharacteristics(aid).get(iid);
subscriptions.batchUpdate();
try {
try (ByteArrayInputStream bais = new ByteArrayInputStream(request.getBody())) {
JsonArray jsonCharacteristics =
Json.createReader(bais).readObject().getJsonArray("characteristics");
for (JsonValue value : jsonCharacteristics) {
JsonObject jsonCharacteristic = (JsonObject) value;
int aid = jsonCharacteristic.getInt("aid");
int iid = jsonCharacteristic.getInt("iid");
Characteristic characteristic = registry.getCharacteristics(aid).get(iid);

if (jsonCharacteristic.containsKey("value")) {
characteristic.setValue(jsonCharacteristic.get("value"));
}
if (jsonCharacteristic.containsKey("ev")
&& characteristic instanceof EventableCharacteristic) {
if (jsonCharacteristic.getBoolean("ev")) {
subscriptions.addSubscription(
aid, iid, (EventableCharacteristic) characteristic, connection);
} else {
subscriptions.removeSubscription((EventableCharacteristic) characteristic, connection);
if (jsonCharacteristic.containsKey("value")) {
characteristic.setValue(jsonCharacteristic.get("value"));
}
if (jsonCharacteristic.containsKey("ev")
&& characteristic instanceof EventableCharacteristic) {
if (jsonCharacteristic.getBoolean("ev")) {
subscriptions.addSubscription(
aid, iid, (EventableCharacteristic) characteristic, connection);
} else {
subscriptions.removeSubscription(
(EventableCharacteristic) characteristic, connection);
}
}
}
}
} finally {
subscriptions.completeUpdateBatch();
}
return new HapJsonNoContentResponse();
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/beowulfe/hap/impl/json/EventController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.beowulfe.hap.impl.json;

import com.beowulfe.hap.characteristics.EventableCharacteristic;
import com.beowulfe.hap.impl.connections.PendingNotification;
import com.beowulfe.hap.impl.http.HttpResponse;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
Expand All @@ -29,4 +31,25 @@ public HttpResponse getMessage(int accessoryId, int iid, EventableCharacteristic
return new EventResponse(dataBytes);
}
}

public HttpResponse getMessage(ArrayList<PendingNotification> notifications) throws Exception {
JsonArrayBuilder characteristics = Json.createArrayBuilder();

for (PendingNotification notification : notifications) {
JsonObjectBuilder characteristicBuilder = Json.createObjectBuilder();
characteristicBuilder.add("aid", notification.aid);
characteristicBuilder.add("iid", notification.iid);
notification.characteristic.supplyValue(characteristicBuilder);
characteristics.add(characteristicBuilder.build());
}

JsonObject data = Json.createObjectBuilder().add("characteristics", characteristics).build();

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Json.createWriter(baos).write(data);
byte[] dataBytes = baos.toByteArray();

return new EventResponse(dataBytes);
}
}
}