ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/framework/dispatcher/DcsResponseBodyEnqueue.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.dispatcher; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import com.baidu.duer.dcs.framework.message.AttachedContentPayload; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import com.baidu.duer.dcs.framework.message.DcsResponseBody; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import com.baidu.duer.dcs.framework.DialogRequestIdHandler; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.framework.message.DialogRequestIdHeader; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import com.baidu.duer.dcs.framework.message.Directive; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import com.baidu.duer.dcs.framework.message.Header; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import com.baidu.duer.dcs.framework.message.Payload; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 26 | |
| TMBOY | 45:2aa9f933c8d2 | 27 | import java.util.HashMap; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import java.util.Iterator; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import java.util.LinkedList; |
| TMBOY | 45:2aa9f933c8d2 | 30 | import java.util.Map; |
| TMBOY | 45:2aa9f933c8d2 | 31 | import java.util.Queue; |
| TMBOY | 45:2aa9f933c8d2 | 32 | |
| TMBOY | 45:2aa9f933c8d2 | 33 | /** |
| TMBOY | 45:2aa9f933c8d2 | 34 | * 带有dialogRequestId response进入dependentQueue队列,否则进入independentQueue |
| TMBOY | 45:2aa9f933c8d2 | 35 | * 带有attached的directive需要知道找到对应的二进制数据才放到上述队列中 |
| TMBOY | 45:2aa9f933c8d2 | 36 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 37 | * Created by wuruisheng on 2017/6/1. |
| TMBOY | 45:2aa9f933c8d2 | 38 | */ |
| TMBOY | 45:2aa9f933c8d2 | 39 | public class DcsResponseBodyEnqueue { |
| TMBOY | 45:2aa9f933c8d2 | 40 | private static final String TAG = DcsResponseBodyEnqueue.class.getSimpleName(); |
| TMBOY | 45:2aa9f933c8d2 | 41 | private final DialogRequestIdHandler dialogRequestIdHandler; |
| TMBOY | 45:2aa9f933c8d2 | 42 | private final Queue<DcsResponseBody> dependentQueue; |
| TMBOY | 45:2aa9f933c8d2 | 43 | private final Queue<DcsResponseBody> independentQueue; |
| TMBOY | 45:2aa9f933c8d2 | 44 | private final Queue<DcsResponseBody> incompleteResponseQueue; |
| TMBOY | 45:2aa9f933c8d2 | 45 | private final Map<String, AudioData> audioDataMap; |
| TMBOY | 45:2aa9f933c8d2 | 46 | |
| TMBOY | 45:2aa9f933c8d2 | 47 | public DcsResponseBodyEnqueue(DialogRequestIdHandler dialogRequestIdHandler, |
| TMBOY | 45:2aa9f933c8d2 | 48 | Queue<DcsResponseBody> dependentQueue, |
| TMBOY | 45:2aa9f933c8d2 | 49 | Queue<DcsResponseBody> independentQueue) { |
| TMBOY | 45:2aa9f933c8d2 | 50 | this.dialogRequestIdHandler = dialogRequestIdHandler; |
| TMBOY | 45:2aa9f933c8d2 | 51 | this.dependentQueue = dependentQueue; |
| TMBOY | 45:2aa9f933c8d2 | 52 | this.independentQueue = independentQueue; |
| TMBOY | 45:2aa9f933c8d2 | 53 | incompleteResponseQueue = new LinkedList<>(); |
| TMBOY | 45:2aa9f933c8d2 | 54 | audioDataMap = new HashMap<>(); |
| TMBOY | 45:2aa9f933c8d2 | 55 | } |
| TMBOY | 45:2aa9f933c8d2 | 56 | |
| TMBOY | 45:2aa9f933c8d2 | 57 | public synchronized void handleResponseBody(DcsResponseBody responseBody) { |
| TMBOY | 45:2aa9f933c8d2 | 58 | incompleteResponseQueue.add(responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 59 | matchAudioDataWithResponseBody(); |
| TMBOY | 45:2aa9f933c8d2 | 60 | } |
| TMBOY | 45:2aa9f933c8d2 | 61 | |
| TMBOY | 45:2aa9f933c8d2 | 62 | public synchronized void handleAudioData(AudioData audioData) { |
| TMBOY | 45:2aa9f933c8d2 | 63 | audioDataMap.put(audioData.contentId, audioData); |
| TMBOY | 45:2aa9f933c8d2 | 64 | matchAudioDataWithResponseBody(); |
| TMBOY | 45:2aa9f933c8d2 | 65 | } |
| TMBOY | 45:2aa9f933c8d2 | 66 | |
| TMBOY | 45:2aa9f933c8d2 | 67 | private void matchAudioDataWithResponseBody() { |
| TMBOY | 45:2aa9f933c8d2 | 68 | for (DcsResponseBody responseBody : incompleteResponseQueue) { |
| TMBOY | 45:2aa9f933c8d2 | 69 | Directive directive = responseBody.getDirective(); |
| TMBOY | 45:2aa9f933c8d2 | 70 | if (directive == null) { |
| TMBOY | 45:2aa9f933c8d2 | 71 | return; |
| TMBOY | 45:2aa9f933c8d2 | 72 | } |
| TMBOY | 45:2aa9f933c8d2 | 73 | |
| TMBOY | 45:2aa9f933c8d2 | 74 | Payload payload = responseBody.getDirective().payload; |
| TMBOY | 45:2aa9f933c8d2 | 75 | if (payload instanceof AttachedContentPayload) { |
| TMBOY | 45:2aa9f933c8d2 | 76 | AttachedContentPayload attachedContentPayload = (AttachedContentPayload) payload; |
| TMBOY | 45:2aa9f933c8d2 | 77 | String contentId = attachedContentPayload.getAttachedContentId(); |
| TMBOY | 45:2aa9f933c8d2 | 78 | AudioData audioData = audioDataMap.remove(contentId); |
| TMBOY | 45:2aa9f933c8d2 | 79 | if (audioData != null) { |
| TMBOY | 45:2aa9f933c8d2 | 80 | attachedContentPayload.setAttachedContent(contentId, audioData.partBytes); |
| TMBOY | 45:2aa9f933c8d2 | 81 | } |
| TMBOY | 45:2aa9f933c8d2 | 82 | } |
| TMBOY | 45:2aa9f933c8d2 | 83 | } |
| TMBOY | 45:2aa9f933c8d2 | 84 | |
| TMBOY | 45:2aa9f933c8d2 | 85 | findCompleteResponseBody(); |
| TMBOY | 45:2aa9f933c8d2 | 86 | } |
| TMBOY | 45:2aa9f933c8d2 | 87 | |
| TMBOY | 45:2aa9f933c8d2 | 88 | private void findCompleteResponseBody() { |
| TMBOY | 45:2aa9f933c8d2 | 89 | Iterator<DcsResponseBody> iterator = incompleteResponseQueue.iterator(); |
| TMBOY | 45:2aa9f933c8d2 | 90 | while (iterator.hasNext()) { |
| TMBOY | 45:2aa9f933c8d2 | 91 | DcsResponseBody responseBody = iterator.next(); |
| TMBOY | 45:2aa9f933c8d2 | 92 | Payload payload = responseBody.getDirective().payload; |
| TMBOY | 45:2aa9f933c8d2 | 93 | if (payload instanceof AttachedContentPayload) { |
| TMBOY | 45:2aa9f933c8d2 | 94 | AttachedContentPayload attachedContentPayload = (AttachedContentPayload) payload; |
| TMBOY | 45:2aa9f933c8d2 | 95 | |
| TMBOY | 45:2aa9f933c8d2 | 96 | if (!attachedContentPayload.requiresAttachedContent()) { |
| TMBOY | 45:2aa9f933c8d2 | 97 | // The front most directive IS complete. |
| TMBOY | 45:2aa9f933c8d2 | 98 | enqueueResponseBody(responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 99 | iterator.remove(); |
| TMBOY | 45:2aa9f933c8d2 | 100 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 101 | break; |
| TMBOY | 45:2aa9f933c8d2 | 102 | } |
| TMBOY | 45:2aa9f933c8d2 | 103 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 104 | // Immediately enqueue any directive which does not contain audio content |
| TMBOY | 45:2aa9f933c8d2 | 105 | enqueueResponseBody(responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 106 | iterator.remove(); |
| TMBOY | 45:2aa9f933c8d2 | 107 | } |
| TMBOY | 45:2aa9f933c8d2 | 108 | } |
| TMBOY | 45:2aa9f933c8d2 | 109 | } |
| TMBOY | 45:2aa9f933c8d2 | 110 | |
| TMBOY | 45:2aa9f933c8d2 | 111 | private void enqueueResponseBody(DcsResponseBody responseBody) { |
| TMBOY | 45:2aa9f933c8d2 | 112 | LogUtil.d(TAG, "DcsResponseBodyEnqueue-RecordThread:" + responseBody.getDirective().rawMessage); |
| TMBOY | 45:2aa9f933c8d2 | 113 | Header header = responseBody.getDirective().header; |
| TMBOY | 45:2aa9f933c8d2 | 114 | DialogRequestIdHeader dialogRequestIdHeader = (DialogRequestIdHeader) header; |
| TMBOY | 45:2aa9f933c8d2 | 115 | if (dialogRequestIdHeader.getDialogRequestId() == null) { |
| TMBOY | 45:2aa9f933c8d2 | 116 | LogUtil.d(TAG, "DcsResponseBodyEnqueue-DialogRequestId is null ,add to independentQueue"); |
| TMBOY | 45:2aa9f933c8d2 | 117 | independentQueue.add(responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 118 | } else if (dialogRequestIdHandler.isActiveDialogRequestId(dialogRequestIdHeader.getDialogRequestId())) { |
| TMBOY | 45:2aa9f933c8d2 | 119 | LogUtil.d(TAG, "DcsResponseBodyEnqueue-DialogRequestId not null,add to dependentQueue"); |
| TMBOY | 45:2aa9f933c8d2 | 120 | dependentQueue.add(responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 121 | } |
| TMBOY | 45:2aa9f933c8d2 | 122 | } |
| TMBOY | 45:2aa9f933c8d2 | 123 | } |
