Exemplo n.º 1
0
func (this *service) publish(msg *message.PublishMessage, onComplete OnCompleteFunc) error {
	//Log.Debugc(func() string{ return fmt.Sprintf("service/publish: Publishing %s", msg)})
	//   Log.Errorc(func() string{ return fmt.Sprintf("msg is : %v", msg)})
	_, err := this.writeMessage(msg)
	if err != nil {
		return fmt.Errorf("(%s) Error sending %s message: %v", this.cid(), msg.Name(), err)
	}

	switch msg.QoS() {
	case message.QosAtMostOnce:
		if onComplete != nil {
			return onComplete(msg, nil, nil)
		}

		return nil

	case message.QosAtLeastOnce:
		return this.sess.Pub1ack.Wait(msg, onComplete)

	case message.QosExactlyOnce:
		return this.sess.Pub2out.Wait(msg, onComplete)
	}

	return nil
}
Exemplo n.º 2
0
// 判断消息是否已读
func (this *service) handlePendingMessage(msg *message.PublishMessage, pending_status *PendingStatus) {
	// 如果QOS=0,则无需等待直接返回
	if string(msg.Topic()) == OnlineStatusChannel {
		return
	}

	// 将msg按照pkt_id,存入pending队列
	pkt_id := msg.PacketId()
	//   pending_msg := NewPendingMessage(msg)

	select {
	case <-pending_status.Done:
	// 消息已成功接收,不再等待
	case <-time.After(time.Second * MsgPendingTime):
		// 重试一次
		Log.Debugc(func() string {
			return fmt.Sprintf("(%s) receive ack %d timeout. try to resend. topic: %s", this.cid(), msg.PacketId(), msg.Topic())
		})
		this.retryPublish(msg)

		select {
		case <-pending_status.Done:
			// 重发的消息已成功接收,不再等待
		case <-time.After(time.Second * MsgPendingTime):
			// 没有回ack,放到离线队列里
			Log.Debugc(func() string {
				return fmt.Sprintf("(%s) receive ack %d timeout. send msg to offline msg queue.topic: %s", this.cid(), msg.PacketId(), msg.Topic())
				//       return fmt.Sprintf("(%s) receive ack timeout. send msg to offline msg queue.topic: %s, payload: %s", this.cid(), msg.Topic(),msg.Payload())
			})
			OfflineTopicQueueProcessor <- msg
		}
	}
	PendingQueue[pkt_id] = nil
}
Exemplo n.º 3
0
// For PUBLISH message, we should figure out what QoS it is and process accordingly
// If QoS == 0, we should just take the next step, no ack required
// If QoS == 1, we should send back PUBACK, then take the next step
// If QoS == 2, we need to put it in the ack queue, send back PUBREC
func (this *service) processPublish(msg *message.PublishMessage) error {
	switch msg.QoS() {
	case message.QosExactlyOnce:
		this.sess.Pub2in.Wait(msg, nil)

		resp := message.NewPubrecMessage()
		resp.SetPacketId(msg.PacketId())

		_, err := this.writeMessage(resp)

		err = this.preDispatchPublish(msg)
		return err

	case message.QosAtLeastOnce:
		resp := message.NewPubackMessage()
		resp.SetPacketId(msg.PacketId())

		if _, err := this.writeMessage(resp); err != nil {
			return err
		}

		err := this.preDispatchPublish(msg)
		return err
	case message.QosAtMostOnce:
		err := this.preDispatchPublish(msg)
		return err
	default:
		fmt.Printf("default: %d\n", msg.QoS())
	}

	return fmt.Errorf("(%s) invalid message QoS %d.", this.cid(), msg.QoS())
}
Exemplo n.º 4
0
func pub(c *service.Client, msg *message.PublishMessage) error {
	log.Printf("on publish: topic=%s payload=%s\n", string(msg.Topic()), string(msg.Payload()))
	pub := message.NewPublishMessage()
	pub.SetTopic([]byte("a/b/c/response"))
	pub.SetQoS(0)
	pub.SetPayload([]byte("Hi MQTT Server"))
	return c.Publish(pub, nil)
}
Exemplo n.º 5
0
//根据指定ID查询客户端在线状态,并推送消息
func (this *service) checkOnlineStatus(msg *message.PublishMessage) {
	client_id := string(msg.Payload())
	online, lasttime, _ := GetOnlineStatus(client_id)

	payload := []byte(fmt.Sprintf(`{"client_id": "%s", "status": "%s", "since": "%s"}`, client_id, online, lasttime))

	msg.SetPayload(payload)
	this.postPublish(msg)
}
Exemplo n.º 6
0
func onPublish(msg *message.PublishMessage) error {
	pr := &netx.PingResult{}
	if err := pr.GobDecode(msg.Payload()); err != nil {
		log.Printf("Error decoding ping result: %v\n", err)
		return err
	}

	log.Println(pr)
	return nil
}
Exemplo n.º 7
0
// onPublish() is called when the server receives a PUBLISH message AND have completed
// the ack cycle. This method will get the list of subscribers based on the publish
// topic, and publishes the message to the list of subscribers.
func (this *service) onPublish(msg *message.PublishMessage) error {
	if msg.Retain() {
		if err := this.topicsMgr.Retain(msg); err != nil {
			glog.Errorf("(%s) Error retaining message: %v", this.cid(), err)
		}
	}

	err := this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &this.subs, &this.qoss)
	if err != nil {
		glog.Errorf("(%s) Error retrieving subscribers list: %v", this.cid(), err)
		return err
	}

	msg.SetRetain(false)

	//glog.Debugf("(%s) Publishing to topic %q and %d subscribers", this.cid(), string(msg.Topic()), len(this.subs))
	for _, s := range this.subs {
		if s != nil {
			fn, ok := s.(*OnPublishFunc)
			if !ok {
				glog.Errorf("Invalid onPublish Function")
				return fmt.Errorf("Invalid onPublish Function")
			} else {
				(*fn)(msg)
			}
		}
	}

	return nil
}
Exemplo n.º 8
0
// Publish sends a single MQTT PUBLISH message to the server. On completion, the
// supplied OnCompleteFunc is called. For QOS 0 messages, onComplete is called
// immediately after the message is sent to the outgoing buffer. For QOS 1 messages,
// onComplete is called when PUBACK is received. For QOS 2 messages, onComplete is
// called after the PUBCOMP message is received.
func (this *Server) Publish(msg *message.PublishMessage, onComplete OnCompleteFunc) error {
	if err := this.checkConfiguration(); err != nil {
		return err
	}

	if msg.Retain() {
		if err := this.topicsMgr.Retain(msg); err != nil {
			glog.Errorf("Error retaining message: %v", err)
		}
	}

	if err := this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &this.subs, &this.qoss); err != nil {
		return err
	}

	msg.SetRetain(false)

	//glog.Debugf("(server) Publishing to topic %q and %d subscribers", string(msg.Topic()), len(this.subs))
	for _, s := range this.subs {
		if s != nil {
			fn, ok := s.(*OnPublishFunc)
			if !ok {
				glog.Errorf("Invalid onPublish Function")
			} else {
				(*fn)(msg)
			}
		}
	}

	return nil
}
Exemplo n.º 9
0
func (this *rnode) rinsert(topic []byte, msg *message.PublishMessage) error {
	// If there's no more topic levels, that means we are at the matching rnode.
	if len(topic) == 0 {
		l := msg.Len()

		// Let's reuse the buffer if there's enough space
		if l > cap(this.buf) {
			this.buf = make([]byte, l)
		} else {
			this.buf = this.buf[0:l]
		}

		if _, err := msg.Encode(this.buf); err != nil {
			return err
		}

		// Reuse the message if possible
		if this.msg == nil {
			this.msg = message.NewPublishMessage()
		}

		if _, err := this.msg.Decode(this.buf); err != nil {
			return err
		}

		return nil
	}

	// Not the last level, so let's find or create the next level snode, and
	// recursively call it's insert().

	// ntl = next topic level
	ntl, rem, err := nextTopicLevel(topic)
	if err != nil {
		return err
	}

	level := string(ntl)

	// Add snode if it doesn't already exist
	n, ok := this.rnodes[level]
	if !ok {
		n = newRNode()
		this.rnodes[level] = n
	}

	return n.rinsert(rem, msg)
}
Exemplo n.º 10
0
func (this *Session) RetainMessage(msg *message.PublishMessage) error {
	this.mu.Lock()
	defer this.mu.Unlock()

	this.rbuf = make([]byte, msg.Len())
	this.Retained = message.NewPublishMessage()

	if _, err := msg.Encode(this.rbuf); err != nil {
		return err
	}

	if _, err := this.Retained.Decode(this.rbuf); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 11
0
// processPublish() is called when the server receives a PUBLISH message AND have completed
// the ack cycle. This method will get the list of subscribers based on the publish
// topic, and publishes the message to the list of subscribers.
func (this *service) postPublish(msg *message.PublishMessage) (err error) {
	//   if msg.Retain() {
	//     if err = this.topicsMgr.Retain(msg); err != nil {
	//       Log.Errorc(func() string{ return fmt.Sprintf("(%s) Error retaining message: %v", this.cid(), err)})
	//     }
	//   }

	//   var subs []interface{}
	topic := string(msg.Topic())

	if !IsOnline(topic) {
		Log.Debugc(func() string {
			return fmt.Sprintf("(%s) this client is offline, send %d to offline queue.", this.cid(), msg.PacketId())
		})
		OfflineTopicQueueProcessor <- msg
		return nil
	}

	subs := _get_temp_subs()
	defer _return_temp_subs(subs)

	err = this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &subs, nil)
	if err != nil {
		Log.Errorc(func() string {
			return fmt.Sprintf("(%s) Error retrieving subscribers list: %v", this.cid(), err)
		})
		return err
	}

	//   Log.Errorc(func() string{ return fmt.Sprintf("(%s) Publishing to topic %q and %d subscribers", this.cid(), string(msg.Topic()), len(this.subs))})
	//   fmt.Printf("value: %v\n", config.GetModel())
	//   done := make(chan bool)
	pending_status := NewPendingStatus(topic, msg)
	pkt_id := msg.PacketId()
	PendingQueue[pkt_id] = pending_status

	go this.handlePendingMessage(msg, pending_status)

	for _, s := range subs {
		if s != nil {
			fn, ok := s.(*OnPublishFunc)
			if !ok {
				Log.Errorc(func() string {
					return fmt.Sprintf("Invalid onPublish Function: %T", s)
				})
				return fmt.Errorf("Invalid onPublish Function")
			} else {
				(*fn)(msg)
				//         Log.Errorc(func() string{ return fmt.Sprintf("OfflineTopicQueue[%s]: %v, len is: %d\n", msg.Topic(), OfflineTopicQueue[string(msg.Topic())], len(OfflineTopicQueue[string(msg.Topic())]))})
			}
		}
	}

	return nil
}
Exemplo n.º 12
0
// 预投递publish类型的消息,如果是特殊频道特殊处理,否则正常处理
func (this *service) preDispatchPublish(msg *message.PublishMessage) (err error) {
	switch string(msg.Topic()) {
	case BroadCastChannel:
		go OnGroupPublish(msg, this)
	case SendChannel:
		go this.onReceiveBadge(msg)
	case ApnPushChannel:
		go onAPNsPush(msg, this)
	case ApnInvalidTokensChannel:
		go getInvalidApnTokens(this)
	case OnlineStatusChannel:
		go this.checkOnlineStatus(msg)
	default:
		msg.SetPacketId(getNextPktId())
		Log.Infoc(func() string {
			return fmt.Sprintf("(%s) process private message.pkt_id: %d, payload size: %d", this.cid(), msg.PacketId(), len(msg.Payload()))
		})
		go this.postPublish(msg)
	}
	return
}
Exemplo n.º 13
0
// Publish sends a single MQTT PUBLISH message to the server. On completion, the
// supplied OnCompleteFunc is called. For QOS 0 messages, onComplete is called
// immediately after the message is sent to the outgoing buffer. For QOS 1 messages,
// onComplete is called when PUBACK is received. For QOS 2 messages, onComplete is
// called after the PUBCOMP message is received.
func (this *Server) Publish(msg *message.PublishMessage, onComplete OnCompleteFunc) (err error) {
	if err = this.checkConfiguration(); err != nil {
		return err
	}

	if err = this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &this.subs, &this.qoss); err != nil {
		//   if err = this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &this.subs, &this.qoss); err != nil {
		return err
	}

	subs := _get_temp_subs()
	defer _return_temp_subs(subs)
	//   defer _return_tmp_msg(msg)

	Log.Debugc(func() string {
		return fmt.Sprintf("(server) Publishing to topic %s and %d subscribers", string(msg.Topic()), len(subs))
	})

	for _, s := range subs {
		if s != nil {
			fn, ok := s.(*OnPublishFunc)
			if !ok {
				Log.Error("Invalid onPublish Function")
			} else {
				err = (*fn)(msg)
			}
		}
	}

	return err
}
Exemplo n.º 14
0
func (this *memTopics) Retain(msg *message.PublishMessage) error {
	this.rmu.Lock()
	defer this.rmu.Unlock()

	// So apparently, at least according to the MQTT Conformance/Interoperability
	// Testing, that a payload of 0 means delete the retain message.
	// https://eclipse.org/paho/clients/testing/
	if len(msg.Payload()) == 0 {
		return this.rroot.rremove(msg.Topic())
	}

	return this.rroot.rinsert(msg.Topic(), msg)
}
Exemplo n.º 15
0
func (this *service) retryPublish(msg *message.PublishMessage) (err error) {
	//   if msg.Retain() {
	//     if err = this.topicsMgr.Retain(msg); err != nil {
	//       Log.Errorc(func() string{ return fmt.Sprintf("(%s) Error retaining message: %v", this.cid(), err)})
	//     }
	//   }

	//   var subs []interface{}
	topic := string(msg.Topic())

	subs := _get_temp_subs()
	defer _return_temp_subs(subs)

	err = this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &subs, nil)
	if err != nil {
		Log.Errorc(func() string {
			return fmt.Sprintf("(%s) Error retrieving subscribers list: %v", this.cid(), err)
		})
		return err
	}

	pending_status := NewPendingStatus(topic, msg)
	pkt_id := msg.PacketId()
	PendingQueue[pkt_id] = pending_status

	for _, s := range subs {
		if s != nil {
			fn, ok := s.(*OnPublishFunc)
			if !ok {
				Log.Errorc(func() string {
					return fmt.Sprintf("Invalid onPublish Function: %T", s)
				})
				return fmt.Errorf("Invalid onPublish Function")
			} else {
				(*fn)(msg)
			}
		}
	}

	return nil
}
Exemplo n.º 16
0
func (this *service) onReceiveBadge(msg *message.PublishMessage) (err error) {
	var badge_message BadgeMessage

	datas := strings.Split(string(msg.Payload()), ":")
	//   datas := strings.Split(fmt.Sprintf("%s", msg.Payload()), ":")
	if len(datas) != 2 {
		Log.Errorc(func() string {
			return fmt.Sprintf("(%s) invalid message payload: %s", this.cid(), msg.Payload())
		})
		return errors.New(fmt.Sprintf("invalid message payload: %s", msg.Payload()))
	}

	account_id := datas[0]
	payload_base64 := datas[1]

	if payload_base64 == "" {
		return errors.New(fmt.Sprintf("(%s) blank base64 payload, abort. %s", this.cid(), msg.Payload()))
	}

	payload_bytes, err := base64.StdEncoding.DecodeString(payload_base64)
	if err != nil {
		Log.Errorc(func() string {
			return fmt.Sprintf("(%s) can't decode payload: %s", this.cid(), payload_base64)
		})
	}

	err = ffjson.Unmarshal([]byte(payload_bytes), &badge_message)
	if err != nil {
		Log.Errorc(func() string {
			return fmt.Sprintf("(%s) can't parse badge json: account_id: %s, payload: %s", this.cid(), account_id, payload_bytes)
		})
		return
	}
	//   Log.Infoc(func() string{ return fmt.Sprintf("badge: %v, type: %T\n", badge_message.Data, badge_message.Data)})

	go this.processBadge(account_id, &badge_message)
	return
}
Exemplo n.º 17
0
func onPublishFunc(msg *message.PublishMessage) error {
	jklog.L().Infoln("Recevied on publish func")
	jklog.L().Infoln("name: ", msg.Name())
	jklog.L().Infoln("payload: ", string(msg.Payload()))
	return nil
}
Exemplo n.º 18
0
func assertPublishMessage(t *testing.T, msg *message.PublishMessage, qos byte) {
	require.Equal(t, "abc", string(msg.Payload()))
	require.Equal(t, qos, msg.QoS())
}