/* 一个连接断开后的回调方法 */ func closeConnCallback(name string) { if name == nodeStore.SuperName { targetNode := nodeStore.Get(nodeStore.Root.IdInfo.GetId(), false, nodeStore.Root.IdInfo.GetId()) if targetNode == nil { return } if Init_role == C_role_client { nodeStore.SuperName = engine.AddClientConn(targetNode.Addr, targetNode.TcpPort, false) } else { session, _ := engine.GetController().GetSession(string(targetNode.IdInfo.Build())) nodeStore.SuperName = session.GetName() } return } // session, ok := engine.GetController().GetSession(name) // if err != nil { // fmt.Println("客户端离线,但找不到这个session") // } node := nodeStore.Get(nodeStore.ParseId(name), false, "") fmt.Println("节点下线", node) if node != nil && !node.IsSuper { fmt.Println("自己代理的节点下线:", nodeStore.ParseId(name)) } nodeStore.DelNode(nodeStore.ParseId(name)) }
func startUp(node *nodeStore.Node) { fmt.Println("本机id为:", Init_IdInfo.GetId()) /* 启动消息服务器 */ engine.InitEngine(string(Init_IdInfo.Build())) /* 生成密钥文件 */ var err error //生成密钥 privateKey, err = rsa.GenerateKey(rand.Reader, 512) if err != nil { fmt.Println("生成密钥错误", err.Error()) return } /* 启动分布式哈希表 */ nodeStore.InitNodeStore(node) /* 设置关闭连接回调函数后监听 */ engine.SetAuth(new(Auth)) engine.SetCloseCallback(closeConnCallback) engine.Listen(Init_LocalIP, int32(Init_LocalPort)) if Init_role != C_role_root { /* 连接到超级节点 */ host, portStr, _ := net.SplitHostPort(getSuperAddrOne()) port, err := strconv.Atoi(portStr) if err != nil { return } nodeStore.SuperName = engine.AddClientConn(host, int32(port), false) //给目标机器发送自己的名片 introduceSelf() } go read() }