Esempio n. 1
0
// Handles the load balancing event of a topio.
func (o *Overlay) handleBalance(msg *proto.Message, topicId *big.Int, prevHop *big.Int) (bool, error) {
	sid := topicId.String()

	// Fetch the topic or report not found
	o.lock.RLock()
	top, ok := o.topics[sid]
	topName := o.names[sid]
	o.lock.RUnlock()
	if !ok {
		// No error, but not handled either
		return false, nil
	}
	// Fetch the recipient and either forward or deliver
	node, err := top.Balance(prevHop)
	if err != nil {
		return true, err
	}
	// If it's a remote node, forward
	if node.Cmp(o.pastry.Self()) != 0 {
		o.fwdBalance(node, msg)
		return true, nil
	}
	// Remove all carrier headers and decrypt
	head := msg.Head.Meta.(*header)
	msg.Head.Meta = head.Meta
	if err := msg.Decrypt(); err != nil {
		return true, err
	}
	// Deliver to the application on the specific topic
	o.app.HandleBalance(head.Sender, topName, msg)
	return true, nil
}
Esempio n. 2
0
// Handles the receiving of a direct message and delivers the contents upstream.
func (o *Overlay) handleDirect(msg *proto.Message) error {
	// Remove all scribe headers and decrypt contents
	head := msg.Head.Meta.(*header)
	msg.Head.Meta = head.Meta
	if err := msg.Decrypt(); err != nil {
		return err
	}
	// Deliver the message upstream
	o.app.HandleDirect(head.Sender, msg)
	return nil
}