Skip to content

Commit

Permalink
feat(plc4j/connection-cache): Added support for subscriptions to the …
Browse files Browse the repository at this point in the history
…plc connection cache.
  • Loading branch information
chrisdutz committed Dec 31, 2022
1 parent b741171 commit 1f81060
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,18 @@ public PlcWriteRequest.Builder writeRequestBuilder() {

@Override
public PlcSubscriptionRequest.Builder subscriptionRequestBuilder() {
throw new UnsupportedOperationException();
if (closed) {
throw new IllegalStateException("Trying to build a Request on a closed Connection!");
}
return new CachedSubscriptionRequestBuilder(this, this.getActiveConnection().subscriptionRequestBuilder());
}

@Override
public PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder() {
throw new UnsupportedOperationException();
if (closed) {
throw new IllegalStateException("Trying to build a Request on a closed Connection!");
}
return new CachedUnsubscriptionRequestBuilder(this, this.getActiveConnection().unsubscriptionRequestBuilder());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.plc4x.java.utils.connectionpool2;

import org.apache.plc4x.java.api.messages.*;
import org.apache.plc4x.java.api.model.PlcSubscriptionTag;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class CachedSubscriptionRequest implements PlcSubscriptionRequest {

private final CachedPlcConnection parent;
private final PlcSubscriptionRequest innerRequest;

public CachedSubscriptionRequest(CachedPlcConnection parent, PlcSubscriptionRequest innerRequest) {
this.parent = parent;
this.innerRequest = innerRequest;
}

@Override
public CompletableFuture<? extends PlcSubscriptionResponse> execute() {
// Only allowed if connection is still active
return innerRequest.execute();
}

@Override
public int getNumberOfTags() {
return innerRequest.getNumberOfTags();
}

@Override
public LinkedHashSet<String> getTagNames() {
return innerRequest.getTagNames();
}

@Override
public PlcSubscriptionTag getTag(String name) {
return innerRequest.getTag(name);
}

@Override
public List<PlcSubscriptionTag> getTags() {
return innerRequest.getTags();
}

@Override
public Map<String, List<Consumer<PlcSubscriptionEvent>>> getPreRegisteredConsumers() {
return innerRequest.getPreRegisteredConsumers();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.plc4x.java.utils.connectionpool2;

import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
import org.apache.plc4x.java.api.model.PlcTag;

import java.time.Duration;
import java.util.function.Consumer;

public class CachedSubscriptionRequestBuilder implements PlcSubscriptionRequest.Builder {

private final CachedPlcConnection parent;
private final PlcSubscriptionRequest.Builder builder;

public CachedSubscriptionRequestBuilder(CachedPlcConnection parent, PlcSubscriptionRequest.Builder builder) {
this.parent = parent;
this.builder = builder;
}

@Override
public PlcSubscriptionRequest build() {
return builder.build();
}

@Override
public PlcSubscriptionRequest.Builder addCyclicTagAddress(String name, String tagAddress, Duration pollingInterval) {
return builder.addCyclicTagAddress(name, tagAddress, pollingInterval);
}

@Override
public PlcSubscriptionRequest.Builder addCyclicTag(String name, PlcTag tag, Duration pollingInterval) {
return builder.addCyclicTag(name, tag, pollingInterval);
}

@Override
public PlcSubscriptionRequest.Builder addChangeOfStateTagAddress(String name, String tagAddress) {
return builder.addChangeOfStateTagAddress(name, tagAddress);
}

@Override
public PlcSubscriptionRequest.Builder addChangeOfStateTag(String name, PlcTag tag) {
return builder.addChangeOfStateTag(name, tag);
}

@Override
public PlcSubscriptionRequest.Builder addEventTagAddress(String name, String tagAddress) {
return builder.addEventTagAddress(name, tagAddress);
}

@Override
public PlcSubscriptionRequest.Builder addEventTag(String name, PlcTag tag) {
return builder.addEventTag(name, tag);
}

@Override
public PlcSubscriptionRequest.Builder addPreRegisteredConsumer(String name, Consumer<PlcSubscriptionEvent> preRegisteredConsumer) {
return builder.addPreRegisteredConsumer(name, preRegisteredConsumer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.plc4x.java.utils.connectionpool2;

import org.apache.plc4x.java.api.messages.*;
import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class CachedUnsubscriptionRequest implements PlcUnsubscriptionRequest {

private final CachedPlcConnection parent;
private final PlcUnsubscriptionRequest innerRequest;

public CachedUnsubscriptionRequest(CachedPlcConnection parent, PlcUnsubscriptionRequest innerRequest) {
this.parent = parent;
this.innerRequest = innerRequest;
}

@Override
public CompletableFuture<PlcUnsubscriptionResponse> execute() {
return innerRequest.execute();
}
@Override
public List<PlcSubscriptionHandle> getSubscriptionHandles() {
return innerRequest.getSubscriptionHandles();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.plc4x.java.utils.connectionpool2;

import org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest;
import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
import org.apache.plc4x.java.api.model.PlcTag;

import java.time.Duration;
import java.util.Collection;
import java.util.function.Consumer;

public class CachedUnsubscriptionRequestBuilder implements PlcUnsubscriptionRequest.Builder {

private final CachedPlcConnection parent;
private final PlcUnsubscriptionRequest.Builder builder;

public CachedUnsubscriptionRequestBuilder(CachedPlcConnection parent, PlcUnsubscriptionRequest.Builder builder) {
this.parent = parent;
this.builder = builder;
}

@Override
public PlcUnsubscriptionRequest build() {
return builder.build();
}

@Override
public PlcUnsubscriptionRequest.Builder addHandles(PlcSubscriptionHandle plcSubscriptionHandle) {
return builder.addHandles(plcSubscriptionHandle);
}

@Override
public PlcUnsubscriptionRequest.Builder addHandles(PlcSubscriptionHandle plcSubscriptionHandle, PlcSubscriptionHandle... plcSubscriptionHandles) {
return builder.addHandles(plcSubscriptionHandle, plcSubscriptionHandles);
}

@Override
public PlcUnsubscriptionRequest.Builder addHandles(Collection<PlcSubscriptionHandle> plcSubscriptionHandles) {
return builder.addHandles(plcSubscriptionHandles);
}

}

0 comments on commit 1f81060

Please sign in to comment.