-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
84 lines (64 loc) · 1.7 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package gomq
import (
"context"
"net/url"
"sync"
"github.com/workspace-9/gomq/transport"
"github.com/workspace-9/gomq/zmtp"
)
// SocketDriver represents a type of socket in a communitcation pattern.
type SocketDriver interface {
// Name of the type.
Name() string
// Connect to the remote address using the given transport.
Connect(tp transport.Transport, url *url.URL) error
// Disconnect from the address.
Disconnect(url *url.URL) error
// Bind to the given address using the given transport.
Bind(tp transport.Transport, url *url.URL) error
// Unbind from the bound address.
Unbind(url *url.URL) error
// Send a message over the socket.
Send([]zmtp.Message) error
// Recv either a command or a message on the socket.
Recv() ([]zmtp.Message, error)
// Close the socket
Close() error
}
// SocketConstructor constructs a socket.
type SocketConstructor func(
ctx context.Context,
mech zmtp.Mechanism,
conf *Config,
eventBus EventBus,
) (SocketDriver, error)
var registeredTypes struct {
types map[string]SocketConstructor
sync.RWMutex
}
func RegisterSocketType(
name string,
constructor SocketConstructor,
) error {
registeredTypes.Lock()
defer registeredTypes.Unlock()
if registeredTypes.types == nil {
registeredTypes.types = make(map[string]SocketConstructor)
}
if _, ok := registeredTypes.types[name]; ok {
return ErrTypeExists
}
registeredTypes.types[name] = constructor
return nil
}
type typeExists struct{}
func (typeExists) Error() string {
return "Type exists"
}
var ErrTypeExists typeExists
func FindSocketType(name string) (SocketConstructor, bool) {
registeredTypes.RLock()
defer registeredTypes.RUnlock()
cons, ok := registeredTypes.types[name]
return cons, ok
}