/
module.go
130 lines (108 loc) · 3.64 KB
/
module.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* BitMonster - A Monster handling your Bits
* Copyright (C) 2015 Roland Singer <roland.singer[at]desertbit.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package bitmonster
import (
"fmt"
"github.com/desertbit/bitmonster/log"
)
var (
modules = make(map[string]*Module)
)
//####################//
//### Module Type ####//
//####################//
// A Module contains and handles methods and events.
type Module struct {
name string
methods map[string]*methodContext
events map[string]*Event
}
// NewModule creates and register a new BitMonster Module.
// This method is not thread-safe and should be called only
// during application initialization.
func NewModule(name string) (*Module, error) {
// Validate the name.
if len(name) == 0 {
return nil, fmt.Errorf("empty module name")
}
// Check if a module with the same name exists already.
if _, ok := modules[name]; ok {
return nil, fmt.Errorf("a module with the name '%s' exists already", name)
}
// Create a new module value.
m := &Module{
name: name,
methods: make(map[string]*methodContext),
events: make(map[string]*Event),
}
// Add the module to the modules map.
modules[name] = m
return m, nil
}
// Name returns the module's name.
func (m *Module) Name() string {
return m.name
}
// AddMethod adds a method which is callable from the client-side.
// Optionally pass hooks which are processed before.
// This method is not thread-safe and should be only called during
// module initialization.
func (m *Module) AddMethod(name string, method Method, hooks ...Hook) {
// Check if the method name already exists.
if _, ok := m.methods[name]; ok {
// Log the warning.
log.L.Warningf("module '%s': method '%s': a method with the name is already registered", m.name, name)
}
// Create the method context and set the method and its hooks.
mc := newMethodContext(method, hooks)
// Add it to the methods map.
m.methods[name] = mc
}
// AddMethods adds the methods in the methods map.
// Optionally pass hooks which are processed before.
// This method is not thread-safe and should be only called during
// module initialization.
func (m *Module) AddMethods(methodsMap MethodMap, hooks ...Hook) {
// Iterate through the map and add all methods.
for name, method := range methodsMap {
m.AddMethod(name, method, hooks...)
}
}
// AddEvent adds and registers a new event.
// The newly created event is returned.
func (m *Module) AddEvent(name string, hooks ...Hook) *Event {
// Check if the event name already exists.
if _, ok := m.events[name]; ok {
// Log the warning.
log.L.Warningf("module '%s': event '%s': an event with the name is already registered", m.name, name)
}
// Create a new event.
event := newEvent(name, hooks)
// Add it to the events map.
m.events[name] = event
return event
}
// Event returns the event specified by the name.
func (m *Module) Event(name string) (*Event, error) {
// Try to obtain the event.
e, ok := m.events[name]
if !ok {
return nil, fmt.Errorf("the event with the name '%s' does not exists", name)
}
return e, nil
}