Esempio n. 1
0
func (this *mtTopics) Subscribe(topic []byte, qos byte, sub interface{}, client_id string) (byte, error) {
	topic_str := string(topic)
	if !message.ValidQos(qos) {
		return message.QosFailure, fmt.Errorf("Invalid QoS %d", qos)
	}

	if sub == nil {
		return message.QosFailure, fmt.Errorf("Subscriber cannot be nil")
	}

	if qos > MXMaxQosAllowed {
		//     Log.Printf("invalid qos: %d\n", qos)
		qos = MXMaxQosAllowed
	}
	//   Log.Errorc(func() string{ return fmt.Sprintf("topic: %s, qos: %d,  client_id: %s\n", topic, qos, client_id)})

	this.smu.Lock()
	this.subscriber[topic_str] = sub
	this.smu.Unlock()

	topic_str = string(topic)
	Cmux.Lock()
	Channelcache[client_id] = topic_str
	ChannelReversecache[topic_str] = client_id
	Cmux.Unlock()

	return qos, nil
}
Esempio n. 2
0
// Returned values will be invalidated by the next Subscribers call
func (this *mtTopics) Subscribers(topic []byte, qos byte, subs *[]interface{}, qoss *[]byte) error {
	if !message.ValidQos(qos) {
		return fmt.Errorf("Invalid QoS %d", qos)
	}

	this.smu.RLock()
	(*subs)[0] = this.subscriber[string(topic)]
	this.smu.RUnlock()
	//   *qoss = (*qoss)[0:0]
	return nil
}
Esempio n. 3
0
// Returned values will be invalidated by the next Subscribers call
func (this *memTopics) Subscribers(topic []byte, qos byte, subs *[]interface{}, qoss *[]byte) error {
	if !message.ValidQos(qos) {
		return fmt.Errorf("Invalid QoS %d", qos)
	}

	this.smu.RLock()
	defer this.smu.RUnlock()

	*subs = (*subs)[0:0]
	*qoss = (*qoss)[0:0]

	return this.sroot.smatch(topic, qos, subs, qoss)
}
Esempio n. 4
0
func (this *memTopics) Subscribe(topic []byte, qos byte, sub interface{}) (byte, error) {
	if !message.ValidQos(qos) {
		return message.QosFailure, fmt.Errorf("Invalid QoS %d", qos)
	}

	if sub == nil {
		return message.QosFailure, fmt.Errorf("Subscriber cannot be nil")
	}

	this.smu.Lock()
	defer this.smu.Unlock()

	if qos > MaxQosAllowed {
		qos = MaxQosAllowed
	}

	if err := this.sroot.sinsert(topic, qos, sub); err != nil {
		return message.QosFailure, err
	}

	return qos, nil
}