Ejemplo n.º 1
0
        private void Process()
        {
            while (_running)
            {
                try
                {
                    var ea      = _consumer.Queue.Dequeue();
                    var body    = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    //var routingKey = ea.RoutingKey;

                    try
                    {
                        var data = DataMessage.Deserialize(message);
                        _handler.OnReceive(data);
                    }
                    catch (Exception)
                    {
                        // TODO Handle exceptions
                    }

                    // TODO use notifications
                    //Console.WriteLine(" [x] Received '{0}': with key :'{1}'", routingKey, text.Key);
                }
                catch (EndOfStreamException)
                {
                    // Try to reconnect
                    Disconnect();
                    Connect(_filter);
                }
            }
        }
Ejemplo n.º 2
0
        internal void ProcessRequest()
        {
            var slideNameKey     = "BSLIDE";
            var slideCartNameKey = "SLIDE";

            try
            {
                string responseString = "";

                // Manage Encoding
                _context.Response.ContentEncoding = _context.Request.ContentEncoding;
                // Set CM Server Headers
                _context.Response.Headers.Add("Server", "EBU Content Manager");

                if (_context.Request.HttpMethod == "GET")
                {
                    // GET Request
                    var kvStore = _context.Request.QueryString;
                    switch (RawUrlToCMCommand(_context.Request.RawUrl))
                    {
                    case CMCommand.Update:
                    case CMCommand.IceCast:
                    {
                        var dm = CreateGlobalDataMessage(kvStore);
                        _handler.UpdateGlobalData(dm);
                        _context.Response.StatusCode = 200;
                        responseString = "Ok.";
                    }
                    break;

                    case CMCommand.Change:
                    {
                        if (kvStore.AllKeys.Contains(slideNameKey))
                        {
                            var slideName = kvStore[slideNameKey];
                            _handler.BroadcastSlide(slideName);

                            _context.Response.StatusCode = 200;
                            responseString = "Ok.";
                        }
                        else
                        {
                            _context.Response.StatusCode = 502;
                            responseString = "Missing BSLIDE parameter.";
                        }
                    }
                    break;

                    case CMCommand.UpdateAndBroadcast:
                    {
                        if (kvStore.AllKeys.Contains(slideNameKey))
                        {
                            var dm = CreateGlobalDataMessage(kvStore);
                            _handler.UpdateGlobalData(dm);

                            var slideName = kvStore[slideNameKey];
                            _handler.BroadcastSlide(slideName);

                            _context.Response.StatusCode = 200;
                            responseString = "Ok.";
                        }
                        else
                        {
                            _context.Response.StatusCode = 502;
                            responseString = "Missing BSLIDE parameter.";
                        }
                    }
                    break;

                    case CMCommand.Load:
                    {
                        if (kvStore.AllKeys.Contains(slideCartNameKey + "1"))
                        {
                            _handler.ClearActiveCart();

                            var slideList = new List <string>();

                            var i = 1;

                            while (!string.IsNullOrEmpty(slideCartNameKey + i))
                            {
                                slideList.Add(slideCartNameKey + i);
                                ++i;
                            }
                            _handler.AddSlides(slideList);

                            _context.Response.StatusCode = 200;
                            responseString = "Ok.";
                        }
                        else
                        {
                            _context.Response.StatusCode = 502;
                            responseString = "Missing at least SLIDE1 parameter.";
                        }
                    }
                    break;

                    // TODO Add additional commands
                    default:
                        _context.Response.StatusCode = 404;
                        responseString = "GET Requests generally are not supported.";
                        break;
                    }

                    // TODO For Future use of status return
                    //_context.Response.ContentType = "application/json";
                    //_context.Response.StatusCode = 200;
                    //responseString = ""; //GetJSONStatus();

                    // Write Response
                    var buffer = _context.Request.ContentEncoding.GetBytes(responseString);
                    _context.Response.Close(buffer, false);
                }
                else if (_context.Request.HttpMethod == "POST")
                {
                    if (_context.Request.HasEntityBody)
                    {
                        try
                        {
                            // Read Incoming Content
                            var body          = _context.Request.InputStream;
                            var reader        = new StreamReader(body, _context.Request.ContentEncoding);
                            var requestString = reader.ReadToEnd();
                            body.Close();
                            reader.Close();

                            // Handle the Incoming Content
                            var data = DataMessage.Deserialize(requestString);
                            // Dispatch Data
                            _handler.OnReceive(data);

                            // Responses
                            _context.Response.ContentType = "application/json";
                            responseString = "{\"status\":\"ok\",\"receivedtype\":\"" + data.DataType +
                                             "\",\"receiveddatacount\":" + data.Data.Count + "}";

                            // Write Response
                            var buffer = _context.Request.ContentEncoding.GetBytes(responseString);
                            _context.Response.StatusCode = 200;
                            _context.Response.Close(buffer, false);
                        }
                        catch (Exception e)
                        {
                            // TODO Log this
                            _context.Response.StatusCode = 500;
                            var buffer = _context.Request.ContentEncoding.GetBytes("Error occured.\n" + e.Message);
                            _context.Response.Close(buffer, false);
                        }
                    }
                    else
                    {
                        _context.Response.StatusCode = 400;
                        var buffer = _context.Request.ContentEncoding.GetBytes("POST should contain body content.");
                        _context.Response.Close(buffer, false);
                    }
                }
                else
                {
                    // Generally Unsupported Method
                    _context.Response.StatusCode = 406;
                    var buffer = _context.Request.ContentEncoding.GetBytes(_context.Request.HttpMethod + " method not supported.");
                    _context.Response.Close(buffer, false);
                }
            }
            catch (Exception ex)
            {
                // TODO Log this
                try
                {
                    _context.Response.StatusCode = 500;
                    var buffer = _context.Request.ContentEncoding.GetBytes("Error occured. " + ex.Message);
                    _context.Response.Close(buffer, false);
                }
                catch (Exception)
                {
                    // TODO Log
                }
            }
        }