Ejemplo n.º 1
0
        /// <summary>
        ///
        ///
        /// </summary>
        public void talkToClient()
        {
            while (true)
            {
                NetworkStream networkStream = new NetworkStream(client);
                StreamWriter  streamWriter  = new StreamWriter(networkStream);
                StreamReader  streamReader  = new StreamReader(networkStream);

                // blocking call to wait for input
                string line = streamReader.ReadLine();

                String shortMessage;
                if (line.Length > 20)
                {
                    shortMessage = line.Substring(0, 20) + "...";
                }
                else
                {
                    shortMessage = line;
                }
                log("Read from Client " + id + ": " + shortMessage);

                // get the command from the client
                Match match = Regex.Match(line, @"\[\[(.*?)\]\].*");
                if (match.Success)
                {
                    String command = match.Groups[1].ToString().ToLower();
                    log("Command: " + command);

                    switch (command)
                    {
                    case "exit":
                        log("Disconnecting from Client " + id + ".");
                        client.Close();
                        return;

                    case "quitserver":
                        log("Disconnecting from Client " + id + ".");
                        client.Close();
                        log("Exiting the Server.");
                        gui.exit();
                        return;

                    case "topten":
                        // return nothing if there was no last call...
                        if (alternatives == null)
                        {
                            // do nothing
                        }
                        else
                        {
                            // return the top ten list of the last call...
                            log("Here is the complete list of alternates: ");
                            if (alternatives != null)
                            {
                                for (int i = 0; i < alternatives.Count; i++)
                                {
                                    log(alternatives[i].ToString() + "  [" + alternatives[i].Confidence + "]");
                                    streamWriter.WriteLine(alternatives[i].ToString());
                                }
                                streamWriter.WriteLine("[[endofalternatives]]");
                                streamWriter.Flush();
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    // assume it's just some XML
                    log("XML data received...");
                    Recognizer rec     = new Recognizer();
                    Strokes    strokes = rec.getStrokesFromXML(line);
                    alternatives = null;
                    String topResult = rec.recognize(strokes, out alternatives);
                    log("The top result is: " + topResult);

                    if (topResult != null)
                    {
                        // respond with some ASCII
                        streamWriter.WriteLine(topResult);
                        streamWriter.Flush();
                    }
                    else
                    {
                        log("Invalid Input Data");
                    }
                }
            }
        }