veyron/services/proximity: Move a bunch of daemons out of veyron/runtimes.

Needed to modify the proximity code to make the move possible.

Change-Id: I8a098827788cfc100987503572e1fab6aef5b879
diff --git a/runtimes/google/lib/bluetooth/addr.go b/lib/bluetooth/addr.go
similarity index 100%
rename from runtimes/google/lib/bluetooth/addr.go
rename to lib/bluetooth/addr.go
diff --git a/runtimes/google/lib/bluetooth/bluetooth.go b/lib/bluetooth/bluetooth.go
similarity index 95%
rename from runtimes/google/lib/bluetooth/bluetooth.go
rename to lib/bluetooth/bluetooth.go
index 0981c16..647eddd 100644
--- a/runtimes/google/lib/bluetooth/bluetooth.go
+++ b/lib/bluetooth/bluetooth.go
@@ -12,8 +12,7 @@
 	"time"
 	"unsafe"
 
-	"veyron/runtimes/google/lib/proximity"
-	"veyron/runtimes/google/lib/unit"
+	"veyron/lib/unit"
 	"veyron2/vlog"
 )
 
@@ -201,7 +200,7 @@
 	MAC        net.HardwareAddr
 	id         int
 	descriptor C.int
-	leScanChan chan proximity.ScanReading
+	leScanChan chan ScanReading
 }
 
 func (d *Device) String() string {
@@ -243,12 +242,25 @@
 	return nil
 }
 
+// ScanReading holds a single reading of a Low-Energy scan on the Bluetooth device.
+type ScanReading struct {
+	// Name represents a local name of the remote device.  It can also store
+	// arbitrary application-specific data.
+	Name string
+	// MAC is the hardware address of the remote device.
+	MAC net.HardwareAddr
+	// Distance represents the (power-estimated) distance to the remote device.
+	Distance unit.Distance
+	// Time is the time the advertisement packed was received/scanned.
+	Time time.Time
+}
+
 // StartScan initiates a Low-Energy scan on the Bluetooth device.  The scan
 // will proceed over many duration intervals; within each interval, scan will
 // be ON only for a given duration window.  All scan readings encountered
 // during scan-ON periods are pushed onto the returned channel.  If the scan
 // cannot be started, an error is returned.
-func (d *Device) StartScan(scanInterval, scanWindow time.Duration) (<-chan proximity.ScanReading, error) {
+func (d *Device) StartScan(scanInterval, scanWindow time.Duration) (<-chan ScanReading, error) {
 	if scanInterval < scanWindow {
 		return nil, fmt.Errorf("invalid scan settings: scan interval %d must be greater or equal to scan window %d", scanInterval, scanWindow)
 	}
@@ -281,7 +293,7 @@
 	}
 
 	// Start the reading go-routine.
-	d.leScanChan = make(chan proximity.ScanReading, 10)
+	d.leScanChan = make(chan ScanReading, 10)
 	go d.leScanLoop()
 	return d.leScanChan, nil
 }
@@ -315,7 +327,7 @@
 			vlog.Errorf("invalid MAC address: %v", mac)
 			continue
 		}
