com.digitalpetri.enip.cip.services.MultipleServicePacketService Maven / Gradle / Ivy
package com.digitalpetri.enip.cip.services;
import java.util.List;
import java.util.function.BiConsumer;
import com.digitalpetri.enip.cip.CipResponseException;
import com.digitalpetri.enip.cip.epath.EPath.PaddedEPath;
import com.digitalpetri.enip.cip.epath.LogicalSegment.ClassId;
import com.digitalpetri.enip.cip.epath.LogicalSegment.InstanceId;
import com.digitalpetri.enip.cip.structs.MessageRouterRequest;
import com.digitalpetri.enip.cip.structs.MessageRouterResponse;
import io.netty.buffer.ByteBuf;
import io.netty.util.ReferenceCountUtil;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.synchronizedList;
public class MultipleServicePacketService implements CipService {
public static final int SERVICE_CODE = 0x0A;
private static final PaddedEPath MESSAGE_ROUTER_PATH = new PaddedEPath(
new ClassId(0x02),
new InstanceId(0x01));
private final List> services;
private final List> consumers;
private final List> currentServices;
private final List> currentConsumers;
public MultipleServicePacketService(List> services, List> consumers) {
assert (services.size() == consumers.size());
this.services = synchronizedList(newArrayList(services));
this.consumers = synchronizedList(newArrayList(consumers));
this.currentServices = synchronizedList(newArrayList(services));
this.currentConsumers = synchronizedList(newArrayList(consumers));
}
@Override
public void encodeRequest(ByteBuf buffer) {
MessageRouterRequest request = new MessageRouterRequest(
SERVICE_CODE,
MESSAGE_ROUTER_PATH,
this::encode);
MessageRouterRequest.encode(request, buffer);
}
@Override
public Void decodeResponse(ByteBuf buffer) throws CipResponseException, PartialResponseException {
MessageRouterResponse response = MessageRouterResponse.decode(buffer);
if (response.getGeneralStatus() == 0x00 || response.getGeneralStatus() == 0x1E) {
try {
List