Ejemplo n.º 1
0
        /// <summary>
        /// Adds a result. This is called by the receive thread. It will iterate over all registered
        /// Wait and Callbacks to find a matching one. When a match is found, it will either trigger the Wait, 
        /// or call the callback.
        /// </summary>
        /// <param name="newReturnedObject">
        /// The object returned from the server
        /// </param>
        public void AddResult(ReturnedObject newReturnedObject)
        {
            var callbacksToRemove = new List<RegisteredResultCallback>();

            var waitsToIterate = this.waits.ToArray();
            var callbacksToIterate = this.callbacks.ToArray();

            foreach (var wait in waitsToIterate)
            {
                wait.Value.Filter.HandleReturnObject(newReturnedObject);

                if (wait.Value.Filter.IsCompleted())
                {
                    wait.Value.WaitEvent.Set();
                    wait.Key.SetTriggered();
                }
            }

            foreach (var callback in callbacksToIterate)
            {
                callback.Key.Filter.HandleReturnObject(newReturnedObject);
                if (callback.Key.Filter.IsCompleted())
                {
                    callback.Key.Callback(newReturnedObject);
                    callbacksToRemove.Add(callback.Key);
                }
            }

            foreach (var removedCallback in callbacksToRemove)
            {
                int outInt;
                this.callbacks.TryRemove(removedCallback, out outInt);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The handle return object.
        /// </summary>
        /// <param name="returnedMessage">
        /// The returned object.
        /// </param>
        internal override void HandleReturnObject(ReturnedObject returnedMessage)
        {
            if (returnedMessage.MessageType == "nosub")
            {
                var noSubObject = returnedMessage.ParsedObject.ToObject<NoSubscribe>();

                if (noSubObject.ID == this.subscriptionId)
                {
                    this.nosubCalled = true;
                    this.returnedObject = returnedMessage;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Processes the reply message
        /// </summary>
        /// <param name="client">
        /// The connection to send replies to the server
        /// </param>
        /// <param name="collectionManager">
        /// The CollectionManager
        /// </param>
        /// <param name="resultHandler">
        /// The ResultHandler
        /// </param>
        /// <param name="message">
        /// The message to process
        /// </param>
        /// <returns>
        /// Task to process the message
        /// </returns>
        public Task HandleMessage(
            IDdpConnectionSender client, 
            ICollectionManager collectionManager, 
            IResultHandler resultHandler, 
            string message)
        {
            JObject parsedObject = JObject.Parse(message);
            var result = new ReturnedObject((string)parsedObject["msg"], parsedObject, message);

            resultHandler.AddResult(result);

            return Task.FromResult(true);
        }
Ejemplo n.º 4
0
        public void ConnectedResultFilter_HandleReturnObject_UnknownMessage()
        {
            var connectedResultFilter = new ConnectedResultFilter();

            var ping = new Ping();

            var returnedObject = new ReturnedObject("ping", JObject.FromObject(ping), JsonConvert.SerializeObject(ping));

            connectedResultFilter.HandleReturnObject(returnedObject);

            Assert.IsFalse(connectedResultFilter.IsCompleted());
            Assert.IsNull(connectedResultFilter.GetReturnedObject());
        }
Ejemplo n.º 5
0
        public void ConnectedResultFilter_HandleReturnObject_FailedMessage()
        {
            var connectedResultFilter = new ConnectedResultFilter();

            var failed = new Failed() {Version = "1"};

            var returnedObject = new ReturnedObject("failed", JObject.FromObject(failed),
                JsonConvert.SerializeObject(failed));

            connectedResultFilter.HandleReturnObject(returnedObject);

            Assert.IsTrue(connectedResultFilter.IsCompleted());
            Assert.AreEqual(returnedObject, connectedResultFilter.GetReturnedObject());
        }
Ejemplo n.º 6
0
        public void ResultHandler_WaitForResult_WaitHandler()
        {
            var resultHandler = new ResultHandler();
            var waitHandle = resultHandler.RegisterWaitHandler(ResultFilterFactory.CreateConnectResultFilter());

            var waitTask = resultHandler.WaitForResult(waitHandle);

            var connected = new Connected() {Session = "TestSession"};
            var returnedObject = new ReturnedObject(connected.MessageType, JObject.FromObject(connected),
                JsonConvert.SerializeObject(connected));

            resultHandler.AddResult(returnedObject);

            waitTask.Wait();

            Assert.AreEqual(returnedObject, waitTask.Result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Handles a message received from the server
        /// </summary>
        /// <param name="messageReturned">
        /// The returned object.
        /// </param>
        internal override void HandleReturnObject(ReturnedObject messageReturned)
        {
            if (messageReturned.MessageType == "result" && ((string)messageReturned.ParsedObject["id"]) == this.methodId)
            {
                this.resultCalled = true;
                this.returnedObject = messageReturned;
            }
            else if (messageReturned.MessageType == "updated")
            {
                var updatedObject = messageReturned.ParsedObject.ToObject<Updated>();

                if (updatedObject.Methods.Contains(this.methodId))
                {
                    this.updatedCalled = true;
                }
            }
        }
Ejemplo n.º 8
0
        public void ResultHandler_RegisterResultCallback_CallbackCalled()
        {
            var resultHandler = new ResultHandler();

            bool callbackCalled = false;
            var connected = new Connected() { Session = "TestSession" };
            var returnedObject = new ReturnedObject(connected.MessageType, JObject.FromObject(connected),
                JsonConvert.SerializeObject(connected));

            resultHandler.RegisterResultCallback(ResultFilterFactory.CreateConnectResultFilter(), o =>
            {
                callbackCalled = true;
                Assert.AreEqual(returnedObject, o);
            });

            resultHandler.AddResult(returnedObject);

            Assert.IsTrue(callbackCalled);
        }
Ejemplo n.º 9
0
        public void ResultHandler_WaitForResult_ReactivateWaitHandle()
        {
            var resultHandler = new ResultHandler();
            var waitHandle = resultHandler.RegisterWaitHandler(ResultFilterFactory.CreateConnectResultFilter());

            var waitTask = resultHandler.WaitForResult(waitHandle);

            var connected = new Connected() { Session = "TestSession" };
            var returnedObject = new ReturnedObject(connected.MessageType, JObject.FromObject(connected),
                JsonConvert.SerializeObject(connected));

            resultHandler.AddResult(returnedObject);

            waitTask.Wait();

            Assert.AreEqual(returnedObject, waitTask.Result);

            PCLTesting.ExceptionAssert.Throws<InvalidOperationException>(() => resultHandler.WaitForResult(waitHandle));
        }
Ejemplo n.º 10
0
        public void MethodCallResultFilter_HandleReturnObject_NotHandledID()
        {
            var resultFilter = new MethodCallResultFilter("11");

            var message = new Result() { Id = "10" };
            var updated = new Updated() { Methods = new[] { "10" } };

            var resultReturnedObject = new ReturnedObject("result", JObject.FromObject(message),
                JsonConvert.SerializeObject(message));

            var updatedReturnedObject = new ReturnedObject("updated", JObject.FromObject(updated),
                JsonConvert.SerializeObject(updated));

            resultFilter.HandleReturnObject(updatedReturnedObject);

            Assert.IsFalse(resultFilter.IsCompleted());

            resultFilter.HandleReturnObject(resultReturnedObject);

            Assert.IsFalse(resultFilter.IsCompleted());
            Assert.IsNull(resultFilter.GetReturnedObject());
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Handles an object returned by the server
        /// </summary>
        /// <param name="messageReturned">
        /// The returned object.
        /// </param>
        internal override void HandleReturnObject(ReturnedObject messageReturned)
        {
            if (messageReturned.MessageType == "ready")
            {
                var readyObject = messageReturned.ParsedObject.ToObject<Ready>();

                if (readyObject.SubscriptionsReady.Contains(this.subscriptionId))
                {
                    this.readyCalled = true;
                    this.returnedObject = messageReturned;
                }
            }

            if (messageReturned.MessageType == "nosub")
            {
                var noSubObject = messageReturned.ParsedObject.ToObject<NoSubscribe>();

                if (noSubObject.ID == this.subscriptionId)
                {
                    this.nosubCalled = true;
                    this.returnedObject = messageReturned;
                }
            }
        }