// ROOT ONLY - When we know that there is a new machine pending. // We need to find it place in out net and send the information about it to our children. // We also need to create updated chart and send it to children, too. func (s *Server) HandleNewMachine(socket net.Conn, msg string) { log.Println("Starting parent search") DataMap := sip.InterpreteData(sip.ExtractData(msg)) fatherNode, _ := tree.FindSolution(s.Root, -1) log.Printf("Parent = %s\n", fatherNode.IP) capacity, _ := strconv.Atoi(DataMap["capacity"]) tree.AddNewChild(fatherNode, tree.NewNode(DataMap["ip"], capacity)) newMes := sip.FND(DataMap["ip"], fatherNode.IP) if fatherNode.IP == s.Address { log.Printf("Adding child %s %s\n", s.Address, DataMap["ip"]) s.AddChild(DataMap["ip"]) _, err := socket.Write([]byte(newMes.ToString())) log.Println(err) } else { s.AskChildren(newMes.ToString()) } s.CreateChart() time.Sleep(1 * time.Second) for i := 0; i < s.ChildNumber; i++ { log.Printf("Sending chart to %s\n", s.Children[i]) SendChart(s.Children[i]) } }
// Creates a new server with given ip and capacity: // - if root is false, it gets connected to parent, // - else initializes tree node representing network structure. func NewServer(ip string, parent string, capacity int, root bool) *Server { if !root { return &Server{ip, parent, make([]string, capacity-1), 0, nil} } return &Server{ip, parent, make([]string, capacity-1), 0, tree.NewNode(ip, capacity)} }