예제 #1
0
파일: Parser.cs 프로젝트: kwood/ExchangeApi
        public static IMessageIn Parse(string serialized)
        {
            var data = Json.ParseObject(serialized);

            Condition.Requires(data, "data").IsNotNull();
            string type = (string)data["type"];

            Condition.Requires(type, "type").IsNotNull();
            IMessageIn res = null;

            switch (type)
            {
            case "received":
                res = new OrderReceived();
                break;

            case "open":
                res = new OrderOpen();
                break;

            case "done":
                res = new OrderDone();
                break;

            case "match":
                res = new OrderMatch();
                break;

            case "change":
                res = new OrderChange();
                break;

            default:
                throw new ArgumentException("Unexpected message type: " + type);
            }
            var parser = new MessageParser(data);

            parser.ParseCommon(res);
            return(res.Visit(parser));
        }
예제 #2
0
파일: Parser.cs 프로젝트: kwood/ExchangeApi
        static IMessageIn ParseMessage(JToken root)
        {
            // If `root` isn't a JSON object, throws.
            // If there is no such field, we get a null.
            var channel = (string)root["channel"];

            if (channel == null)
            {
                _log.Error("Incoming message without a channel. Ignoring it.");
                return(null);
            }
            Func <IMessageIn> ctor;

            if (!_messageCtors.TryGetValue(channel, out ctor))
            {
                _log.Error("Incoming message with unknown `channel`: '{0}'. Ignoring it.", channel);
                return(null);
            }
            IMessageIn msg       = ctor.Invoke();
            string     errorcode = (string)root["errorcode"];

            if (errorcode != null)
            {
                msg.Error = (ErrorCode)int.Parse(errorcode);
                return(msg);
            }
            // Note that data can be null. It expected for some messages.
            // MessageParser will throw if it doesn't like null data.
            JToken data = root["data"];

            try
            {
                return(msg.Visit(new MessageParser(data)));
            }
            catch (Exception e)
            {
                _log.Error(e, "Unable to parse incoming message. Ignoring it.");
                return(null);
            }
        }
예제 #3
0
            void PublishUpdate(IMessageIn msg, Order order, Fill fill, bool finished)
            {
                Condition.Requires(order.Callback, "order.Callback").IsNotNull();
                var update = new TimestampedMsg <OrderUpdate>()
                {
                    Received = _received,
                    Value    = new OrderUpdate()
                    {
                        Time     = msg.Time,
                        OrderId  = order.OrderId,
                        Unfilled = order.Unfilled,
                        Fill     = fill,
                        Finished = finished,
                    }
                };
                var cb = order.Callback;

                if (finished)
                {
                    order.Callback = null;
                }
                try { cb(update); }
                catch (Exception e) { _log.Warn(e, "Ignoring exception from order callback"); }
            }
예제 #4
0
 public static string FromMessage(IMessageIn msg)
 {
     return(msg.Visit(new MessageChannel()));
 }