in src/main/java/com/amazonaws/lex/twilio/sample/streaming/AudioEventsSubscription.java [172:208]
public void run() {
try {
while (true) {
if (stop) {
break;
}
long currentDemand = demand.get();
if (currentDemand > 0) {
// try to read from queue of events.
// if nothing in queue at this point, read as many audio events directly from audio stream
for (long i = 0; i < currentDemand; i++) {
if (eventQueue.peek() != null) {
StartConversationRequestEventStream event = eventQueue.take();
subscriber.onNext(event);
demand.decrementAndGet();
// if this was disconnect event, break this loop to stop sending more events.
// tell the subscriber, we are done
if (event instanceof DisconnectionEvent) {
stop = true;
subscriber.onComplete();
}
}
}
}
}
} catch (InterruptedException e) {
throw new RuntimeException("interrupted when reading data to be sent to server");
} catch (Exception e) {
e.printStackTrace();
}
}