⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 primenumberhandler.cs

📁 ajax patterns 这是关于ajax设计模式方面的原代码
💻 CS
字号:
/*
    Copyright 2006 Christian Gross http://www.devspace.com
    From the book Ajax Patterns and Best Practices

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Web;
using System.Web.SessionState;
using System.Threading;
using Devspace.Ajax.InfiniteData.PrimeNumberCalculator;
using Devspace.Ajax.Permutations;
using Devspace.Ajax.PersistentCommunications;
using Devspace.Commons.Tracer;
using System.Collections;

namespace Devspace.Ajax.InfiniteData {
    // TODO: Need to move this to permutations and make the code general as is the case with the Java code
    [Obsolete("Class is going to be moved into the Devspace.Ajax.Permutations namespace and slightly modified")]
    internal class GenericCookieUserResolverImplementation : IUserIdentificationResolver {
        private static Random _randNumber = new Random( );
        protected internal string constCookieIdentifier = "infinite-identifier";
        
        protected internal string GetVariable(HttpRequest req, string identifier) {
            string retval = req.QueryString.Get(identifier);
            if (retval == null || retval.Length == 0) {
                retval = req.Form.Get(identifier);
            }
            return retval;
        }
        
        private class UserImplementation : IUserIdentification {
            bool _isIdentified;
            string _identifier;
            public UserImplementation(string identifier, bool isIdentified) {
                _identifier = identifier;
                _isIdentified = isIdentified;
            }
            public String Identifier {
                get {
                    return _identifier;
                }
            }
            
            public bool IsIdentified {
                get {
                    return _isIdentified;
                }
            }
        }
        public IUserIdentification IdentifyUser(HttpContext context) {
            HttpCookie cookie = context.Request.Cookies.Get(constCookieIdentifier);
            if (cookie == null) {
                GenerateOutput.Write("GenericCookieUserResolverImplementation.IdentifyUser", "User could not be creating generic cookie");
                string buffer = "InfiniteData_" + Convert.ToString(_randNumber.Next());
                cookie = new HttpCookie(constCookieIdentifier, buffer);
                cookie.Path = "/infinitedata/primenumbercalculator";
                context.Response.AppendCookie(cookie);
                return new UserImplementation(buffer, true);
            }
            else {
                GenerateOutput.Write("GenericCookieUserResolverImplementation.IdentifyUser", "User identified using cookie");
                return new UserImplementation(cookie.Value, true);
            }
        }
    }
    
    public class PrimeNumberHandler : RedirectionHttpHandlerBase {
        static Hashtable _tasks = new Hashtable();
        
        public PrimeNumberHandler() {
            AssignUserResolver(new GenericCookieUserResolverImplementation());
        }
        private TaskManagerImpl GetTaskManager() {
            lock (_tasks) {
                if (_tasks.ContainsKey(_identifiedUser.Identifier)) {
                    GenerateOutput.Write("PrimeNumberHandler.GetTaskManager", "TaskManagerImpl existed and is returned.");
                    return (TaskManagerImpl)_tasks[_identifiedUser.Identifier];
                }
                else {
            // TODO: Need to change the storage of the task manager to the path
                    GenerateOutput.Write("PrimeNumberHandler.GetTaskManager", "Created a new TaskManagerImpl instance.");
                    TaskManagerImpl taskmanager = new TaskManagerImpl();
                    _tasks.Add(_identifiedUser.Identifier, taskmanager);
                    return taskmanager;
                }
            }
        }
        protected internal override void DoGet(HttpRequest req, HttpResponse resp) {
            TaskManagerImpl taskManager = GetTaskManager();
            if (taskManager != null) {
                GenerateOutput.Write("PrimeNumberHandler.DoGet", "Retrieving the results for a task.");
                IResult result = taskManager.GetResultWait(10);
                resp.ContentType = "text/xml";
                if (result != null) {
                    PrimeNumberData num = result as PrimeNumberData;
                    if( num != null) {
                        string buffer = Serializer.Generate((PrimeNumberData)result);
                        GenerateOutput.Write("PrimeNumberHandler.DoGet", "Generated result length (" +
                                             buffer.Length + ") value (" + buffer + ")");
                        resp.AddHeader( "X-Last-State", "none");
                        resp.Output.Write( buffer);
                    }
                }
                else {
                    if( ! taskManager.AnyOutstandingTasks()) {
                        GenerateOutput.Write("PrimeNumberHandler.DoGet", "No outstanding tasks");
                        resp.StatusCode = 200;
                        resp.StatusDescription = "OK";
                        resp.Output.Write( "<result>No tasks</result>");
                        
                    }
                    else {
                        GenerateOutput.Write("PrimeNumberHandler.DoGet", "No results");
                        resp.StatusCode = 200;
                        resp.StatusDescription = "OK";
                        resp.Output.Write("<result>No results</result>");
                    }
                }
            }
            else {
                GenerateOutput.Write("PrimeNumberHandler.DoGet", "Invalid taskmanager");
                resp.StatusCode = 200;
                resp.StatusDescription = "OK";
                resp.Output.Write("<result>Invalid taskManager instance</result>");
            }
        }
        
        protected internal override void DoPost(HttpRequest req, HttpResponse resp) {
            TaskManagerImpl taskManager = GetTaskManager();
            GenerateOutput.Write("PrimeNumberHandler.DoPost", "Executing a new task.");
            ActionData data = Serializer.Parse(req.InputStream);
            Console.WriteLine(data.ToString());
            taskManager.AddTask(
                new Calculator(data.Number, data.TransactionIdentifier));
            taskManager.RunThreadedTasks();
        }
        
        protected internal override String BaseURL {
            get {
                return "/infinitedata/primenumbercalculator";
            }
        }
        
        protected internal override String GetURLEnding() {
            return _identifiedUser.Identifier;
        }
        
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -