예제 #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,
                                                                _cache,
                                                                _cacheContextAccessor,
                                                                _namedDependencyProvider,
                                                                _protocolManager,
                                                                message.ContextId);

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

                applicationContext.OnReceive(message);
            }
        }
예제 #2
0
        public void Post(Message message)
        {
            lock (_writer)
            {
                if (Logger.IsEnabled(LogLevel.Verbose))
                {
                    Logger.WriteVerbose(string.Format("[ProcessingQueue]: Post({0})", message));
                }

                _writer.Write(JsonConvert.SerializeObject(message));
            }
        }
예제 #3
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.");
            }
        }
예제 #4
0
        public bool Send(Message message)
        {
            lock (_writer)
            {
                try
                {
                    Trace.TraceInformation("[ProcessingQueue]: Send({0})", message);
                    _writer.Write(JsonConvert.SerializeObject(message));

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

            return false;
        }
        private void TriggerDependeees(string path, string messageType)
        {
            // temp: run [dnu|kpm] restore when project.json changed
            var project = _context.GetProject(path);
            if (project != null)
            {
            //                _packagesRestoreTool.Run(project);
            }

            var seen = new HashSet<string>();
            var results = new HashSet<int>();
            var stack = new Stack<string>();

            stack.Push(path);

            while (stack.Count > 0)
            {
                var projectPath = stack.Pop();

                if (!seen.Add(projectPath))
                {
                    continue;
                }

                int contextId;
                if (_context.ProjectContextMapping.TryGetValue(projectPath, out contextId))
                {
                    results.Add(contextId);

                    foreach (var frameworkProject in _context.Projects[contextId].ProjectsByFramework.Values)
                    {
                        foreach (var dependee in frameworkProject.ProjectDependeees.Keys)
                        {
                            stack.Push(dependee);
                        }
                    }
                }
            }

            foreach (var contextId in results)
            {
                var message = new Message();
                message.HostId = _context.HostId;
                message.ContextId = contextId;
                message.MessageType = messageType;
                _context.Connection.Post(message);
            }
        }
        public void GetDiagnostics(string projectPath)
        {
            int contextId;
            if (!_context.ProjectContextMapping.TryGetValue (projectPath, out contextId))
                throw new InvalidOperationException ("Unknown project.");

            var message = new Message();
            message.HostId = _context.HostId;
            message.ContextId = contextId;
            message.MessageType = "GetDiagnostics";
            try
            {
                _context.Connection.Post(message);
            }
            catch (IOException ex)
            {
                _logger.LogError("Post failed", ex);
            }
        }
 public void ChangeConfiguration(string config)
 {
     foreach (int contextId in _context.ProjectContextMapping.Values)
     {
         var message = new Message();
         message.HostId = _context.HostId;
         message.ContextId = contextId;
         message.MessageType = "ChangeConfiguration";
         var configMessage = new ChangeConfigurationMessage();
         configMessage.Configuration = config;
         message.Payload = JToken.FromObject(configMessage);
         try
         {
             _context.Connection.Post(message);
         }
         catch (IOException ex)
         {
             _logger.LogError("Post failed", ex);
         }
     }
 }
예제 #8
0
 public bool Transmit(Message message)
 {
     message.HostId = _hostId;
     return _queue.Send(message);
 }
예제 #9
0
 public bool IsProtocolNegotiation(Message message)
 {
     return message?.MessageType == NegotiationMessageTypeName;
 }
예제 #10
0
파일: Program.cs 프로젝트: elanwu123/dnx
        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;
            }
        }
예제 #11
0
        private void TriggerDependeees(string path)
        {
            var seen = new HashSet<string>();
            var results = new HashSet<int>();
            var stack = new Stack<string>();

            stack.Push(path);

            while (stack.Count > 0)
            {
                var projectPath = stack.Pop();

                if (!seen.Add(projectPath))
                {
                    continue;
                }

                int contextId;
                if (_context.ProjectContextMapping.TryGetValue(projectPath, out contextId))
                {
                    results.Add(contextId);

                    foreach (var frameworkProject in _context.Projects[contextId].ProjectsByFramework.Values)
                    {
                        foreach (var dependee in frameworkProject.ProjectDependeees.Keys)
                        {
                            stack.Push(dependee);
                        }
                    }
                }
            }

            foreach (var contextId in results)
            {
                var message = new Message();
                message.HostId = _context.HostId;
                message.ContextId = contextId;
                message.MessageType = "FilesChanged";
                _context.Connection.Post(message);
            }
        }