Exemplo n.º 1
0
// handleUpdateUDP listens on conn, parses data into Versions and sends them to `updates` chan.
func handleUpdateUDP(conn net.PacketConn, updates chan<- version.Version) {
	for {
		b := make([]byte, 2048)
		n, addr, err := conn.ReadFrom(b)
		if err != nil {
			l.Print("ERROR: handleUpdateUDP: ReadFrom:\n  ", err)
			continue
		}
		ip := addr.(*net.UDPAddr).IP
		v, err := version.ParsePacket(ip.String(), b[:n])
		if err != nil {
			l.Print("ERROR: handleUpdateUDP: parsePacket:\n  ", err,
				"\n  packet:", string(b[:n]))
			continue
		}
		updates <- v
	}
}
Exemplo n.º 2
0
func (ss storeServer) ApiUpdate(w http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		m := "ERROR: ApiUpdate: only accepts POST requests"
		sendErrorRes(m, w, http.StatusMethodNotAllowed)
		return
	}
	b, err := ioutil.ReadAll(req.Body)
	if handleError(err, "ApiUpdate: ReadAll", w,
		http.StatusInternalServerError) {
		return
	}
	host, _, err := net.SplitHostPort(req.RemoteAddr)
	if err != nil {
		host = ""
	}
	v, err := version.ParsePacket(host, b)
	if err != nil {
		handleError(err, "ApiUpdate: parsePacket", w,
			http.StatusUnsupportedMediaType)
		l.Print("ERROR: packet:", string(b))
		return
	}
	ss.updates <- v
}