From 14c25a6be6d147f90011d0218380bf7cd58b76e5 Mon Sep 17 00:00:00 2001
From: gongshangguo <gongshangguo@admin.com>
Date: 星期二, 01 三月 2022 20:05:27 +0800
Subject: [PATCH] 关闭的通道禁止写入
---
server/server.go | 242 +++++++++++++++++++++++++-----------------------
1 files changed, 125 insertions(+), 117 deletions(-)
diff --git a/server/server.go b/server/server.go
index 205cbc7..ae2f50a 100644
--- a/server/server.go
+++ b/server/server.go
@@ -4,6 +4,7 @@
"basic.com/valib/go-aiot.git/aiotProto/aiot"
"basic.com/valib/go-aiot.git/client"
"basic.com/valib/go-aiot.git/util"
+ "encoding/json"
uuid "github.com/satori/go.uuid"
"go.uber.org/zap"
"net"
@@ -45,37 +46,14 @@
// 鍏ㄥ眬鏈嶅姟
var Srv *Server
-// 閫氳繃masterId鑾峰彇闆嗙兢ID
-func (s *Server) GetClusterIdByMasterId(masterId string) string {
- if clusterId,ok := s.ClusterMaster[masterId];ok {
- return clusterId
- }
- return ""
+// 涓婇攣
+func (s *Server) Lock() {
+ s.deviceLock.Lock()
}
-// 閫氳繃闆嗙兢ID鑾峰彇masterId
-func (s *Server) GetMasterIdByClusterId(clusterId string) string {
- for masterId, cId := range s.ClusterMaster{
- if cId == clusterId {
- return masterId
- }
- }
- return ""
-}
-
-// 缁戝畾闆嗙兢鍜宮aster鍏崇郴
-func (s *Server) SetClusterIdMasterId(clusterId string, masterId string) {
- s.Lock()
- defer s.UnLock()
- s.ClusterMaster[masterId] = clusterId
-}
-
-// 闆嗙兢鏄惁娉ㄥ唽
-func (s *Server) IsMasterOnline(masterId string) bool {
- if _,ok := s.ClusterDevice[masterId];ok {
- return true
- }
- return false
+// 瑙i攣
+func (s *Server) UnLock() {
+ s.deviceLock.Unlock()
}
// 鍒濆鍖栨湇鍔�
@@ -93,92 +71,6 @@
serverCallBack: serverCallBack,
Logger: logger,
}
-}
-
-// 涓婇攣
-func (s *Server) Lock() {
- s.deviceLock.Lock()
-}
-
-// 瑙i攣
-func (s *Server) UnLock() {
- s.deviceLock.Unlock()
-}
-
-// 娉ㄥ唽闆嗙兢ID
-func (s *Server) SetCluster(masterId string, cli *client.Client) bool {
- if masterId == "" {
- return true
- }
-
- // 妫�娴嬮粦鍚嶅崟
- if _,ok := s.ClusterBlackList[masterId];ok {
- return false
- }
-
- // 娣诲姞闆嗙兢ID
- s.Clusters[masterId] = cli
- return true
-}
-
-// 鍒犻櫎闆嗙兢
-func (s *Server) RemoveCluster(masterId string) bool {
- s.Lock()
- defer s.UnLock()
- // 鍏抽棴鑺傜偣杩炴帴
- if clusterDevice,ok := s.ClusterDevice[masterId];ok {
- for deviceId := range clusterDevice{
- // 绉婚櫎闆嗙兢涓殑璁惧
- if _, ok := s.Devices[deviceId];ok {
- delete(s.Devices, deviceId)
- }
- }
- }
- // 绉婚櫎闆嗙兢
- if _,ok := s.Clusters[masterId];ok {
- delete(s.Clusters, masterId)
- return true
- }
- // 鍔犲叆闆嗙兢榛戝悕鍗�
- s.ClusterBlackList[masterId] = struct{}{}
- return false
-}
-
-// 鍒犻櫎璁惧
-func (s *Server) RemoveDevice(deviceId string) {
- // 濡傛灉鏄富鑺傜偣
- if _,ok := s.ClusterDevice[deviceId];ok {
- s.RemoveCluster(deviceId)
- }else{
- // 鍒犻櫎褰撳墠鑺傜偣
- if _, ok := s.Devices[deviceId];ok {
- s.Lock()
- defer s.UnLock()
- delete(s.Devices, deviceId)
- }
- }
-}
-
-// 娉ㄥ唽璁惧淇℃伅
-func (s *Server) SetDeviceList(masterId string, nodeIds []string) bool {
- if len(nodeIds) == 0 {
- return true
- }
- // 閿�
- s.Lock()
- defer s.UnLock()
-
- // 妫�娴嬮粦鍚嶅崟
- if _,ok := s.ClusterBlackList[masterId];ok {
- return false
- }
-
- // 娣诲姞璁惧ID
- for _, nodeId := range nodeIds{
- s.ClusterDevice[masterId][nodeId] = struct{}{}
- s.Devices[nodeId] = struct{}{}
- }
- return true
}
// 鍚姩鏈嶅姟
@@ -256,14 +148,130 @@
// wait
cli.Wait()
+ cli.Close()
+}
+
+// 闆嗙兢鏄惁娉ㄥ唽
+func (s *Server) IsMasterOnline(masterId string) bool {
+ if _,ok := s.ClusterDevice[masterId];ok {
+ return true
+ }
+ return false
+}
+
+// 閫氳繃masterId鑾峰彇闆嗙兢ID
+func (s *Server) GetClusterIdByMasterId(masterId string) string {
+ if clusterId,ok := s.ClusterMaster[masterId];ok {
+ return clusterId
+ }
+ return ""
+}
+
+// 閫氳繃闆嗙兢ID鑾峰彇masterId
+func (s *Server) GetMasterIdByClusterId(clusterId string) string {
+ for masterId, cId := range s.ClusterMaster{
+ if cId == clusterId {
+ return masterId
+ }
+ }
+ return ""
+}
+
+// 缁戝畾闆嗙兢鍜宮aster鍏崇郴
+func (s *Server) SetClusterIdMasterId(clusterId string, masterId string) {
+ s.Lock()
+ defer s.UnLock()
+ s.ClusterMaster[masterId] = clusterId
+}
+
+// 娉ㄥ唽闆嗙兢ID
+func (s *Server) SetCluster(masterId string, cli *client.Client) bool {
+ if masterId == "" {
+ return true
+ }
+
+ // 妫�娴嬮粦鍚嶅崟
+ if _,ok := s.ClusterBlackList[masterId];ok {
+ return false
+ }
+
+ // 娣诲姞闆嗙兢ID
+ s.Clusters[masterId] = cli
+ return true
+}
+
+// 鍒犻櫎闆嗙兢
+func (s *Server) RemoveCluster(masterId string) bool {
+ s.Lock()
+ defer s.UnLock()
+ // 鍏抽棴鑺傜偣杩炴帴
+ if clusterDevice,ok := s.ClusterDevice[masterId];ok {
+ for deviceId := range clusterDevice{
+ // 绉婚櫎闆嗙兢涓殑璁惧
+ if _, ok := s.Devices[deviceId];ok {
+ delete(s.Devices, deviceId)
+ }
+ }
+ }
+ // 绉婚櫎闆嗙兢
+ if _,ok := s.Clusters[masterId];ok {
+ delete(s.Clusters, masterId)
+ return true
+ }
+ // 鍔犲叆闆嗙兢榛戝悕鍗�
+ s.ClusterBlackList[masterId] = struct{}{}
+ return false
+}
+
+// 鍒犻櫎璁惧
+func (s *Server) RemoveDevice(deviceId string) {
+ // 濡傛灉鏄富鑺傜偣
+ if _,ok := s.ClusterDevice[deviceId];ok {
+ s.RemoveCluster(deviceId)
+ }else{
+ // 鍒犻櫎褰撳墠鑺傜偣
+ if _, ok := s.Devices[deviceId];ok {
+ s.Lock()
+ defer s.UnLock()
+ delete(s.Devices, deviceId)
+ }
+ }
+}
+
+// 娉ㄥ唽璁惧淇℃伅
+func (s *Server) SetDeviceList(masterId string, registerData *aiot.DeviceRegister) bool {
+
+ if len(registerData.DeviceList) == 0 {
+ return true
+ }
+ // 閿�
+ s.Lock()
+ defer s.UnLock()
+
+ // 妫�娴嬮粦鍚嶅崟
+ if _,ok := s.ClusterBlackList[masterId];ok {
+ return false
+ }
+
+ // 娣诲姞璁惧ID
+ if s.ClusterDevice[masterId] == nil {
+ s.ClusterDevice[masterId] = make(map[string]struct{})
+ }
+ for _, node := range registerData.DeviceList{
+ s.ClusterDevice[masterId][node.DeviceId] = struct{}{}
+ s.Devices[node.DeviceId] = struct{}{}
+ }
+ return true
}
// 澶勭悊璁惧娉ㄥ唽
-func (s *Server) RegisterDevice(msg *aiot.Protocol, cli *client.Client) {
+func (s *Server) RegisterDevice(cli *client.Client, msg *aiot.Protocol) {
// 璁剧疆鑺傜偣ID
cli.SetDeviceId(msg.SenderId)
// 娣诲姞闆嗙兢ID
s.SetCluster(msg.SenderId, cli)
// 璁剧疆闆嗙兢
- s.SetDeviceList(msg.SenderId, msg.DeviceProto.DeviceIds)
+ registerData := &aiot.DeviceRegister{}
+ _ = json.Unmarshal(msg.Data, registerData)
+ s.SetDeviceList(msg.SenderId, registerData)
}
--
Gitblit v1.8.0