private static bool SpeechkitInitialize()
        {
            try
            {
                _speechKit = SpeechKit.initialize(
                    NuanceServiceInfo.SpeechKitAppId,
                    NuanceServiceInfo.SpeechKitServer,
                    NuanceServiceInfo.SpeechKitPort,
                    NuanceServiceInfo.SpeechKitSsl,
                    NuanceServiceInfo.SpeechKitApplicationKey);
            }
            catch (Exception ex)
            {
                TraceHelper.AddMessage("Exception in SpeechKitInitialize: " + ex.Message);
                return(false);
            }

            _beep = _speechKit.defineAudioPrompt("beep.wav");
            _speechKit.setDefaultRecognizerPrompts(_beep, null, null, null);
            _speechKit.connect();
            Thread.Sleep(10); // to guarantee the time to load prompt resource

            return(true);
        }
        public static void Stop(SpeechHelper.SpeechToTextCallbackDelegate del)
        {
            // get the last chunk of speech
            int len = mic.GetData(speechBuffer);

            // stop listening
            mic.Stop();

            // remove the mic eventhandler
            mic.BufferReady            -= MicBufferReady;
            initializedBufferReadyEvent = false;

            // trace the operation
            TraceHelper.AddMessage(String.Format("Final Frame: {0} bytes of speech", len));

            // create a properly sized copy of the last buffer
            byte[] speechChunk = new byte[len];
            Array.Copy(speechBuffer, speechChunk, len);

            // add the last speech buffer to the folder
            speechBufferList.Add(speechChunk);

            // if the encode flag is set, encode the chunk before sending it
            if (encode)
            {
                // do this on a background thread because it is CPU-intensive
                ThreadPool.QueueUserWorkItem(delegate
                {
                    // create a new mutex object for this frame
                    AutoResetEvent bufferMutex = new AutoResetEvent(false);
                    bufferMutexList.Add(bufferMutex);

                    // encode the frame
                    TraceHelper.AddMessage(String.Format("Final Frame: About to encode speech"));
                    byte[] encodedBuf = EncodeSpeech(speechChunk, speechChunk.Length);
                    TraceHelper.AddMessage(String.Format("Final Frame: Encoded down to {0} bytes", encodedBuf.Length));

                    // wait until the previous frame has been sent
                    int frameIndex = bufferMutexList.Count - 1;
                    if (frameIndex > 0)
                    {
                        bufferMutexList[frameIndex - 1].WaitOne();
                    }

                    // send the last frame and retrieve the response
                    TraceHelper.AddMessage(String.Format("Sending Final Frame: {0} bytes", encodedBuf.Length));
                    NetworkHelper.EndSpeech(encodedBuf, encodedBuf.Length, del, new NetworkDelegate(NetworkCallback));

                    // repeat the sentence back to the user
                    PlaybackSpeech();
                });
            }
            else
            {
                // send the operation immediately
                TraceHelper.AddMessage(String.Format("Sending Final Frame: {0} bytes", speechChunk.Length));
                NetworkHelper.EndSpeech(speechChunk, speechChunk.Length, del, new NetworkDelegate(NetworkCallback));

                // play back the speech immediately
                PlaybackSpeech();
            }
        }
        private static void MicBufferReady(object sender, EventArgs e)
        {
            // get the data from the mic
            int len = mic.GetData(speechBuffer);

            numBytes += len;

            // make a copy of the frame index and then increment it
            int frameIndex = frameCounter++;

            // trace the operation
            TraceHelper.AddMessage(String.Format("Frame {0}: {1} bytes of speech", frameIndex, len));

            // create a properly sized copy of the speech buffer
            byte[] speechChunk = new byte[len];
            Array.Copy(speechBuffer, speechChunk, len);

            // add the chunk to the buffer folder
            speechBufferList.Add(speechChunk);

            // send the chunk to the service
            try
            {
                // if the encode flag is set, encode the chunk before sending it
                if (encode)
                {
                    // do this on a background thread because it is CPU-intensive
                    ThreadPool.QueueUserWorkItem(delegate
                    {
                        // create a new mutex object for this frame
                        AutoResetEvent bufferMutex = new AutoResetEvent(false);
                        bufferMutexList.Add(bufferMutex);

                        // encode the frame
                        TraceHelper.AddMessage(String.Format("Frame {0}: About to encode speech", frameIndex));
                        byte[] encodedBuf = EncodeSpeech(speechChunk, speechChunk.Length);
                        TraceHelper.AddMessage(String.Format("Frame {0}: Encoded down to {1} bytes", frameIndex, encodedBuf.Length));

                        // wait until the previous frame has been sent
                        if (frameIndex > 0)
                        {
                            bufferMutexList[frameIndex - 1].WaitOne();
                        }

                        // send the frame
                        TraceHelper.AddMessage(String.Format("Sending Frame {0}: {1} bytes", frameIndex, encodedBuf.Length));
                        NetworkHelper.SendSpeech(encodedBuf, encodedBuf.Length, null, new NetworkDelegate(NetworkCallback));

                        // set the current frame's mutex
                        bufferMutex.Set();
                    });
                }
                else
                {
                    // just send the frame
                    TraceHelper.AddMessage(String.Format("Sending Frame {0}: {1} bytes", frameIndex, speechChunk.Length));
                    NetworkHelper.SendSpeech(speechChunk, speechChunk.Length, null, new NetworkDelegate(NetworkCallback));
                }
            }
            catch (Exception ex)
            {
                // stop listening
                mic.Stop();

                // remove the mic eventhandler
                mic.BufferReady            -= MicBufferReady;
                initializedBufferReadyEvent = false;

                // trace the exception
                TraceHelper.AddMessage(String.Format("Mic buffer ready: ex: {0}", ex.Message));

                speechOperationInProgress = false;
                return;
            }

#if DEBUG
            // signal the caller that the chunk has been sent
            speechStateDelegate.DynamicInvoke(SpeechHelper.SpeechState.Listening, numBytes.ToString());
#endif
        }
