veyron/profiles: Create profile init functions.

Change-Id: I06e5308de6edca7e11bb752fefeb4bb9d670c541
diff --git a/profiles/chrome/chromeinit.go b/profiles/chrome/chromeinit.go
new file mode 100644
index 0000000..b14e464
--- /dev/null
+++ b/profiles/chrome/chromeinit.go
@@ -0,0 +1,25 @@
+// Package chrome implements a profile for use within Chrome, in particular
+// for use by Chrome extensions.
+package chrome
+
+import (
+	"v.io/core/veyron2"
+	"v.io/core/veyron2/context"
+
+	_ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
+	grt "v.io/core/veyron/runtimes/google/rt"
+)
+
+func init() {
+	veyron2.RegisterProfileInit(Init)
+}
+
+func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
+	runtime := &grt.RuntimeX{}
+	ctx, shutdown, err := runtime.Init(ctx, nil)
+	if err != nil {
+		return nil, nil, shutdown, err
+	}
+	veyron2.GetLogger(ctx).VI(1).Infof("Initializing chrome profile.")
+	return runtime, ctx, shutdown, nil
+}
diff --git a/profiles/gce/init.go b/profiles/gce/init.go
index 4e72975..d25d7f6 100644
--- a/profiles/gce/init.go
+++ b/profiles/gce/init.go
@@ -5,7 +5,6 @@
 package gce
 
 import (
-	"flag"
 	"fmt"
 	"net"
 
@@ -15,7 +14,6 @@
 	"v.io/core/veyron2/rt"
 
 	"v.io/core/veyron/lib/appcycle"
-	"v.io/core/veyron/lib/flags"
 	"v.io/core/veyron/lib/netstate"
 	"v.io/core/veyron/profiles/internal/gce"
 	"v.io/core/veyron/profiles/internal/platform"
@@ -26,15 +24,12 @@
 )
 
 var (
-	commonFlags *flags.Flags
-
 	// ListenSpec is an initialized instance of ipc.ListenSpec that can
 	// be used with ipc.Listen.
 	ListenSpec ipc.ListenSpec
 )
 
 func init() {
-	commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
 	rt.RegisterProfile(&profile{})
 }
 
diff --git a/profiles/gce/initx.go b/profiles/gce/initx.go
new file mode 100644
index 0000000..839e270
--- /dev/null
+++ b/profiles/gce/initx.go
@@ -0,0 +1,67 @@
+package gce
+
+import (
+	"flag"
+	"fmt"
+	"net"
+
+	"v.io/core/veyron2"
+	"v.io/core/veyron2/context"
+	"v.io/core/veyron2/ipc"
+
+	"v.io/core/veyron/lib/appcycle"
+	"v.io/core/veyron/lib/flags"
+	"v.io/core/veyron/lib/netstate"
+	"v.io/core/veyron/profiles/internal/gce"
+	_ "v.io/core/veyron/runtimes/google/ipc/protocols/tcp"
+	_ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
+	_ "v.io/core/veyron/runtimes/google/ipc/protocols/wsh"
+	grt "v.io/core/veyron/runtimes/google/rt"
+)
+
+var (
+	commonFlags *flags.Flags
+)
+
+func init() {
+	commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
+	veyron2.RegisterProfileInit(Init)
+}
+
+func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
+	if !gce.RunningOnGCE() {
+		return nil, nil, nil, fmt.Errorf("GCE profile used on a non-GCE system")
+	}
+
+	runtime := &grt.RuntimeX{}
+	ctx, shutdown, err := runtime.Init(ctx, nil)
+	if err != nil {
+		return nil, nil, shutdown, err
+	}
+	veyron2.GetLogger(ctx).VI(1).Infof("Initializing GCE profile.")
+
+	lf := commonFlags.ListenFlags()
+	listenSpec := ipc.ListenSpec{
+		Addrs: ipc.ListenAddrs(lf.Addrs),
+		Proxy: lf.ListenProxy,
+	}
+
+	if ip, err := gce.ExternalIPAddress(); err != nil {
+		return nil, nil, shutdown, err
+	} else {
+		listenSpec.AddressChooser = func(network string, addrs []ipc.Address) ([]ipc.Address, error) {
+			return []ipc.Address{&netstate.AddrIfc{&net.IPAddr{IP: ip}, "gce-nat", nil}}, nil
+		}
+	}
+	ctx = runtime.SetListenSpec(ctx, listenSpec)
+
+	ac := appcycle.New()
+	ctx = runtime.SetAppCycle(ctx, ac)
+
+	profileShutdown := func() {
+		shutdown()
+		ac.Shutdown()
+	}
+
+	return runtime, ctx, profileShutdown, nil
+}
diff --git a/profiles/genericinit.go b/profiles/genericinit.go
index 35c955a..d82a726 100644
--- a/profiles/genericinit.go
+++ b/profiles/genericinit.go
@@ -21,6 +21,7 @@
 	if err != nil {
 		return nil, nil, nil, err
 	}
