func main() { flag.Parse() client := adb.NewHostClient(adb.ClientConfig{}) fmt.Println("Starting server…") adb.StartServer() serverVersion, err := client.GetServerVersion() if err != nil { log.Fatal(err) } fmt.Println("Server version:", serverVersion) devices, err := client.ListDevices() if err != nil { log.Fatal(err) } fmt.Println("Devices:") for _, device := range devices { fmt.Printf("\t%+v\n", *device) } PrintDeviceInfoAndError(adb.AnyDevice()) PrintDeviceInfoAndError(adb.AnyLocalDevice()) PrintDeviceInfoAndError(adb.AnyUsbDevice()) serials, err := client.ListDeviceSerials() if err != nil { log.Fatal(err) } for _, serial := range serials { PrintDeviceInfoAndError(adb.DeviceWithSerial(serial)) } fmt.Println() fmt.Println("Watching for device state changes.") watcher := adb.NewDeviceWatcher(adb.ClientConfig{}) for event := range watcher.C() { fmt.Printf("\t[%s]%+v\n", time.Now(), event) } if watcher.Err() != nil { printErr(watcher.Err()) } //fmt.Println("Killing server…") //client.KillServer() }
func main() { cli.Initialize(appName, &config.BaseConfig) eventLog := cli.NewEventLog(appName, "") defer eventLog.Finish() config.InitializePaths() eventLog.Infof("using mount root %s", config.MountRoot) deviceWatcher := goadb.NewDeviceWatcher(config.ClientConfig()) defer deviceWatcher.Shutdown() signals := make(chan os.Signal) signal.Notify(signals, os.Kill, os.Interrupt) processes := cli.NewProcessTracker() defer processes.Shutdown() cli.Log.Info("automounter ready.") defer cli.Log.Info("exiting.") for { select { case event := <-deviceWatcher.C(): if event.CameOnline() { eventLog.Debugf("device connected: %s", event.Serial) processes.Go(event.Serial, mountDevice) } else if event.WentOffline() { eventLog.Debugf("device disconnected: %s", event.Serial) } else { eventLog.Debugf("unknown device event: %+v", event) } case signal := <-signals: eventLog.Debugf("got signal %v", signal) if signal == os.Kill || signal == os.Interrupt { eventLog.Infof("shutting down all mount processes…") processes.Shutdown() eventLog.Infof("all processes shutdown.") return } } } }
func watchForDeviceDisconnected(clientConfig goadb.ClientConfig, serial string) { watcher := goadb.NewDeviceWatcher(clientConfig) defer watcher.Shutdown() for { select { case event, ok := <-watcher.C(): if !ok { // Channel closed. break } if event.Serial == serial && event.WentOffline() { cli.Log.Infoln("device disconnected:", serial) handleDeviceDisconnected() } } } if err := watcher.Err(); err != nil { cli.Log.Warn("DeviceWatcher disconnected with error:", err) } }