mirror of
https://github.com/go-i2p/goSam.git
synced 2025-07-05 10:24:44 -04:00
Always send Hello on NewClient
This commit is contained in:
41
client.go
41
client.go
@ -13,10 +13,12 @@ type Client struct {
|
||||
toSam *bufio.Writer
|
||||
}
|
||||
|
||||
// create a new client, connecting to the default host:port at localhost:7656
|
||||
func NewDefaultClient() (*Client, error) {
|
||||
return NewClient("localhost:7656")
|
||||
}
|
||||
|
||||
// create a new client, connecting to a specified port
|
||||
func NewClient(addr string) (*Client, error) {
|
||||
conn, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
@ -27,40 +29,29 @@ func NewClient(addr string) (*Client, error) {
|
||||
fromSam: bufio.NewReader(conn),
|
||||
toSam: bufio.NewWriter(conn),
|
||||
}
|
||||
return c, nil
|
||||
return c, c.hello()
|
||||
}
|
||||
|
||||
func (c *Client) Hello() (err error) {
|
||||
if _, err = c.toSam.WriteString("HELLO VERSION MIN=3.0 MAX=3.0\n"); err != nil {
|
||||
// send the initial handshake command and check that the reply is ok
|
||||
func (c *Client) hello() (err error) {
|
||||
const hello = "HELLO VERSION MIN=3.0 MAX=3.0\n"
|
||||
var r *Reply
|
||||
|
||||
r, err = c.sendCmd(hello)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = c.toSam.Flush(); err != nil {
|
||||
return err
|
||||
if r.Topic != "HELLO" {
|
||||
return fmt.Errorf("Unknown Reply: %+v\n", r)
|
||||
}
|
||||
|
||||
for {
|
||||
line, err := c.fromSam.ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reply, err := parseReply(line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if reply.Topic != "HELLO" {
|
||||
return fmt.Errorf("Unknown Reply: %+v\n", reply)
|
||||
}
|
||||
|
||||
if reply.Pairs["RESULT"] != "OK" {
|
||||
return fmt.Errorf("Handshake did not succeed")
|
||||
}
|
||||
|
||||
break
|
||||
if r.Pairs["RESULT"] != "OK" || r.Pairs["VERSION"] != "3.0\n" {
|
||||
return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (c *Client) Close() error {
|
||||
|
@ -23,11 +23,10 @@ func teardown() {
|
||||
func TestClientHello(t *testing.T) {
|
||||
var err error
|
||||
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
err = client.Hello()
|
||||
client, err = NewDefaultClient()
|
||||
if err != nil {
|
||||
t.Errorf("client.Hello() should not throw an error.\n%s\n", err)
|
||||
}
|
||||
|
||||
client.Close()
|
||||
}
|
||||
|
9
dial.go
9
dial.go
@ -8,7 +8,10 @@ import (
|
||||
|
||||
// implements the net.Dial function to be used as http.Transport
|
||||
func (c *Client) Dial(network, addr string) (net.Conn, error) {
|
||||
addr = addr[:strings.Index(addr, ":")]
|
||||
portIdx := strings.Index(addr, ":")
|
||||
if portIdx >= 0 {
|
||||
addr = addr[:portIdx]
|
||||
}
|
||||
addr, err := c.Lookup(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -26,10 +29,6 @@ func (c *Client) Dial(network, addr string) (net.Conn, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if newC.Hello() != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println("newC Hello OK")
|
||||
|
||||
if newC.StreamConnect(id, addr) != nil {
|
||||
|
@ -11,8 +11,6 @@ func TestClientLookupInvalid(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
client.Hello()
|
||||
|
||||
addr, err := client.Lookup("abci2p")
|
||||
if addr != "" || err == nil {
|
||||
t.Error("client.Lookup() should throw an error.")
|
||||
@ -30,8 +28,6 @@ func ExampleClient_Lookup() {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
client.Hello()
|
||||
|
||||
_, err = client.Lookup("zzz.i2p")
|
||||
if err != nil {
|
||||
fmt.Printf("client.Lookup() should not throw an error.\n%s\n", err)
|
||||
|
Reference in New Issue
Block a user