Example #1
0
        protected override void OnDataReceived(Byte[] receiveData)
        {
            MtnUSSDTCPPDU pdu = null;

            try
            {
                pdu = MtnUSSDTCPPDU.FromXML(Encoding.ASCII.GetString(receiveData));
            }
            catch (Exception ex)
            {
                LogManager.LogError("XML Invalid: " + ex.Message);
                return;
            }

            lastActivity = DateTime.Now;
            try
            {
                if (pdu != null)
                {
                    if (pdu.Pdu == MtnUSSDTCPPDU.PDUTypes.CTRL.ToString())
                    {
                        LogManager.LogStatus("  ** <-- ping returned   ** ");
                        //Console.WriteLine("--> ping sent");
                    }
                    else
                    {
                        LogManager.LogStatus("Received " + Encoding.ASCII.GetString(receiveData));

                        String requestData = Encoding.ASCII.GetString(receiveData);
                        USSDHandlerRequestType.RequestTypes requestType = USSDHandlerRequestType.RequestTypes.TCP;
                        IUSSDHandler ussdHandler = USSDHandlerFactory.GetHandler(mobileNetwork, requestData, requestType);
                        Boolean      isTimeout   = false;
                        Boolean      isInvalid   = false;
                        ussdHandler.Initialize(requestData, requestType, out isTimeout, out isInvalid);
                        if (!isTimeout)
                        {
                            LogManager.LogStatus("Handler {0} found for request", ussdHandler.HandlerID);
                            String responseData = ussdHandler.ProcessRequest(requestData);
                            SendResponse(pdu, responseData);
                        }
                        else
                        {
                            LogManager.LogStatus("Timout request for handler {0} received", ussdHandler.HandlerID);
                        }
                    }
                }
                else
                {
                    SendResponse(pdu, "Invalid/Empty Request");
                }
            }

            catch (Exception ex)
            {
                LogManager.LogError(ex);
                SendResponse(pdu, "Invalid request. Please retry.");
            }
        }
Example #2
0
        protected void OnHttpRequestReceived(IAsyncResult asyncResult)
        {
            if (listenerState != ListenerStates.Running)
            {
                return;
            }

            HttpListener httpListener = (HttpListener)asyncResult.AsyncState;

            //Console.WriteLine(DateTime.Now.ToString() + " Request received");

            if (httpListener.IsListening)
            {
                HttpListenerResponse response = null;
                try
                {
                    // Call EndGetContext to complete the asynchronous operation.
                    HttpListenerContext context = httpListener.EndGetContext(asyncResult);
                    HttpListenerRequest request = context.Request;
                    response = context.Response;

                    String requestData = GetRequestData(request);

                    if (requestData.Length != 0)
                    {
                        USSDHandlerRequestType.RequestTypes requestType = USSDHandlerRequestType.GetRequestType(request.HttpMethod);
                        IUSSDHandler ussdHandler = USSDHandlerFactory.GetHandler(mobileNetwork, requestData, requestType);
                        Boolean      isTimeout   = false;
                        Boolean      isInvalid   = false;

                        ussdHandler.Initialize(requestData, requestType, out isTimeout, out isInvalid);

                        if (!isTimeout)
                        {
                            LogManager.LogStatus("Handler {0} found for request", ussdHandler.HandlerID);
                            String responseData = ussdHandler.ProcessRequest(requestData);
                            WriteResponse(responseData, response);
                        }
                        else
                        {
                            LogManager.LogStatus("Timout request for handler {0} received", ussdHandler.HandlerID);
                        }
                    }
                    else
                    {
                        WriteResponse("Invalid/Empty Request", response);
                    }
                }
                catch (Exception ex)
                {
                    LogManager.LogError(ex);
                    if (response != null)
                    {
                        WriteResponse(ex.Message, response);
                    }
                }
                finally
                {
                    httpListener.BeginGetContext(new AsyncCallback(OnHttpRequestReceived), httpListener);
                }
            }
        }