浏览代码

add active Master and Slave

dujunchen 6 天之前
父节点
当前提交
fd34fe9540
共有 4 个文件被更改,包括 35 次插入23 次删除
  1. 8 9
      internal/app/index.go
  2. 1 1
      internal/app/stc/active/index.go
  3. 20 8
      internal/app/stc/broadcast/stc-broadcast.go
  4. 6 5
      internal/app/stc/index.go

+ 8 - 9
internal/app/index.go

@@ -18,10 +18,10 @@ func StartApp() {
 	//Get cab number acording to IP
 	SetMasterCabNum()
 
-	lfshook.NewLogger().Infof("=================cab number:%s========Master:%+v===", active.CabNum, active.Master)
+	//lfshook.NewLogger().Infof("=================cab number:%s========Master:%+v===", active.CabNum, active.Master)
 
 	//init the active status
-	active.ActivedCab = ""
+	//active.ActivedCab = ""
 
 	//get priority
 	priority.GetPriority()
@@ -33,10 +33,10 @@ func StartApp() {
 	go stc.StartStcConnection(socket.Conn8, "8")
 
 	//启动连接到Master服务器,检查Master是否在线
-	if active.CabNum == "8" {
-		socket.ConnectedMaster = false
-		go stc.StartConnectionToSipServer(socket.ConnToMaster)
-	}
+	//if active.CabNum == "8" {
+	//	socket.ConnectedMaster = false
+	//	go stc.StartConnectionToSipServer(socket.ConnToMaster)
+	//}
 	// 启动其他服务...
 	// 启动 AMI
 	go func() {
@@ -59,11 +59,10 @@ func SetMasterCabNum() {
 	}
 	// Init cab number and master role
 	if !ext {
-		active.Master = false
 		active.CabNum = "8"
+		active.Master = false
 	} else {
-		active.Master = true
 		active.CabNum = "1"
+		active.Master = true
 	}
-
 }

+ 1 - 1
internal/app/stc/active/index.go

@@ -6,7 +6,7 @@ import "time"
 var CabNum string
 var ActivedCab = "1"
 
-var Master bool
+var Master = false
 
 var QueueTimer *time.Timer = nil
 

+ 20 - 8
internal/app/stc/broadcast/stc-broadcast.go

@@ -59,12 +59,6 @@ func HandleStcCmd(ctx context.Context, conn net.Conn) {
 // 处理单个数据包(原 switch 逻辑迁移过来)
 func processPacket(packet []byte) {
 
-	//check if Master role
-	if !active.Master {
-		lfshook.NewLogger().Logger.Infof("=========Not Master Role Ignore data=============")
-		return
-	}
-
 	if len(packet) < 6 {
 		fmt.Println("Invalid packet length")
 		return
@@ -77,7 +71,13 @@ func processPacket(packet []byte) {
 
 	//check if the cmd type is avtive
 	if packet[5] == 0x03 { // ACTIVE
-		Active([1]byte{packet[8]})
+		Active([2]byte{packet[8], packet[9]})
+		return
+	}
+
+	//check if Master role
+	if !active.Master {
+		lfshook.NewLogger().Logger.Infof("=========Not Master Role Ignore data=============")
 		return
 	}
 
@@ -159,6 +159,7 @@ func processPacket(packet []byte) {
 				}
 			}
 		*/
+		break
 	case 0x02: // STN
 		if priority.CheckPriority("STN") {
 			action.HangupRunningTask("STN") //STN interrupt other
@@ -258,13 +259,24 @@ func StationAnn(data []byte) (err error) {
 }
 
 // 激活信号
-func Active(data [1]byte) {
+func Active(data [2]byte) {
 
 	//var info model.Sysinfo
 	//active.Actived = true
 	Signal := int(data[0])
+	Master := int(data[1])
 	//lfshook.NewLogger().Logger.Infof("Active data : %x", Signal)
 
+	if !active.Master && active.CabNum == "8" { //slave role , check the Master data from STC
+		if Master == 8 {
+			active.Master = true
+		}
+	} else if !active.Master && active.CabNum == "1" {
+		if Master == 1 {
+			active.Master = true
+		}
+	}
+
 	switch Signal {
 	case 0:
 

+ 6 - 5
internal/app/stc/index.go

@@ -150,6 +150,7 @@ func CreateConnection(RemoteCab string) (net.Conn, error) {
 	}
 }
 
+/*
 func StartConnectionToSipServer(conn net.Conn) {
 
 	var connMux sync.Mutex // 保护 conn 的读写
@@ -200,13 +201,13 @@ func StartConnectionToSipServer(conn net.Conn) {
 		time.Sleep(time.Second) // 重连前等待
 
 		//check connected Master tag; connection err change to Master role ,exit
-		if socket.ConnectedMaster {
-			active.Master = true
-			return
-		}
+		//if socket.ConnectedMaster {
+		//	active.Master = true
+		//	return
+		//}
 	}
 }
-
+*/
 // 连接Master sipserver
 func CreateConnectionSipServer() (net.Conn, error) {
 	dialer := &net.Dialer{