Ejemplo n.º 1
0
        ///// <summary>
        ///// Key: UserId + '_' + Symbol, Value: List of the closed position. Yiyang: Change to documents instead of Queue
        ///// </summary>
        //public Dictionary<string, List<ClosePosition>> ClosedPositionTable
        //{
        //    get;
        //    set;
        //}

        ///// <summary>
        ///// Key: UserId + '_' + Symbol, Value: List of the open position. Yiyang: Change to documents instead of Queue
        ///// </summary>
        //public Dictionary<string, List<OpenPosition>> OpenPositionTable
        //{
        //    get;
        //    set;
        //}

        /// <summary>
        /// Add a valid (verified) user
        /// </summary>
        /// <param name="user"></param>
        public enumStatus AddUser(User user)
        {
            string jsonDoc = user.ToString();

            try
            {
                Users.Add(user.Id, jsonDoc);
                UserTable.Add(user.UserName, user);
                Positions.Add(user.Id, new List <Position>());
                return(enumStatus.UserAdded);
            }
            catch (Exception ex)
            {
                ServerLog.LogException(ex, string.Format("Add User: {0}", user.ToString()));
                return(enumStatus.UserAddFailed);
            }
            //lock (syncRoot)
            //{

            //}
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Since balance is the only mutuable property in user, update User means update balance.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public enumStatus UpdateUser(User user)
        {
            string jsonDoc = user.ToString();

            try
            {
                lock (syncRoot)
                {
                    if (!Users.ContainsKey(user.Id) || !UserTable.ContainsKey(user.UserName))
                    {
                        return(enumStatus.Error);
                    }
                    Users[user.Id]           = jsonDoc;
                    UserTable[user.UserName] = user;
                    return(enumStatus.BalanceAdjusted);
                }
            }
            catch (Exception ex)
            {
                ServerLog.LogException(ex, string.Format("Update User: {0}", jsonDoc));
                return(enumStatus.BalanceAdjustFailed);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// reflection
        /// </summary>
        /// <param name="method">method Name</param>
        /// <param name="o">object that contains the method</param>
        /// <param name="command">command name, close to method</param>
        /// <param name="args">method arguments</param>
        /// <param name="jsonInput">we support json Input as well </param>
        /// <param name="contentType"></param>
        /// <param name="redirect"></param>
        /// <param name="httpStatus">Rest Status, 200 or 500</param>
        /// <returns></returns>
        private object CallRestMethodInternal(MethodInfo method, object o, string command, Dictionary <string, string> args, string jsonInput, out string contentType, out string redirect, out int httpStatus)
        {
            try
            {
                var      pinfo      = method.GetParameters();
                object[] paramArray = new object[pinfo.Length];

                int contentTypeIndex = -1;
                int RedirectIndex    = -1;
                int httpStatusIndex  = -1;

                int n = 0;
                foreach (var p in pinfo)
                {
                    if (p.Name == "jsonInput")
                    {
                        paramArray[n] = jsonInput;
                    }
                    else if (p.Name == "outContentType" && p.IsOut)
                    {
                        paramArray[n]    = null;
                        contentTypeIndex = n;
                    }
                    else if (p.Name == "outRedirect" && p.IsOut)
                    {
                        paramArray[n] = null;
                        RedirectIndex = n;
                    }
                    else if (p.Name == "httpStatus" && p.IsOut)
                    {
                        paramArray[n]   = null;
                        httpStatusIndex = n;
                    }
                    else
                    {
                        if (p.ParameterType != typeof(string))
                        {
                            throw new ApplicationException(string.Format("Rest Method {0} has a parameter with invalid type", command));
                        }
                        string val;
                        if (!args.TryGetValue(p.Name, out val))
                        {
                            paramArray[n] = null;
                        }
                        else
                        {
                            paramArray[n] = val;
                        }
                    }

                    n++;
                }

                object ret = method.Invoke(o, paramArray);

                if (contentTypeIndex != -1)
                {
                    contentType = paramArray[contentTypeIndex] as string;
                }
                else
                {
                    contentType = "application/json";
                }

                if (RedirectIndex != -1)
                {
                    redirect = paramArray[RedirectIndex] as string;
                }
                else
                {
                    redirect = null;
                }

                if (httpStatusIndex != -1)
                {
                    httpStatus = (int)paramArray[httpStatusIndex];
                }
                else
                {
                    httpStatus = 200;
                }

                return(ret);
            }
            catch (TargetInvocationException tie)
            {
                ServerLog.LogException(tie.InnerException, "Exception thrown in bound REST method " + method.Name);
                throw tie.InnerException;
            }
        }