//接收 //name 自己的名称 //@return remoteName 对方服务器的名称 func (this *Auth) RecvKey(conn net.Conn, name string) (remoteName string, err error) { /* 获取对方的名称 */ if remoteName, err = GetNameForConn(conn); err != nil { return } /* 开始验证对方客户端名称 */ clientIdInfo := new(nodeStore.IdInfo) json.Unmarshal([]byte(remoteName), clientIdInfo) /* 这是新节点,需要给他生成一个id */ if clientIdInfo.Id == Str_zaro { //生成id之前先检查这个id是否存在 // fmt.Println("在网络中注册一个域名:", clientIdInfo.Domain) targetId := utils.GetHashForDomain(clientIdInfo.Domain) msgOne := &msgcenter.Message{ TargetId: targetId, ProtoId: msgcenter.MSGID_findDomain, CreateTime: time.Now().Unix(), Sender: nodeStore.ParseId(nodeStore.GetRootIdInfoString()), Content: []byte(clientIdInfo.Domain), Accurate: false, } msgOne.Hash = msgcenter.GetHash(msgOne) ret := <-msgcenter.SendTimeOutMsg(msgOne, time.Second*10) if ret == "timeout" { err = errors.New("查询域名超时") return } if ret == "true" { err = errors.New("这个域名已经被注册了") return } *clientIdInfo = nodeStore.NewIdInfo(clientIdInfo.Name, clientIdInfo.Email, clientIdInfo.Domain, nodeStore.ParseId(name)) //给服务器发送生成的id newName := string(clientIdInfo.Build()) conn.Write(GetBytesForName(newName)) err = errors.New("给新节点分配一个idinfo") return } /* 验证成功后,向对方发送自己的名称 */ //得到对方名称 conn.Write(GetBytesForName(name)) return }
/* 连接超级节点,得到一个id @ addr 超级节点ip地址 */ func GetId(idInfo nodeStore.IdInfo) (newIdInfo *nodeStore.IdInfo, err error) { // idInfo = nodeStore.IdInfo{ // Id: Str_zaro, // Name: "nimei", // Email: "*****@*****.**", // Domain: "djfkafjkls", // } conn, err := net.Dial("tcp", getSuperAddrOne()) if err != nil { err = errors.New("连接超级节点失败") return } /* 向对方发送自己的名称 */ lenght := int32(len(idInfo.Build())) buf := bytes.NewBuffer([]byte{}) binary.Write(buf, binary.BigEndian, lenght) buf.Write(idInfo.Build()) conn.Write(buf.Bytes()) /* 对方服务器创建好id后,发送给自己 */ lenghtByte := make([]byte, 4) io.ReadFull(conn, lenghtByte) nameLenght := binary.BigEndian.Uint32(lenghtByte) nameByte := make([]byte, nameLenght) n, e := conn.Read(nameByte) if e != nil { err = e return } //得到对方生成的名称 newIdInfo = new(nodeStore.IdInfo) json.Unmarshal(nameByte[:n], newIdInfo) conn.Close() return }
/* 自己保存这个节点,只能保存超级节点 */ func (this *NodeManager) saveNode(findNode *FindNode, c engine.Controller) { //自己不是超级节点 if !nodeStore.Root.IsSuper { //代理节点查找的备用超级节点 // if findNode.WantId == "left" || findNode.WantId == "right" { // fmt.Println("添加备用节点:", nodeStore.ParseId(findNode.FindId)) // // store.AddNode(node) // // return // } //查找到的节点和自己的超级节点不一样,则连接新的超级节点 if nodeStore.SuperName != findNode.FindId { oldSuperName := nodeStore.SuperName session, _ := c.GetNet().AddClientConn(findNode.Addr, nodeStore.GetRootIdInfoString(), findNode.TcpPort, false) nodeStore.SuperName = session.GetName() if _, ok := c.GetNet().GetSession(nodeStore.SuperName); ok { } if session, ok := c.GetNet().GetSession(oldSuperName); ok { session.Close() } return } } findNodeIdInfo := new(nodeStore.IdInfo) json.Unmarshal([]byte(findNode.FindId), findNodeIdInfo) // nodeStore.Parse(findNode.FindId) // shouldNodeInt, _ := new(big.Int).SetString(findNode.FindId, nodeStore.IdStrBit) newNode := &nodeStore.Node{ IdInfo: *findNodeIdInfo, IsSuper: findNode.IsSuper, Addr: findNode.Addr, TcpPort: findNode.TcpPort, UdpPort: findNode.UdpPort, } //是否需要这个节点 if isNeed, replace := nodeStore.CheckNeedNode(findNodeIdInfo.GetId()); isNeed { //------------start-------------------------- //这一块只为打印一个日志,可以去掉 // ishave := false // for _, value := range nodeStore.GetAllNodes() { // if value.IdInfo.GetId() == findNodeIdInfo.GetId() { // ishave = true // break // } // } // if ishave { // fmt.Println("需要这个节点", findNodeIdInfo.GetId()) // } //------------end-------------------------- nodeStore.AddNode(newNode) //把替换的节点连接删除 if replace != "" { //是否要替换超级节点 if session, ok := c.GetNet().GetSession(nodeStore.SuperName); ok { if replace == session.GetName() { session.Close() session, _ := c.GetNet().AddClientConn(newNode.Addr, nodeStore.GetRootIdInfoString(), newNode.TcpPort, false) nodeStore.SuperName = session.GetName() introduceSelf(session) } } if session, ok := c.GetSession(replace); ok { session.Close() nodeStore.DelNode(replace) } } if nodeStore.Root.IsSuper { //自己不会连接自己 if nodeStore.GetRootIdInfoString() == string(newNode.IdInfo.Build()) { return } //检查这个session是否存在 if _, ok := c.GetNet().GetSession(string(newNode.IdInfo.Build())); !ok { session, err := c.GetNet().AddClientConn(newNode.Addr, nodeStore.GetRootIdInfoString(), newNode.TcpPort, false) if err != nil { fmt.Println(newNode) fmt.Println("连接客户端出错") } else { introduceSelf(session) } } } } }