/// <summary> /// 从服务器进行注销 /// </summary> /// <returns></returns> private bool UnregsiterFromServer() { try { SUnregisteNode node = new SUnregisteNode(); node.id = GetCurCashierNodeID(); HttpSendResult result = RequestSender.SendRequestToServer(node); if (result.IsSendSuccessed() && result.ErrorStatus == 1) { LOGGER.INFO($"Node unregiste successed. NodeID = {node.id}"); return(true); } else { LOGGER.WARN($"Node unregiste failed. NodeID = {node.id}"); return(false); } } catch (Exception e) { LOGGER.ERROR($"Node unregiste error occured. Error = {e.ToString()}"); return(false); } }
/// <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); } }
/// <summary> /// 处理流水查询任务 /// </summary> private void HandleBillTask() { try { SBillTaskInfo info = m_BillTasksList.Peek(); // 取出消息,不删除 LOGGER.INFO($"Handle a new bill task:\n {info.ToLogString()} \n, still left {m_BillTasksList.Count - 1} tasks in queue."); SBillTaskResult dealResult = new SBillTaskResult(); dealResult.taskID = info.taskID; dealResult.nodeID = GetCurCashierNodeID(); dealResult.bankId = info.bankId; // 记录当前在处理的任务ID SetCurTaskID(info.taskID); // 开始处理 AutomationBill(info, ref dealResult); LOGGER.WARN($"Auto get bill task is done. Now ready to send msg to server. TaskID = {info.taskID}.", info.taskID); // 发送回馈消息给服务器完毕 HttpSendResult SendResult = RequestSender.SendRequestToServer(dealResult); if (SendResult.IsSendSuccessed()) { SetLastTaskID(info.taskID); SetCurTaskID(INVALID_TASK_ID); LOGGER.INFO($"Send get bill result successed. TaskID = {info.taskID}. \n {SendResult.ToLogString()}"); } else { // result = null; 无论是否发送给服务器成功,只要执行成功,就不允许清除 SetLastTaskID(INVALID_TASK_ID); SetCurTaskID(INVALID_TASK_ID); LOGGER.ERROR($"Send get bill result failed. TaskID = {info.taskID}. \n {SendResult.ToLogString()}", info.taskID); } LOGGER.WARN($"Get bill task deal over. TaskID = {info.taskID}.", info.taskID); } catch (Exception e) { LOGGER.ERROR($"HandleBillTask exception.msg: {e.Message}."); } finally { SetCurTaskID(INVALID_TASK_ID); m_BillTasksList.Dequeue(); // 无论如何,该任务必须删除 } return; }
/// <summary> /// 处理流水查询任务 /// </summary> private void HandleOutTask() { try { SOutTaskInfo info = m_OutTasksList.Peek(); // 取出消息,不删除 LOGGER.INFO($"Handle a new out task:\n {info.ToLogString()} \n, still left {m_OutTasksList.Count - 1} tasks in queue."); SOutTaskResult dealResult = new SOutTaskResult(); dealResult.taskID = info.taskID; dealResult.nodeID = GetCurCashierNodeID(); // 记录当前在处理的任务ID SetCurTaskID(info.taskID); // 开始处理 if (!AutomationOut(info, ref dealResult)) { } LOGGER.WARN($"Auto out task is done. Now ready to send msg to server. TaskID = {info.taskID}.", info.taskID); // 通知服务器处理结果 HttpSendResult sendResult = RequestSender.SendRequestToServer(dealResult); if (sendResult.IsSendSuccessed()) { SetLastTaskID(info.taskID); SetCurTaskID(INVALID_TASK_ID); LOGGER.INFO($"Send auto out task successed. TaskID = {info.taskID}. \n {sendResult.ToLogString()}"); } else { SetLastTaskID(INVALID_TASK_ID); SetCurTaskID(INVALID_TASK_ID); LOGGER.ERROR($"Send auto out task failed. TaskID = {info.taskID}. \n {sendResult.ToLogString()}", info.taskID); } LOGGER.WARN($"Out task deal over. TaskID = {info.taskID}.", info.taskID); } catch (Exception e) { LOGGER.ERROR($"Deal out task error occured. Error = {e.ToString()}"); } finally { SetCurTaskID(INVALID_TASK_ID); m_OutTasksList.Dequeue(); // 无论如何,该任务必须删除 } return; }
/// <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()}"); } }