Beispiel #1
0
        /// <summary>
        /// 向服务器注册
        /// </summary>
        private bool RegsiterToServer()
        {
            // 先尝试检查远程服务器是否可以ping通
            bool bIsRomateCanPing = IsCanPingPass();

            if (!bIsRomateCanPing)
            {
                LOGGER.ERROR("Can't pass server ip ping test, will not use registe node and heart-beat");
                return(false);
            }

            // 开始注册
            SRegisteNode node = new SRegisteNode();

            node.bankCardNo = FKConfig.BankCardNo;
            node.ip         = string.IsNullOrEmpty(FKConfig.LocalIP) ? FKBaseUtils.FKSystemEnviSettingHelper.GetLocalIPAddressByUri(FKConfig.ServerRegisteUrl) : FKConfig.LocalIP;
            node.publicKey  = RSAKeyContainer.GetInstance.GetCSharpPublicKey();
            LOGGER.INFO($"Begin to register to server. Bank card No = {node.bankCardNo}, Ip = {node.ip}");
            HttpSendResult result = RequestSender.SendRequestToServer(node);

            if (result.IsSendSuccessed() && result.ErrorStatus == 1 && (!string.IsNullOrEmpty(result.publicKey)))
            {
                SetCurCashierNodeID(result.NodeID);
                RSAKeyContainer.GetInstance.SetJavaPublicKey(result.publicKey);
                LOGGER.INFO($"Register to server successed. Node id = {result.NodeID}");
                return(true);
            }
            else
            {
                SetCurCashierNodeID(INVALID_NODE_ID);
                LOGGER.ERROR($"Register to server failed. but still use registe node and heart-beat. Node id = {GetCurCashierNodeID()}");
                return(true);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 心跳函数
        /// </summary>
        private static void OnHeartbeatTimerEvent(Object source, ElapsedEventArgs e, string strHeartbeatUrl, MainForm form)
        {
            Thread.CurrentThread.Name = "FKHeartbeatThread";
            if (!FKConfig.IsUseHttpServer)
            {
                return;
            }
            try
            {
                if (form.GetCurCashierNodeID() == INVALID_NODE_ID)
                {
                    // NodeID无效,则重新注册
                    form.ClearLostHeartbeatCount();

                    SRegisteNode node = new SRegisteNode();
                    node.bankCardNo = FKConfig.BankCardNo;
                    node.ip         = string.IsNullOrEmpty(FKConfig.LocalIP) ? FKBaseUtils.FKSystemEnviSettingHelper.GetLocalIPAddressByUri(FKConfig.ServerRegisteUrl) : FKConfig.LocalIP;
                    node.publicKey  = RSAKeyContainer.GetInstance.GetCSharpPublicKey();
                    LOGGER.INFO($"Begin to register to server. Bank card No = {node.bankCardNo}, Ip = {node.ip}, C# public key = {node.publicKey}");
                    HttpSendResult result = RequestSender.SendRequestToServer(node);

                    if (result.IsSendSuccessed() && result.ErrorStatus == 1 && (!string.IsNullOrEmpty(result.publicKey)))
                    {
                        form.SetCurCashierNodeID(result.NodeID);
                        RSAKeyContainer.GetInstance.SetJavaPublicKey(result.publicKey);
                        LOGGER.INFO($"Register to server successed. Node id = {result.NodeID}, Java public key = {RSAKeyContainer.GetInstance.GetJavaPublicKey()}");
                        return;
                    }

                    LOGGER.ERROR($"Register to server failed. Node id = {result.NodeID}, Java public key = {RSAKeyContainer.GetInstance.GetJavaPublicKey()}");
                    return;
                }
                else
                {
                    // 有效NodeID,表示注册成功,则发送心跳
                    SHeartbeat node = new SHeartbeat();
                    node.id     = form.GetCurCashierNodeID();
                    node.taskID = form.GetCurTaskID();
                    HttpSendResult result = RequestSender.SendRequestToServer(node);

                    if (result.IsSendSuccessed() && result.ErrorStatus == 1 && result.NodeID > 0)
                    {
                        LOGGER.INFO($"Heart-beat successed. NodeID = {node.id}, TaskID = {node.taskID}");
                        return;
                    }
                    else
                    {
                        // 发送心跳失败
                        form.AddLostHeartbeatCount();   // 追加一次失败计数
                        if (form.GetLostHeartbeatCount() >= MAX_ALLOW_HEARTBEAT_FAILED_TIME)
                        {
                            // 心跳失联太多次
                            form.SetCurCashierNodeID(INVALID_NODE_ID);
                            RSAKeyContainer.GetInstance.SetJavaPublicKey(string.Empty);
                            LOGGER.WARN($"Heart-beat disconnect for a long time... Ready to register node again.");
                        }
                        else
                        {
                            // 心跳的确失联了,但还能忍
                            LOGGER.INFO($"Heart-beat connect failed {form.GetLostHeartbeatCount()} times. NodeID = {node.id}, TaskID = {node.taskID}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LOGGER.ERROR($"Heart-beat error occured. Error = {ex.ToString()}");
            }
        }