001 /*
002 * File: $HeadURL: https://svn.sourceforge.net/svnroot/jvoicexml/trunk/src/org/jvoicexml/Application.java$
003 * Version: $LastChangedRevision: 2905 $
004 * Date: $Date: 2012-01-24 09:15:03 +0100 (Di, 24 Jan 2012) $
005 * Author: $LastChangedBy: schnelle $
006 *
007 * JVoiceXML - A free VoiceXML implementation.
008 *
009 * Copyright (C) 2010-2012 JVoiceXML group - http://jvoicexml.sourceforge.net
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Library General Public
013 * License as published by the Free Software Foundation; either
014 * version 2 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Library General Public License for more details.
020 *
021 * You should have received a copy of the GNU Library General Public
022 * License along with this library; if not, write to the Free Software
023 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024 *
025 */
026
027 package org.jvoicexml.implementation.external;
028
029 import org.apache.log4j.Logger;
030 import org.jvoicexml.RecognitionResult;
031 import org.jvoicexml.implementation.ExternalRecognitionListener;
032
033 /**
034 * Class to send RecognitionResults as String
035 * to external clients.
036 *
037 * @author Josua Arndt
038 * @author Dirk Schnelle-Walka
039 * @version $Revision: 2905 $
040 * @since 0.7.4
041 */
042 public final class SocketExternalRecognitionListener
043 implements ExternalRecognitionListener {
044 /** Logger instance. */
045 private static final Logger LOGGER =
046 Logger.getLogger(SocketExternalRecognitionListener.class);
047
048 /** the port to be listening on. */
049 private int port;
050
051 /** internal Thread handling all connections. */
052 private SocketExternalListenerWorker worker = null;
053
054 /** "recognition"
055 * - The workerthread will use this string to mark log messages. */
056 private static final String ASSIGNMENT = "recognition";
057
058 /** Status of this listener. */
059 private boolean running = false;
060
061 /**
062 * Constructs a new SocketExternalRecognitionListener.
063 */
064 public SocketExternalRecognitionListener() {
065 }
066
067 /**
068 * Set the port to be used.
069 *
070 * @param portnumber
071 * used port
072 */
073 public void setPort(final int portnumber) {
074 port = portnumber;
075 }
076
077 /**
078 * Post a message to all connected Clients via this class's Worker.
079 * @param msg
080 * the textmessage to be sent
081 * @since 0.7.5
082 */
083 private void postMessage(final String msg) {
084 worker.postMessage(msg);
085 }
086
087 /**
088 * {@inheritDoc}
089 * Sends recognized result text as string.
090 */
091 @Override
092 public void resultAccepted(final RecognitionResult result) {
093 final String[] textArry = result.getWords();
094 final StringBuffer text = new StringBuffer();
095
096 for (int i = 0; i < textArry.length; i++) {
097 text.append(textArry[i].toString());
098 text.append(" ");
099 }
100
101 if (running) {
102 if (LOGGER.isDebugEnabled()) {
103 LOGGER.debug("Recognition Listener sent Message:"
104 + text.toString() + "...");
105 }
106 postMessage("Recognized: '" + text.toString() + "'.");
107 }
108 }
109
110 /**
111 * {@inheritDoc}
112 * Sends "Recognition Listener: result rejected." text as String.
113 */
114 @Override
115 public void resultRejected(final RecognitionResult result) {
116 final String msg = "Recognition Listener: result rejected.";
117 if (running) {
118 if (LOGGER.isDebugEnabled()) {
119 LOGGER.debug(msg);
120 }
121 postMessage(msg);
122 }
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 public void start() {
129 //if the workerthread has not been stopped, stop it
130 if (worker != null) {
131 worker.stopWorker();
132 }
133
134 worker = new SocketExternalListenerWorker(port, ASSIGNMENT);
135 worker.start();
136 running = true;
137
138 if (LOGGER.isInfoEnabled()) {
139 LOGGER.info("started socket external recognition listener at port '"
140 + port + "...");
141 }
142 }
143
144 /**
145 * {@inheritDoc}
146 */
147 public void stop() {
148 running = false;
149 if (worker != null) {
150 worker.stopWorker();
151 worker = null;
152 }
153 if (LOGGER.isInfoEnabled()) {
154 LOGGER.info("...stopped socket external recognition listener");
155 }
156 }
157 }