Exemplo n.º 1
0
//	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])
	}
}
Exemplo n.º 2
0
//	Determines how to react for a SIP message depending on its type.
func (s *Server) SIPMessageReaction(socket net.Conn, msg string) {
	log.Print("Message type: %s\n", sip.ExtractType(msg))
	switch sip.ExtractType(msg) {
	case "BLD", "REQ":
		sip.SendInfo(socket, msg)
		if s.Root != nil {
			s.HandleNewMachine(socket, msg)
		} else {
			s.TellParent(msg)
		}
	case "TRA":
		sip.SendInfo(socket, msg)
		s.HandleChartTransfer(socket)
	case "FND":
		sip.SendInfo(socket, msg)
		DataMap := sip.InterpreteData(sip.ExtractData(msg))

		if s.Address == DataMap["parent"] {
			s.AddChild(DataMap["child"])
			break
		}
		s.AskChildren(msg)
	}
}