-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable command passing from clients to content apps (#19506)
- Loading branch information
Showing
18 changed files
with
270 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...android/App/platform-app/src/main/java/com/matter/tv/server/service/ResponseRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.matter.tv.server.service; | ||
|
||
import android.util.Log; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class ResponseRegistry { | ||
|
||
private static final String TAG = "ResponseRegistry"; | ||
|
||
private AtomicInteger messageCounter = new AtomicInteger(); | ||
|
||
private Map<Integer, String> responses = new ConcurrentHashMap<>(); | ||
|
||
private Map<Integer, CountDownLatch> latches = new ConcurrentHashMap<>(); | ||
|
||
public int getNextMessageCounter() { | ||
int counter = messageCounter.incrementAndGet(); | ||
// MAX_VALUE used for error scenarios | ||
if (counter == Integer.MAX_VALUE) { | ||
counter = messageCounter.incrementAndGet(); | ||
} | ||
latches.put(counter, new CountDownLatch(1)); | ||
return counter; | ||
} | ||
|
||
public void waitForMessage(int counter, long timeout, TimeUnit unit) { | ||
CountDownLatch latch = latches.get(counter); | ||
if (latch == null) { | ||
return; | ||
} | ||
try { | ||
if (!latch.await(timeout, unit)) { | ||
Log.i(TAG, "Timed out while waiting for response for message " + counter); | ||
} | ||
} catch (InterruptedException e) { | ||
Log.i(TAG, "Interrupted while waiting for response for message " + counter); | ||
} | ||
} | ||
|
||
public void receivedMessageResponse(int counter, String response) { | ||
CountDownLatch latch = latches.remove(counter); | ||
if (latch == null) { | ||
// no point adding response to memory if no one is going to read it. | ||
return; | ||
} | ||
responses.put(counter, response); | ||
latch.countDown(); | ||
} | ||
|
||
public String readAndRemoveResponse(int counter) { | ||
// caller should manage null values | ||
return responses.remove(counter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.