|
| 1 | +/** |
| 2 | + * Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package com.ibm.watson.developer_cloud.speech_to_text.v1.websocket; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import com.google.gson.Gson; |
| 21 | +import com.google.gson.JsonObject; |
| 22 | +import com.google.gson.JsonParser; |
| 23 | +import com.ibm.watson.developer_cloud.http.HttpHeaders; |
| 24 | +import com.ibm.watson.developer_cloud.speech_to_text.v1.RecognizeOptions; |
| 25 | + |
| 26 | +import okhttp3.OkHttpClient; |
| 27 | +import okhttp3.Request; |
| 28 | +import okhttp3.RequestBody; |
| 29 | +import okhttp3.Response; |
| 30 | +import okhttp3.ResponseBody; |
| 31 | +import okhttp3.ws.WebSocket; |
| 32 | +import okhttp3.ws.WebSocketCall; |
| 33 | +import okhttp3.ws.WebSocketListener; |
| 34 | +import okio.Buffer; |
| 35 | + |
| 36 | +/** |
| 37 | + * WebSocket manager |
| 38 | + */ |
| 39 | +public class WebSocketManager implements WebSocketListener { |
| 40 | + |
| 41 | + private static final String MODEL = "model"; |
| 42 | + private static final String START = "start"; |
| 43 | + private static final String STOP = "stop"; |
| 44 | + private static final String ACTION = "action"; |
| 45 | + |
| 46 | + private String url; |
| 47 | + private OkHttpClient client; |
| 48 | + private ResponseListener listener; |
| 49 | + |
| 50 | + private volatile boolean connecting; |
| 51 | + private volatile boolean connected; |
| 52 | + |
| 53 | + private static final int FOUR_KB = 4096; |
| 54 | + |
| 55 | + /** The socket. */ |
| 56 | + public WebSocket socket; |
| 57 | + |
| 58 | + private String token; |
| 59 | + |
| 60 | + /** |
| 61 | + * Instantiates a new web socket manager. |
| 62 | + * |
| 63 | + * @param url the url |
| 64 | + * @param client the client |
| 65 | + * @param token the token |
| 66 | + * @param listener the listener |
| 67 | + */ |
| 68 | + public WebSocketManager(String url, OkHttpClient client, String token, ResponseListener listener) { |
| 69 | + this.url = url; |
| 70 | + this.client = client; |
| 71 | + this.token = token; |
| 72 | + this.listener = listener; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Connect. |
| 77 | + * |
| 78 | + * @param options the options |
| 79 | + */ |
| 80 | + public void connect(RecognizeOptions options) { |
| 81 | + if(connecting != true) { |
| 82 | + connecting = true; |
| 83 | + |
| 84 | + String speechModel = options.getModel() != null ? "?model=" + options.getModel() : ""; |
| 85 | + Request connectionRequest = new Request.Builder() |
| 86 | + .url(url + speechModel) |
| 87 | + .addHeader(HttpHeaders.X_WATSON_AUTHORIZATION_TOKEN, token) |
| 88 | + .build(); |
| 89 | + WebSocketCall.create(client, connectionRequest).enqueue(this); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Force close. |
| 95 | + * |
| 96 | + * @throws IOException Signals that an I/O exception has occurred. |
| 97 | + */ |
| 98 | + public void forceClose() throws IOException { |
| 99 | + socket.close(1000, "User forced socket close"); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Send input steam. |
| 104 | + * |
| 105 | + * @param inputStream the input stream |
| 106 | + */ |
| 107 | + public void sendInputSteam(InputStream inputStream) { |
| 108 | + if (!connected || socket == null) |
| 109 | + throw new IllegalStateException("WebSocket not connected; call connect()"); |
| 110 | + |
| 111 | + try { |
| 112 | + byte[] buffer = new byte[FOUR_KB]; |
| 113 | + int read; |
| 114 | + while ((read = inputStream.read(buffer)) > 0) { |
| 115 | + if (read == FOUR_KB) |
| 116 | + socket.sendMessage(RequestBody.create(WebSocket.BINARY, buffer)); |
| 117 | + else |
| 118 | + socket.sendMessage(RequestBody.create(WebSocket.BINARY, Arrays.copyOfRange(buffer, 0, read))); |
| 119 | + |
| 120 | + Thread.sleep(20); |
| 121 | + } |
| 122 | + inputStream.close(); |
| 123 | + } catch (Exception e) { |
| 124 | + throw new RuntimeException(e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Recognize. |
| 130 | + * |
| 131 | + * @param stream the stream |
| 132 | + * @param options the options |
| 133 | + */ |
| 134 | + public void recognize(InputStream stream, RecognizeOptions options) { |
| 135 | + if (connected != true) |
| 136 | + connect(options); |
| 137 | + |
| 138 | + try { |
| 139 | + socket.sendMessage(RequestBody.create(WebSocket.TEXT, buildStartMessage(options).body().toString())); |
| 140 | + sendInputSteam(stream); |
| 141 | + socket.sendMessage(RequestBody.create(WebSocket.TEXT, buildStopMessage().body().toString())); |
| 142 | + } catch (IOException e) { |
| 143 | + e.printStackTrace(); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Builds the start message. |
| 151 | + * |
| 152 | + * @param options the options |
| 153 | + * @return the request |
| 154 | + */ |
| 155 | + public Request buildStartMessage(RecognizeOptions options) { |
| 156 | + JsonObject startMessage = new JsonParser().parse(new Gson().toJson(options)).getAsJsonObject(); |
| 157 | + startMessage.remove(MODEL); |
| 158 | + startMessage.addProperty(ACTION, START); |
| 159 | + return new Request.Builder().post(RequestBody.create(WebSocket.TEXT, startMessage.toString())).build(); |
| 160 | + } |
| 161 | + |
| 162 | + private Request buildStopMessage() { |
| 163 | + JsonObject stopMessage = new JsonObject(); |
| 164 | + stopMessage.addProperty(ACTION, STOP); |
| 165 | + return new Request.Builder().post(RequestBody.create(WebSocket.TEXT, stopMessage.toString())).build(); |
| 166 | + } |
| 167 | + |
| 168 | + /* (non-Javadoc) |
| 169 | + * @see okhttp3.ws.WebSocketListener#onOpen(okhttp3.ws.WebSocket, okhttp3.Response) |
| 170 | + */ |
| 171 | + @Override |
| 172 | + public void onOpen(WebSocket webSocket, Response response) { |
| 173 | + socket = webSocket; |
| 174 | + connecting = false; |
| 175 | + connected = true; |
| 176 | + } |
| 177 | + |
| 178 | + /* (non-Javadoc) |
| 179 | + * @see okhttp3.ws.WebSocketListener#onFailure(java.io.IOException, okhttp3.Response) |
| 180 | + */ |
| 181 | + @Override |
| 182 | + public void onFailure(IOException e, Response response) { |
| 183 | + connecting = false; |
| 184 | + connected = false; |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + /* (non-Javadoc) |
| 189 | + * @see okhttp3.ws.WebSocketListener#onMessage(okhttp3.ResponseBody) |
| 190 | + */ |
| 191 | + @Override |
| 192 | + public void onMessage(ResponseBody responseBody) throws IOException { |
| 193 | + listener.onResponse(responseBody.string()); |
| 194 | + } |
| 195 | + |
| 196 | + /* (non-Javadoc) |
| 197 | + * @see okhttp3.ws.WebSocketListener#onPong(okio.Buffer) |
| 198 | + */ |
| 199 | + @Override |
| 200 | + public void onPong(Buffer buffer) { |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + /* (non-Javadoc) |
| 205 | + * @see okhttp3.ws.WebSocketListener#onClose(int, java.lang.String) |
| 206 | + */ |
| 207 | + @Override |
| 208 | + public void onClose(int i, String s) { |
| 209 | + connecting = false; |
| 210 | + connected = false; |
| 211 | + } |
| 212 | + |
| 213 | +} |