-
Notifications
You must be signed in to change notification settings - Fork 0
/
sssp.go
70 lines (58 loc) · 1.39 KB
/
sssp.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
package savdi
import (
"bufio"
"errors"
"fmt"
"net"
)
var (
ErrNotRecognised = errors.New("request was not recognised")
ErrIncorrectVersion = errors.New("the SSSP version number was incorrect")
ErrOptionsError = errors.New("there was an error in the OPTIONS list")
ErrTooMuchData = errors.New("SCANDATA was trying to send too much data")
ErrNotPermitted = errors.New("the request is not permitted")
ErrServerClosedConnection = errors.New("server closed the connection")
)
// Client instance
type Client struct {
Version string
Socket net.Conn
}
// GetVersion returns the SSSP version
func (c *Client) GetVersion() string {
return c.Version
}
// Close the connection gracefully
func (c *Client) Close() {
fmt.Fprintln(c.Socket, "BYE")
var resp string
fmt.Fscanln(c.Socket, &resp)
if resp == "BYE" {
c.Socket.Close()
}
return
}
// NewClient creates a new connection to SAV-DI
func NewClient(uri string) (c Client, err error) {
c.Socket, err = net.Dial("tcp", uri)
if err != nil {
return c, err
}
scanner := bufio.NewScanner(c.Socket)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
return c, err
}
line := scanner.Text()
if line == "" {
return c, errors.New("no response")
}
r := scanResponsePattern.FindStringSubmatch(line)
switch r[2] {
case "OK":
c.Version = r[3]
}
break
}
return c, nil
}