Exemplo n.º 1
0
        public void Activate(IChannelHandlerContext context)
        {
            this.context           = context;
            this.send_message_flag = true;

            this.executor_handler = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    if (this.send_message_flag)
                    {
                        Send();
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("Unhandled exception " + e.Message);
                }
            }, 10, 10);

            this.thread_send_message = new Thread(new ThreadStart(() =>
            {
                while (this.send_message_flag)
                {
                    try
                    {
                        if (this.message_queue.TryDequeue(out Message message))
                        {
                            this.context.WriteAndFlushAsync(message.GetSendData()).ContinueWith(task =>
                            {
                                if (!task.IsCompleted && !this.channel.IsDisconnect)
                                {
                                    Logger.Error(
                                        string.Format("Fail send to {0}, {1}", this.context.Channel.RemoteAddress, message));
                                }
                                else
                                {
                                    Logger.Debug(
                                        string.Format("Send message {0}", message.Type.ToString()));
                                }
                            });
                        }
                        else
                        {
                            Thread.Sleep(10);
                            continue;
                        }
                    }
                    catch (System.Exception e)
                    {
                        Logger.Error(
                            string.Format("Fail send to {0}, error info: {1}", this.context.Channel.RemoteAddress, e.Message));
                    }
                }
            }))
            {
                Name = "SendMsgThread - " + this.context.Channel.RemoteAddress,
            };
            this.thread_send_message.Start();
        }
Exemplo n.º 2
0
        public static ScheduledExecutorHandle Scheduled(IRunnable command, int due_time, int period)
        {
            ScheduledExecutorHandle handle = new ScheduledExecutorHandle(due_time, period);
            new Thread(handle.Process).Start(command);

            return handle;
        }
Exemplo n.º 3
0
        public void Init()
        {
            this.fetch_handle = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    if (this.is_fetch)
                    {
                        this.is_fetch = false;
                        StartFetchSyncBlock();
                    }
                }
                catch (System.Exception)
                {
                    Logger.Error("Fetch sync block error.");
                }
            }, 10 * 1000, 1 * 1000);

            this.block_handle = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    if (this.is_handle)
                    {
                        this.is_handle = false;
                        HandleSyncBlock();
                    }
                }
                catch (System.Exception)
                {
                    Logger.Error("Handle sync block error.");
                }
            }, 10 * 1000, 1 * 1000);
        }
Exemplo n.º 4
0
        public static ScheduledExecutorHandle Scheduled(Action action, int due_time, int period)
        {
            ScheduledExecutorHandle handle = new ScheduledExecutorHandle(due_time, period);
            new Thread(handle.Process).Start(action);

            return handle;
        }
Exemplo n.º 5
0
        public void Init()
        {
            if (Args.Instance.IsFastForward)
            {
                return;
            }

            this.handle_spread = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    ConsumerInventoryToSpread();
                }
                catch (System.Exception e)
                {
                    Logger.Error("Spread thread error. " + e.Message);
                }
            }, 100, 30);

            this.handle_fetch = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    ConsumerInventoryToFetch();
                }
                catch (System.Exception e)
                {
                    Logger.Error("Fetch thread error." + e.Message);
                }
            }, 100, 30);
        }
Exemplo n.º 6
0
        public void Init()
        {
            this.timer_pool = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    FillUp();
                }
                catch (System.Exception e)
                {
                    Logger.Error("Exception in sync worker", e);
                }
            }, 30 * 1000, 3600);

            this.timer_log = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    LogActivePeers();
                }
                catch
                {
                }
            }, 30000, 10000);
        }
Exemplo n.º 7
0
        public void Start()
        {
            this.discover = ScheduledExecutorService.Scheduled(
                new DiscoverTask(this.node_manager), 1, (int)KademliaOptions.DISCOVER_CYCLE * 1000);

            this.refresh = ScheduledExecutorService.Scheduled(
                new RefreshTask(this.node_manager), 1, (int)KademliaOptions.BUCKET_REFRESH);
        }
Exemplo n.º 8
0
 public void Close()
 {
     if (this.timer_ping != null && !this.timer_ping.IsCanceled)
     {
         this.timer_ping.Cancel();
         this.timer_ping = null;
     }
 }
Exemplo n.º 9
0
 public override void HandlerAdded(IChannelHandlerContext context)
 {
     this.timer_ping = ScheduledExecutorService.Scheduled(() =>
     {
         if (!this.has_ping)
         {
             this.send_ping_time = Helper.CurrentTimeMillis();
             this.has_ping       = this.message_quque.SendMessage(new PingMessage());
         }
     }, 10 * 1000, 10 * 1000);
 }
