/// <summary>
        /// Finally make current block time in the range of (expected_mining_time, expected_mining_time + time_for_each_block)
        /// </summary>
        /// <param name="miningInterval"></param>
        /// <param name="originExpectedMiningTime"></param>
        /// <param name="expectedMiningTime"></param>
        private void TuneExpectedMiningTimeForTinyBlock(int miningInterval, Timestamp originExpectedMiningTime,
                                                        ref Timestamp expectedMiningTime)
        {
            var timeForEachBlock = miningInterval.Div(AEDPoSContractConstants.TotalTinySlots);
            var currentBlockTime = Context.CurrentBlockTime;

            while (expectedMiningTime < currentBlockTime &&
                   expectedMiningTime < originExpectedMiningTime.AddMilliseconds(miningInterval))
            {
                expectedMiningTime = expectedMiningTime.AddMilliseconds(timeForEachBlock);
                var toPrint = expectedMiningTime.Clone();
                Context.LogDebug(() => $"Moving to next tiny block time slot. {toPrint}");
            }
        }
Exemplo n.º 2
0
        private static void HandleClient(Socket client, ImmutableArray <ConcurrentQueue <ICommand> > queues)
        {
            try
            {
                var visitor = new ExecuteCommandVisitor(() => _root, Factory, Initialize, Timestamp);
                var buffer  = new byte[32000];

                while (!visitor.Exit)
                {
                    var data            = client.ReceiveUntilEof(buffer);
                    var receivedCommand = data.To <ICommand>();

                    Console.WriteLine(receivedCommand);
                    receivedCommand.Accept(visitor);
                    var treeClone       = _root.Clone();
                    var timestampClone  = Timestamp.Clone();
                    var statefulCommand = new StatefulCommand(treeClone, receivedCommand, timestampClone);

                    Responses.Clear();

                    foreach (var queue in queues)
                    {
                        queue.Enqueue(statefulCommand);
                    }

                    if (visitor.AwaitResponse)
                    {
                        ReceiveAndPickResponse(client, receivedCommand);
                    }
                    else
                    {
                        SendResponse(client, receivedCommand, visitor);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemplo n.º 3
0
        public void LaunchOn(int port)
        {
            new Thread(() =>
            {
                var address      = IpAddressUtils.GetLocal();
                var local        = new IPEndPoint(address, port);
                using var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(local);
                socket.Listen(1);

                while (true)
                {
                    try
                    {
                        using var client = socket.Accept();
                        client.ReceiveUntilEof(new byte[32]);

                        lock (_mutex)
                        {
                            var tree       = LastTree?.Clone();
                            var timeStamp  = Timestamp?.Clone();
                            var backupData = new BackupData(tree, timeStamp);

                            if (tree != null)
                            {
                                var files = GetFiles(tree);
                                backupData.Files.AddRange(files);
                            }

                            client.SendCompletelyWithEof(backupData.ToBytes());
                            Console.WriteLine($"Send backup data of {backupData.Files.Count} files.");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }).Start();
        }