Ejemplo n.º 1
0
func startVoting() {
	fmt.Println("Start voting!")
	time.Sleep(time.Second * 10)
	// Send you adress to all. In this case it really doesn't matter
	msg := message.NewVoteMessage(myAddress)
	mm, _ := xml.Marshal(msg)
	_, err := writeMulticast.WriteToUDP(mm, multicastAddr)
	check(err)

	// Wait 10 seconds
	c := time.After(time.Second * 10)
	<-c

	// Check if we got any address that is bigger
	for addr, _ := range knownAddresess {
		if addr > myAddress {
			return
		}
	}

	// Become the server if we are the greatest
	msga := message.NewCoordinatorMessage(string(myAddress))
	mmm, _ := xml.Marshal(msga)
	writeMulticast.WriteToUDP(mmm, multicastAddr)
	fmt.Println("NOW I AM BECOME DEATH")
}
Ejemplo n.º 2
0
func main() {
	port := "224.0.1.60:1888"
	laddr, err := net.ResolveUDPAddr("udp", ":0")
	check(err)
	mcaddr, err := net.ResolveUDPAddr("udp", port)
	check(err)
	conn, err := net.ListenMulticastUDP("udp", nil, mcaddr)
	check(err)
	lconn, err := net.ListenUDP("udp", laddr)
	check(err)
	reader := bufio.NewReader(os.Stdin)

	knownAddresess = make(map[int]bool, 1)
	myAddress = os.Getpid()
	log.Println("My address is ", myAddress)
	msg := message.NewVoteMessage(myAddress)
	mm, _ := xml.Marshal(msg)
	fmt.Println(string(mm))
	listenMulticast = conn
	writeMulticast = lconn
	multicastAddr = mcaddr
	go listen(listenMulticast)
	go startVoting()
	for {
		// Sleep 20 seconds to give time to spawn more clients
		// time.Sleep(time.Second * 10)
		txt, _, err := reader.ReadLine()
		b := make([]byte, 256)
		copy(b, txt)
		check(err)
		_, err = writeMulticast.WriteToUDP(b, mcaddr)
		check(err)
	}
}
Ejemplo n.º 3
0
func broadcastMessageWithMyPid(myaddr int) {
	msg := message.NewVoteMessage(myaddr)
	mm, _ := xml.Marshal(msg)
	_, err := writeMulticast.WriteToUDP(mm, multicastAddr)
	if err != nil {
		log.Println("[Client] [On multicast] Error sending myPid", err)
	}
}