Exemplo n.º 1
0
func (s *Swim) serveOnce() error {
	var in, out gen.SwimMessage
	// out.Reset()
	n, addr, err := s.serverConn.ReadFromUDP(s.client.buf[0:])
	if err != nil {
		glog.Error("error serving swim: ", err)
		return err
	}
	glog.V(2).Infof("received pkt len %d form %s", n, addr)

	err = proto.Unmarshal(s.client.buf[0:n], &in)
	if err != nil {
		glog.Error("error unmarshalling swim: ", err)
		return nil //ignore error, continue loop
	}
	glog.V(2).Info("swim serving inpkt ", &in)
	//first, apply all db updates
	s.onUpdatePkts(in.DisseminationUpdates)
	out.Seq = in.Seq
	switch {
	case in.Ping != nil:
		out.Ack = s.servePing(in.Ping)
	case in.PingReq != nil:
		out.Ack = s.servePingReq(in.PingReq)
	}
	//add db
	out.DisseminationUpdates = s.updates
	bv, err := proto.Marshal(&out)
	if err != nil {
		glog.Error("error marshalling swim response: ", err)
		return nil //ignore error, continue loop
	}
	if len(bv) > len(s.client.buf) {
		glog.Error("packet is too big to send over UDP")
		return nil //ignore error, continue loop
	}
	glog.V(2).Infof("sending %d bytes packet to node %s: %v", len(bv), addr, &out)
	sent, err := s.serverConn.WriteToUDP(bv, addr) //responding to the original peer address
	if err != nil {
		glog.Error("error sending swim response: ", err)
		return nil //ignore error, continue loop
	}
	if sent != len(bv) {
		return fmt.Errorf("Unexpected number of bytes sent. expected %d sent %d", len(bv), sent)
	}
	return nil
}
Exemplo n.º 2
0
func (s *swimmer) sendRequest(addr *net.UDPAddr, req *gen.SwimMessage) error {
	//add dissemination info to all outbound pkt
	req.DisseminationUpdates = s.s.updates
	bv, err := proto.Marshal(req)
	if err != nil {
		return err
	}
	if len(bv) > len(s.buf) {
		return errors.New("packet too big to send over UDP")
	}
	glog.V(2).Infof("sending %d bytes packet to node %s: %v", len(bv), addr, req)
	sent, err := s.clientConn.WriteToUDP(bv, addr)
	if err != nil {
		return err
	}
	if sent != len(bv) {
		return fmt.Errorf("Unexpected number of bytes sent. expected %d sent %d", len(bv), sent)
	}
	return nil
}