ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/framework/DcsResponseDispatcher.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.framework; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import com.baidu.duer.dcs.framework.dispatcher.AudioData; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import com.baidu.duer.dcs.framework.dispatcher.DcsResponseBodyEnqueue; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import com.baidu.duer.dcs.framework.dispatcher.WithDialogIdBlockThread; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.framework.dispatcher.WithoutDialogIdBlockThread; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import com.baidu.duer.dcs.framework.message.DcsResponseBody; |
| TMBOY | 45:2aa9f933c8d2 | 23 | |
| TMBOY | 45:2aa9f933c8d2 | 24 | import java.util.concurrent.BlockingQueue; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import java.util.concurrent.LinkedBlockingDeque; |
| TMBOY | 45:2aa9f933c8d2 | 26 | |
| TMBOY | 45:2aa9f933c8d2 | 27 | /** |
| TMBOY | 45:2aa9f933c8d2 | 28 | * response调度中心,带有dialogRequestId directives按照顺序执行,对于speak指令播报完才执行下一个指令; |
| TMBOY | 45:2aa9f933c8d2 | 29 | * 设备端收到不带dialogRequestId的directives立即执行 |
| TMBOY | 45:2aa9f933c8d2 | 30 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 31 | * Created by wuruisheng on 2017/6/1. |
| TMBOY | 45:2aa9f933c8d2 | 32 | */ |
| TMBOY | 45:2aa9f933c8d2 | 33 | public class DcsResponseDispatcher { |
| TMBOY | 45:2aa9f933c8d2 | 34 | private final WithDialogIdBlockThread withDialogIdBlockThread; |
| TMBOY | 45:2aa9f933c8d2 | 35 | private final WithoutDialogIdBlockThread withoutDialogIdBlockThread; |
| TMBOY | 45:2aa9f933c8d2 | 36 | private final BlockingQueue<DcsResponseBody> dependentQueue; |
| TMBOY | 45:2aa9f933c8d2 | 37 | private final BlockingQueue<DcsResponseBody> independentQueue; |
| TMBOY | 45:2aa9f933c8d2 | 38 | private final DcsResponseBodyEnqueue dcsResponseBodyEnqueue; |
| TMBOY | 45:2aa9f933c8d2 | 39 | private final IDcsResponseHandler responseHandler; |
| TMBOY | 45:2aa9f933c8d2 | 40 | |
| TMBOY | 45:2aa9f933c8d2 | 41 | public DcsResponseDispatcher(final DialogRequestIdHandler dialogRequestIdHandler, |
| TMBOY | 45:2aa9f933c8d2 | 42 | final IDcsResponseHandler responseHandler) { |
| TMBOY | 45:2aa9f933c8d2 | 43 | this.responseHandler = responseHandler; |
| TMBOY | 45:2aa9f933c8d2 | 44 | dependentQueue = new LinkedBlockingDeque<>(); |
| TMBOY | 45:2aa9f933c8d2 | 45 | independentQueue = new LinkedBlockingDeque<>(); |
| TMBOY | 45:2aa9f933c8d2 | 46 | dcsResponseBodyEnqueue = new DcsResponseBodyEnqueue(dialogRequestIdHandler, dependentQueue, |
| TMBOY | 45:2aa9f933c8d2 | 47 | independentQueue); |
| TMBOY | 45:2aa9f933c8d2 | 48 | |
| TMBOY | 45:2aa9f933c8d2 | 49 | withDialogIdBlockThread = new WithDialogIdBlockThread(dependentQueue, responseHandler, |
| TMBOY | 45:2aa9f933c8d2 | 50 | "withDialogIdBlockThread"); |
| TMBOY | 45:2aa9f933c8d2 | 51 | withoutDialogIdBlockThread = new WithoutDialogIdBlockThread(independentQueue, responseHandler, |
| TMBOY | 45:2aa9f933c8d2 | 52 | "withoutDialogIdBlockThread"); |
| TMBOY | 45:2aa9f933c8d2 | 53 | withDialogIdBlockThread.start(); |
| TMBOY | 45:2aa9f933c8d2 | 54 | withoutDialogIdBlockThread.start(); |
| TMBOY | 45:2aa9f933c8d2 | 55 | } |
| TMBOY | 45:2aa9f933c8d2 | 56 | |
| TMBOY | 45:2aa9f933c8d2 | 57 | public void interruptDispatch() { |
| TMBOY | 45:2aa9f933c8d2 | 58 | // 先清空队列,比如播放一首歌:speak+play指令组合的方式,在speak播报过程中进行打断,play就不需要执行了 |
| TMBOY | 45:2aa9f933c8d2 | 59 | withDialogIdBlockThread.clear(); |
| TMBOY | 45:2aa9f933c8d2 | 60 | // 让其处于等待新的指令处理 |
| TMBOY | 45:2aa9f933c8d2 | 61 | unBlockDependentQueue(); |
| TMBOY | 45:2aa9f933c8d2 | 62 | } |
| TMBOY | 45:2aa9f933c8d2 | 63 | |
| TMBOY | 45:2aa9f933c8d2 | 64 | public void blockDependentQueue() { |
| TMBOY | 45:2aa9f933c8d2 | 65 | withDialogIdBlockThread.block(); |
| TMBOY | 45:2aa9f933c8d2 | 66 | } |
| TMBOY | 45:2aa9f933c8d2 | 67 | |
| TMBOY | 45:2aa9f933c8d2 | 68 | public void unBlockDependentQueue() { |
| TMBOY | 45:2aa9f933c8d2 | 69 | withDialogIdBlockThread.unblock(); |
| TMBOY | 45:2aa9f933c8d2 | 70 | } |
| TMBOY | 45:2aa9f933c8d2 | 71 | |
| TMBOY | 45:2aa9f933c8d2 | 72 | public void onResponseBody(DcsResponseBody responseBody) { |
| TMBOY | 45:2aa9f933c8d2 | 73 | dcsResponseBodyEnqueue.handleResponseBody(responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 74 | } |
| TMBOY | 45:2aa9f933c8d2 | 75 | |
| TMBOY | 45:2aa9f933c8d2 | 76 | public void onAudioData(AudioData audioData) { |
| TMBOY | 45:2aa9f933c8d2 | 77 | dcsResponseBodyEnqueue.handleAudioData(audioData); |
| TMBOY | 45:2aa9f933c8d2 | 78 | } |
| TMBOY | 45:2aa9f933c8d2 | 79 | |
| TMBOY | 45:2aa9f933c8d2 | 80 | public void onParseFailed(String unParseMessage) { |
| TMBOY | 45:2aa9f933c8d2 | 81 | if (responseHandler != null) { |
| TMBOY | 45:2aa9f933c8d2 | 82 | responseHandler.onParseFailed(unParseMessage); |
| TMBOY | 45:2aa9f933c8d2 | 83 | } |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | |
| TMBOY | 45:2aa9f933c8d2 | 86 | public void release() { |
| TMBOY | 45:2aa9f933c8d2 | 87 | withDialogIdBlockThread.stopThread(); |
| TMBOY | 45:2aa9f933c8d2 | 88 | withoutDialogIdBlockThread.stopThread(); |
| TMBOY | 45:2aa9f933c8d2 | 89 | } |
| TMBOY | 45:2aa9f933c8d2 | 90 | |
| TMBOY | 45:2aa9f933c8d2 | 91 | public interface IDcsResponseHandler { |
| TMBOY | 45:2aa9f933c8d2 | 92 | void onResponse(DcsResponseBody responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 93 | |
| TMBOY | 45:2aa9f933c8d2 | 94 | void onParseFailed(String unParseMessage); |
| TMBOY | 45:2aa9f933c8d2 | 95 | } |
| TMBOY | 45:2aa9f933c8d2 | 96 | } |
