veyron/runtimes/google/rt: RuntimeX.Init should be a function instead of a method.

This makes much more sense as a constructor.

Change-Id: I6da0bd362c7600febc53ef74d42d8cbc06e945e1
diff --git a/profiles/chrome/chromeinit.go b/profiles/chrome/chromeinit.go
index b14e464..e7c37cb 100644
--- a/profiles/chrome/chromeinit.go
+++ b/profiles/chrome/chromeinit.go
@@ -15,8 +15,7 @@
 }
 
 func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
-	runtime := &grt.RuntimeX{}
-	ctx, shutdown, err := runtime.Init(ctx, nil)
+	runtime, ctx, shutdown, err := grt.Init(ctx, nil)
 	if err != nil {
 		return nil, nil, shutdown, err
 	}
diff --git a/profiles/gce/initx.go b/profiles/gce/initx.go
index 2353eae..60f1e69 100644
--- a/profiles/gce/initx.go
+++ b/profiles/gce/initx.go
@@ -37,8 +37,7 @@
 		return nil, nil, nil, fmt.Errorf("GCE profile used on a non-GCE system")
 	}
 
-	runtime := &grt.RuntimeX{}
-	ctx, shutdown, err := runtime.Init(ctx, nil)
+	runtime, ctx, shutdown, err := grt.Init(ctx, nil)
 	if err != nil {
 		return nil, nil, shutdown, err
 	}
diff --git a/profiles/genericinit.go b/profiles/genericinit.go
index d82a726..73224f6 100644
--- a/profiles/genericinit.go
+++ b/profiles/genericinit.go
@@ -16,8 +16,7 @@
 }
 
 func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
-	runtime := &grt.RuntimeX{}
-	ctx, shutdown, err := runtime.Init(ctx, nil)
+	runtime, ctx, shutdown, err := grt.Init(ctx, nil)
 	if err != nil {
 		return nil, nil, nil, err
 	}
diff --git a/profiles/roaming/roaminginit.go b/profiles/roaming/roaminginit.go
index 98e74ee..fcefbdd 100644
--- a/profiles/roaming/roaminginit.go
+++ b/profiles/roaming/roaminginit.go
@@ -37,8 +37,7 @@
 }
 
 func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
-	runtime := &grt.RuntimeX{}
-	ctx, shutdown, err := runtime.Init(ctx, nil)
+	runtime, ctx, shutdown, err := grt.Init(ctx, nil)
 	if err != nil {
 		return nil, nil, shutdown, err
 	}
diff --git a/profiles/static/staticinit.go b/profiles/static/staticinit.go
index c91777c..4def643 100644
--- a/profiles/static/staticinit.go
+++ b/profiles/static/staticinit.go
@@ -31,10 +31,7 @@
 }
 
 func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
-	var err error
-	var shutdown veyron2.Shutdown
-	runtime := &grt.RuntimeX{}
-	ctx, shutdown, err = runtime.Init(ctx, nil)
+	runtime, ctx, shutdown, err := grt.Init(ctx, nil)
 	if err != nil {
 		return nil, nil, nil, err
 	}
diff --git a/runtimes/google/rt/runtimex.go b/runtimes/google/rt/runtimex.go
index bc60c1d..0ff2d36 100644
--- a/runtimes/google/rt/runtimex.go
+++ b/runtimes/google/rt/runtimex.go
@@ -94,6 +94,112 @@
 	wait     *sync.Cond
 }
 
