Exemplo n.º 1
0
        private void ForkIndexedStack(
            string sourceNamespace,
            string destinationNamespace,
            long branchPointIndex,
            IImmutableSet <Address> addressesToStrip,
            Func <string, string> getPath,
            Func <string, Address, string> getAddressPath,
            Func <Stream, IEnumerable <long> > getIndexedStack)
        {
            string sourceDir = getPath(sourceNamespace);
            string targetDir = getPath(destinationNamespace);
            bool   copied    = CopyDirectory(sourceDir, targetDir);

            if (!copied && addressesToStrip.Any())
            {
                throw new NamespaceNotFoundException(
                          sourceNamespace,
                          "The source namespace to be forked does not exist.");
            }

            foreach (Address address in addressesToStrip)
            {
                StripIndexedStack(
                    getAddressPath(destinationNamespace, address),
                    branchPointIndex,
                    getIndexedStack);
            }
        }
Exemplo n.º 2
0
        private static bool IsAppProtocolVersionValid(
            Peer remotePeer,
            AppProtocolVersion localVersion,
            AppProtocolVersion remoteVersion,
            IImmutableSet <PublicKey> trustedAppProtocolVersionSigners,
            DifferentAppProtocolVersionEncountered differentAppProtocolVersionEncountered)
        {
            if (remoteVersion.Equals(localVersion))
            {
                return(true);
            }

            if (!(trustedAppProtocolVersionSigners is null) &&
                !trustedAppProtocolVersionSigners.Any(remoteVersion.Verify))
            {
                return(false);
            }

            if (differentAppProtocolVersionEncountered is null)
            {
                return(false);
            }

            return(differentAppProtocolVersionEncountered(remotePeer, remoteVersion, localVersion));
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override void ForkStateReferences <T>(
            string sourceNamespace,
            string destinationNamespace,
            Block <T> branchPoint,
            IImmutableSet <Address> addressesToStrip)
        {
            string sourceDir = GetStateReferencePath(sourceNamespace);
            string targetDir = GetStateReferencePath(destinationNamespace);
            bool   copied    = CopyDirectory(sourceDir, targetDir);

            if (!copied && addressesToStrip.Any())
            {
                throw new NamespaceNotFoundException(
                          sourceNamespace,
                          "The source namespace to be forked does not exist.");
            }

            foreach (Address address in addressesToStrip)
            {
                StripStateReference(
                    destinationNamespace,
                    address,
                    branchPoint.Index);
            }
        }
Exemplo n.º 4
0
        public CancelSubscription Subscribe <TEvent>(Action <TEvent> eventHandler)
        {
            subscriptions.AddOrUpdate(
                typeof(TEvent),
                t => ImmutableHashSet.Create <object>(eventHandler),
                (t, l) => l.Add(eventHandler));

            return(() =>
            {
                IImmutableSet <object> comparisonValue;
                while (subscriptions.TryGetValue(typeof(TEvent), out comparisonValue))
                {
                    if (comparisonValue.Contains(eventHandler))
                    {
                        IImmutableSet <object> newValue = comparisonValue.Remove(eventHandler);
                        if (newValue.Any())
                        {
                            if (subscriptions.TryUpdate(typeof(TEvent), newValue, comparisonValue))
                            {
                                return;
                            }
                        }
                        else
                        {
                            if (((ICollection <KeyValuePair <Type, IImmutableSet <object> > >)subscriptions).Remove(new KeyValuePair <Type, IImmutableSet <object> >(
                                                                                                                        typeof(TEvent),
                                                                                                                        comparisonValue)))
                            {
                                return;
                            }
                        }
                    }
                }
            });
        }
        public Task<CommandStatusResult> GetCommandStatusAsync(IImmutableSet<IProjectTree> nodes, long commandId, bool focused, string commandText, CommandStatus status) {
            if (_session.IsHostRunning && _commandIds.Contains(commandId)) {
                if (nodes.Any(IsRData)) {
                    status |= CommandStatus.Supported | CommandStatus.Enabled;
                    return Task.FromResult(new CommandStatusResult(true, commandText, status));
                }
            }

            return Task.FromResult(CommandStatusResult.Unhandled);
        }
Exemplo n.º 6
0
        public Task <CommandStatusResult> GetCommandStatusAsync(IImmutableSet <IProjectTree> nodes, long commandId, bool focused, string commandText, CommandStatus status)
        {
            if (_session.IsHostRunning && _commandIds.Contains(commandId))
            {
                if (nodes.Any(IsRData))
                {
                    status |= CommandStatus.Supported | CommandStatus.Enabled;
                    return(Task.FromResult(new CommandStatusResult(true, commandText, status)));
                }
            }

            return(Task.FromResult(CommandStatusResult.Unhandled));
        }
Exemplo n.º 7
0
        public CommandStatusResult GetCommandStatus(IImmutableSet <IProjectTree> items, long commandId, bool focused, string?commandText, CommandStatus progressiveStatus)
        {
            if (CanAddReference(commandId))
            {
                progressiveStatus &= ~CommandStatus.Invisible;
                progressiveStatus |= CommandStatus.Enabled | CommandStatus.Supported;

                if (items.Any(tree => tree.IsFolder))
                {   // Hide these commands for Folder -> Add
                    progressiveStatus |= CommandStatus.InvisibleOnContextMenu;
                }

                return(new CommandStatusResult(handled: true, commandText, progressiveStatus));
            }

            return(CommandStatusResult.Unhandled);
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        public override void ForkStateReferences <T>(
            string srcNamespace,
            string destNamespace,
            Block <T> branchPoint,
            IImmutableSet <Address> addressesToStrip)
        {
            string srcCollId = StateRefId(srcNamespace);
            string dstCollId = StateRefId(destNamespace);
            LiteCollection <StateRefDoc> srcColl = _db.GetCollection <StateRefDoc>(srcCollId),
                                         dstColl = _db.GetCollection <StateRefDoc>(dstCollId);

            dstColl.InsertBulk(srcColl.Find(Query.LTE("BlockIndex", branchPoint.Index)));

            if (dstColl.Count() < 1 && addressesToStrip.Any())
            {
                throw new NamespaceNotFoundException(
                          srcNamespace,
                          "The source namespace to be forked does not exist."
                          );
            }
        }
        protected override Try <JournalRow> Serialize(IPersistentRepresentation persistentRepr, IImmutableSet <string> tTags, long timeStamp = 0)
        {
            try
            {
                var serializer = _serializer.FindSerializerForType(persistentRepr.Payload.GetType(), _journalConfig.DefaultSerializer);
                // TODO: hack. Replace when https://github.com/akkadotnet/akka.net/issues/3811
                string manifest = "";
                var    binary   = Akka.Serialization.Serialization.WithTransport(_serializer.System, () =>
                {
                    if (serializer is SerializerWithStringManifest stringManifest)
                    {
                        manifest =
                            stringManifest.Manifest(persistentRepr.Payload);
                    }
                    else
                    {
                        if (serializer.IncludeManifest)
                        {
                            manifest = persistentRepr.Payload.GetType().TypeQualifiedName();
                        }
                    }

                    return(serializer.ToBinary(persistentRepr.Payload));
                });
                return(new Try <JournalRow>(new JournalRow()
                {
                    manifest = manifest,
                    message = binary,
                    persistenceId = persistentRepr.PersistenceId,
                    tags = tTags.Any()?  tTags.Aggregate((tl, tr) => tl + _separator + tr) : "",
                    Identifier = serializer.Identifier,
                    sequenceNumber = persistentRepr.SequenceNr,
                    Timestamp = persistentRepr.Timestamp == 0?  timeStamp: persistentRepr.Timestamp
                }));
            }
            catch (Exception e)
            {
                return(new Try <JournalRow>(e));
            }
        }
        public void ProcessNotification(AuditableAction action, Dictionary <string, string> tokens, IImmutableSet <string> notificationChannelIDs)
        {
            List <Exception> rethrowableExceptions = new List <Exception>();

            foreach (var channel in this.NotificationChannelDefinitions)
            {
                if (notificationChannelIDs.Any(t => string.Equals(t, channel.Id, StringComparison.OrdinalIgnoreCase)))
                {
                    if (!channel.Enabled)
                    {
                        this.logger.LogTrace($"Skipping delivery of audit notification to {channel.Id} as it is currently disabled");
                        continue;
                    }

                    try
                    {
                        if (channel.Mandatory && action.IsSuccess)
                        {
                            Send(action, tokens, channel, true);
                        }
                        else
                        {
                            this.queue.TryWrite(() => Send(action, tokens, channel, false));
                        }
                    }
                    catch (Exception ex)
                    {
                        rethrowableExceptions.Add(ex);
                    }
                }
            }

            if (action.IsSuccess && rethrowableExceptions.Count > 0)
            {
                throw new AuditLogFailureException(rethrowableExceptions);
            }
        }
Exemplo n.º 11
0
 private bool IsCompoundExpression(SyntaxNode node)
 {
     return(compoundExpressionKinds.Any(node.IsKind));
 }
Exemplo n.º 12
0
 private bool IsDurable(string key) => _durableKeys.Contains(key) || (_durableWildcards.Count > 0 && _durableWildcards.Any(key.StartsWith));
Exemplo n.º 13
0
        /// <inheritdoc/>
        public void ForkStateReferences <T>(
            string srcNamespace,
            string destNamespace,
            Block <T> branchPoint,
            IImmutableSet <Address> addressesToStrip)
            where T : IAction, new()
        {
            long branchPointIndex     = branchPoint.Index;
            List <LiteFileInfo> files =
                _db.FileStorage
                .Find($"{StateRefIdPrefix}{srcNamespace}")
                .ToList();

            if (!files.Any() && addressesToStrip.Any())
            {
                throw new NamespaceNotFoundException(
                          srcNamespace,
                          "The source namespace to be forked does not exist.");
            }

            foreach (LiteFileInfo srcFile in files)
            {
                string destId =
                    $"{StateRefIdPrefix}{destNamespace}/{srcFile.Filename}";
                _db.FileStorage.Upload(
                    destId,
                    srcFile.Filename,
                    new MemoryStream());

                LiteFileInfo destFile = _db.FileStorage.FindById(destId);
                using (LiteFileStream srcStream = srcFile.OpenRead())
                    using (LiteFileStream destStream = destFile.OpenWrite())
                    {
                        while (srcStream.Position < srcStream.Length)
                        {
                            var hashBytes  = new byte[HashDigest <SHA256> .Size];
                            var indexBytes = new byte[sizeof(long)];

                            srcStream.Read(hashBytes, 0, hashBytes.Length);
                            srcStream.Read(indexBytes, 0, indexBytes.Length);

                            long currentIndex =
                                BitConverter.ToInt64(indexBytes, 0);

                            if (currentIndex <= branchPointIndex)
                            {
                                destStream.Write(hashBytes, 0, hashBytes.Length);
                                destStream.Write(indexBytes, 0, indexBytes.Length);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                if (destFile.Length == 0)
                {
                    _db.FileStorage.Delete(destId);
                }
            }
        }
Exemplo n.º 14
0
 private bool IsCompoundExpression(SyntaxNode node)
 {
     return(CompoundExpressionKinds.Any(k => node.IsKind(k)));
 }
Exemplo n.º 15
0
        Run(FileInfo mainScriptFile, string[] argv,
            bool inspect, bool pauseDebuggerOnStart,
            IImmutableSet <string> inspectLoadSet,
            Func <CancellationToken, Task <string> > f,
            Process parentProcess,
            bool verbose)
        {
            var rootDir = mainScriptFile.Directory;

            Debug.Assert(rootDir != null);

            var settings = new V8Settings
            {
                EnableDebugging =
                    inspect || pauseDebuggerOnStart ||
                    inspectLoadSet.Any(),
                AwaitDebuggerAndPauseOnStart =
                    pauseDebuggerOnStart || inspectLoadSet.Any(),
            };

            using (var engine = new V8JsEngine(settings))
            {
                var scheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler;
                var console   = ConsoleService.Default;

                void Load(string module)
                {
                    var path   = Path.Combine(rootDir.FullName, module);
                    var source = File.ReadAllText(path);

                    if (inspectLoadSet.Contains(source))
                    {
                        source = "debugger;" + source;
                    }
                    engine.Execute(source, module);
                }

                using (var host = new Host(Load, console, scheduler))
                {
                    string FormatMessage(object sender, string message)
                    {
                        var senderName = sender is string s ? s : sender.GetType().Name;
                        var formatted  = $"{senderName}[{Thread.CurrentThread.ManagedThreadId}]: {message}";

                        return(formatted.FormatFoldedLines().TrimNewLineAtTail());
                    }

                    void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args) =>
                    console.Error(FormatMessage(sender, args.Exception.ToString()));

                    TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;

                    var infoLog = !verbose ? null : new LogEventHandler((sender, message) =>
                                                                        host.Console.Info(FormatMessage(sender, message)));

                    var warnLog = !verbose ? null : new ErrorLogEventHandler((sender, e, message) =>
                                                                             host.Console.Warn(FormatMessage(sender, e == null ? message : string.IsNullOrEmpty(message) ? e.ToString() : message + Environment.NewLine + e)));

                    var errorLog = !verbose ? null : new ErrorLogEventHandler((sender, e, message) =>
                                                                              host.Console.Error(FormatMessage(sender, string.IsNullOrEmpty(message) ? e.ToString() : message + Environment.NewLine + e)));

                    foreach (var service in new ILogSource[] { host, host.Timer, host.Xhr })
                    {
                        service.InfoLog  = infoLog;
                        service.WarnLog  = warnLog;
                        service.ErrorLog = errorLog;
                    }

                    var tasks = new List <NamedTask>();

                    void AddTask(NamedTask task)
                    {
                        lock (tasks) tasks.Add(task);
                    }

                    void RemoveTask(NamedTask task)
                    {
                        lock (tasks) tasks.Remove(task);
                    }

                    host.TaskStarting  += (_, task) => AddTask(task);
                    host.TaskFinishing += (_, task) => RemoveTask(task);

                    if (verbose)
                    {
                        host.ServiceCreated += (_, service) =>
                        {
                            service.InfoLog  = infoLog;
                            service.WarnLog  = warnLog;
                            service.ErrorLog = errorLog;
                        };
                    }

                    engine.EmbedHostObject("host", host);
                    var     initScript = GetManifestResourceStream("init.js", typeof(Program)).ReadAsText();
                    dynamic init       = engine.Evaluate(initScript, "__init.js");
                    init(host, engine.Evaluate("this"), argv);

                    if (settings.AwaitDebuggerAndPauseOnStart)
                    {
                        console.Warn(FormatMessage(nameof(Program), "Will wait for debugger to attach."));
                    }

                    Load(mainScriptFile.Name);

                    void Schedule(string name, Action <AsyncTaskControl> action)
                    {
                        var task = AsyncTask.Create(name, thisTask =>
                        {
                            Exception error = null;
                            try
                            {
                                action(thisTask);
                            }
                            catch (Exception e)
                            {
                                error = e;
                            }
                            RemoveTask(thisTask);
                            switch (error)
                            {
                            case null:
                                thisTask.FlagSuccess();
                                break;

                            case OperationCanceledException e:
                                thisTask.FlagCanceled(e.CancellationToken);
                                break;

                            default:
                                errorLog?.Invoke(nameof(Program), error, null);
                                thisTask.FlagError(error);
                                break;
                            }
                        });

                        AddTask(task);
                        task.Start(scheduler);
                    }

                    var parentProcessTask = parentProcess.AsTask(dispose: true, p => p.ExitCode,
                                                                 _ => null,
                                                                 exit => exit);

                    while (true)
                    {
                        var readCommandTask = f(CancellationToken.None);
                        if (parentProcessTask != null)
                        {
                            if (parentProcessTask == await Task.WhenAny(readCommandTask, parentProcessTask))
                            {
                                break;
                            }
                        }
                        else
                        {
                            await readCommandTask;
                        }

                        var command = (await readCommandTask)?.Trim();

                        if (command == null)
                        {
                            break;
                        }

                        if (command.Length == 0)
                        {
                            continue;
                        }

                        const string ondata = "ondata";
                        Schedule(ondata, delegate
                        {
                            infoLog?.Invoke(nameof(Program), "STDIN: " + command);
                            engine.CallFunction(ondata, command);
                        });
                    }

                    const string onclose = "onclose";
                    Schedule(onclose, delegate
                    {
                        engine.Execute(@"if (typeof onclose === 'function') onclose();");
                    });

                    host.Timer.CancelAll();
                    host.Xhr.AbortAll();

                    infoLog?.Invoke(typeof(Program), "Shutting down...");

                    ImmutableArray <Task> tasksSnapshot;

                    lock (tasks)
                        tasksSnapshot = ImmutableArray.CreateRange(from t in tasks select t.Task);

                    if (await tasksSnapshot.WhenAll(TimeSpan.FromSeconds(30)))
                    {
                        Debug.Assert(tasks.Count == 0);
                    }
                    else
                    {
                        warnLog?.Invoke(typeof(Program), null, "Timed-out waiting for all tasks to end for a graceful shutdown!");
                    }

                    infoLog?.Invoke(typeof(Program), "Shutdown completed.");

                    TaskScheduler.UnobservedTaskException -= OnUnobservedTaskException;
                }
            }
        }