示例#1
0
// Signal counterpart of Signl-Slot design pattern.
func (g *Gilmour) Signal(topic string, msg *Message) (sender string, err error) {
	if msg == nil {
		msg = NewMessage()
	}

	sender = proto.SenderId()
	msg.setSender(sender)
	return sender, g.publish(g.slotDestination(topic), msg)
}
示例#2
0
// Request part of Request-Reply design pattern.
func (g *Gilmour) request(topic string, msg *Message, opts *RequestOpts) (*Response, error) {
	if has, err := g.backend.HasActiveSubscribers(g.requestDestination(topic)); err != nil {
		return nil, err
	} else if !has {
		return nil, errors.New("No active listeners for: " + topic)
	}

	if msg == nil {
		msg = NewMessage()
	}

	sender := proto.SenderId()
	msg.setSender(sender)
	respChannel := proto.ResponseTopic(sender)

	if opts == nil {
		opts = NewRequestOpts()
	}

	//Wait for a responseHandler
	f := make(chan *Message, 1)

	rOpts := NewHandlerOpts().setOneShot().sendResponse(false)
	g.ReplyTo(respChannel, func(req *Request, _ *Message) {
		f <- req.gData
	}, rOpts)

	// Send Timeout message to channel, Need to send timeout over the wire,
	// to ensure gilmour cleans up the oneShot response handlers as well.
	timeout := opts.GetTimeout()
	if timeout > 0 {
		time.AfterFunc(time.Duration(timeout)*time.Second, func() {
			g.sendTimeout(sender, respChannel)
		})
	}

	g.publish(g.requestDestination(topic), msg)

	response := newResponse(1)
	response.write(<-f)
	return response, nil
}
示例#3
0
func NewMessage() *Message {
	x := &Message{}
	x.setSender(proto.SenderId())
	return x
}