Exemplo n.º 10
0
 public void Init()
 {
     this.handler_peer_status = ScheduledExecutorService.Scheduled(() =>
     {
         try
         {
             StatusCheck();
         }
         catch (System.Exception e)
         {
             Logger.Error("Unhandled exception. " + e.Message);
         }
     }, 5000, 2000);
 }
Exemplo n.º 11
0
 private void HandleSmartContract()
 {
     this.handle_contract = ScheduledExecutorService.Scheduled(() =>
     {
         try
         {
             while (wait_queue.Count < MAX_SMART_CONTRACT_SUBMIT_SIZE)
             {
                 if (this.contract_queue.TryDequeue(out TxEvent tx_event))
                 {
                     ThreadPool.QueueUserWorkItem(new WaitCallback(HandleTransaction), new object[] { tx_event.Peer, tx_event.Message });
                 }
             }
         }
         catch (System.Exception)
         {
             Logger.Error("Handle smart contract exception.");
         }
     }, 1000, 20);
 }
Exemplo n.º 12
0
        public void Init()
        {
            Logger.Info(
                string.Format("Fast forward config, isWitness: {0}, keySize: {1}, fastForwardNodes: {2}",
                              Args.Instance.IsWitness,
                              this.key_size,
                              this.nodes.Count));

            if (!Args.Instance.IsWitness ||
                this.key_size == 0 ||
                this.nodes.Count == 0)
            {
                return;
            }

            this.nodes          = Args.Instance.Node.FastForward.Count > 0 ? Args.Instance.Node.FastForward : this.nodes;
            this.handle_service = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    if (Manager.Instance.DBManager.Witness.Get(this.witness_address) != null &&
                        Manager.Instance.BackupManager.Status == BackupManager.BackupStatus.MASTER &&
                        !WitnessService.IsNeedSyncCheck)
                    {
                        Connect();
                    }
                    else
                    {
                        Disconnect();
                    }
                }
                catch (System.Exception e)
                {
                    Logger.Info("Execute failed." + e.Message);
                }
            }, 0, 60 * 1000);
        }
Exemplo n.º 13
0
        public void Close()
        {
            this.send_message_flag = false;

            if (this.executor_handler != null && !this.executor_handler.IsCanceled)
            {
                this.executor_handler.Cancel();
                this.executor_handler = null;
            }

            if (this.thread_send_message != null)
            {
                try
                {
                    this.thread_send_message.Join(20);
                    this.thread_send_message = null;
                }
                catch
                {
                    Logger.Warning(
                        string.Format("Join send thread failed, peer {0}", this.context.Channel.RemoteAddress));
                }
            }
        }
Exemplo n.º 14
0
        public void ChannelActivated()
        {
            if (this.is_inited)
            {
                return;
            }

            this.is_inited = true;

            try
            {
                IPAddress address = Dns.GetHostAddresses(Dns.GetHostName())
                                    .FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);

                this.local_ip = address.ToString();
            }
            catch
            {
                Logger.Warning("Get local ip failed.");
            }

            foreach (String member in Args.Instance.Node.Backup.Members)
            {
                if (!this.local_ip.Equals(member))
                {
                    members.Add(member);
                }
            }

            Logger.Info(
                string.Format("Backup localIp:{0}, members: size= {1}, {2}",
                              this.local_ip,
                              members.Count,
                              members));

            this.status = BackupStatus.INIT;
            this.last_keep_alive_time = Helper.CurrentTimeMillis();

            this.service_handler = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    if (this.status != BackupStatus.MASTER &&
                        Helper.CurrentTimeMillis() - this.last_keep_alive_time > this.keep_alive_timeout)
                    {
                        if (this.status == BackupStatus.SLAVER)
                        {
                            this.status = BackupStatus.INIT;
                            this.last_keep_alive_time = Helper.CurrentTimeMillis();
                        }
                        else
                        {
                            this.status = BackupStatus.MASTER;
                        }
                    }

                    if (this.status == BackupStatus.SLAVER)
                    {
                        return;
                    }

                    foreach (string member in this.members)
                    {
                        this.message_handler.Accept(
                            new UdpEvent(new KeepAliveMessage(this.status.Equals(BackupStatus.MASTER), priority),
                                         new IPEndPoint(IPAddress.Parse(member), port)));
                    }
                }
                catch (System.Exception e)
                {
                    Logger.Error("Exception in send keep alive message : " + e.Message);
                }
            }, 1000, 1000);
        }