Example #1
0
// Listen UDP packet and write to channel
func (this *Watcher) listen() (chan string, error) {
	lis, err := net.ListenPacket("udp", this.Addr)
	if err != nil {
		return nil, err
	}
	beelog.Info("start listen packet from", this.Addr)

	ch := make(chan string)
	go func() {
		defer lis.Close()
		buf := make([]byte, 1000)
		for {
			n, addr, err := lis.ReadFrom(buf)
			msg := string(buf[:n])
			beelog.Debug("Receive from", addr, msg)
			if err != nil {
				beelog.Warn(err)
				continue
			}
			ip := strings.Split(addr.String(), ":")[0]
			ch <- ip
		}
	}()
	return ch, nil
}
Example #2
0
// auto decrease host HP
func (this *Watcher) drain(notify chan Host) {
	beelog.Debug("drain stated")
	for {
		this.Lock()
		for _, host := range this.Hosts {
			var state = host.Alive
			this.hurt(host.IP)
			beelog.Debug("drain", host.IP, host.HP, host.Alive)
			if host.Alive != state {
				notify <- *host
			}
		}
		this.Unlock()
		time.Sleep(this.RecycleDuration)
	}
}
Example #3
0
// tell the master I'am alive
func Beat(addr string, msg string) {
	conn, err := net.DialTimeout("udp", addr, time.Second*2)
	if err != nil {
		beelog.Error(err)
		return
	}
	defer conn.Close()
	beelog.Debug("beat send msg:", msg)
	_, err = conn.Write([]byte(msg))
	return
}