-		d.leScanChan <- proximity.ScanReading{
+		d.leScanChan <- ScanReading{
 			Name:     name,
 			MAC:      mac,
 			Distance: distanceFromRSSI(int(rssi)),
diff --git a/runtimes/google/lib/bluetooth/bluetooth_test.go b/lib/bluetooth/bluetooth_test.go
similarity index 93%
rename from runtimes/google/lib/bluetooth/bluetooth_test.go
rename to lib/bluetooth/bluetooth_test.go
index 47d7ce8..ae70f11 100644
--- a/runtimes/google/lib/bluetooth/bluetooth_test.go
+++ b/lib/bluetooth/bluetooth_test.go
@@ -6,7 +6,7 @@
 	"math"
 	"testing"
 
-	"veyron/runtimes/google/lib/unit"
+	"veyron/lib/unit"
 )
 
 func TestDistanceFromRSSI(t *testing.T) {
diff --git a/runtimes/google/lib/bluetooth/bt.c b/lib/bluetooth/bt.c
similarity index 100%
rename from runtimes/google/lib/bluetooth/bt.c
rename to lib/bluetooth/bt.c
diff --git a/runtimes/google/lib/bluetooth/bt.h b/lib/bluetooth/bt.h
similarity index 100%
rename from runtimes/google/lib/bluetooth/bt.h
rename to lib/bluetooth/bt.h
diff --git a/runtimes/google/lib/bluetooth/conn.go b/lib/bluetooth/conn.go
similarity index 100%
rename from runtimes/google/lib/bluetooth/conn.go
rename to lib/bluetooth/conn.go
diff --git a/runtimes/google/lib/bluetooth/listener.go b/lib/bluetooth/listener.go
similarity index 100%
rename from runtimes/google/lib/bluetooth/listener.go
rename to lib/bluetooth/listener.go
diff --git a/runtimes/google/lib/unit/distance.go b/lib/unit/distance.go
similarity index 100%
rename from runtimes/google/lib/unit/distance.go
rename to lib/unit/distance.go
diff --git a/runtimes/google/lib/unit/distance_test.go b/lib/unit/distance_test.go
similarity index 100%
rename from runtimes/google/lib/unit/distance_test.go
rename to lib/unit/distance_test.go
diff --git a/runtimes/google/lib/unit/doc.go b/lib/unit/doc.go
similarity index 100%
rename from runtimes/google/lib/unit/doc.go
rename to lib/unit/doc.go
diff --git a/runtimes/google/lib/proximity/scanner.go b/runtimes/google/lib/proximity/scanner.go
deleted file mode 100644
index 8633cfc..0000000
--- a/runtimes/google/lib/proximity/scanner.go
+++ /dev/null
@@ -1,39 +0,0 @@
-package proximity
-
-import (
-	"net"
-	"time"
-
-	"veyron/runtimes/google/lib/unit"
-)
-
-// Scanner denotes a (local) entity that is capable of scanning for nearby
-// devices (e.g., Bluetooth).
-type Scanner interface {
-	// StartScan initiates a scan on the local device.  The scan will
-	// proceed over many duration intervals; within each interval, scan will
-	// be ON only for a given duration window.  All scan readings
-	// encountered during scan-ON periods will be pushed onto the returned
-	// channel.  If the scan cannot be started, an error is returned.
-	StartScan(scanInterval, scanWindow time.Duration) (<-chan ScanReading, error)
-	// StopScan stops any scan in progress on the local device, closing
-	// the channel returned by the previous call to StartScan().
-	// If the device is not scanning, this function will be a noop.
-	StopScan() error
-}
-
-// ScanReading holds a single reading of a neighborhood device's advertisement
-// during a scan.  Typically, there will be many such readings for a particular
-// neighborhood device.
-type ScanReading struct {
-	// Name represents a local name of the remote device.  It can also store
-	// arbitrary application-specific data.
-	Name string
-	// MAC is the hardware address of the remote device.
-	MAC net.HardwareAddr
-	// Distance represents the (estimated) distance to the neighborhood
-	// device.
-	Distance unit.Distance
-	// Time is the time the advertisement packed was received/scanned.
-	Time time.Time
-}
diff --git a/runtimes/google/daemon/gatewayd/main.go b/services/gateway/gatewayd/main.go
similarity index 97%
rename from runtimes/google/daemon/gatewayd/main.go
rename to services/gateway/gatewayd/main.go
index 6a1e938..897b542 100644
--- a/runtimes/google/daemon/gatewayd/main.go
+++ b/services/gateway/gatewayd/main.go
@@ -12,7 +12,7 @@
 
 	"veyron/lib/signals"
 
-	"veyron/runtimes/google/lib/gateway"
+	"veyron/services/gateway/lib"
 	"veyron2/rt"
 	"veyron2/vlog"
 )
diff --git a/runtimes/google/lib/gateway/client_linux.go b/services/gateway/lib/client_linux.go
similarity index 99%
rename from runtimes/google/lib/gateway/client_linux.go
rename to services/gateway/lib/client_linux.go
index 026d172..a70ffd1 100644
--- a/runtimes/google/lib/gateway/client_linux.go
+++ b/services/gateway/lib/client_linux.go
@@ -8,7 +8,7 @@
 	"strings"
 	"time"
 
-	"veyron/runtimes/google/lib/unit"
+	"veyron/lib/unit"
 	"veyron2/rt"
 	"veyron2/services/proximity"
 	"veyron2/vlog"
diff --git a/runtimes/google/lib/gateway/doc_linux.go b/services/gateway/lib/doc_linux.go
similarity index 100%
rename from runtimes/google/lib/gateway/doc_linux.go
rename to services/gateway/lib/doc_linux.go
diff --git a/runtimes/google/lib/gateway/gateway_darwin.go b/services/gateway/lib/gateway_darwin.go
similarity index 100%
rename from runtimes/google/lib/gateway/gateway_darwin.go
rename to services/gateway/lib/gateway_darwin.go
diff --git a/runtimes/google/lib/gateway/gateway_linux.go b/services/gateway/lib/gateway_linux.go
similarity index 97%
rename from runtimes/google/lib/gateway/gateway_linux.go
rename to services/gateway/lib/gateway_linux.go
index b143b84..d7700e2 100644
--- a/runtimes/google/lib/gateway/gateway_linux.go
+++ b/services/gateway/lib/gateway_linux.go
@@ -3,7 +3,7 @@
 import (
 	"fmt"
 
-	"veyron/runtimes/google/lib/bluetooth"
+	"veyron/lib/bluetooth"
 	"veyron2/services/proximity"
 	"veyron2/vlog"
 )
diff --git a/runtimes/google/lib/gateway/server_linux.go b/services/gateway/lib/server_linux.go
similarity index 100%
rename from runtimes/google/lib/gateway/server_linux.go
rename to services/gateway/lib/server_linux.go
diff --git a/runtimes/google/lib/gateway/util_linux.go b/services/gateway/lib/util_linux.go
similarity index 100%
rename from runtimes/google/lib/gateway/util_linux.go
rename to services/gateway/lib/util_linux.go
diff --git a/runtimes/google/lib/proximity/advertiser.go b/services/proximity/lib/advertiser.go
similarity index 100%
rename from runtimes/google/lib/proximity/advertiser.go
rename to services/proximity/lib/advertiser.go
diff --git a/runtimes/google/lib/proximity/doc.go b/services/proximity/lib/doc.go
similarity index 100%
rename from runtimes/google/lib/proximity/doc.go
rename to services/proximity/lib/doc.go
diff --git a/services/proximity/lib/scanner.go b/services/proximity/lib/scanner.go
new file mode 100644
index 0000000..bb2fdd6
--- /dev/null
+++ b/services/proximity/lib/scanner.go
@@ -0,0 +1,93 @@
+package proximity
+
+import (
+	"fmt"
+	"net"
+	"time"
+
+	"veyron/lib/bluetooth"
+	"veyron/lib/unit"
+)
+
+// Scanner denotes a (local) entity that is capable of scanning for nearby
+// devices (e.g., Bluetooth).
+type Scanner interface {
+	// StartScan initiates a scan on the local device.  The scan will
+	// proceed over many duration intervals; within each interval, scan will
+	// be ON only for a given duration window.  All scan readings
+	// encountered during scan-ON periods will be pushed onto the returned
+	// channel.  If the scan cannot be started, an error is returned.
+	StartScan(scanInterval, scanWindow time.Duration) (<-chan ScanReading, error)
+	// StopScan stops any scan in progress on the local device, closing
+	// the channel returned by the previous call to StartScan().
+	// If the device is not scanning, this function will be a noop.
+	StopScan() error
+}
+
+// ScanReading holds a single reading of a neighborhood device's advertisement
+// during a scan.  Typically, there will be many such readings for a particular
+// neighborhood device.
+type ScanReading struct {
+	// Name represents a local name of the remote device.  It can also store
+	// arbitrary application-specific data.
+	Name string
+	// MAC is the hardware address of the remote device.
+	MAC net.HardwareAddr
+	// Distance represents the (estimated) distance to the neighborhood
+	// device.
+	Distance unit.Distance
+	// Time is the time the advertisement packed was received/scanned.
+	Time time.Time
+}
+
+type BluetoothScanner struct {
+	device *bluetooth.Device
+	c      chan ScanReading
+}
+
+func (s *BluetoothScanner) StartScan(scanInterval, scanWindow time.Duration) (<-chan ScanReading, error) {
+	if s.device != nil || s.c != nil {
+		return nil, fmt.Errorf("scan already in progress")
+	}
+	var err error
+	s.device, err = bluetooth.OpenFirstAvailableDevice()
+	if err != nil {
+		return nil, fmt.Errorf("couldn't find an available bluetooth device: %v", err)
+	}
+	bc, err := s.device.StartScan(scanInterval, scanWindow)
+	if err != nil {
+		return nil, fmt.Errorf("couldn't start bluetooth scan: %v", err)
+	}
+	s.c = make(chan ScanReading, 10)
+	go func() {
+		for r := range bc {
+			s.c <- ScanReading{
+				Name:     r.Name,
+				MAC:      r.MAC,
+				Distance: r.Distance,
+				Time:     r.Time,
+			}
+		}
+	}()
+	return s.c, nil
+}
+
+func (s *BluetoothScanner) StopScan() error {
+	if s.device == nil || s.c == nil {
+		if s.device != nil {
+			s.device.StopScan()
+			s.device.Close()
+			s.device = nil
+		} else {
+			close(s.c)
+			s.c = nil
+		}
+		return fmt.Errorf("scan not in progress")
+	}
+	s.device.StopScan()
+	s.device.Close()
+	s.device = nil
+	close(s.c)
+	s.c = nil
+	return nil
+}
diff --git a/runtimes/google/lib/proximity/service.go b/services/proximity/lib/service.go
similarity index 99%
rename from runtimes/google/lib/proximity/service.go
rename to services/proximity/lib/service.go
index fcba302..166b5a3 100644
--- a/runtimes/google/lib/proximity/service.go
+++ b/services/proximity/lib/service.go
@@ -7,7 +7,7 @@
 	"sync"
 	"time"
 
-	"veyron/runtimes/google/lib/unit"
+	"veyron/lib/unit"
 	"veyron2/ipc"
 	prox "veyron2/services/proximity"
 	"veyron2/vlog"
diff --git a/runtimes/google/lib/proximity/service_test.go b/services/proximity/lib/service_test.go
similarity index 98%
rename from runtimes/google/lib/proximity/service_test.go
rename to services/proximity/lib/service_test.go
index ee6878b..9d086c5 100644
--- a/runtimes/google/lib/proximity/service_test.go
+++ b/services/proximity/lib/service_test.go
@@ -5,7 +5,7 @@
 	"testing"
 	"time"
 
-	"veyron/runtimes/google/lib/unit"
+	"veyron/lib/unit"
 	"veyron2/services/proximity"
 )
 
