-
Notifications
You must be signed in to change notification settings - Fork 17
/
router.go
68 lines (58 loc) · 1.54 KB
/
router.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
package router
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
"github.com/datachainlab/cross/x/packets"
)
type Router interface {
AddRoute(typeName string, h PacketHandler)
GetRoute(typeName string) (PacketHandler, bool)
}
type PacketHandler interface {
HandlePacket(
ctx sdk.Context,
packet channeltypes.Packet,
ip packets.IncomingPacket,
) (*sdk.Result, *packets.PacketAcknowledgementData, error)
HandleACK(
ctx sdk.Context,
packet channeltypes.Packet,
ip packets.IncomingPacket,
ipa packets.IncomingPacketAcknowledgement,
) (*sdk.Result, error)
}
type router struct {
routes map[string]PacketHandler
sealed bool
}
func NewRouter() Router {
return &router{
routes: make(map[string]PacketHandler),
}
}
func (r *router) AddRoute(typeName string, h PacketHandler) {
if r.sealed {
panic(fmt.Sprintf("router sealed; cannot register %s route callbacks", typeName))
}
if _, ok := r.routes[typeName]; ok {
panic(fmt.Sprintf("Route type '%v' already exist", typeName))
}
r.routes[typeName] = h
}
func (r *router) GetRoute(typeName string) (PacketHandler, bool) {
route, ok := r.routes[typeName]
return route, ok
}
// Seal prevents the Router from any subsequent route handlers to be registered.
// Seal will panic if called more than once.
func (r *router) Seal() {
if r.sealed {
panic("router already sealed")
}
r.sealed = true
}
// Sealed returns a boolean signifying if the Router is sealed or not.
func (r router) Sealed() bool {
return r.sealed
}