Exemple #4
0
        /// <summary>
        /// This function prepares the request queue for a "Connect with Existing Account" operation
        /// If the queue only contains default items (because the user hasn't made any changes), remove the
        /// default objects so that we don't create duplicates on the server
        ///
        /// Otherwise, change the names of all the "local" folders to include " (Phone)" so the user can
        /// distinguish these folders from the ones that they already have in their existing account
        /// Also, remove any operations that have to do with the ClientSettings folder so that we don't have a duplicate
        /// </summary>
        public static void PrepareUserQueueForAccountConnect()
        {
            bool   deleteQueue = true;
            string queueName   = RequestQueue.UserQueue;
            var    requests    = GetAllRequestRecords(queueName);

            if (requests != null)
            {
                foreach (var r in requests)
                {
                    if (r.IsDefaultObject == false)
                    {
                        deleteQueue = false;
                        break;
                    }
                }
            }

            if (deleteQueue)
            {
                DeleteQueue(queueName);
                return;
            }

            // append (Phone) to all user folders
            foreach (var req in requests)
            {
                if (req.BodyTypeName == typeof(Folder).Name && req.ReqType == RequestRecord.RequestType.Insert)
                {
                    req.DeserializeBody();
                    var folder = req.Body as Folder;
                    if (folder != null)
                    {
                        folder.Name += " (Phone)";
                        req.SerializeBody();
                    }
                }
            }

            DataContractJsonSerializer dc = new DataContractJsonSerializer(requests.GetType());

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                lock (fileLocks[queueName])
                {
                    // try block because the using block below will throw if the file doesn't exist
                    try
                    {
                        // if the file opens, read the contents
                        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(QueueFilename(queueName), FileMode.Truncate, file))
                        {
                            try
                            {
                                dc.WriteObject(stream, requests);
                            }
                            catch (Exception ex)
                            {
                                TraceHelper.AddMessage("PrepareQueueForAccountConnect: Error rewriting request queue file; ex: " + ex.Message);
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // could not open the isolated storage file
                        TraceHelper.AddMessage("PrepareQueueForAccountConnect: Error opening request queue file; ex: " + ex.Message);
                        return;
                    }
                }
            }
        }
