-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
101 lines (82 loc) · 2.85 KB
/
handler.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
package cmppserver
import (
"context"
"errors"
"fmt"
protocol "github.com/hujm2023/go-sms-protocol"
"github.com/hujm2023/go-sms-protocol/cmpp"
"github.com/hujm2023/go-sms-protocol/cmpp/cmpp20"
"github.com/hujm2023/hlog"
)
var ErrInvalidPDUAssert = errors.New("invalid pdu assert")
type CommandHandler func(ctx context.Context, pdu protocol.PDU) (resp []byte, err error)
type Dispatcher struct {
handlers map[protocol.ICommander]CommandHandler
}
func newDisPatcher() *Dispatcher {
return &Dispatcher{
handlers: make(map[protocol.ICommander]CommandHandler),
}
}
func (d *Dispatcher) Register(cmd protocol.ICommander, handler CommandHandler) {
if _, ok := d.handlers[cmd]; ok {
panic(fmt.Sprintf("%s has been registered", cmd.String()))
}
d.handlers[cmd] = handler
}
func (d *Dispatcher) Dispatch(ctx context.Context, data []byte) (resp []byte, err error) {
pdu, err := cmpp20.DecodeCMPP20(data)
if err != nil {
return nil, fmt.Errorf("decode cmpp20 error: %w", err)
}
cmd := pdu.GetCommand()
handler, ok := d.handlers[cmd]
if !ok {
return nil, fmt.Errorf("%s not implemented", cmd.String())
}
return handler(ctx, pdu)
}
var cmpp20Dispatcher = newDisPatcher()
func init() {
cmpp20Dispatcher.Register(cmpp.CommandConnect, cmpp20Connect)
cmpp20Dispatcher.Register(cmpp.CommandSubmit, cmpp20Submit)
cmpp20Dispatcher.Register(cmpp.CommandActiveTest, cmpp20ActiveTest)
cmpp20Dispatcher.Register(cmpp.CommandActiveTestResp, cmpp20ActiveTestResp)
cmpp20Dispatcher.Register(cmpp.CommandDeliverResp, cmpp20DeliveyResp)
}
func cmpp20Connect(ctx context.Context, pdu protocol.PDU) (resp []byte, err error) {
connect, ok := pdu.(*cmpp20.PduConnect)
if !ok {
return nil, ErrInvalidPDUAssert
}
hlog.CtxInfo(ctx, "[cmpp20Connect] user:%s", connect.SourceAddr)
// TODO: handle auth
return connect.GenEmptyResponse().IEncode()
}
func cmpp20Submit(ctx context.Context, pdu protocol.PDU) (resp []byte, err error) {
// handle sumit content
submit, ok := pdu.(*cmpp20.PduSubmit)
if !ok {
return nil, ErrInvalidPDUAssert
}
content, err := protocol.DecodeCMPPCContent(ctx, submit.MsgContent, submit.MsgFmt)
if err != nil {
hlog.CtxWarn(ctx, "[cmpp20Submit] decode content error: %v", err)
return nil, nil
}
submitResp := submit.GenEmptyResponse().(*cmpp20.PduSubmitResp)
submitResp.MsgID = GenMsgID()
submitResp.Result = 0
hlog.CtxInfo(ctx, "[cmpp20Submit] content:%s, msgID:%d", content, submitResp.MsgID)
return submitResp.IEncode()
}
func cmpp20ActiveTest(ctx context.Context, pdu protocol.PDU) (resp []byte, err error) {
return pdu.GenEmptyResponse().IEncode()
}
func cmpp20ActiveTestResp(ctx context.Context, pdu protocol.PDU) (resp []byte, err error) {
hlog.CtxInfo(ctx, "[cmpp20ActiveTestResp] received an active test resp")
return nil, nil
}
func cmpp20DeliveyResp(ctx context.Context, pdu protocol.PDU) (resp []byte, err error) {
return nil, nil
}