NewClient now opens the TCP socket itself

This commit is contained in:
Henry
2014-02-10 19:27:24 +01:00
parent ff10ed51aa
commit a534c1afaa
2 changed files with 15 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"net"
)
type Client struct {
@ -13,11 +14,19 @@ type Client struct {
toSam *bufio.Writer
}
func NewClient(samConn io.ReadWriteCloser) (*Client, error) {
func NewDefaultClient() (*Client, error) {
return NewClient("localhost:7656")
}
func NewClient(addr string) (*Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
c := &Client{
samConn: samConn,
fromSam: bufio.NewReader(samConn),
toSam: bufio.NewWriter(samConn),
samConn: conn,
fromSam: bufio.NewReader(conn),
toSam: bufio.NewWriter(conn),
}
return c, nil
}

View File

@ -1,9 +1,6 @@
package goSam
import (
"net"
"testing"
)
import "testing"
var (
client *Client
@ -13,12 +10,7 @@ func setup() {
var err error
// these tests expect a running SAM brige on this address
conn, err := net.Dial("tcp", "localhost:7656")
if err != nil {
panic(err)
}
client, err = NewClient(conn)
client, err = NewDefaultClient()
if err != nil {
panic(err)
}