Skip to content

Commit

Permalink
Accept a CodecFactory when constructing DataTypeCodecSessionInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Sep 17, 2022
1 parent c8c1503 commit 6dae1fa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.milo.opcua.sdk.core.types.DynamicStructCodec;
import org.eclipse.milo.opcua.stack.client.UaStackClient;
import org.eclipse.milo.opcua.stack.core.NodeIds;
import org.eclipse.milo.opcua.stack.core.encoding.DataTypeCodec;
import org.eclipse.milo.opcua.stack.core.types.structured.DataTypeDefinition;
import org.eclipse.milo.opcua.stack.core.types.structured.StructureDefinition;
import org.eclipse.milo.opcua.stack.core.util.Tree;
Expand All @@ -30,6 +31,25 @@ public class DataTypeCodecSessionInitializer implements SessionFsm.SessionInitia
private static final Logger LOGGER =
LoggerFactory.getLogger(DataTypeCodecSessionInitializer.class);

private final CodecFactory codecFactory;

/**
* Create a {@link DataTypeCodecSessionInitializer} that with the default {@link CodecFactory}
* that uses {@link DynamicStructCodec}.
*/
public DataTypeCodecSessionInitializer() {
this(DynamicStructCodec::new);
}

/**
* Create a {@link DataTypeCodecSessionInitializer} with a custom {@link CodecFactory}.
*
* @param codecFactory the custom {@link CodecFactory} that will create {@link DataTypeCodec}s.
*/
public DataTypeCodecSessionInitializer(CodecFactory codecFactory) {
this.codecFactory = codecFactory;
}

@Override
public CompletableFuture<Unit> initialize(UaStackClient stackClient, OpcUaSession session) {
String treeKey = DataTypeTreeSessionInitializer.SESSION_ATTRIBUTE_KEY;
Expand All @@ -51,7 +71,7 @@ public CompletableFuture<Unit> initialize(UaStackClient stackClient, OpcUaSessio
}
}

private static void registerCodecs(UaStackClient stackClient, DataTypeTree dataTypeTree) {
private void registerCodecs(UaStackClient stackClient, DataTypeTree dataTypeTree) {
Tree<DataType> structureNode = dataTypeTree.getTreeNode(NodeIds.Structure);

if (structureNode != null) {
Expand All @@ -66,7 +86,7 @@ private static void registerCodecs(UaStackClient stackClient, DataTypeTree dataT

stackClient.getDynamicDataTypeManager().registerType(
dataType.getNodeId(),
new DynamicStructCodec(dataTypeTree, dataType),
codecFactory.create(dataType, dataTypeTree),
dataType.getBinaryEncodingId(),
dataType.getXmlEncodingId(),
dataType.getJsonEncodingId()
Expand All @@ -79,4 +99,17 @@ private static void registerCodecs(UaStackClient stackClient, DataTypeTree dataT
}
}

public interface CodecFactory {

/**
* Create a {@link DataTypeCodec} instance for {@code dataType}.
*
* @param dataType the {@link DataType} to create the codec for.
* @param dataTypeTree the {@link DataTypeTree}.
* @return a {@link DataTypeCodec} for {@code dataType}.
*/
DataTypeCodec create(DataType dataType, DataTypeTree dataTypeTree);

}

}
Expand Up @@ -56,7 +56,7 @@ public class DynamicStructCodec extends GenericDataTypeCodec<DynamicStruct> {
private final DataType dataType;
private final StructureDefinition structureDefinition;

public DynamicStructCodec(DataTypeTree dataTypeTree, DataType dataType) {
public DynamicStructCodec(DataType dataType, DataTypeTree dataTypeTree) {
this.dataType = dataType;
this.structureDefinition = (StructureDefinition) dataType.getDataTypeDefinition();

Expand Down

0 comments on commit 6dae1fa

Please sign in to comment.