+	veyron2.GetLogger(ctx).VI(1).Infof("Initializing generic profile.")
 
 	ac := appcycle.New()
 	ctx = runtime.SetAppCycle(ctx, ac)
diff --git a/profiles/roaming/roaming.go b/profiles/roaming/roaming.go
index a6661a9..d4c1f82 100644
--- a/profiles/roaming/roaming.go
+++ b/profiles/roaming/roaming.go
@@ -34,12 +34,7 @@
 	sflag "v.io/core/veyron/security/flag"
 )
 
-const (
-	SettingsStreamName = "roaming"
-)
-
 var (
-	commonFlags *flags.Flags
 	// ListenSpec is an initialized instance of ipc.ListenSpec that can
 	// be used with ipc.Listen.
 	ListenSpec ipc.ListenSpec
diff --git a/profiles/roaming/roaminginit.go b/profiles/roaming/roaminginit.go
new file mode 100644
index 0000000..98e74ee
--- /dev/null
+++ b/profiles/roaming/roaminginit.go
@@ -0,0 +1,161 @@
+package roaming
+
+import (
+	"flag"
+
+	"v.io/core/veyron2"
+	"v.io/core/veyron2/config"
+	"v.io/core/veyron2/context"
+	"v.io/core/veyron2/ipc"
+
+	"v.io/core/veyron/lib/appcycle"
+	"v.io/core/veyron/lib/flags"
+	"v.io/core/veyron/lib/netconfig"
+	"v.io/core/veyron/lib/netstate"
+	"v.io/core/veyron/profiles/internal"
+	_ "v.io/core/veyron/runtimes/google/ipc/protocols/tcp"
+	_ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
+	_ "v.io/core/veyron/runtimes/google/ipc/protocols/wsh"
+	grt "v.io/core/veyron/runtimes/google/rt"
+	"v.io/core/veyron/services/mgmt/debug"
+
+	// TODO(cnicolaou,ashankar): move this into flags.
+	sflag "v.io/core/veyron/security/flag"
+)
+
+const (
+	SettingsStreamName = "roaming"
+)
+
+var (
+	commonFlags *flags.Flags
+)
+
+func init() {
+	commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
+	veyron2.RegisterProfileInit(Init)
+}
+
+func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
+	runtime := &grt.RuntimeX{}
+	ctx, shutdown, err := runtime.Init(ctx, nil)
+	if err != nil {
+		return nil, nil, shutdown, err
+	}
+	log := runtime.GetLogger(ctx)
+
+	ctx = runtime.SetReservedNameDispatcher(ctx, debug.NewDispatcher(log.LogDir(), sflag.NewAuthorizerOrDie()))
+
+	lf := commonFlags.ListenFlags()
+	listenSpec := ipc.ListenSpec{
+		Addrs: ipc.ListenAddrs(lf.Addrs),
+		Proxy: lf.ListenProxy,
+	}
+
+	// Our address is private, so we test for running on GCE and for its
+	// 1:1 NAT configuration.
+	if !internal.HasPublicIP(log) {
+		if addr := internal.GCEPublicAddress(log); addr != nil {
+			listenSpec.AddressChooser = func(string, []ipc.Address) ([]ipc.Address, error) {
+				return []ipc.Address{&netstate.AddrIfc{addr, "nat", nil}}, nil
+			}
+			return runtime, ctx, shutdown, nil
+		}
+	}
+
+	ac := appcycle.New()
+	ctx = runtime.SetAppCycle(ctx, ac)
+
+	publisher := veyron2.GetPublisher(ctx)
+
+	// Create stream in Init function to avoid a race between any
+	// goroutines started here and consumers started after Init returns.
+	ch := make(chan config.Setting)
+	stop, err := publisher.CreateStream(SettingsStreamName, SettingsStreamName, ch)
+	if err != nil {
+		log.Errorf("failed to create publisher: %s", err)
+		ac.Shutdown()
+		return nil, nil, shutdown, err
+	}
+
+	prev, err := netstate.GetAccessibleIPs()
+	if err != nil {
+		log.VI(2).Infof("failed to determine network state")
+		ac.Shutdown()
+		return nil, nil, shutdown, err
+	}
+
+	// Start the dhcp watcher.
+	watcher, err := netconfig.NewNetConfigWatcher()
+	if err != nil {
+		log.VI(2).Infof("Failed to get new config watcher: %s", err)
+		ac.Shutdown()
+		return nil, nil, shutdown, err
+	}
+
+	cleanupCh := make(chan struct{})
+	watcherCh := make(chan struct{})
+
+	listenSpec.StreamPublisher = veyron2.GetPublisher(ctx)
+	listenSpec.StreamName = SettingsStreamName
+	listenSpec.AddressChooser = internal.IPAddressChooser
+
+	ctx = runtime.SetListenSpec(ctx, listenSpec)
+
+	go monitorNetworkSettingsX(ctx, watcher, prev, stop, cleanupCh, watcherCh, ch, ListenSpec)
+	profileShutdown := func() {
+		close(cleanupCh)
+		shutdown()
+		ac.Shutdown()
+		<-watcherCh
+	}
+	return runtime, ctx, profileShutdown, nil
+}
+
+// monitorNetworkSettings will monitor network configuration changes and
+// publish subsequent Settings to reflect any changes detected.
+func monitorNetworkSettingsX(ctx *context.T, watcher netconfig.NetConfigWatcher, prev netstate.AddrList, pubStop, cleanup <-chan struct{},
+	watcherLoop chan<- struct{}, ch chan<- config.Setting, listenSpec ipc.ListenSpec) {
+	defer close(ch)
+
+	log := veyron2.GetLogger(ctx)
+
+	// TODO(cnicolaou): add support for listening on multiple network addresses.
+
+done:
+	for {
+		select {
+		case <-watcher.Channel():
+			cur, err := netstate.GetAccessibleIPs()
+			if err != nil {
+				log.Errorf("failed to read network state: %s", err)
+				continue
+			}
+			removed := netstate.FindRemoved(prev, cur)
+			added := netstate.FindAdded(prev, cur)
+			log.VI(2).Infof("Previous: %d: %s", len(prev), prev)
+			log.VI(2).Infof("Current : %d: %s", len(cur), cur)
+			log.VI(2).Infof("Added   : %d: %s", len(added), added)
+			log.VI(2).Infof("Removed : %d: %s", len(removed), removed)
+			if len(removed) == 0 && len(added) == 0 {
+				log.VI(2).Infof("Network event that lead to no address changes since our last 'baseline'")
+				continue
+			}
+			if len(removed) > 0 {
+				log.VI(2).Infof("Sending removed: %s", removed)
+				ch <- ipc.NewRmAddrsSetting(removed)
+			}
+			// We will always send the best currently available address
+			if chosen, err := listenSpec.AddressChooser(listenSpec.Addrs[0].Protocol, cur); err == nil && chosen != nil {
+				ch <- ipc.NewAddAddrsSetting(chosen)
+			}
+			prev = cur
+		case <-cleanup:
+			break done
+		case <-pubStop:
+			goto done
+		}
+	}
+	watcher.Stop()
+	close(watcherLoop)
+}
diff --git a/runtimes/google/rt/runtimex.go b/runtimes/google/rt/runtimex.go
index 1dce0b1..7c977b1 100644
--- a/runtimes/google/rt/runtimex.go
+++ b/runtimes/google/rt/runtimex.go
@@ -209,7 +209,7 @@
 		return nil, nil, err
 	}
 
-	// The clinet we attach here is incomplete (has a nil principal) and only works
+	// The client we attach here is incomplete (has a nil principal) and only works
 	// because the agent uses anonymous unix sockets and VCSecurityNone.
 	// After security is initialized we will attach a real client.
 	ctx, _, err = r.SetNewClient(ctx)
@@ -235,6 +235,9 @@
 		return nil, nil, err
 	}
 
+	// Initialize the config publisher.
+	ctx = context.WithValue(ctx, publisherKey, config.NewPublisher())
+
 	// TODO(suharshs,mattr): Go through the rt.Cleanup function and make sure everything
 	// gets cleaned up.