forked from Argus-Labs/world-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_manager.go
103 lines (87 loc) · 3.15 KB
/
message_manager.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package cardinal
import (
"errors"
"reflect"
"github.com/rotisserie/eris"
"pkg.world.dev/world-engine/cardinal/types"
)
type MessageManager interface {
RegisterMessage(msgType types.Message, msgReflectType reflect.Type) error
GetRegisteredMessages() []types.Message
GetMessageByID(id types.MessageID) types.Message
GetMessageByFullName(fullName string) (types.Message, bool)
GetMessageByType(mType reflect.Type) (types.Message, bool)
}
type messageManager struct {
// registeredMessages maps message FullNames to a types.Message.
registeredMessages map[string]types.Message
registeredMessagesByType map[reflect.Type]types.Message
nextMessageID types.MessageID
}
func newMessageManager() MessageManager {
return &messageManager{
registeredMessages: map[string]types.Message{},
registeredMessagesByType: map[reflect.Type]types.Message{},
nextMessageID: 1,
}
}
func (m *messageManager) RegisterMessage(msgType types.Message, msgReflectType reflect.Type) error {
fullName := msgType.FullName()
// Checks if the message is already previously registered.
if err := errors.Join(m.isMessageFullNameUnique(fullName), m.isMessageTypeUnique(msgReflectType)); err != nil {
return err
}
// Set the message ID.
// TODO(scott): we should probably deprecate this and just decide whether we want to use fullName or ID.
err := msgType.SetID(m.nextMessageID)
if err != nil {
return eris.Errorf("failed to set id on message %q", msgType.Name())
}
m.registeredMessages[fullName] = msgType
m.registeredMessagesByType[msgReflectType] = msgType
m.nextMessageID++
return nil
}
// GetRegisteredMessages returns the list of all registered messages
func (m *messageManager) GetRegisteredMessages() []types.Message {
msgs := make([]types.Message, 0, len(m.registeredMessages))
for _, msg := range m.registeredMessages {
msgs = append(msgs, msg)
}
return msgs
}
// GetMessageByID iterates over the all registered messages and returns the types.Message associated with the
// MessageID.
func (m *messageManager) GetMessageByID(id types.MessageID) types.Message {
for _, msg := range m.registeredMessages {
if id == msg.ID() {
return msg
}
}
return nil
}
// GetMessageByFullName returns the message with the given full name, if it exists.
func (m *messageManager) GetMessageByFullName(fullName string) (types.Message, bool) {
msg, ok := m.registeredMessages[fullName]
return msg, ok
}
func (m *messageManager) GetMessageByType(mType reflect.Type) (types.Message, bool) {
msg, ok := m.registeredMessagesByType[mType]
return msg, ok
}
// isMessageFullNameUnique checks if the message name already exist in messages map.
func (m *messageManager) isMessageFullNameUnique(fullName string) error {
_, ok := m.registeredMessages[fullName]
if ok {
return eris.Errorf("message %q is already registered", fullName)
}
return nil
}
// isMessageTypeUnique checks if the message type name already exist in messages map.
func (m *messageManager) isMessageTypeUnique(msgReflectType reflect.Type) error {
_, ok := m.registeredMessagesByType[msgReflectType]
if ok {
return eris.Errorf("message type %q is already registered", msgReflectType)
}
return nil
}