예제 #1
0
func (lr *LogReceiver) handleConnection(conn net.Conn) {
	defer conn.Close()
	for {
		var m map[string]string
		n, buf := tcp_pack.UnPack(conn)
		if n <= 0 {
			break
		}
		err := json.Unmarshal(buf, &m)
		if err == nil {
			_, ok := m["req"]
			if ok {
				m = lr.handleRegister(m)
				data, _ := json.Marshal(m)
				conn.Write(tcp_pack.Pack(data))
			} else {
				lr.handleLog(m)
			}
		}
	}
}
예제 #2
0
파일: heart_beat.go 프로젝트: tengteng/logd
//向monitor注册或取消注册,reg为true表示注册,否则是取消注册,返回true表示成功
func (this *HeartBeat) registerSelf(reg bool) bool {
	conn, err := lib.GetConnection(this.monitorAddr)
	if err != nil {
		return false
	}
	defer conn.Close()

	req := "register"
	if !reg {
		req = "unregister"
	}
	m := map[string]string{"req": req, "ip": lib.GetIp(), "port": this.port, "hostname": lib.GetHostname(), "role": this.role}
	msg, err := json.Marshal(m)
	if err != nil {
		loglib.Error("marshal " + req + " info error " + err.Error())
		return false
	}
	_, err = conn.Write(tcp_pack.Pack(msg))
	if err != nil {
		loglib.Error("send " + req + " info failed" + err.Error())
		return false
	} else {
		plen, ret := tcp_pack.UnPack(conn)

		if plen > 0 {
			err = json.Unmarshal(ret, &m)
			r, ok := m["err"]
			if err == nil && ok && r == "0" {
				loglib.Info(req + " to monitor success!")
				return true
			}
			loglib.Error(req + " heart beat failed!")
		}
	}
	return false
}