Ejemplo n.º 1
0
func (ps *PubSub) Subscribe(si SubscribeInfo, reply *string) error {
	ps.mux.Lock()
	defer ps.mux.Unlock()
	if si.Transport != utils.META_HTTP_POST {
		*reply = "Unsupported transport type"
		return errors.New(*reply)
	}
	var expTime time.Time
	if si.LifeSpan > 0 {
		expTime = time.Now().Add(si.LifeSpan)
	}
	rsr, err := utils.ParseRSRFields(si.EventFilter, utils.INFIELD_SEP)
	if err != nil {
		*reply = err.Error()
		return err
	}
	key := utils.InfieldJoin(si.Transport, si.Address)
	ps.subscribers[key] = &SubscriberData{
		ExpTime: expTime,
		Filters: rsr,
	}
	ps.saveSubscriber(key)
	*reply = utils.OK
	return nil
}
Ejemplo n.º 2
0
func (ps *PubSub) Unsubscribe(si SubscribeInfo, reply *string) error {
	ps.mux.Lock()
	defer ps.mux.Unlock()
	if si.Transport != utils.META_HTTP_POST {
		*reply = "Unsupported transport type"
		return errors.New(*reply)
	}
	key := utils.InfieldJoin(si.Transport, si.Address)
	delete(ps.subscribers, key)
	ps.removeSubscriber(key)
	*reply = utils.OK
	return nil
}
Ejemplo n.º 3
0
func TestSubscribe(t *testing.T) {
	ps := NewPubSub(accountingStorage, false)
	var r string
	if err := ps.Subscribe(SubscribeInfo{
		EventFilter: "EventName/test",
		Transport:   utils.META_HTTP_POST,
		Address:     "url",
		LifeSpan:    time.Second,
	}, &r); err != nil {
		t.Error("Error subscribing: ", err)
	}
	if subData, exists := ps.subscribers[utils.InfieldJoin(utils.META_HTTP_POST, "url")]; !exists || subData.ExpTime.IsZero() {
		t.Error("Error adding subscriber: ", ps.subscribers)
	}
}