Exemple #5
0
        private static void SendData(byte[] buffer, int len, EventHandler <SocketAsyncEventArgs> eh, Delegate netOpInProgressDel)
        {
            // a request must be in progress
            if (isRequestInProgress == false)
            {
                return;
            }

            SocketAsyncEventArgs socketSendEventArg = new SocketAsyncEventArgs();

            socketSendEventArg.RemoteEndPoint = endPoint;
            if (eh != null)
            {
                socketSendEventArg.Completed += eh;
            }

            byte[] sendbuf = null;

            // if the buffer is null, it means that we need to send the terminating chunk
            if (buffer == null)
            {
                sendbuf = Encoding.UTF8.GetBytes("0\r\n\r\n");
            }
            else
            {
                // if the length was passed in as zero, compute it from the buffer length
                if (len == 0)
                {
                    len = buffer.Length;
                }

                // if the length is positive (it'll never be zero), we need to prefix the
                // length (expressed in hex) to the buffer.  A length of -1 signals to send
                // the buffer as-is (this is for sending the headers, which need no length)
                if (len > -1)
                {
                    sendbuf = CreateBuffer(buffer, len);
                }
                else
                {
                    sendbuf = buffer;
                }
            }

            // send the buffer
            try
            {
                // set the buffer and send the chunk asynchronously
                socketSendEventArg.SetBuffer(sendbuf, 0, sendbuf.Length);
                bool ret = socket.SendAsync(socketSendEventArg);
                if (ret == false)
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();
                }
            }
            catch (Exception ex)
            {
                // trace network error
                TraceHelper.AddMessage("SendData: ex: " + ex.Message);

                // signal that a network operation is done and unsuccessful
                netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                // clean up the socket
                CleanupSocket();
            }
        }
Exemple #6
0
        private static void ProcessNetworkResponse(Delegate del, Delegate netOpInProgressDel)
        {
            if (isRequestInProgress == false)
            {
                return;
            }

            SocketAsyncEventArgs socketReceiveEventArg = new SocketAsyncEventArgs();

            socketReceiveEventArg.RemoteEndPoint = endPoint;
            socketReceiveEventArg.Completed     += new EventHandler <SocketAsyncEventArgs>(delegate(object o, SocketAsyncEventArgs e)
            {
                if (e.SocketError != SocketError.Success)
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();

                    return;
                }

                // response was received
                int num = e.BytesTransferred;
                if (num > 0)
                {
                    // get the response as a string
                    string resp = Encoding.UTF8.GetString(e.Buffer, 0, num);

                    string[] lines = resp.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                    if (lines == null || lines.Length < 2 ||
                        lines[0] != "HTTP/1.1 200 OK")
                    {
                        // signal that a network operation is done and unsuccessful
                        netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                        // clean up the socket
                        CleanupSocket();
                    }

                    // signal that a network operation is done and successful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Success);

                    // discover the content type (default to json)
                    string contentType = "application/json";
                    foreach (var line in lines)
                    {
                        if (line.StartsWith("Content-Type:"))
                        {
                            string compositeContentType = line.Split(':')[1];
                            contentType = compositeContentType.Split(';')[0].Trim();
                            break;
                        }
                    }

                    // get a stream over the last component of the network response
                    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(lines[lines.Length - 1]));

                    // deserialize the response string
                    HttpMessageBodyWrapper <string> body =
                        (HttpMessageBodyWrapper <string>)HttpWebResponseWrapper <string> .DeserializeResponseBody(
                            stream, contentType, typeof(HttpMessageBodyWrapper <string>));

                    // signal that a network operation is done and successful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Success);

                    // reset the request in progress flag
                    isRequestInProgress = false;

                    // * leave the socket open for a potential next transaction *
                    // CleanupSocket();

                    // invoke the delegate passed in with the actual response text to return to the caller
                    del.DynamicInvoke(body == null ? "" : body.Value);
                }
                else
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();
                }
            });

            // set the receive buffer
            byte[] buffer = new byte[32768];
            socketReceiveEventArg.SetBuffer(buffer, 0, buffer.Length);

            // receive the response
            try
            {
                bool ret = socket.ReceiveAsync(socketReceiveEventArg);
                if (ret == false)
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();
                }
            }
            catch (Exception ex)
            {
                // trace network error
                TraceHelper.AddMessage("ProcessNetworkResponse: ex: " + ex.Message);

                // signal that a network operation is done and unsuccessful
                netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                // clean up the socket
                CleanupSocket();
            }
        }