diff --git a/runtimes/google/lib/proximity/sorting.go b/services/proximity/lib/sorting.go
similarity index 100%
rename from runtimes/google/lib/proximity/sorting.go
rename to services/proximity/lib/sorting.go
diff --git a/runtimes/google/lib/proximity/util_test.go b/services/proximity/lib/util_test.go
similarity index 100%
rename from runtimes/google/lib/proximity/util_test.go
rename to services/proximity/lib/util_test.go
diff --git a/runtimes/google/daemon/proximityd/main.go b/services/proximity/proximityd/main.go
similarity index 86%
rename from runtimes/google/daemon/proximityd/main.go
rename to services/proximity/proximityd/main.go
index 308b224..245eb4d 100644
--- a/runtimes/google/daemon/proximityd/main.go
+++ b/services/proximity/proximityd/main.go
@@ -11,9 +11,9 @@
 
 	"veyron/lib/signals"
 
-	"veyron/runtimes/google/lib/bluetooth"
-	"veyron/runtimes/google/lib/proximity"
+	"veyron/lib/bluetooth"
 	vflag "veyron/security/flag"
+	"veyron/services/proximity/lib"
 	"veyron2/ipc"
 	"veyron2/rt"
 	prox "veyron2/services/proximity"
@@ -49,12 +49,7 @@
 		vlog.Fatal("couldn't find an available bluetooth device")
 	}
 	defer advertiser.Close()
-	scanner, err := bluetooth.OpenFirstAvailableDevice()
-	if err != nil {
-		vlog.Fatal("couldn't find an available bluetooth device")
-	}
-	defer scanner.Close()
-	p, err := proximity.New(advertiser, scanner, 1*time.Second, 500*time.Millisecond)
+	p, err := proximity.New(advertiser, &proximity.BluetoothScanner{}, 1*time.Second, 500*time.Millisecond)
 	if err != nil {
 		vlog.Fatal("couldn't create proximity service:", err)
 	}
diff --git a/runtimes/google/daemon/proxyd/main.go b/services/proxy/proxyd/main.go
similarity index 100%
rename from runtimes/google/daemon/proxyd/main.go
rename to services/proxy/proxyd/main.go