/*
* Copyright 2016 higherfrequencytrading.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.openhft.chronicle.engine.server.internal;
import net.openhft.chronicle.engine.api.tree.Asset;
import net.openhft.chronicle.engine.api.tree.AssetNotFoundException;
import net.openhft.chronicle.engine.api.tree.RequestContext;
import net.openhft.chronicle.engine.tree.TopologySubscription;
import net.openhft.chronicle.network.connection.WireOutPublisher;
import net.openhft.chronicle.wire.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.BiConsumer;
import static net.openhft.chronicle.network.connection.CoreFields.reply;
import static net.openhft.chronicle.network.connection.CoreFields.tid;
/**
* Created by Rob Austin
*/
public class TopologySubscriptionHandler extends SubscriptionHandler<TopologySubscription> {
@Nullable
private final BiConsumer<WireIn, Long> dataConsumer = (inWire, inputTid) -> {
eventName.setLength(0);
@NotNull final ValueIn valueIn = inWire.readEventName(eventName);
assert startEnforceInValueReadCheck(inWire);
try {
if (before(inputTid, valueIn)) return;
} catch (AssetNotFoundException e) {
throw new AssertionError(e);
} finally {
assert endEnforceInValueReadCheck(inWire);
}
outWire.writeDocument(true, wire -> outWire.writeEventName(tid).int64(inputTid));
writeData(inWire, out -> {
if (after(eventName)) return;
if (EventId.notifyEvent.contentEquals(eventName)) {
subscription.notifyEvent(valueIn.typedMarshallable());
outWire.writeEventName(reply).int8(subscription.entrySubscriberCount());
}
});
};
void process(@NotNull final WireIn inWire,
@NotNull final RequestContext requestContext,
@NotNull final WireOutPublisher publisher,
@NotNull final Asset asset, final long tid,
@NotNull final Wire outWire,
@NotNull final TopologySubscription subscription) {
setOutWire(outWire);
this.outWire = outWire;
this.subscription = subscription;
this.requestContext = requestContext;
this.publisher = publisher;
this.asset = asset;
assert dataConsumer != null;
dataConsumer.accept(inWire, tid);
}
public enum EventId implements ParameterizeWireKey {
notifyEvent;
private final WireKey[] params;
<P extends WireKey> EventId(P... params) {
this.params = params;
}
@NotNull
public <P extends WireKey> P[] params() {
return (P[]) this.params;
}
}
}