示例#1
0
func (c *conn) handleAck(p *proto.Proto) error {
	queue := p.Queue()

	if len(queue) == 0 {
		return c.protoError(http.StatusForbidden, "queue must supplied")
	}

	ch, ok := c.channels[queue]
	if !ok {
		return c.protoError(http.StatusForbidden, "invalid queue")
	}

	msgId, err := strconv.ParseInt(p.MsgId(), 10, 64)
	if err != nil {
		return err
	}

	ch.Ack(msgId)

	return nil
}
示例#2
0
func (c *conn) handlePublish(p *proto.Proto) error {
	tp := p.PubType()
	queue := p.Queue()
	routingKey := p.RoutingKey()

	message := p.Body

	if err := checkPublish(queue, routingKey, tp, message); err != nil {
		return c.protoError(http.StatusBadRequest, err.Error())
	}

	msg, err := c.app.saveMsg(queue, routingKey, tp, message)
	if err != nil {
		return c.protoError(http.StatusInternalServerError, err.Error())
	}

	q := c.app.qs.Get(queue)
	q.Push(msg)

	np := proto.NewPublishOKProto(strconv.FormatInt(msg.id, 10))

	c.writeProto(np.P)

	return nil
}
示例#3
0
func (c *conn) handleBind(p *proto.Proto) error {
	queue := p.Queue()
	routingKey := p.RoutingKey()

	if err := checkBind(queue, routingKey); err != nil {
		return c.protoError(http.StatusBadRequest, err.Error())
	}

	noAck := (p.Value(proto.NoAckStr) == "1")

	ch, ok := c.channels[queue]
	if !ok {
		q := c.app.qs.Get(queue)
		ch = newChannel(&connMsgPusher{c}, q, routingKey, noAck)
		c.channels[queue] = ch
	} else {
		ch.Reset(routingKey, noAck)
	}

	np := proto.NewBindOKProto(queue)

	c.writeProto(np.P)

	return nil
}
示例#4
0
func (c *conn) handleUnbind(p *proto.Proto) error {
	queue := p.Queue()
	if len(queue) == 0 {
		c.unBindAll()

		np := proto.NewUnbindOKProto(queue)

		c.writeProto(np.P)
		return nil
	}

	if ch, ok := c.channels[queue]; ok {
		delete(c.channels, queue)
		ch.Close()
	}

	np := proto.NewUnbindOKProto(queue)

	c.writeProto(np.P)

	return nil
}