// New creates a new instance of Peter, complete with a Node and the underlying Wendy Cluster, and registers itself to receive callbacks from the Cluster events. func New(id wendy.NodeID, localIP, globalIP, region string, port int) *Peter { node := wendy.NewNode(id, localIP, globalIP, region, port) cluster := wendy.NewCluster(node, nil) peter := &Peter{ subscriptions: newSubscriptionMap(), parents: newParentMap(), cluster: cluster, log: log.New(os.Stdout, "peter("+id.String()+") ", log.LstdFlags), logLevel: LogLevelWarn, } cluster.RegisterCallback(peter) return peter }
func (s *subscriptionMap) insert(t Topic, id wendy.NodeID) bool { s.Lock() defer s.Unlock() if ids, set := s.items[t]; set { for _, i := range ids { if id.Equals(i) { return false } } s.items[t] = append(s.items[t], id) } else { s.items[t] = []wendy.NodeID{id} } return true }
func (s *subscriptionMap) unsafeRemove(t Topic, id wendy.NodeID) (removed, empty bool) { if ids, set := s.items[t]; set { for pos, i := range ids { if id.Equals(i) { empty := false if len(s.items[t]) == 1 { s.items[t] = []wendy.NodeID{} empty = true } else if pos == 0 { s.items[t] = s.items[t][1:] } else if pos+1 == len(s.items[t]) { s.items[t] = s.items[t][:pos] } else { s.items[t] = append(s.items[t][:pos], s.items[t][pos+1:]...) } return true, empty } } } return false, false }