forked from egirna/icap-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdriver.go
More file actions
99 lines (76 loc) · 1.85 KB
/
Copy pathdriver.go
File metadata and controls
99 lines (76 loc) · 1.85 KB
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
package icapclient
import (
"bufio"
"context"
"errors"
"fmt"
"strings"
"time"
)
// Driver os the one responsible for driving the transport layer operations
type Driver struct {
Host string
Port int
DialerTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
tcp *transport
}
// NewDriver is the factory function for Driver
func NewDriver(host string, port int) *Driver {
return &Driver{
Host: host,
Port: port,
}
}
// Connect fires up a tcp socket connection with the icap server
func (d *Driver) Connect() error {
d.tcp = &transport{
network: "tcp",
addr: fmt.Sprintf("%s:%d", d.Host, d.Port),
timeout: d.DialerTimeout,
readTimeout: d.ReadTimeout,
writeTimeout: d.WriteTimeout,
}
return d.tcp.dial()
}
// ConnectWithContext connects to the server satisfying the context
func (d *Driver) ConnectWithContext(ctx context.Context) error {
d.tcp = &transport{
network: "tcp",
addr: fmt.Sprintf("%s:%d", d.Host, d.Port),
timeout: d.DialerTimeout,
readTimeout: d.ReadTimeout,
writeTimeout: d.WriteTimeout,
}
return d.tcp.dialWithContext(ctx)
}
// Close closes the socket connection
func (d *Driver) Close() error {
if d.tcp == nil {
return errors.New(ErrConnectionNotOpen)
}
return d.tcp.close()
}
// Send sends a request to the icap server
func (d *Driver) Send(data []byte) error {
_, err := d.tcp.write(data)
if err != nil {
return err
}
return nil
}
// Receive returns the respone from the tcp socket connection
func (d *Driver) Receive() (*Response, error) {
msg, err := d.tcp.read()
if err != nil {
return nil, err
}
resp, err := ReadResponse(bufio.NewReader(strings.NewReader(msg)))
if err != nil {
return nil, err
}
logDebug("The final *ic.Response from tcp messages...")
dumpDebug(resp)
return resp, nil
}