Пример #1
0
func ReadBytes(r io.Reader) []byte {
	var size uint16
	ext.AssertE(binary.Read(r, DEFAULT_BYTE_ORDER, &size))
	data := make([]byte, size)
	ext.AssertE(binary.Read(r, DEFAULT_BYTE_ORDER, data))
	ext.LogDebug("READ\t%d", size)
	return data
}
Пример #2
0
func NewAcceptor(addr string, newAgent NewAgent) Stopper {
	listener, err := net.Listen("tcp", addr)
	ext.AssertE(err)
	a := &acceptor{Listener: listener, NewAgent: newAgent, stopper: NewStopper()}
	go ext.PCall(a.Run)
	return a
}
Пример #3
0
func (r *nodeReceiver) Run() {
	defer r.Stopped()
	defer r.Conn.Close()

	if RELEASE {
		ext.AssertE(r.Conn.SetReadDeadline(time.Now().Add(10 * time.Second)))
	}
	infoData := ReadBytes(r.Conn)

	var info nodeInfo
	GobDecode(infoData, &info)

	ext.Assert(info.Cookie == MyInfo().Cookie)
	ext.Assert(info.ID != MyInfo().ID)

	r.nodeInfo = info
	AddNode(r.ID, r.nodeInfo)
	ext.LogDebug("ESTABLISHED\tLOCAL\t%s\tREMOTE\t%s\tINFO\t%v", r.LocalAddr().String(), r.RemoteAddr().String(), r.nodeInfo)

	for !r.StopRequested() {
		if RELEASE {
			ext.AssertE(r.Conn.SetReadDeadline(time.Now().Add(time.Minute)))
		}
		data := ReadBytes(r.Conn)
		if r.Decompress != nil {
			data = r.Decompress(data)
		}

		buf := bytes.NewBuffer(data)

		dec := gob.NewDecoder(buf)

		var source, target PID
		var msgSize int
		ext.AssertE(dec.Decode(source))
		ext.AssertE(dec.Decode(target))
		ext.AssertE(dec.Decode(msgSize))

		msg := r.Decode(buf.Bytes())
		ext.Assert(msg != nil)

		Cast(source, target, msg)
	}
}
Пример #4
0
func (s *nodeSender) Run() {
	defer s.Stopped()

	if RELEASE {
		ext.AssertE(s.Conn.SetWriteDeadline(time.Now().Add(10 * time.Second)))
	}
	WriteBytes(s.Conn, GobEncode(MyInfo()))

	for !s.StopRequested() {
		source, target, msg := s.Pop()
		data := s.Encode(msg)
		ext.Assert(data != nil)

		var buf bytes.Buffer

		enc := gob.NewEncoder(&buf)
		ext.AssertE(enc.Encode(source))
		ext.AssertE(enc.Encode(target))
		ext.AssertE(enc.Encode(len(data)))
		ext.AssertE(enc.Encode(data))

		data = buf.Bytes()
		if s.Compress != nil {
			data = s.Compress(buf.Bytes())
		}

		if RELEASE {
			ext.AssertE(s.Conn.SetWriteDeadline(time.Now().Add(time.Minute)))
		}
		WriteBytes(s.Conn, data)
	}
}
Пример #5
0
func GobEncode(data interface{}) []byte {
	var buf bytes.Buffer
	encoder := gob.NewEncoder(&buf)
	ext.AssertE(encoder.Encode(data))
	return buf.Bytes()
}
Пример #6
0
func GobDecode(bin []byte, data interface{}) {
	buf := bytes.NewBuffer(bin)
	decoder := gob.NewDecoder(buf)
	ext.AssertE(decoder.Decode(data))
}
Пример #7
0
func WriteBytes(w io.Writer, data []byte) {
	ext.AssertE(binary.Write(w, DEFAULT_BYTE_ORDER, uint16(len(data))))
	ext.AssertE(binary.Write(w, DEFAULT_BYTE_ORDER, data))
	ext.LogDebug("WRITTEN\t%d", len(data))
}