Exemple #1
0
        public CRMBridge()
        {
            int notReadyTime    = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["NotReadyTime"]);
            int afterEndIVRTime = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["afterEndIVRTime"]);

            log.Debug("Creating CRMBridge service");
            server    = new HttpBridge(notReadyTime, afterEndIVRTime, this);
            trans     = new Transaction();
            trans.mne = new MensajeNegocio();
            log.Debug("Tiempo máximo para responder a ODMS: " + System.Configuration.ConfigurationSettings.AppSettings["ResponseTime"]);
            timeResponse = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["ResponseTime"]);
        }
    public ServerEmulator(Func <string, JObject, JObject> onEventReceived, Action <Uri> onClientConnected = null, Action <Uri> onClientDisconnected = null)
    {
        this.ssdpServer = new Rssdp.Infrastructure.SsdpCommunicationsServer(new Rssdp.SocketFactory(IPAddress.Any.ToString()), 1900, 1);
        this.httpBridge = new HttpBridge(this.OnHttpServerRequested, 44642);

        this.OnClientDisconnected = onClientDisconnected;
        this.OnClientConnected    = onClientConnected;
        this.OnEventReceived      = onEventReceived;

        // Starts to listen
        this.ssdpServer.Request​Received += OnSsdpServerRequested;
        this.ssdpServer.BeginListeningForBroadcasts();
    }
    public void OnHttpServerRequested(HttpListenerContext context)
    {
        // Get a parsed input
        var parsedRequest = new HttpBridge.ParsedReceivedRequest(context.Request);



// ################################################################
        Console.WriteLine("\n\nA request de método: " + context.Request.HttpMethod + " e URL: " + context.Request.Url.OriginalString +
                          " vinda de: " + context.Request.RemoteEndPoint.Address.ToString() + ": HEADERS:\n");

        foreach (var key in parsedRequest.Headers.AllKeys)
        {
            Console.WriteLine("Value: \"" + key + "\" Value: \"" + parsedRequest.Headers[key] + "\"");
        }

        Console.WriteLine("\nBODY:\n\"" + parsedRequest.Body + "\"");
// ################################################################



        // ################################
        // Process the input

        JObject responseMsg;

        // It's a register message
        if (context.Request.HttpMethod == HttpMethod.Post.ToString() && context.Request.Url.AbsolutePath == "/dtv/remote-mediaplayer/" &&
            context.Request.ContentType == "application/json; charset=utf-8" && context.Request.AcceptTypes.Contains("application/json; charset=utf-8") &&
            parsedRequest.Body.ContainsKey("location") && parsedRequest.Body.GetValue("location") != null)
        {
            try{
                responseMsg = this.FinishRegister(parsedRequest.Body);
                context.Response.StatusCode = (int)HttpStatusCode.OK;
            }catch (Exception e) {
                responseMsg = new JObject(new JProperty("error", "Registration failed:\n\n" + e.ToString()));
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }

            // It's a event message
        }
        else if (context.Request.HttpMethod == HttpMethod.Post.ToString() && context.Request.Url.AbsolutePath.StartsWith(EVENT_PREFIX) &&
                 context.Request.ContentType == "application/json; charset=utf-8" && context.Request.AcceptTypes.Contains("application/json; charset=utf-8"))
        {
            try{
                responseMsg = this.ProcessEvent(context.Request.Url.AbsolutePath.Substring(EVENT_PREFIX.Length), parsedRequest.Body);
                context.Response.StatusCode = (int)HttpStatusCode.OK;
            }catch (Exception e) {
                responseMsg = new JObject(new JProperty("error", "Event not accepted:\n\n" + e.ToString()));
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }
        }
        else
        {
            responseMsg = new JObject(new JProperty("error", "unrecognizable request."));
            context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
        }

        // ################################
        // Send the output

        HttpBridge.SendRequestResponse(context.Response, responseMsg, Encoding.UTF8, new Tuple <string, string>[] {
            Tuple.Create("Content-Type", "application/json; charset=utf-8")
        });

        // Release the context, clean up
        parsedRequest = null;
        context       = null;

        // ################################
        // Do some post processing maybe

        Console.WriteLine("Enrolando pra terminar a thread getRequest-sendResponse...");
        Thread.Sleep(10000);
        Console.WriteLine("Terminei!");
    }