Skip to content

Commit

Permalink
Merge pull request #47 from pck/dial_tcp_transport
Browse files Browse the repository at this point in the history
Add support for Dial("tcp:host=..,port=..")
  • Loading branch information
philips committed Apr 7, 2016
2 parents 6fd6e8a + 645fb65 commit d40f887
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
17 changes: 6 additions & 11 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,16 +621,11 @@ func dereferenceAll(vs []interface{}) []interface{} {

// getKey gets a key from a the list of keys. Returns "" on error / not found...
func getKey(s, key string) string {
i := strings.Index(s, key)
if i == -1 {
return ""
}
if i+len(key)+1 >= len(s) || s[i+len(key)] != '=' {
return ""
}
j := strings.Index(s, ",")
if j == -1 {
j = len(s)
for _, keyEqualsValue := range strings.Split(s, ",") {
keyValue := strings.SplitN(keyEqualsValue, "=", 2)
if len(keyValue) == 2 && keyValue[0] == key {
return keyValue[1]
}
}
return s[i+len(key)+1 : j]
return ""
}
13 changes: 13 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ func benchmarkServeAsync(b *testing.B, srv, cli *Conn) {
}
<-done
}

func TestGetKey(t *testing.T) {
keys := "host=1.2.3.4,port=5678,family=ipv4"
if host := getKey(keys, "host"); host != "1.2.3.4" {
t.Error(`Expected "1.2.3.4", got`, host)
}
if port := getKey(keys, "port"); port != "5678" {
t.Error(`Expected "5678", got`, port)
}
if family := getKey(keys, "family"); family != "ipv4" {
t.Error(`Expected "ipv4", got`, family)
}
}
43 changes: 43 additions & 0 deletions transport_tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//+build !windows

package dbus

import (
"errors"
"net"
)

func init() {
transports["tcp"] = newTcpTransport
}

func tcpFamily(keys string) (string, error) {
switch getKey(keys, "family") {
case "":
return "tcp", nil
case "ipv4":
return "tcp4", nil
case "ipv6":
return "tcp6", nil
default:
return "", errors.New("dbus: invalid tcp family (must be ipv4 or ipv6)")
}
}

func newTcpTransport(keys string) (transport, error) {
host := getKey(keys, "host")
port := getKey(keys, "port")
if host == "" || port == "" {
return nil, errors.New("dbus: unsupported address (must set host and port)")
}

protocol, err := tcpFamily(keys)
if err != nil {
return nil, err
}
socket, err := net.Dial(protocol, net.JoinHostPort(host, port))
if err != nil {
return nil, err
}
return NewConn(socket)
}
26 changes: 26 additions & 0 deletions transport_tcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dbus

import (
"fmt"
"net"
"testing"
)

func TestTcpConnection(t *testing.T) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal("Failed to create listener")
}
host, port, err := net.SplitHostPort(listener.Addr().String())
if err != nil {
t.Fatal("Failed to parse host/port")
}

conn, err := Dial(fmt.Sprintf("tcp:host=%s,port=%s", host, port))
if err != nil {
t.Error("Expected no error, got", err)
}
if conn == nil {
t.Error("Expected connection, got nil")
}
}

0 comments on commit d40f887

Please sign in to comment.