• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

linxGnu / gosmpp / 4076575789

pending completion
4076575789

push

github

Emmanuel BOUTON
feat: add descriptions to command status and enhance error message on binding error

4 of 4 new or added lines in 1 file covered. (100.0%)

419 of 481 relevant lines covered (87.11%)

24.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.73
/connect.go
1
package gosmpp
2

3
import (
4
        "fmt"
5
        "net"
6

7
        "github.com/linxGnu/gosmpp/data"
8
        "github.com/linxGnu/gosmpp/pdu"
9
)
10

11
var (
12
        // NonTLSDialer is non-tls connection dialer.
13
        NonTLSDialer = func(addr string) (net.Conn, error) {
15✔
14
                return net.Dial("tcp", addr)
15✔
15
        }
15✔
16
)
17

18
// Dialer is connection dialer.
19
type Dialer func(addr string) (net.Conn, error)
20

21
// Auth represents basic authentication to SMSC.
22
type Auth struct {
23
        // SMSC is SMSC address.
24
        SMSC       string
25
        SystemID   string
26
        Password   string
27
        SystemType string
28
}
29

30
type BindError struct {
31
        CommandStatus data.CommandStatusType
32
}
33

34
func (err BindError) Error() string {
1✔
35
        return fmt.Sprintf("binding error (%s): %s", err.CommandStatus, err.CommandStatus.Desc())
1✔
36
}
1✔
37

38
func newBindRequest(s Auth, bindingType pdu.BindingType) (bindReq *pdu.BindRequest) {
15✔
39
        bindReq = pdu.NewBindRequest(bindingType)
15✔
40
        bindReq.SystemID = s.SystemID
15✔
41
        bindReq.Password = s.Password
15✔
42
        bindReq.SystemType = s.SystemType
15✔
43
        return
15✔
44
}
15✔
45

46
// Connector is connection factory interface.
47
type Connector interface {
48
        Connect() (conn *Connection, err error)
49
}
50

51
type connector struct {
52
        dialer      Dialer
53
        auth        Auth
54
        bindingType pdu.BindingType
55
}
56

57
func (c *connector) Connect() (conn *Connection, err error) {
15✔
58
        conn, err = connect(c.dialer, c.auth.SMSC, newBindRequest(c.auth, c.bindingType))
15✔
59
        return
15✔
60
}
15✔
61

62
func connect(dialer Dialer, addr string, bindReq *pdu.BindRequest) (c *Connection, err error) {
15✔
63
        conn, err := dialer(addr)
15✔
64
        if err != nil {
15✔
65
                return
×
66
        }
×
67

68
        // create wrapped connection
69
        c = NewConnection(conn)
15✔
70

15✔
71
        // send binding request
15✔
72
        _, err = c.WritePDU(bindReq)
15✔
73
        if err != nil {
15✔
74
                _ = conn.Close()
×
75
                return
×
76
        }
×
77

78
        // catching response
79
        var (
15✔
80
                p    pdu.PDU
15✔
81
                resp *pdu.BindResp
15✔
82
        )
15✔
83

15✔
84
        for {
30✔
85
                if p, err = pdu.Parse(c); err != nil {
15✔
86
                        _ = conn.Close()
×
87
                        return
×
88
                }
×
89

90
                if pd, ok := p.(*pdu.BindResp); ok {
30✔
91
                        resp = pd
15✔
92
                        break
15✔
93
                }
94
        }
95

96
        if resp.CommandStatus != data.ESME_ROK {
16✔
97
                err = BindError{CommandStatus: resp.CommandStatus}
1✔
98
                _ = conn.Close()
1✔
99
        } else {
15✔
100
                c.systemID = resp.SystemID
14✔
101
        }
14✔
102

103
        return
15✔
104
}
105

106
// TXConnector returns a Transmitter (TX) connector.
107
func TXConnector(dialer Dialer, auth Auth) Connector {
5✔
108
        return &connector{
5✔
109
                dialer:      dialer,
5✔
110
                auth:        auth,
5✔
111
                bindingType: pdu.Transmitter,
5✔
112
        }
5✔
113
}
5✔
114

115
// RXConnector returns a Receiver (RX) connector.
116
func RXConnector(dialer Dialer, auth Auth) Connector {
3✔
117
        return &connector{
3✔
118
                dialer:      dialer,
3✔
119
                auth:        auth,
3✔
120
                bindingType: pdu.Receiver,
3✔
121
        }
3✔
122
}
3✔
123

124
// TRXConnector returns a Transceiver (TRX) connector.
125
func TRXConnector(dialer Dialer, auth Auth) Connector {
2✔
126
        return &connector{
2✔
127
                dialer:      dialer,
2✔
128
                auth:        auth,
2✔
129
                bindingType: pdu.Transceiver,
2✔
130
        }
2✔
131
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc