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" "bufio"
"fmt" "fmt"
"io" "io"
"net"
) )
type Client struct { type Client struct {
@ -13,11 +14,19 @@ type Client struct {
toSam *bufio.Writer 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{ c := &Client{
samConn: samConn, samConn: conn,
fromSam: bufio.NewReader(samConn), fromSam: bufio.NewReader(conn),
toSam: bufio.NewWriter(samConn), toSam: bufio.NewWriter(conn),
} }
return c, nil return c, nil
} }

View File

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