Exemple #7
0
        private static void InvokeNetworkRequest(Delegate del, Delegate netOpInProgressDel)
        {
            // this code is non-reentrant
            if (isRequestInProgress == true)
            {
                return;
            }

            // set the request in progress flag
            isRequestInProgress = true;

            // signal that a network operation is starting
            netOpInProgressDel.DynamicInvoke(true, OperationStatus.Started);

            // get a Uri for the service - this will be used to decode the host / port
            Uri uri = new Uri(SpeechUrl);

            // create the socket
            if (socket == null)
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                if (uri.Host == "localhost")
                {
                    endPoint = new IPEndPoint(IPAddress.Loopback, uri.Port);
                }
                else
                {
                    endPoint = new DnsEndPoint(uri.Host, uri.Port, AddressFamily.InterNetwork);
                }
            }

            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

            socketEventArg.RemoteEndPoint = endPoint;

            // set the connect completion delegate
            socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(delegate(object o, SocketAsyncEventArgs e)
            {
                if (e.SocketError != SocketError.Success)
                {
                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();

                    return;
                }

                // get the current network interface info
                NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface();

                // invoke the completion delegate with the network type info
                del.DynamicInvoke(netInterfaceInfo);
            });

            // if the socket isn't connected, connect now
            if (socket.Connected == false)
            {
                // connect to the service
                try
                {
                    bool ret = socket.ConnectAsync(socketEventArg);
                    if (ret == false)
                    {
                        // signal that a network operation is done and unsuccessful
                        netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                        // clean up the socket
                        CleanupSocket();
                    }
                }
                catch (Exception ex)
                {
                    // trace network error
                    TraceHelper.AddMessage("InvokeNetworkRequest: ex: " + ex.Message);

                    // signal that a network operation is done and unsuccessful
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);

                    // clean up the socket
                    CleanupSocket();
                }
            }
            else
            {
                // socket already connected

                // get the current network interface info
                NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface();

                // invoke the completion delegate with the network type info
                del.DynamicInvoke(netInterfaceInfo);
            }
        }
Exemple #8
0
        // Common code to process the response from any web service call.  This is invoked from the callback
        // method for the web service, and passed a Type for deserializing the response body.
        // This method will also invoke the delegate with the result of the Web Service call
        private static void ProcessResponse <T>(IAsyncResult result)
        {
            WebServiceState state = result.AsyncState as WebServiceState;

            if (state == null)
            {
                return;
            }

            // get the network operation status delegate
            Delegate netOpInProgressDel = state.NetworkOperationInProgressDelegate as Delegate;

            // get the web response and make sure it's not null (failed)
            HttpWebResponseWrapper <T> resp = GetWebResponse <T>(result);

            if (resp == null)
            {
                // signal that the network operation completed unsuccessfully
                if (netOpInProgressDel != null)
                {
                    netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);
                }
                return;
            }
            else
            {
                OperationStatus status = AsOperationStatus(resp.StatusCode);
                if (resp.StatusCode == HttpStatusCode.Unauthorized)
                {     // using this status code to indicate cookie has expired or is invalid
                    if (authCookie != null)
                    { // remove cookie and retry with credentials
                        status     = OperationStatus.Retry;
                        authCookie = null;
                    }
                }
                if (resp.StatusCode == HttpStatusCode.Forbidden)
                {   // remove cookie and force authentication on next request
                    authCookie = null;
                }

                if (netOpInProgressDel != null)
                {   // signal the network operation completed and whether it completed successfully
                    netOpInProgressDel.DynamicInvoke(false, status);
                    if (status == OperationStatus.Retry)
                    {   // delegate will retry, exit now
                        return;
                    }
                }
            }

            // get the method-specific delegate
            Delegate del = state.Delegate as Delegate;

            if (del == null)
            {
                return;  // if no delegate was passed, the results can't be processed
            }
            // invoke the delegate with the response body
            try
            {
                T resultObject = resp.GetBody();
                del.DynamicInvoke(resultObject);
            }
            catch (Exception ex)
            {
                TraceHelper.AddMessage("ProcessResponse: exception from GetBody or DynamicInvoke; ex: " + ex.Message);
                del.DynamicInvoke(null);
            }
        }
