listener.go 835 Bytes
Newer Older
1 2 3 4
package p2p

type Listener interface {
	Protocol() string
Łukasz Magiera's avatar
Łukasz Magiera committed
5 6
	ListenAddress() string
	TargetAddress() string
7 8 9 10 11

	// Close closes the listener. Does not affect child streams
	Close() error
}

12 13 14 15 16 17
type listenerKey struct {
	proto  string
	listen string
	target string
}

18 19
// ListenerRegistry is a collection of local application proto listeners.
type ListenerRegistry struct {
20
	Listeners map[listenerKey]Listener
21 22
}

23 24 25
// Register registers listenerInfo in this registry
func (c *ListenerRegistry) Register(l Listener) {
	c.Listeners[getListenerKey(l)] = l
26 27 28
}

// Deregister removes p2p listener from this registry
29 30 31 32 33 34 35 36 37 38
func (c *ListenerRegistry) Deregister(k listenerKey) {
	delete(c.Listeners, k)
}

func getListenerKey(l Listener) listenerKey {
	return listenerKey{
		proto:  l.Protocol(),
		listen: l.ListenAddress(),
		target: l.TargetAddress(),
	}
39
}