Ejemplo 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();
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public void SendPing()
        {
            PingMessage message = new PingMessage(this.node_manager.PublicHomeNode, this.node);

            this.ping_sequence = message.Timestamp;
            this.wait_pong     = true;
            this.ping_sent     = Helper.CurrentTimeMillis();

            SendMessage(message);

            if (this.node_manager.TimerPong != null && this.node_manager.TimerPong.IsShutdown)
            {
                return;
            }

            this.node_manager.TimerPong = ScheduledExecutorService.Scheduled(() =>
            {
                try
                {
                    if (this.wait_pong)
                    {
                        this.wait_pong = false;
                        HandleTimedOut();
                    }
                }
                catch (System.Exception e)
                {
                    Logger.Error("Unhandled exception " + e.Message);
                }
            }, (int)PingTimeout, (int)PingTimeout);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
 }
Ejemplo n.º 8
0
 public void Init()
 {
     this.handler_peer_status = ScheduledExecutorService.Scheduled(() =>
     {
         try
         {
             StatusCheck();
         }
         catch (System.Exception e)
         {
             Logger.Error("Unhandled exception. " + e.Message);
         }
     }, 5000, 2000);
 }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
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);
        }