Skip to content

Commit

Permalink
more ClientConnection API refinement.
Browse files Browse the repository at this point in the history
Move DeviceInfo specific processing to Common Rules implementation.
  • Loading branch information
atsushieno committed Mar 3, 2024
1 parent 7cbe9ad commit 7638650
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,12 @@ class ClientConnection(

val pendingChunkManager = PropertyChunkManager()

private fun updateRemoteProperty(msg: Message.GetPropertyDataReply) {
val req = openRequests.firstOrNull { it.requestId == msg.requestId } ?: return
openRequests.remove(req)
val status = propertyClient.getHeaderFieldInteger(msg.header, PropertyCommonHeaderKeys.STATUS) ?: return

if (status == PropertyExchangeStatus.OK) {
val propertyId = propertyClient.getPropertyIdForHeader(req.header)
propertyClient.propertyValueUpdated(propertyId, msg.body)
properties.updateValue(propertyId, msg)
}
}

private fun updateLocalProperty(ourMUID: Int, msg: Message.SubscribeProperty): Pair<String?, Message.SubscribePropertyReply?> {
private fun updatePropertyBySubscribe(msg: Message.SubscribeProperty): Pair<String?, Message.SubscribePropertyReply?> {
val command = properties.updateValue(msg)
return Pair(
command,
Message.SubscribePropertyReply(
Message.Common(ourMUID, msg.sourceMUID, msg.address, msg.group),
Message.Common(parent.muid, msg.sourceMUID, msg.address, msg.group),
msg.requestId,
propertyClient.createStatusHeader(PropertyExchangeStatus.OK), listOf()
)
Expand All @@ -133,9 +121,6 @@ class ClientConnection(
))
}

private fun addPendingRequest(msg: Message.GetPropertyData) {
openRequests.add(msg)
}
private fun addPendingSubscription(requestId: Byte, subscriptionId: String?, propertyId: String) {
val sub = ClientSubscription(
requestId,
Expand Down Expand Up @@ -219,7 +204,7 @@ class ClientConnection(

// unlike the other overload, it is not specific to Common Rules for PE
fun sendGetPropertyData(msg: Message.GetPropertyData) {
addPendingRequest(msg)
openRequests.add(msg)
parent.messenger.send(msg)
}

Expand Down Expand Up @@ -268,22 +253,22 @@ class ClientConnection(
}

fun processGetDataReply(msg: Message.GetPropertyDataReply) {
updateRemoteProperty(msg)

val propertyId = propertyClient.getPropertyIdForHeader(msg.header)
// If the reply was ResourceList, and the parsed body contained an entry for DeviceInfo, and
// if it is configured as auto-queried, then send another Get Property Data request for it.
if (parent.config.autoSendGetDeviceInfo && propertyId == PropertyResourceNames.RESOURCE_LIST) {
val def = propertyClient.getMetadataList()?.firstOrNull { it.propertyId == PropertyResourceNames.DEVICE_INFO } as CommonRulesPropertyMetadata?
if (def != null)
sendGetPropertyData(def.propertyId, def.encodings.firstOrNull())
val req = openRequests.firstOrNull { it.requestId == msg.requestId }
?: return
openRequests.remove(req)
val status = propertyClient.getHeaderFieldInteger(msg.header, PropertyCommonHeaderKeys.STATUS)
?: return
if (status == PropertyExchangeStatus.OK) {
val propertyId = propertyClient.getPropertyIdForHeader(req.header)
properties.updateValue(propertyId, msg)
propertyClient.propertyValueUpdated(propertyId, msg.body)
}
}

fun processSubscribeProperty(msg: Message.SubscribeProperty) {
val reply = when (propertyClient.getHeaderFieldString(msg.header, PropertyCommonHeaderKeys.COMMAND)) {
MidiCISubscriptionCommand.END -> handleUnsubscriptionNotification(parent.muid, msg)
else -> updateLocalProperty(parent.muid, msg)
else -> updatePropertyBySubscribe(msg)
}
if (reply.second != null)
parent.messenger.send(reply.second!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class CommonRulesPropertyClient(private val device: MidiCIDevice, private val co
resourceList.clear()
resourceList.addAll(list)
propertyCatalogUpdated.forEach { it() }

// If the parsed body contained an entry for DeviceInfo, and
// if it is configured as auto-queried, then send another Get Property Data request for it.
if (device.config.autoSendGetDeviceInfo) {
val def = getMetadataList().firstOrNull { it.propertyId == PropertyResourceNames.DEVICE_INFO } as CommonRulesPropertyMetadata?
if (def != null)
conn.sendGetPropertyData(PropertyResourceNames.DEVICE_INFO, def.encodings.firstOrNull())
}
}
// If it is about DeviceInfo, then store the list internally.
PropertyResourceNames.DEVICE_INFO -> {
Expand Down

0 comments on commit 7638650

Please sign in to comment.