Exemple #9
0
        // Common code for invoking all the web service calls.
        // GET requests will be served directly from this method,
        // POST/PUT/DELETE requests are served from the InvokeWebServiceRequest_Inner method (which is an async callback)
        private static void InvokeWebServiceRequest(User user, string url, string verb, object obj, Delegate del, Delegate netOpInProgressDel, AsyncCallback callback)
        {
            // this code is non-reentrant
            if (isRequestInProgress == true)
            {
                return;
            }

            // signal that a network operation is starting
            if (netOpInProgressDel != null)
            {
                netOpInProgressDel.DynamicInvoke(true, OperationStatus.Started);
            }

            Uri uri = null;

            if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri) == false ||
                uri.Scheme != "http" && uri.Scheme != "https")
            {
                TraceHelper.AddMessage("InvokeWebServiceRequest: bad URL: " + url);
                return;
            }

#if IOS
            request           = (HttpWebRequest)WebRequest.Create(uri);
            request.UserAgent = UserAgents.IOSPhone;
#else
            request           = WebRequest.CreateHttp(url);
            request.UserAgent = UserAgents.WinPhone;
#endif
            request.Accept = "application/json";
            request.Method = verb == null ? "GET" : verb;

            if (authCookie != null)
            {   // send auth cookie
                request.Headers[authRequestHeader] = authCookie;
            }
            else if (user != null)
            {   // send credentials in authorization header
                string credentials  = string.Format("{0}:{1}", user.Name, user.Password);
                string encodedCreds = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(credentials));
                request.Headers[authorizationHeader] = string.Format("Basic {0}", encodedCreds);
            }

            // set the session ID header
            var sessionToken = TraceHelper.SessionToken;
            if (sessionToken != null)
            {
                request.Headers[HttpApplicationHeaders.Session] = sessionToken;
            }

            // if this is a GET request, we can execute from here
            if (request.Method == "GET")
            {
                // execute the web request and get the response
                try
                {
                    WebServiceState reqState = new WebServiceState()
                    {
                        Delegate = del,
                        NetworkOperationInProgressDelegate = netOpInProgressDel
                    };
                    IAsyncResult result = request.BeginGetResponse(callback, reqState);
                    if (result != null)
                    {
                        isRequestInProgress = true;
                    }
                }
                catch (Exception ex)
                {
                    isRequestInProgress = false;

                    // trace the exception
                    TraceHelper.AddMessage("Exception in BeginGetResponse: " + ex.Message);

                    // signal that a network operation is done and unsuccessful
                    if (netOpInProgressDel != null)
                    {
                        netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);
                    }
                }
            }
            else
            {
                // this is a request that contains a body (PUT, POST, DELETE)
                // need to nest another async call - this time to get the request stream
                try
                {
                    IAsyncResult result = request.BeginGetRequestStream(
                        new AsyncCallback(InvokeWebServiceRequest_Inner),
                        new WebInvokeServiceState()
                    {
                        Callback = callback,
                        Delegate = del,
                        NetworkOperationInProgressDelegate = netOpInProgressDel,
                        RequestBody = obj
                    });
                    if (result != null)
                    {
                        isRequestInProgress = true;
                    }
                }
                catch (Exception)
                {
                    isRequestInProgress = false;
                    // signal that a network operation is done and unsuccessful
                    if (netOpInProgressDel != null)
                    {
                        netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed);
                    }
                }
            }
        }