func (r *RequestListener) Notify(msg message.Message, requestID message.ID) { if l, ok := r.listeners[requestID]; ok { l <- msg return } log.Println("No listener found for request", requestID, "type", msg.MessageType()) }
func (r *defaultRouter) Route(msg message.Message) error { from := msg.Destination() peer, ok := r.peers[from.String()] if !ok { return fmt.Errorf("Peer Not found") } peer.Commit(msg) return nil }
func (r *defaultRouter) Handle(c peer.NodePeer, msg message.Message) message.Message { fn, ok := r.handlers[msg.MessageType()] if !ok { log.Fatalf("Handler %s not found", msg.MessageType()) return &message.Error{} } result, err := fn(c, msg) if err != nil { log.Fatalf("Handler error on message type %s error %s", msg.MessageType(), err) //To handle a correct response will need casting! return &message.Error{} } return result }
func (s *JsonSerializer) Serialize(m message.Message) ([]byte, error) { t := m.MessageType() data := map[string]interface{}{"type": t, "msg": m} return json.Marshal(&data) }