Exemple #1
0
func NewPubSub(node Node, onMessage OnPubSubMessage) PubSub {
	ps := &pubsub{}
	ps.node = node
	ps.commandChan = utility.NewNonblockingChan(128)
	ps.onMessage = onMessage
	ps.subscribers = make(map[string]map[string]bool)
	ps.nodeTopics = make(map[string]map[string]bool)
	node.RegisterMessage(subscribedTopicsMessage{})
	node.RegisterMessage(subscribeMessage{})
	node.RegisterMessage(unsubscribeMessage{})
	node.RegisterMessage(publishMessage{})
	onNodeJoin := NodeJoinListener(func(nc NodeConnection) {
		nc.AddMessageHandler(MessageHandler(
			func(message interface{}) bool {
				return ps.handleMessage(nc.Info().Address, message)
			}))
		command := func() {
			ps.sendMyTopicsTo(nc)
		}
		ps.postCommand(command)
	})
	onNodeDrop := NodeDropListener(func(nc NodeConnection) {
		command := func() {
			ps.removeSubscribersByAddress(nc.Info().Address)
		}
		ps.postCommand(command)
	})
	ps.node.AddNodeJoinListener(onNodeJoin)
	ps.node.AddNodeDropListener(onNodeDrop)
	return ps
}
Exemple #2
0
func NewTransporter(encoder Encoder,
	decoder Decoder,
	dispatcher Dispatcher,
	errorHandler ErrorHandler) Transporter {
	var t transporter
	t.encoder = encoder
	t.decoder = decoder
	t.dispatcher = dispatcher
	t.errorHandler = errorHandler
	t.sendbox = utility.NewNonblockingChan(128)
	return &t
}