Пример #1
0
        public void SendingCommandObjectSetsCommandOnBus()
        {
            var messageBus = new Mock<IMessageBus>();
            var counters = new Mock<IPerformanceCounterWriter>();
            Message message = null;
            messageBus.Setup(m => m.Publish(It.IsAny<Message>())).Returns<Message>(m =>
            {
                message = m;
                return TaskAsyncHelper.Empty;
            });

            var serializer = new JsonNetSerializer();
            var traceManager = new Mock<ITraceManager>();
            var connection = new Connection(messageBus.Object,
                                            serializer,
                                            "signal",
                                            "connectonid",
                                            new[] { "a", "signal", "connectionid" },
                                            new string[] { },
                                            traceManager.Object,
                                            counters.Object);

            connection.Send("a", new Command
            {
                Type = CommandType.AddToGroup,
                Value = "foo"
            });

            Assert.NotNull(message);
            Assert.True(message.IsCommand);
            var command = serializer.Parse<Command>(message.Value);
            Assert.Equal(CommandType.AddToGroup, command.Type);
            Assert.Equal("foo", command.Value);
        }
Пример #2
0
        public void SendingCommandObjectSetsCommandOnBus()
        {
            var messageBus = new Mock<INewMessageBus>();
            Message message = null;
            messageBus.Setup(m => m.Publish(It.IsAny<Message>())).Callback<Message>(m => message = m);
            var serializer = new JsonNetSerializer();
            var traceManager = new Mock<ITraceManager>();
            var connection = new Connection(messageBus.Object,
                                            serializer,
                                            "signal",
                                            "connectonid",
                                            new[] { "a", "signal", "connectionid" },
                                            new string[] { },
                                            traceManager.Object);

            connection.Send("a", new Command
            {
                Type = CommandType.AddToGroup,
                Value = "foo"
            });

            Assert.NotNull(message);
            Assert.True(message.IsCommand);
            var command = serializer.Parse<Command>(message.Value);
            Assert.Equal(CommandType.AddToGroup, command.Type);
            Assert.Equal(@"{""Name"":""foo"",""Cursor"":null}", command.Value);
        }
Пример #3
0
        public void SendingCommandObjectSetsCommandOnBus()
        {
            var     messageBus = new Mock <IMessageBus>();
            Message message    = null;

            messageBus.Setup(m => m.Publish(It.IsAny <Message>())).Callback <Message>(m => message = m);
            var serializer   = new JsonNetSerializer();
            var traceManager = new Mock <ITraceManager>();
            var connection   = new Connection(messageBus.Object,
                                              serializer,
                                              "signal",
                                              "connectonid",
                                              new[] { "a", "signal", "connectionid" },
                                              new string[] { },
                                              traceManager.Object);

            connection.Send("a", new Command
            {
                Type  = CommandType.AddToGroup,
                Value = "foo"
            });

            Assert.NotNull(message);
            Assert.True(message.IsCommand);
            var command = serializer.Parse <Command>(message.Value);

            Assert.Equal(CommandType.AddToGroup, command.Type);
            Assert.Equal(@"{""Name"":""foo"",""Cursor"":null}", command.Value);
        }
Пример #4
0
        public HubRequest Parse(string data)
        {
            var serializer       = new JsonNetSerializer();
            var deserializedData = serializer.Parse <HubInvocation>(data);

            var request = new HubRequest();

            request.Hub             = deserializedData.Hub;
            request.Method          = deserializedData.Method;
            request.Id              = deserializedData.Id;
            request.State           = GetState(deserializedData);
            request.ParameterValues = (deserializedData.Args != null) ? deserializedData.Args.Select(value => new JRawValue(value)).ToArray() : _emptyArgs;

            return(request);
        }
