Ejemplo n.º 1
0
        public void OnReceive(Message message)
        {
            // Mark the sender on the incoming message
            message.Sender = this;

            if (_protocolManager.IsProtocolNegotiation(message))
            {
                _protocolManager.Negotiate(message);
            }
            else
            {
                ApplicationContext applicationContext;
                if (!_contexts.TryGetValue(message.ContextId, out applicationContext))
                {
                    Logger.TraceInformation("[ConnectionContext]: Creating new application context for {0}", message.ContextId);

                    applicationContext = new ApplicationContext(_services,
                                                                _applicationEnvironment,
                                                                _runtimeEnvironment,
                                                                _loadContextAccessor,
                                                                _protocolManager,
                                                                _compilationEngine,
                                                                _frameworkResolver,
                                                                message.ContextId);

                    _contexts.Add(message.ContextId, applicationContext);
                }

                applicationContext.OnReceive(message);
            }
        }
Ejemplo n.º 2
0
        public void OnReceive(Message message)
        {
            // Mark the sender on the incoming message
            message.Sender = this;

            if (_protocolManager.IsProtocolNegotiation(message))
            {
                _protocolManager.Negotiate(message);
            }
            else
            {
                ApplicationContext applicationContext;
                if (!_contexts.TryGetValue(message.ContextId, out applicationContext))
                {
                    Logger.TraceInformation("[ConnectionContext]: Creating new application context for {0}", message.ContextId);

                    applicationContext = new ApplicationContext(_services,
                                                                _protocolManager,
                                                                new CompilationEngineFactory(_cache),
                                                                message.ContextId);

                    _contexts.Add(message.ContextId, applicationContext);
                }

                applicationContext.OnReceive(message);
            }
        }
Ejemplo n.º 3
0
        public void IsProtocolNegotiationPositive()
        {
            var mgr = new ProtocolManager(0);
            var message = new Message
            {
                MessageType = ProtocolManager.NegotiationMessageTypeName
            };

            Assert.True(mgr.IsProtocolNegotiation(message));
        }
Ejemplo n.º 4
0
        public void NegotiateMissingVersion()
        {
            var mgr = new ProtocolManager(5);
            var message = new Message
            {
                MessageType = ProtocolManager.NegotiationMessageTypeName
            };

            Assert.Equal(1, mgr.CurrentVersion);
        }
Ejemplo n.º 5
0
        public void NegotiateVersionZeroIsIgnored()
        {
            var mgr = new ProtocolManager(5);
            var message = new Message
            {
                MessageType = ProtocolManager.NegotiationMessageTypeName,
                Payload = JToken.FromObject(new { Version = 0 })
            };

            Assert.Equal(1, mgr.CurrentVersion);
        }
Ejemplo n.º 6
0
        public void Negotiate(Message message)
        {
            if (!IsProtocolNegotiation(message))
            {
                return;
            }

            Logger.TraceInformation($"[{nameof(ProtocolManager)}]: Initializing the protocol negotiation.");

            if (EnvironmentOverridden)
            {
                Logger.TraceInformation($"[{nameof(ProtocolManager)}]: DTH protocol negotiation is override by environment variable {EnvDthProtocol} and set to {CurrentVersion}.");
                return;
            }

            var tokenValue = message.Payload?["Version"];
            if (tokenValue == null)
            {
                Logger.TraceInformation($"[{nameof(ProtocolManager)}]: Protocol negotiation failed. Version property is missing in payload.");
                return;
            }

            var preferredVersion = tokenValue.Value<int>();
            if (preferredVersion == 0)
            {
                // the preferred version can't be zero. either property is missing or the the payload is corrupted.
                Logger.TraceInformation($"[{nameof(ProtocolManager)}]: Protocol negotiation failed. Protocol version 0 is invalid.");
                return;
            }

            CurrentVersion = Math.Min(preferredVersion, MaxVersion);
            Logger.TraceInformation($"[{nameof(ProtocolManager)}]: Protocol negotiation successed. Use protocol {CurrentVersion}");

            if (message.Sender != null)
            {
                Logger.TraceInformation($"[{nameof(ProtocolManager)}]: Respond to protocol negotiation.");
                message.Sender.Transmit(new Message
                {
                    ContextId = 0,
                    MessageType = NegotiationMessageTypeName,
                    Payload = JToken.FromObject(new { Version = CurrentVersion })
                });
            }
            else
            {
                Logger.TraceWarning($"[{nameof(ProtocolManager)}]: {nameof(Message.Sender)} is null.");
            }
        }
Ejemplo n.º 7
0
        public void NegotiateVersion(int highestVersion, int requestVersion, int expectResult)
        {
            var mgr = new ProtocolManager(highestVersion);
            var message = new Message
            {
                ContextId = 0,
                HostId = Guid.NewGuid().ToString(),
                MessageType = ProtocolManager.NegotiationMessageTypeName,
                Sender = null,  // send is allowed to be null
                Payload = JToken.FromObject(new { Version = requestVersion })
            };

            mgr.Negotiate(message);

            Assert.Equal(expectResult, mgr.CurrentVersion);
        }
Ejemplo n.º 8
0
        public bool Send(Message message)
        {
            lock (_writer)
            {
                try
                {
                    Logger.TraceInformation("[ProcessingQueue]: Send({0})", message);
                    _writer.Write(JsonConvert.SerializeObject(message));

                    return true;
                }
                catch (IOException)
                {
                    // Swallow those
                }
                catch (Exception ex)
                {
                    Logger.TraceInformation("[ProcessingQueue]: Error sending {0}", ex);
                    throw;
                }
            }

            return false;
        }
Ejemplo n.º 9
0
 public bool Transmit(Message message)
 {
     message.HostId = _hostId;
     return _queue.Send(message);
 }
Ejemplo n.º 10
0
        private static void WriteProjectContexts(Message message, ProcessingQueue queue, IDictionary<int, ApplicationContext> contexts)
        {
            try
            {
                var projectContexts = contexts.Values.Select(p => new
                {
                    Id = p.Id,
                    ProjectPath = p.ApplicationPath
                })
                .ToList();

                var versionToken = message.Payload.HasValues ? message.Payload?["Version"] : null;
                var version = versionToken != null ? versionToken.Value<int>() : 0;

                queue.Send(writer =>
                {
                    if (version == 0)
                    {
                        writer.Write("ProjectContexts");
                        writer.Write(projectContexts.Count);
                        for (int i = 0; i < projectContexts.Count; i++)
                        {
                            writer.Write(projectContexts[i].ProjectPath);
                            writer.Write(projectContexts[i].Id);
                        }
                    }
                    else
                    {
                        var obj = new JObject();
                        obj["MessageType"] = "ProjectContexts";
                        var projects = new JObject();
                        obj["Projects"] = projects;

                        foreach (var pair in projectContexts)
                        {
                            projects[pair.ProjectPath] = pair.Id;
                        }

                        writer.Write(obj.ToString(Formatting.None));
                    }
                });
            }
            catch (Exception ex)
            {
                var error = new JObject();
                error["Message"] = ex.Message;

                queue.Send(new Message
                {
                    MessageType = "Error",
                    Payload = error
                });

                throw;
            }
        }
Ejemplo n.º 11
0
 public bool IsProtocolNegotiation(Message message)
 {
     return message?.MessageType == NegotiationMessageTypeName;
 }
Ejemplo n.º 12
0
        public void IsProtocolNegotiationWrongMessageTypeName()
        {
            var mgr = new ProtocolManager(4);
            var message = new Message
            {
                MessageType = "Initialization"
            };

            Assert.False(mgr.IsProtocolNegotiation(message));
        }