// Unsubscribe broadcasts that the current Node is no longer interested in a Topic, preventing it from receiving future event notifications for that Topic. func (p *Peter) Unsubscribe(t Topic) error { key, err := wendy.NodeIDFromBytes([]byte(string(t))) if err != nil { return err } msg := p.cluster.NewMessage(MSG_UNSUBSCRIBE, key, []byte{}) err = p.cluster.Send(msg) return err }
// Broadcast sends an event notification for a Topic that will be sent to every Node subscribed to that Topic. func (p *Peter) Broadcast(t Topic, body []byte) error { key, err := wendy.NodeIDFromBytes([]byte(string(t))) if err != nil { return err } msg := p.cluster.NewMessage(MSG_EVENT, key, body) err = p.cluster.Send(msg) return err }
// OnNodeJoin fulfills the wendy.Application interface, and will be called whenever a Node leaves the Cluster. Peter will detect which topics would use that Node as a parent in the subscription tree, and re-subscribe to those topics to repair the subscription tree. func (p *Peter) OnNodeJoin(node wendy.Node) { topics := p.parents.topics() for _, topic := range topics { key, err := wendy.NodeIDFromBytes([]byte(string(topic))) if err != nil { p.err(err.Error()) } target, err := p.cluster.Route(key) if err != nil { p.err(err.Error()) } if node.ID.Equals(target.ID) { err = p.Unsubscribe(topic) if err != nil { p.err(err.Error()) } err = p.Subscribe(topic) if err != nil { p.err(err.Error()) } } } }