Skip to content

Commit

Permalink
Changing caching logic to match video players using hostname before o…
Browse files Browse the repository at this point in the history
…ther attributes
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Oct 12, 2023
1 parent ef8b80d commit 9bed45d
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class DiscoveredNodeData {
public DiscoveredNodeData(NsdServiceInfo serviceInfo) {
Map<String, byte[]> attributes = serviceInfo.getAttributes();
this.deviceName = new String(attributes.get(KEY_DEVICE_NAME), StandardCharsets.UTF_8);
if (serviceInfo.getHost() != null) {
this.hostName = serviceInfo.getHost().getHostName();
}
this.deviceType =
Long.parseLong(new String(attributes.get(KEY_DEVICE_TYPE), StandardCharsets.UTF_8));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class VideoPlayer {

private int numIPs;
private List<InetAddress> ipAddresses;
private String hostName;

private boolean isInitialized = false;

Expand All @@ -50,6 +51,7 @@ public VideoPlayer(
List<ContentApp> contentApps,
int numIPs,
List<InetAddress> ipAddresses,
String hostName,
boolean isConnected) {
this.nodeId = nodeId;
this.fabricIndex = fabricIndex;
Expand All @@ -61,6 +63,7 @@ public VideoPlayer(
this.isConnected = isConnected;
this.numIPs = numIPs;
this.ipAddresses = ipAddresses;
this.hostName = hostName;
this.isInitialized = true;
}

Expand All @@ -70,6 +73,11 @@ public boolean isSameAs(DiscoveredNodeData discoveredNodeData) {
return false;
}

// return true if hostNames match
if (Objects.equals(hostName, discoveredNodeData.getHostName())) {
return true;
}

// return false because deviceNames are different
if (Objects.equals(deviceName, discoveredNodeData.getDeviceName()) == false) {
return false;
Expand Down Expand Up @@ -133,6 +141,9 @@ public java.lang.String toString() {
+ ", ipAddresses="
+ ipAddresses
+ ", isInitialized="
+ ", hostName='"
+ hostName
+ '\''
+ isInitialized
+ '}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

CHIP_ERROR convertJAppParametersToCppAppParams(jobject appParameters, AppParams & outAppParams)
{
ChipLogProgress(AppServer, "convertJContentAppToTargetEndpointInfo called");
ChipLogProgress(AppServer, "convertJAppParametersToCppAppParams called");
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturnError(appParameters != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

Expand Down Expand Up @@ -149,7 +149,13 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe
jfieldID getDeviceNameField = env->GetFieldID(jVideoPlayerClass, "deviceName", "Ljava/lang/String;");
jstring jDeviceName = static_cast<jstring>(env->GetObjectField(videoPlayer, getDeviceNameField));
const char * deviceName = env->GetStringUTFChars(jDeviceName, 0);
outTargetVideoPlayerInfo.Initialize(nodeId, fabricIndex, nullptr, nullptr, vendorId, productId, deviceType, deviceName);

jfieldID getHostNameField = env->GetFieldID(jVideoPlayerClass, "hostName", "Ljava/lang/String;");
jstring jHostName = static_cast<jstring>(env->GetObjectField(videoPlayer, getHostNameField));
const char * hostName = env->GetStringUTFChars(jHostName, 0);

outTargetVideoPlayerInfo.Initialize(nodeId, fabricIndex, nullptr, nullptr, vendorId, productId, deviceType, deviceName,
hostName);

jfieldID jContentAppsField = env->GetFieldID(jVideoPlayerClass, "contentApps", "Ljava/util/List;");
jobject jContentApps = env->GetObjectField(videoPlayer, jContentAppsField);
Expand Down Expand Up @@ -189,8 +195,8 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
jclass jVideoPlayerClass;
ReturnErrorOnFailure(
chip::JniReferences::GetInstance().GetClassRef(env, "com/chip/casting/VideoPlayer", jVideoPlayerClass));
jmethodID jVideoPlayerConstructor =
env->GetMethodID(jVideoPlayerClass, "<init>", "(JBLjava/lang/String;IIILjava/util/List;ILjava/util/List;Z)V");
jmethodID jVideoPlayerConstructor = env->GetMethodID(
jVideoPlayerClass, "<init>", "(JBLjava/lang/String;IIILjava/util/List;ILjava/util/List;Ljava/lang/String;Z)V");

jobject jContentAppList = nullptr;
TargetEndpointInfo * endpoints = targetVideoPlayerInfo->GetEndpoints();
Expand All @@ -208,6 +214,9 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
jstring deviceName =
targetVideoPlayerInfo->GetDeviceName() == nullptr ? nullptr : env->NewStringUTF(targetVideoPlayerInfo->GetDeviceName());

jstring hostName =
targetVideoPlayerInfo->GetHostName() == nullptr ? nullptr : env->NewStringUTF(targetVideoPlayerInfo->GetHostName());

jobject jIPAddressList = nullptr;
const chip::Inet::IPAddress * ipAddresses = targetVideoPlayerInfo->GetIpAddresses();
if (ipAddresses != nullptr)
Expand All @@ -233,7 +242,7 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
outVideoPlayer = env->NewObject(jVideoPlayerClass, jVideoPlayerConstructor, targetVideoPlayerInfo->GetNodeId(),
targetVideoPlayerInfo->GetFabricIndex(), deviceName, targetVideoPlayerInfo->GetVendorId(),
targetVideoPlayerInfo->GetProductId(), targetVideoPlayerInfo->GetDeviceType(),
jContentAppList, targetVideoPlayerInfo->GetNumIPs(), jIPAddressList,
jContentAppList, targetVideoPlayerInfo->GetNumIPs(), jIPAddressList, hostName,
targetVideoPlayerInfo->GetOperationalDeviceProxy() != nullptr);
}
return CHIP_NO_ERROR;
Expand All @@ -259,7 +268,7 @@ CHIP_ERROR convertJDiscoveredNodeDataToCppDiscoveredNodeData(jobject jDiscovered
env->GetStringUTFChars(jHostName, 0));
}

jfieldID getInstanceNameField = env->GetFieldID(jDiscoveredNodeDataClass, "deviceName", "Ljava/lang/String;");
jfieldID getInstanceNameField = env->GetFieldID(jDiscoveredNodeDataClass, "instanceName", "Ljava/lang/String;");
jstring jInstanceName = static_cast<jstring>(env->GetObjectField(jDiscoveredNodeData, getInstanceNameField));
if (jInstanceName != nullptr)
{
Expand Down Expand Up @@ -289,8 +298,11 @@ CHIP_ERROR convertJDiscoveredNodeDataToCppDiscoveredNodeData(jobject jDiscovered

jfieldID getDeviceNameField = env->GetFieldID(jDiscoveredNodeDataClass, "deviceName", "Ljava/lang/String;");
jstring jDeviceName = static_cast<jstring>(env->GetObjectField(jDiscoveredNodeData, getDeviceNameField));
chip::Platform::CopyString(outCppDiscoveredNodeData.commissionData.deviceName, chip::Dnssd::kMaxDeviceNameLen + 1,
env->GetStringUTFChars(jDeviceName, 0));
if (jDeviceName != nullptr)
{
chip::Platform::CopyString(outCppDiscoveredNodeData.commissionData.deviceName, chip::Dnssd::kMaxDeviceNameLen + 1,
env->GetStringUTFChars(jDeviceName, 0));
}

// TODO: map rotating ID
jfieldID jRotatingIdLenField = env->GetFieldID(jDiscoveredNodeDataClass, "rotatingIdLen", "I");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ - (void)stopMatterServer
self->_previouslyConnectedVideoPlayer->Initialize(currentTargetVideoPlayerInfo->GetNodeId(),
currentTargetVideoPlayerInfo->GetFabricIndex(), nullptr, nullptr, currentTargetVideoPlayerInfo->GetVendorId(),
currentTargetVideoPlayerInfo->GetProductId(), currentTargetVideoPlayerInfo->GetDeviceType(),
currentTargetVideoPlayerInfo->GetDeviceName(), currentTargetVideoPlayerInfo->GetNumIPs(),
currentTargetVideoPlayerInfo->GetDeviceName(), currentTargetVideoPlayerInfo->GetHostName(),
currentTargetVideoPlayerInfo->GetNumIPs(),
const_cast<chip::Inet::IPAddress *>(currentTargetVideoPlayerInfo->GetIpAddresses()));

TargetEndpointInfo * prevEndpoints = self->_previouslyConnectedVideoPlayer->GetEndpoints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,22 @@ + (CHIP_ERROR)convertToCppDiscoveredNodeDataFrom:(DiscoveredNodeData * _Nonnull)
outDiscoveredNodeData.commissionData.longDiscriminator = objCDiscoveredNodeData.longDiscriminator;
outDiscoveredNodeData.commissionData.commissioningMode = objCDiscoveredNodeData.commissioningMode;
outDiscoveredNodeData.commissionData.pairingHint = objCDiscoveredNodeData.pairingHint;
chip::Platform::CopyString(outDiscoveredNodeData.commissionData.deviceName, chip::Dnssd::kMaxDeviceNameLen + 1,
[objCDiscoveredNodeData.deviceName UTF8String]);
memset(outDiscoveredNodeData.commissionData.deviceName, '\0', sizeof(outDiscoveredNodeData.commissionData.deviceName));
if (objCDiscoveredNodeData.deviceName != nullptr) {
chip::Platform::CopyString(outDiscoveredNodeData.commissionData.deviceName, chip::Dnssd::kMaxDeviceNameLen + 1,
[objCDiscoveredNodeData.deviceName UTF8String]);
}
outDiscoveredNodeData.commissionData.rotatingIdLen = objCDiscoveredNodeData.rotatingIdLen;
memcpy(
outDiscoveredNodeData.commissionData.rotatingId, objCDiscoveredNodeData.rotatingId, objCDiscoveredNodeData.rotatingIdLen);

// setting CommonResolutionData
outDiscoveredNodeData.resolutionData.port = objCDiscoveredNodeData.port;
chip::Platform::CopyString(outDiscoveredNodeData.resolutionData.hostName, chip::Dnssd::kHostNameMaxLength + 1,
[objCDiscoveredNodeData.hostName UTF8String]);
memset(outDiscoveredNodeData.resolutionData.hostName, '\0', sizeof(outDiscoveredNodeData.resolutionData.hostName));
if (objCDiscoveredNodeData.hostName != nullptr) {
chip::Platform::CopyString(outDiscoveredNodeData.resolutionData.hostName, chip::Dnssd::kHostNameMaxLength + 1,
[objCDiscoveredNodeData.hostName UTF8String]);
}
outDiscoveredNodeData.resolutionData.interfaceId = chip::Inet::InterfaceId(objCDiscoveredNodeData.platformInterface);
outDiscoveredNodeData.resolutionData.numIPs = objCDiscoveredNodeData.numIPs;
for (size_t i = 0; i < objCDiscoveredNodeData.numIPs; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ class CastingServer
uint16_t mTargetVideoPlayerProductId = 0;
chip::DeviceTypeId mTargetVideoPlayerDeviceType = 0;
char mTargetVideoPlayerDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char mTargetVideoPlayerHostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
size_t mTargetVideoPlayerNumIPs = 0; // number of valid IP addresses
chip::Inet::IPAddress mTargetVideoPlayerIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PersistenceManager
kVideoPlayerProductIdTag,
kVideoPlayerDeviceTypeIdTag,
kVideoPlayerDeviceNameTag,
kVideoPlayerHostNameTag,
kVideoPlayerNumIPsTag,
kVideoPlayerIPAddressTag,
kIpAddressesContainerTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ class TargetVideoPlayerInfo
chip::NodeId GetNodeId() const { return mNodeId; }
chip::FabricIndex GetFabricIndex() const { return mFabricIndex; }
const char * GetDeviceName() const { return mDeviceName; }
const char * GetHostName() const { return mHostName; }
size_t GetNumIPs() const { return mNumIPs; }
const chip::Inet::IPAddress * GetIpAddresses() const { return mIpAddress; }
bool IsSameAs(const chip::Dnssd::DiscoveredNodeData * discoveredNodeData);
bool IsSameAs(const char * deviceName, size_t numIPs, const chip::Inet::IPAddress * ipAddresses);
bool IsSameAs(const char * hostName, const char * deviceName, size_t numIPs, const chip::Inet::IPAddress * ipAddresses);

chip::OperationalDeviceProxy * GetOperationalDeviceProxy()
{
Expand All @@ -58,8 +59,8 @@ class TargetVideoPlayerInfo
CHIP_ERROR Initialize(chip::NodeId nodeId, chip::FabricIndex fabricIndex,
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure, uint16_t vendorId = 0, uint16_t productId = 0,
chip::DeviceTypeId deviceType = 0, const char * deviceName = {}, size_t numIPs = 0,
chip::Inet::IPAddress * ipAddressList = nullptr);
chip::DeviceTypeId deviceType = 0, const char * deviceName = {}, const char * hostName = {},
size_t numIPs = 0, chip::Inet::IPAddress * ipAddressList = nullptr);
CHIP_ERROR FindOrEstablishCASESession(std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure);
TargetEndpointInfo * GetOrAddEndpoint(chip::EndpointId endpointId);
Expand Down Expand Up @@ -110,6 +111,7 @@ class TargetVideoPlayerInfo
uint16_t mProductId = 0;
chip::DeviceTypeId mDeviceType = 0;
char mDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char mHostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
size_t mNumIPs = 0; // number of valid IP addresses
chip::Inet::IPAddress mIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(Dnssd::Discovered
}
chip::Platform::CopyString(mTargetVideoPlayerDeviceName, chip::Dnssd::kMaxDeviceNameLen + 1,
selectedCommissioner->commissionData.deviceName);
chip::Platform::CopyString(mTargetVideoPlayerHostName, chip::Dnssd::kHostNameMaxLength + 1,
selectedCommissioner->resolutionData.hostName);
return CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
Expand Down Expand Up @@ -412,7 +414,8 @@ void CastingServer::DeviceEventCallback(const DeviceLayer::ChipDeviceEvent * eve
CastingServer::GetInstance()->mOnConnectionFailureClientCallback,
CastingServer::GetInstance()->mTargetVideoPlayerVendorId, CastingServer::GetInstance()->mTargetVideoPlayerProductId,
CastingServer::GetInstance()->mTargetVideoPlayerDeviceType, CastingServer::GetInstance()->mTargetVideoPlayerDeviceName,
CastingServer::GetInstance()->mTargetVideoPlayerNumIPs, CastingServer::GetInstance()->mTargetVideoPlayerIpAddress);
CastingServer::GetInstance()->mTargetVideoPlayerHostName, CastingServer::GetInstance()->mTargetVideoPlayerNumIPs,
CastingServer::GetInstance()->mTargetVideoPlayerIpAddress);

if (err != CHIP_NO_ERROR)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ CHIP_ERROR PersistenceManager::WriteAllVideoPlayers(TargetVideoPlayerInfo videoP
ReturnErrorOnFailure(tlvWriter.PutBytes(TLV::ContextTag(kVideoPlayerDeviceNameTag),
(const uint8_t *) videoPlayer->GetDeviceName(),
static_cast<uint32_t>(strlen(videoPlayer->GetDeviceName()) + 1)));
ReturnErrorOnFailure(tlvWriter.PutBytes(TLV::ContextTag(kVideoPlayerHostNameTag),
(const uint8_t *) videoPlayer->GetHostName(),
static_cast<uint32_t>(strlen(videoPlayer->GetHostName()) + 1)));

ReturnErrorOnFailure(
tlvWriter.Put(TLV::ContextTag(kVideoPlayerNumIPsTag), static_cast<uint64_t>(videoPlayer->GetNumIPs())));
const Inet::IPAddress * ipAddress = videoPlayer->GetIpAddresses();
Expand Down Expand Up @@ -205,6 +209,7 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
uint16_t productId = 0;
uint16_t deviceType = 0;
char deviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char hostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
size_t numIPs = 0;
Inet::IPAddress ipAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];
CHIP_ERROR err;
Expand Down Expand Up @@ -254,6 +259,12 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
continue;
}

if (videoPlayersContainerTagNum == kVideoPlayerHostNameTag)
{
ReturnErrorOnFailure(reader.GetBytes(reinterpret_cast<uint8_t *>(hostName), chip::Dnssd::kHostNameMaxLength + 1));
continue;
}

if (videoPlayersContainerTagNum == kVideoPlayerNumIPsTag)
{
ReturnErrorOnFailure(reader.Get(reinterpret_cast<uint64_t &>(numIPs)));
Expand Down Expand Up @@ -301,7 +312,7 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
if (videoPlayersContainerTagNum == kContentAppEndpointsContainerTag)
{
outVideoPlayers[videoPlayerIndex].Initialize(nodeId, fabricIndex, nullptr, nullptr, vendorId, productId, deviceType,
deviceName, numIPs, ipAddress);
deviceName, hostName, numIPs, ipAddress);
// Entering Content App Endpoints container
TLV::TLVType contentAppEndpointArrayContainerType = TLV::kTLVType_Array;
ReturnErrorOnFailure(reader.EnterContainer(contentAppEndpointArrayContainerType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ CASEClientPool<CHIP_CONFIG_DEVICE_MAX_ACTIVE_CASE_CLIENTS> gCASEClientPool;
CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIndex,
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure, uint16_t vendorId,
uint16_t productId, DeviceTypeId deviceType, const char * deviceName, size_t numIPs,
chip::Inet::IPAddress * ipAddress)
uint16_t productId, chip::DeviceTypeId deviceType, const char * deviceName,
const char * hostName, size_t numIPs, chip::Inet::IPAddress * ipAddress)
{
ChipLogProgress(NotSpecified, "TargetVideoPlayerInfo nodeId=0x" ChipLogFormatX64 " fabricIndex=%d", ChipLogValueX64(nodeId),
fabricIndex);
Expand All @@ -42,7 +42,18 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn
mIpAddress[i] = ipAddress[i];
}

chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen + 1, deviceName);
memset(mDeviceName, '\0', sizeof(mDeviceName));
if (deviceName != nullptr)
{
chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen + 1, deviceName);
}

memset(mHostName, '\0', sizeof(mHostName));
if (hostName != nullptr)
{
chip::Platform::CopyString(mHostName, chip::Dnssd::kHostNameMaxLength + 1, hostName);
}

for (auto & endpointInfo : mEndpoints)
{
endpointInfo.Reset();
Expand Down Expand Up @@ -128,8 +139,15 @@ void TargetVideoPlayerInfo::PrintInfo()
}
}

bool TargetVideoPlayerInfo::IsSameAs(const char * deviceName, size_t numIPs, const chip::Inet::IPAddress * ipAddresses)
bool TargetVideoPlayerInfo::IsSameAs(const char * hostName, const char * deviceName, size_t numIPs,
const chip::Inet::IPAddress * ipAddresses)
{
// return true if the hostNames match
if (strcmp(mHostName, hostName) == 0)
{
return true;
}

// return false because deviceNames are different
if (strcmp(mDeviceName, deviceName) != 0)
{
Expand Down Expand Up @@ -173,6 +191,6 @@ bool TargetVideoPlayerInfo::IsSameAs(const chip::Dnssd::DiscoveredNodeData * dis
return false;
}

return IsSameAs(discoveredNodeData->commissionData.deviceName, discoveredNodeData->resolutionData.numIPs,
discoveredNodeData->resolutionData.ipAddress);
return IsSameAs(discoveredNodeData->resolutionData.hostName, discoveredNodeData->commissionData.deviceName,
discoveredNodeData->resolutionData.numIPs, discoveredNodeData->resolutionData.ipAddress);
}

0 comments on commit 9bed45d

Please sign in to comment.