Пример #5
0
        public void SendingCommandObjectSetsCommandOnBus()
        {
            var     messageBus = new Mock <IMessageBus>();
            var     counters   = new PerformanceCounterManager();
            Message message    = null;

            messageBus.Setup(m => m.Publish(It.IsAny <Message>())).Returns <Message>(m =>
            {
                message = m;
                return(TaskAsyncHelper.Empty);
            });

            var serializer   = new JsonNetSerializer();
            var traceManager = new Mock <ITraceManager>();
            var connection   = new Connection(messageBus.Object,
                                              serializer,
                                              "signal",
                                              "connectonid",
                                              new[] { "a", "signal", "connectionid" },
                                              new string[] { },
                                              traceManager.Object,
                                              new AckHandler(completeAcksOnTimeout: false,
                                                             ackThreshold: TimeSpan.Zero,
                                                             ackInterval: TimeSpan.Zero),
                                              counters,
                                              new Mock <IProtectedData>().Object);

            connection.Send("a", new Command
            {
                CommandType = CommandType.AddToGroup,
                Value       = "foo"
            });

            Assert.NotNull(message);
            Assert.True(message.IsCommand);
            var command = serializer.Parse <Command>(message.Value, message.Encoding);

            Assert.Equal(CommandType.AddToGroup, command.CommandType);
            Assert.Equal("foo", command.Value);
        }
Пример #6
0
        private static IDictionary <string, object> GetState(HubInvocation deserializedData)
        {
            if (deserializedData.State == null)
            {
                return(new Dictionary <string, object>());
            }

            // Get the raw JSON string and check if it's over 4K
            string json = deserializedData.State.ToString();

            if (json.Length > 4096)
            {
                throw new InvalidOperationException(Resources.Error_StateExceededMaximumLength);
            }

            var settings = new JsonSerializerSettings();

            settings.Converters.Add(new SipHashBasedDictionaryConverter());
            var serializer = new JsonNetSerializer(settings);

            return(serializer.Parse <IDictionary <string, object> >(json));
        }
Пример #7
0
        protected override Task OnReceivedAsync(IRequest request, string connectionId, string data)
        {
            string        userName = HttpContext.Current.User != null ? HttpContext.Current.User.Identity.Name : string.Empty;
            ProgressState ps       = new ProgressState();
            //try
            //{
            ActionData ad = jns.Parse <ActionData>(data);

            //start:开始分配;stop:停止分配
            switch (ad.ActionType)
            {
            case "start":
                Execute(connectionId, data, ps, GetCancellationTokenSource(connectionId).Token, userName);
                break;

            case "stop":
                GetCancellationTokenSource(connectionId).Cancel();
                ps.State = StateType.Stop;
                return(Connection.Send(connectionId, ps.Clone()));

            default:
                break;
            }
            //}
            //catch (Exception e)
            //{
            //    ps.State = StateType.Error;
            //    ps.Messages.Add(e.Message);
            //}
            if (GetCancellationTokenSource(connectionId).Token.IsCancellationRequested)
            {
                ps.Messages.Clear();
                ps.Errors.Clear();
                ps.Messages.Add("用户已中止当前处理!");
            }
            ps.State = StateType.Complete;
            return(Connection.Send(connectionId, ps.Clone()));
        }
Пример #8
0
        public void SendingCommandObjectSetsCommandOnBus()
        {
            var messageBus = new Mock<IMessageBus>();
            var counters = new PerformanceCounterManager();
            Message message = null;
            messageBus.Setup(m => m.Publish(It.IsAny<Message>())).Returns<Message>(m =>
            {
                message = m;
                return TaskAsyncHelper.Empty;
            });

            var serializer = new JsonNetSerializer();
            var traceManager = new Mock<ITraceManager>();
            var connection = new Connection(messageBus.Object,
                                            serializer,
                                            "signal",
                                            "connectonid",
                                            new[] { "a", "signal", "connectionid" },
                                            new string[] { },
                                            traceManager.Object,
                                            new AckHandler(completeAcksOnTimeout: false,
                                                           ackThreshold: TimeSpan.Zero,
                                                           ackInterval: TimeSpan.Zero),
                                            counters);

            connection.Send("a", new Command
            {
                CommandType = CommandType.AddToGroup,
                Value = "foo"
            });

            Assert.NotNull(message);
            Assert.True(message.IsCommand);
            var command = serializer.Parse<Command>(message.Value);
            Assert.Equal(CommandType.AddToGroup, command.CommandType);
            Assert.Equal("foo", command.Value);
        }