+func Init(ctx *context.T, protocols []string) (*RuntimeX, *context.T, veyron2.Shutdown, error) {
+	r := &RuntimeX{}
+	r.wait = sync.NewCond(&r.mu)
+
+	handle, err := exec.GetChildHandle()
+	switch err {
+	case exec.ErrNoVersion:
+		// The process has not been started through the veyron exec
+		// library. No further action is needed.
+	case nil:
+		// The process has been started through the veyron exec
+		// library.
+	default:
+		return nil, nil, nil, err
+	}
+
+	// Parse runtime flags.
+	flagsOnce.Do(func() {
+		var config map[string]string
+		if handle != nil {
+			config = handle.Config.Dump()
+		}
+		runtimeFlags.Parse(os.Args[1:], config)
+	})
+	flags := runtimeFlags.RuntimeFlags()
+
+	r.initLogging(ctx)
+	ctx = context.WithValue(ctx, loggerKey, vlog.Log)
+
+	// Set the preferred protocols.
+	if len(protocols) > 0 {
+		ctx = context.WithValue(ctx, protocolsKey, protocols)
+	}
+
+	// Setup i18n.
+	ctx = i18n.ContextWithLangID(ctx, i18n.LangIDFromEnv())
+	if len(flags.I18nCatalogue) != 0 {
+		cat := i18n.Cat()
+		for _, filename := range strings.Split(flags.I18nCatalogue, ",") {
+			err := cat.MergeFromFile(filename)
+			if err != nil {
+				fmt.Fprintf(os.Stderr, "%s: i18n: error reading i18n catalogue file %q: %s\n", os.Args[0], filename, err)
+			}
+		}
+	}
+
+	// Setup the program name.
+	ctx = verror2.ContextWithComponentName(ctx, filepath.Base(os.Args[0]))
+
+	// Setup the initial trace.
+	ctx, err = ivtrace.Init(ctx, flags.Vtrace)
+	if err != nil {
+		return nil, nil, nil, err
+	}
+	ctx, _ = vtrace.SetNewTrace(ctx)
+
+	// Enable signal handling.
+	r.initSignalHandling(ctx)
+
+	// Set the initial namespace.
+	ctx, _, err = r.setNewNamespace(ctx, flags.NamespaceRoots...)
+	if err != nil {
+		return nil, nil, nil, err
+	}
+
+	// Set the initial stream manager.
+	ctx, _, err = r.setNewStreamManager(ctx)
+	if err != nil {
+		return nil, nil, nil, err
+	}
+
+	// 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)
+	if err != nil {
+		return nil, nil, nil, err
+	}
+
+	// Initialize security.
+	principal, err := initSecurity(ctx, handle, flags.Credentials)
+	if err != nil {
+		return nil, nil, nil, err
+	}
+	ctx = context.WithValue(ctx, principalKey, principal)
+
+	// Set up secure client.
+	ctx, _, err = r.SetNewClient(ctx)
+	if err != nil {
+		return nil, nil, nil, err
+	}
+
+	// Initialize management.
+	if err := initMgmt(ctx, r.GetAppCycle(ctx), handle); err != nil {
+		return nil, 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.
+
+	return r, ctx, r.cancel, nil
+}
+
 func (r *RuntimeX) addChild(ctx *context.T, stop func()) error {
 	// TODO(mattr): Remove this hack once the transition is over.
 	if r == nil {
@@ -140,110 +246,6 @@
 	vlog.FlushLog()
 }
 
-func (r *RuntimeX) Init(ctx *context.T, protocols []string) (*context.T, veyron2.Shutdown, error) {
-	r.wait = sync.NewCond(&r.mu)
-	handle, err := exec.GetChildHandle()
-	switch err {
-	case exec.ErrNoVersion:
-		// The process has not been started through the veyron exec
-		// library. No further action is needed.
-	case nil:
-		// The process has been started through the veyron exec
-		// library.
-	default:
-		return nil, nil, err
-	}
-
-	// Parse runtime flags.
-	flagsOnce.Do(func() {
-		var config map[string]string
-		if handle != nil {
-			config = handle.Config.Dump()
-		}
-		runtimeFlags.Parse(os.Args[1:], config)
-	})
-	flags := runtimeFlags.RuntimeFlags()
-
-	r.initLogging(ctx)
-	ctx = context.WithValue(ctx, loggerKey, vlog.Log)
-
-	// Set the preferred protocols.
-	if len(protocols) > 0 {
-		ctx = context.WithValue(ctx, protocolsKey, protocols)
-	}
-
-	// Setup i18n.
-	ctx = i18n.ContextWithLangID(ctx, i18n.LangIDFromEnv())
-	if len(flags.I18nCatalogue) != 0 {
-		cat := i18n.Cat()
-		for _, filename := range strings.Split(flags.I18nCatalogue, ",") {
-			err := cat.MergeFromFile(filename)
-			if err != nil {
-				fmt.Fprintf(os.Stderr, "%s: i18n: error reading i18n catalogue file %q: %s\n", os.Args[0], filename, err)
-			}
-		}
-	}
-
-	// Setup the program name.
-	ctx = verror2.ContextWithComponentName(ctx, filepath.Base(os.Args[0]))
-
-	// Setup the initial trace.
-	ctx, err = ivtrace.Init(ctx, flags.Vtrace)
-	if err != nil {
-		return nil, nil, err
-	}
-	ctx, _ = vtrace.SetNewTrace(ctx)
-
-	// Enable signal handling.
-	r.initSignalHandling(ctx)
-
-	// Set the initial namespace.
-	ctx, _, err = r.setNewNamespace(ctx, flags.NamespaceRoots...)
-	if err != nil {
-		return nil, nil, err
-	}
-
-	// Set the initial stream manager.
-	ctx, _, err = r.setNewStreamManager(ctx)
-	if err != nil {
-		return nil, nil, err
-	}
-
-	// 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)
-	if err != nil {
-		return nil, nil, err
-	}
-
-	// Initialize security.
-	principal, err := initSecurity(ctx, handle, flags.Credentials)
-	if err != nil {
-		return nil, nil, err
-	}
-	ctx = context.WithValue(ctx, principalKey, principal)
-
-	// Set up secure client.
-	ctx, _, err = r.SetNewClient(ctx)
-	if err != nil {
-		return nil, nil, err
-	}
-
-	// Initialize management.
-	if err := initMgmt(ctx, r.GetAppCycle(ctx), handle); err != nil {
-		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.
-
-	return ctx, r.cancel, nil
-}
-
 // initLogging configures logging for the runtime. It needs to be called after
 // flag.Parse and after signal handling has been initialized.
 func (r *RuntimeX) initLogging(ctx *context.T) error {
diff --git a/runtimes/google/rt/runtimex_test.go b/runtimes/google/rt/runtimex_test.go
index 6beb75d..d74041d 100644
--- a/runtimes/google/rt/runtimex_test.go
+++ b/runtimes/google/rt/runtimex_test.go
@@ -13,8 +13,7 @@
 // InitForTest creates a context for use in a test.
 func InitForTest(t *testing.T) (*rt.RuntimeX, *context.T, veyron2.Shutdown) {
 	ctx, cancel := context.WithCancel(nil)
-	r := &rt.RuntimeX{}
-	ctx, shutdown, err := r.Init(ctx, nil)
+	r, ctx, shutdown, err := rt.Init(ctx, nil)
 	if err != nil {
 		t.Fatal(err)
 	}