Removed nilSafeDialer and moved AdbPort into goadb package.

This commit is contained in:
Zach Klippenstein 2015-05-03 06:11:09 -07:00
parent a6b8a6fa03
commit 925d93caef
6 changed files with 38 additions and 15 deletions

View file

@ -10,12 +10,16 @@ import (
adb "github.com/zach-klippenstein/goadb" adb "github.com/zach-klippenstein/goadb"
) )
var port = flag.Int("p", goadb.AdbPort, "") var port = flag.Int("p", adb.AdbPort, "")
func main() { func main() {
flag.Parse() flag.Parse()
client := adb.NewHostClientPort(*port) client, err := adb.NewHostClientPort(*port)
if err != nil {
log.Fatal(err)
}
fmt.Println("Starting server…") fmt.Println("Starting server…")
client.StartServer() client.StartServer()
@ -100,6 +104,14 @@ func PrintDeviceInfo(device *adb.DeviceClient) error {
} }
} }
fmt.Println("\tnon-existent file:")
stat, err = device.Stat("/supercalifragilisticexpialidocious")
if err != nil {
fmt.Println("\terror:", err)
} else {
fmt.Printf("\tstat: %+v\n", stat)
}
fmt.Print("\tload avg: ") fmt.Print("\tload avg: ")
loadavgReader, err := device.OpenRead("/proc/loadavg") loadavgReader, err := device.OpenRead("/proc/loadavg")
if err != nil { if err != nil {

View file

@ -10,7 +10,7 @@ import (
// DeviceClient communicates with a specific Android device. // DeviceClient communicates with a specific Android device.
type DeviceClient struct { type DeviceClient struct {
dialer nilSafeDialer dialer wire.Dialer
descriptor *DeviceDescriptor descriptor *DeviceDescriptor
} }

View file

@ -12,7 +12,7 @@ func TestGetAttribute(t *testing.T) {
Status: wire.StatusSuccess, Status: wire.StatusSuccess,
Messages: []string{"value"}, Messages: []string{"value"},
} }
client := &DeviceClient{nilSafeDialer{s}, deviceWithSerial("serial")} client := &DeviceClient{s, deviceWithSerial("serial")}
v, err := client.getAttribute("attr") v, err := client.getAttribute("attr")
assert.Equal(t, "host-serial:serial:attr", s.Requests[0]) assert.Equal(t, "host-serial:serial:attr", s.Requests[0])
@ -25,7 +25,7 @@ func TestRunCommandNoArgs(t *testing.T) {
Status: wire.StatusSuccess, Status: wire.StatusSuccess,
Messages: []string{"output"}, Messages: []string{"output"},
} }
client := &DeviceClient{nilSafeDialer{s}, anyDevice()} client := &DeviceClient{s, anyDevice()}
v, err := client.RunCommand("cmd") v, err := client.RunCommand("cmd")
assert.Equal(t, "host:transport-any", s.Requests[0]) assert.Equal(t, "host:transport-any", s.Requests[0])

View file

@ -2,6 +2,7 @@
package goadb package goadb
import ( import (
"errors"
"os/exec" "os/exec"
"strconv" "strconv"
@ -26,19 +27,22 @@ See list of services at https://android.googlesource.com/platform/system/core/+/
*/ */
// TODO(z): Finish implementing host services. // TODO(z): Finish implementing host services.
type HostClient struct { type HostClient struct {
dialer nilSafeDialer dialer wire.Dialer
} }
func NewHostClient() *HostClient { func NewHostClient() (*HostClient, error) {
return NewHostClientDialer(nil) return NewHostClientPort(AdbPort)
} }
func NewHostClientPort(port int) *HostClient { func NewHostClientPort(port int) (*HostClient, error) {
return NewHostClientDialer(wire.NewDialer("", port)) return NewHostClientDialer(wire.NewDialer("localhost", port))
} }
func NewHostClientDialer(d wire.Dialer) *HostClient { func NewHostClientDialer(d wire.Dialer) (*HostClient, error) {
return &HostClient{nilSafeDialer{d}} if d == nil {
return nil, errors.New("dialer cannot be nil.")
}
return &HostClient{d}, nil
} }
// GetServerVersion asks the ADB server for its internal version number. // GetServerVersion asks the ADB server for its internal version number.

View file

@ -14,7 +14,8 @@ func TestGetServerVersion(t *testing.T) {
Status: wire.StatusSuccess, Status: wire.StatusSuccess,
Messages: []string{"000a"}, Messages: []string{"000a"},
} }
client := NewHostClientDialer(s) client, err := NewHostClientDialer(s)
assert.NoError(t, err)
v, err := client.GetServerVersion() v, err := client.GetServerVersion()
assert.Equal(t, "host:version", s.Requests[0]) assert.Equal(t, "host:version", s.Requests[0])

View file

@ -1,6 +1,7 @@
package wire package wire
import ( import (
"errors"
"fmt" "fmt"
"net" "net"
"runtime" "runtime"
@ -27,10 +28,15 @@ func NewDialer(host string, port int) Dialer {
func (d *netDialer) Dial() (*Conn, error) { func (d *netDialer) Dial() (*Conn, error) {
host := d.Host host := d.Host
if host == "" { if host == "" {
host = "localhost" return nil, errors.New("Must specify adb hostname (cannot be empty).")
} }
netConn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, d.Port)) port := d.Port
if port == 0 {
return nil, errors.New("Must specify port (cannot be 0).")
}
netConn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil { if err != nil {
return nil, err return nil, err
} }