Exemple #1
0
        public void Begin()
        {
            try
            {
                log.Debug("Starting processoring");
                config = ConfigurationManager.AppSettings;
                host = ConfigurationManager.AppSettings["bindhost"];
                port = int.Parse(ConfigurationManager.AppSettings["bindport"]);
                stats = new Stats();
                //BT = new BatchTranscoder();
                //LS = new ListenServer(host, port, BT);
                presets = new Presets();
                Dictionary<string, IHttpRequestHandler> handlers = new Dictionary<string, IHttpRequestHandler>();

                jobs = new ConcurrentDictionary<string, TranscodeJob>();

                expireCancelToken = new CancellationTokenSource();
                TaskExecutorFactory.Begin(ExpireJobs, 300000, 100, Timeout.Infinite, -1, expireCancelToken.Token);

                transcodingPool = new Sprite.ThreadPool(int.Parse(ConfigurationManager.AppSettings["maxtasks"]), Sprite.WaitStrategy.MODERATE);

                Directory.CreateDirectory(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["thumb-destination"]);
                Directory.CreateDirectory(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"]);

                // Initialize all the endpoint handlers
                var jobHandler = new JobHandler(jobs, transcodingPool);
                var presetHandler = new PresetHandler(presets);
                var thumbHandler = new ThumbnailHandler();
                var adaptiveHandler = new AdaptiveHandler(jobHandler.Jobs);
                var metaHandler = new MetaHandler();
                var videoHandler = new VideoHandler();
                handlers.Add("/stats", stats);
                handlers.Add("/jobs", jobHandler);
                handlers.Add("/presets", presetHandler);
                handlers.Add("/thumbs", thumbHandler);
                handlers.Add("/videos", videoHandler);
                handlers.Add("/playlist", adaptiveHandler);
                handlers.Add("/metadata", metaHandler);
                Server server = new Server(host, port, handlers);

                //server = new Httpd(5120, handlers);
                thread = new Thread(new ThreadStart(server.Listen));
                thread.Start();
                transcodingPool.StartPool();
            }
            catch (Exception ex)
            {
                log.Fatal("A fatal exception has occured", ex);
            }
        }
Exemple #2
0
        public void HandlePost(HttpListenerRequest request, HttpListenerResponse response, Server.RequestData data)
        {
            var presetName = data.PostParameters.GetFirstValue("preset");
            var value = data.PostParameters.GetFirstValue("value");

            if (string.IsNullOrEmpty(presetName))
            {
                response.StatusCode = 400;
                response.WriteResponse("Missing preset name");
                return;
            }
            if (string.IsNullOrEmpty(value))
            {
                response.StatusCode = 400;
                response.WriteResponse("Missing preset value");
                return;
            }

            // TODO do we need to do filtering on this value?
            presets.Add(presetName, value);
            response.WriteResponse(200, "Preset added");
        }
Exemple #3
0
 public void HandlePost(System.Net.HttpListenerRequest request, System.Net.HttpListenerResponse response, Server.RequestData data)
 {
     throw new NotImplementedException();
 }
Exemple #4
0
 public void HandlePost(HttpListenerRequest request, HttpListenerResponse response, Server.RequestData data)
 {
     response.StatusCode = 400;
     response.WriteResponse("Bad Request");
     //return new Httpd.HttpResponse(400, "Bad Request");
 }
Exemple #5
0
 public void HandlePut(HttpListenerRequest request, HttpListenerResponse response, Server.RequestData data)
 {
     throw new NotImplementedException();
 }
Exemple #6
0
 public void HandlePost(HttpListenerRequest request, HttpListenerResponse response, Server.RequestData data)
 {
     response.StatusCode = 404;
     response.WriteResponse("404 Page Not Found");
 }
        public object Handle()
        {
            try
            {
                if (handler == null)
                {
                    log.Error("HTTP Request Handler is null");
                }
                switch (context.Request.HttpMethod.ToLower())
                {
                case "get":

                    handler.HandleGet(context.Request, context.Response);
                    break;

                case "head":
                    if (handler is IHttpExtendedRequestHandler)
                    {
                        ((IHttpExtendedRequestHandler)handler).HandleHead(context.Request, context.Response);
                    }
                    else
                    {
                        context.Response.StatusCode = 404;
                        //context.Response.Close();
                    }
                    break;

                case "delete":
                    if (handler is IHttpExtendedRequestHandler)
                    {
                        ((IHttpExtendedRequestHandler)handler).HandleDelete(context.Request, context.Response);
                    }
                    else
                    {
                        context.Response.StatusCode = 404;
                        //context.Response.Close();
                    }
                    break;

                case "post":
                    Server.RequestData data = null;
                    // Handle multipart requests with file data
                    if (context.Request.ContentType != null && context.Request.ContentType.StartsWith("multipart/form-data;"))
                    {
                        var boundary = "--" + context.Request.ContentType.Substring(30);

                        byte[]       buffer  = new byte[4096];
                        int          read    = 0;
                        MemoryStream mstream = new MemoryStream();
                        while ((read = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            mstream.Write(buffer, 0, read);
                        }
                        try
                        {
                            data = processMultipartData(mstream, boundary);
                        }
                        catch (Exception ex)
                        {
                            log.Error("Failed to process http post request", ex);
                            context.Response.WriteResponse(500, ex.Message);
                            return(null);
                        }
                    }
                    else
                    {
                        var post_string = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding).ReadToEnd();
                        var parts       = Server.ParseQueryString(post_string);
                        data = new Server.RequestData();
                        data.PostParameters = parts;
                    }

                    handler.HandlePost(context.Request, context.Response, data);
                    break;
                }
            }
            catch (Exception ex)
            {
                log.Error("HTTP Processing Exception", ex);
                if (context != null)
                {
                    if (context.Response != null)
                    {
                        if (context.Response.OutputStream.CanWrite)
                        {
                            try
                            {
                                context.Response.WriteResponse(500, ex.ToString());
                            }
                            catch (Exception innerException)
                            {
                                log.Error("Failed to write response", innerException);
                                //Console.WriteLine(innerException);
                            }
                        }
                    }
                }
            }

            try
            {
                if (context.Response.OutputStream.CanWrite)
                {
                    context.Response.Close();
                }
            }
            catch (Exception ex)
            {
                log.Error("Error closing stream", ex);
            }
            return(null);
        }