示例#1
0
        /*
         * The following events are divided.
         * public virtual InfoPlusResponse OnActionDoing(InfoPlusEvent e) { return this.Update(e); }
         * public virtual InfoPlusResponse OnActionDone(InfoPlusEvent e) { return this.Update(e); }
         * public virtual InfoPlusResponse OnStepRendering(InfoPlusEvent e) { return this.Update(e); }
         * public virtual InfoPlusResponse OnStepRendered(InfoPlusEvent e) { return this.Update(e); }
         * public virtual InfoPlusResponse OnActionClicking(InfoPlusEvent e) { return this.Update(e); }
         * public virtual InfoPlusResponse OnStepPrinting(InfoPlusEvent e) { return this.Update(e); }
         * public virtual InfoPlusResponse OnStepExpiring(InfoPlusEvent e) { return this.Update(e); }
         * public virtual InfoPlusResponse OnStepExpired(InfoPlusEvent e) { return this.Update(e); }
         */

        /// <summary>
        /// Suggesting is implemented for cache & pinyin by default.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public virtual InfoPlusResponse OnFieldSuggesting(InfoPlusEvent e)
        {
            if (null == e.Suggestion)
            {
                return(this.Update(e));
            }
            try
            {
                string code = e.Suggestion.Code;
                var    cl   = AbstractMessenger.cachedCodeLists;
                var    ce   = AbstractMessenger.cachedCodeExpires;
                if (e.Suggestion.Dirty || false == cl.ContainsKey(code) || ce[code] < UnixTime.ToInt64(DateTime.Now))
                {
                    lock (_code_lock)
                    {
                        if (e.Suggestion.Dirty || false == cl.ContainsKey(code) || ce[code] < UnixTime.ToInt64(DateTime.Now))
                        {
                            CodeList list = this.OnQueryCodeTable(code);
                            if (null == list || null == list.Items || 0 == list.Items.Count)
                            {
                                throw new Exception("CodeTable " + code + " invalid.");
                            }
                            foreach (var item in list.Items)
                            {
                                item.CalculateSpell();
                            }
                            // add to cache
                            AbstractMessenger.cachedCodeLists[code]   = list;
                            AbstractMessenger.cachedCodeExpires[code] = UnixTime.ToInt64(DateTime.Now.AddHours(1));
                        }
                    }
                }
                CodeList l        = this.Suggest(e.Suggestion);
                var      response = new InfoPlusResponse();
                response.Codes = new List <CodeList>();
                response.Codes.Add(l);
                return(response);
            }
            catch (Exception ex)
            {
                return(new InfoPlusResponse(true, true, ex.Message));
            }
        }
示例#2
0
        public string OnEvent(string verify, string version, string eventType, string eventData)
        {
            // Trace
            string s = string.Format("eventType:{0},eventData:{1}", eventType, eventData);

            System.Diagnostics.Trace.WriteLine(s);
            InfoPlusResponse r = new InfoPlusResponse();

            try
            {
                // 1.Data type conversion.
                EventTypes eTypes = (EventTypes)Enum.Parse(typeof(EventTypes), eventType);
                // just return the default response when ECHO
                if (eTypes == EventTypes.ECHO)
                {
                    return(JsonConvert.ExportToString(r));
                }

                InfoPlusEvent e = JsonConvert.Import <InfoPlusEvent>(eventData);
                if (null == e)
                {
                    return(JsonConvert.ExportToString(new InfoPlusResponse(true, true, "InfoPlusEvent malformed")));
                }

                // 2.Retrieve messengers
                IList <AbstractMessenger> messengers = this.settings.Messengers;
                var targets = from m in messengers where m.Match(e) select m;

                // 3.Dispatch messages.
                // Devoted to Charles Petzold, the first Win32 program, Orz by marstone, 2011/06/29
                // Changed switch(eventType) to Reflection. Devoted to Brian Cantwell Smith, 2011/07
                bool   verified = !targets.Any();
                string detail   = null;
                foreach (AbstractMessenger messenger in targets)
                {
                    // 4.Check verify.
                    verified = true;
                    if (messenger.RequireVerification && ServiceType.Insecure != ApplicationSettings.ServiceType)
                    {
                        ResponseEntity re = this.CheckParameters(messenger, verify, version, version, eventType, eventData);
                        if (false == re.Successful)
                        {
                            verified = false;
                            detail   = re.error;
                            break;
                        }
                    }

                    // Notice: the CurrentEvent is thread safe. by marstone, 2011/10/18
                    messenger.CurrentEvent = e;

                    InfoPlusResponse response = null;
                    try
                    {
                        var words = (from t in eTypes.ToString().Split(new char[] { '_' })
                                     select t[0] + t.Substring(1).ToLower());
                        string method = "On" + string.Join("", words.ToArray());

                        string log = string.Format("Dispatching {0} to {1}->{2}", eTypes, messenger.GetType().Name, method);
                        System.Diagnostics.Trace.WriteLine(log);

                        response = (InfoPlusResponse)messenger.GetType().InvokeMember(
                            method,
                            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
                            null,
                            messenger,
                            new object[] { e }
                            );
                    }
                    catch (Exception mex)
                    {
                        System.Diagnostics.Trace.WriteLine(mex);
                        String message = mex.Message + "\n" + mex.StackTrace;
                        while (true)
                        {
                            if (mex.InnerException == null || mex.InnerException == mex)
                            {
                                break;
                            }
                            mex      = mex.InnerException;
                            message += "\n" + mex.Message + "\n" + mex.StackTrace;
                        }
                        response = new InfoPlusResponse(true, true, "发生未知的错误", message);
                    }
                    if (null != response)
                    {
                        r += response;
                        // skip next messenger if said Break.
                        if (response.Break)
                        {
                            break;
                        }
                    }
                }

                if (false == verified)
                {
                    r = new InfoPlusResponse(true, true, "Verification Failed.", detail);
                }

                return(JsonConvert.ExportToString(r));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                return(JsonConvert.ExportToString(new InfoPlusResponse(true, true, "发生未知的错误", ex.Message + ex.StackTrace)));
            }
        }