Skip to content

Commit

Permalink
Implemented LookupInstance and GetKeyValue CDR methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmorato committed Dec 17, 2024
1 parent 0300a52 commit 9ed55bb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 42 deletions.
43 changes: 19 additions & 24 deletions Native/CSharpCDRImplTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1181,23 +1181,18 @@
return ReturnCode.BadParameter;
}

var str = _typeSupport.EncodeToString(data);
var ptr = MarshalHelper.NativeUtf8FromString(str);
IntPtr ptrData = IntPtr.Zero;
UIntPtr sizeData = UIntPtr.Zero;

var ret = (ReturnCode)<%TYPE%>DataReaderNative.GetKeyValue(_native, ref ptr, handle);
var ret = (ReturnCode)<%TYPE%>DataReaderNative.GetKeyValue(_native, ref ptrData, ref sizeData, handle);

if (ret == ReturnCode.Ok)
{
var json_data = MarshalHelper.StringFromNativeUtf8(ptr) ?? string.Empty;

if (!string.IsNullOrWhiteSpace(json_data))
{
var sample = _typeSupport.DecodeFromString(json_data);
byte[] managedArray = new byte[(int)sizeData];
Marshal.Copy(ptrData, managedArray, 0, (int)sizeData);

data.MemberwiseCopy(sample);
}

MarshalHelper.ReleaseNativeStringPointer(ptr);
var sample = _typeSupport.DecodeFromBytes(managedArray);
data.MemberwiseCopy(sample);
}

return ret;
Expand All @@ -1207,9 +1202,9 @@
{
InstanceHandle ret = InstanceHandle.HandleNil;

var json_data = _typeSupport.EncodeToString(instance);
var bytes = _typeSupport.EncodeToBytes(instance);

ret = <%TYPE%>DataReaderNative.LookupInstance(_native, json_data);
ret = <%TYPE%>DataReaderNative.LookupInstance(_native, bytes, (UIntPtr)bytes.Length);

return ret;
}
Expand Down Expand Up @@ -1295,14 +1290,14 @@
internal static partial int TakeNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo);

[SuppressUnmanagedCodeSecurity]
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Json", StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
internal static partial int LookupInstance(IntPtr dr, string jsonData);
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Cdr")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })]
internal static partial int LookupInstance(IntPtr dr, byte[] cdrData, UIntPtr sizeData);

[SuppressUnmanagedCodeSecurity]
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Json")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
internal static partial int GetKeyValue(IntPtr dr, ref IntPtr data, int handle);
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })]
internal static partial int GetKeyValue(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, int handle);
#else
[SuppressUnmanagedCodeSecurity]
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Narrow", CallingConvention = CallingConvention.Cdecl)]
Expand Down Expand Up @@ -1365,11 +1360,11 @@
internal static extern int TakeNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo);

[SuppressUnmanagedCodeSecurity]
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Json", CallingConvention = CallingConvention.Cdecl)]
internal static extern int LookupInstance(IntPtr dr, [MarshalAs(UnmanagedType.LPStr), In]string jsonData);
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Cdr", CallingConvention = CallingConvention.Cdecl)]
internal static extern int LookupInstance(IntPtr dr, byte[] cdrData, UIntPtr sizeData);

[SuppressUnmanagedCodeSecurity]
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Json", CallingConvention = CallingConvention.Cdecl)]
internal static extern int GetKeyValue(IntPtr dr, [In, Out] ref IntPtr data, int handle);
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr", CallingConvention = CallingConvention.Cdecl)]
internal static extern int GetKeyValue(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, int handle);
#endif
}
6 changes: 5 additions & 1 deletion Native/CWrapperHeaderTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeWithCon

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, const char* json_data);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_LookupInstance_Cdr(<%SCOPED%>DataReader_ptr dr, const char* cdr_data, size_t size);

EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstance_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates);

EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates);
Expand Down Expand Up @@ -120,9 +122,11 @@ EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextIns

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_GetKeyValue_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, int handle);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, int handle);

/*
#include <fstream>
using std::ofstream;
using std::ofstream;
#include <iostream>
using std::cout;
using std::endl;
Expand Down
23 changes: 23 additions & 0 deletions Native/CWrapperImplTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,14 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
return dr->lookup_instance(sample);
}


int <%SCOPED_METHOD%>DataReader_LookupInstance_Cdr(<%SCOPED%>DataReader_ptr dr, const char* cdr_data, size_t size)
{
<%SCOPED%> sample = <%SCOPED_METHOD%>_deserialize_from_bytes(cdr_data, size);

return dr->lookup_instance(sample);
}

::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstance_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates)
{
<%SCOPED%>Seq received_data;
Expand Down Expand Up @@ -787,3 +795,18 @@ int <%SCOPED_METHOD%>DataReader_GetKeyValue_Json(<%SCOPED%>DataReader_ptr dr, ch

return ret;
}

int <%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size_data, int handle)
{
<%SCOPED%> sample_key;
::DDS::ReturnCode_t ret = dr->get_key_value(sample_key, handle);

if (ret == ::DDS::RETCODE_OK)
{
<%SCOPED%> sample;
<%SCOPED_METHOD%>_CopyKeys(&sample_key, &sample);
<%SCOPED_METHOD%>_serialize_to_bytes(sample, cdr_data, size_data);
}

return ret;
}
18 changes: 9 additions & 9 deletions Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void TestCreateReadCondition()
Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);

// Create a read condition with the full parameters overload
condition = reader.CreateReadCondition(
Expand All @@ -343,7 +343,7 @@ public void TestCreateReadCondition()
Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);

reader.DeleteContainedEntities();
_subscriber.DeleteDataReader(reader);
Expand All @@ -370,7 +370,7 @@ public void TestCreateQueryCondition()
Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);
Assert.AreEqual(expression, condition.QueryExpression);

var parameters = new List<string>();
Expand All @@ -392,7 +392,7 @@ public void TestCreateQueryCondition()
Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);
Assert.AreEqual(expression, condition.QueryExpression);

parameters = new List<string>();
Expand Down Expand Up @@ -433,15 +433,15 @@ public void TestDeleteReadCondition()
Assert.AreEqual(InstanceStateMask.AnyInstanceState, readCondition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, readCondition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, readCondition.ViewStateMask);
Assert.AreEqual(false, readCondition.TriggerValue);
Assert.IsFalse(readCondition.TriggerValue);

// Create a QueryCondition with the simplest overload
var queryCondition = reader.CreateQueryCondition(expression, parameter1, parameter2);
Assert.IsNotNull(queryCondition);
Assert.AreEqual(InstanceStateMask.AnyInstanceState, queryCondition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, queryCondition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, queryCondition.ViewStateMask);
Assert.AreEqual(false, queryCondition.TriggerValue);
Assert.IsFalse(queryCondition.TriggerValue);
Assert.AreEqual(expression, queryCondition.QueryExpression);

var parameters = new List<string>();
Expand Down Expand Up @@ -1178,8 +1178,8 @@ public void TestGetMatchedPublicationData()
drQos.Reliability.Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos;

// OPENDDS ISSUE: Cannot use ExclusiveOwnership for the test because when calling delete_datareader
// the BitPubListenerImpl::on_data_available take_next_sample method enter in a infinite loop if we already called
// the GetMatchedPublicationData. It tries to take a not_read_sample but it doesn't exists because it is already marked
// the BitPubListenerImpl::on_data_available take_next_sample method enter an infinite loop if we already called
// the GetMatchedPublicationData. It tries to take a not_read_sample, but it doesn't exist because it is already marked
// as read in the GetMatchedPublicationData call.
drQos.Ownership.Kind = OwnershipQosPolicyKind.SharedOwnershipQos;
var reader = _subscriber.CreateDataReader(_topic, drQos);
Expand All @@ -1194,7 +1194,7 @@ public void TestGetMatchedPublicationData()
// https://github.com/OpenDDS/OpenDDS/blob/master/docs/design/RTPS

// OPENDDS ISSUE: GetMatchedSubscriptions returns local entities but GetMatchedSubscriptionData doesn't
// because is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally?
// because it is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally?
// WORKAROUND: Create another participant for the DataReader.
var otherParticipant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN);
Assert.IsNotNull(otherParticipant);
Expand Down
16 changes: 8 additions & 8 deletions Tests/OpenDDSharp.UnitTest/DataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public void TestCreateReadCondition()
Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);

// Create a read condition with the full parameters overload
condition = reader.CreateReadCondition(
Expand All @@ -342,7 +342,7 @@ public void TestCreateReadCondition()
Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);

reader.DeleteContainedEntities();
_subscriber.DeleteDataReader(reader);
Expand All @@ -369,7 +369,7 @@ public void TestCreateQueryCondition()
Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);
Assert.AreEqual(expression, condition.QueryExpression);

var parameters = new List<string>();
Expand All @@ -391,7 +391,7 @@ public void TestCreateQueryCondition()
Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask);
Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask);
Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask);
Assert.AreEqual(false, condition.TriggerValue);
Assert.IsFalse(condition.TriggerValue);
Assert.AreEqual(expression, condition.QueryExpression);

parameters = new List<string>();
Expand Down Expand Up @@ -432,15 +432,15 @@ public void TestDeleteReadCondition()
Assert.AreEqual(InstanceStateMask.AnyInstanceState, readCondition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, readCondition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, readCondition.ViewStateMask);
Assert.AreEqual(false, readCondition.TriggerValue);
Assert.IsFalse(readCondition.TriggerValue);

// Create a QueryCondition with the simplest overload
var queryCondition = reader.CreateQueryCondition(expression, parameter1, parameter2);
Assert.IsNotNull(queryCondition);
Assert.AreEqual(InstanceStateMask.AnyInstanceState, queryCondition.InstanceStateMask);
Assert.AreEqual(SampleStateMask.AnySampleState, queryCondition.SampleStateMask);
Assert.AreEqual(ViewStateMask.AnyViewState, queryCondition.ViewStateMask);
Assert.AreEqual(false, queryCondition.TriggerValue);
Assert.IsFalse(queryCondition.TriggerValue);
Assert.AreEqual(expression, queryCondition.QueryExpression);

var parameters = new List<string>();
Expand Down Expand Up @@ -1178,7 +1178,7 @@ public void TestGetMatchedPublicationData()

// OPENDDS ISSUE: Cannot use ExclusiveOwnership for the test because when calling delete_datareader
// the BitPubListenerImpl::on_data_available take_next_sample method enter in a infinite loop if we already called
// the GetMatchedPublicationData. It tries to take a not_read_sample but it doesn't exists because it is already marked
// the GetMatchedPublicationData. It tries to take a not_read_sample but it doesn't exist because it is already marked
// as read in the GetMatchedPublicationData call.
drQos.Ownership.Kind = OwnershipQosPolicyKind.SharedOwnershipQos;
var reader = _subscriber.CreateDataReader(_topic, drQos);
Expand All @@ -1193,7 +1193,7 @@ public void TestGetMatchedPublicationData()
// https://github.com/OpenDDS/OpenDDS/blob/master/docs/design/RTPS

// OPENDDS ISSUE: GetMatchedSubscriptions returns local entities but GetMatchedSubscriptionData doesn't
// because is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally?
// because it is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally?
// WORKAROUND: Create another participant for the DataReader.
var otherParticipant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN);
Assert.IsNotNull(otherParticipant);
Expand Down

0 comments on commit 9ed55bb

Please sign in to comment.