veyron/services/mgmt: Use VDL-generated stubs

Switch to using the VDL-generated stubs for the services behind the
debug dispatcher. Now that these services aren't directly part of the
runtime, there is no reason to avoid using the stubs.

Change-Id: I4e10329c47fd51f2c4f8cbed83ee3a8785bf6279
diff --git a/services/mgmt/logreader/impl/logfile.go b/services/mgmt/logreader/impl/logfile.go
index b29f416..fd6f354 100644
--- a/services/mgmt/logreader/impl/logfile.go
+++ b/services/mgmt/logreader/impl/logfile.go
@@ -12,6 +12,7 @@
 	"strings"
 
 	"veyron.io/veyron/veyron2/ipc"
+	"veyron.io/veyron/veyron2/services/mgmt/logreader"
 	"veyron.io/veyron/veyron2/services/mgmt/logreader/types"
 	"veyron.io/veyron/veyron2/verror"
 	"veyron.io/veyron/veyron2/vlog"
@@ -26,7 +27,7 @@
 
 // NewLogFileService returns a new log file server.
 func NewLogFileService(root, suffix string) interface{} {
-	return &logfileService{filepath.Clean(root), suffix}
+	return logreader.LogFileServer(&logfileService{filepath.Clean(root), suffix})
 }
 
 // translateNameToFilename returns the file name that corresponds to the object
@@ -53,7 +54,7 @@
 }
 
 // Size returns the size of the log file, in bytes.
-func (i *logfileService) Size(call ipc.ServerCall) (int64, error) {
+func (i *logfileService) Size(ipc.ServerContext) (int64, error) {
 	vlog.VI(1).Infof("%v.Size()", i.suffix)
 	fname, err := translateNameToFilename(i.root, i.suffix)
 	if err != nil {
@@ -74,7 +75,7 @@
 }
 
 // ReadLog returns log entries from the log file.
-func (i *logfileService) ReadLog(call ipc.ServerCall, startpos int64, numEntries int32, follow bool) (int64, error) {
+func (i *logfileService) ReadLog(ctx logreader.LogFileReadLogContext, startpos int64, numEntries int32, follow bool) (int64, error) {
 	vlog.VI(1).Infof("%v.ReadLog(%v, %v, %v)", i.suffix, startpos, numEntries, follow)
 	fname, err := translateNameToFilename(i.root, i.suffix)
 	if err != nil {
@@ -87,7 +88,7 @@
 		}
 		return 0, errOperationFailed
 	}
-	reader := newFollowReader(call, f, startpos, follow)
+	reader := newFollowReader(ctx, f, startpos, follow)
 	if numEntries == types.AllEntries {
 		numEntries = int32(math.MaxInt32)
 	}
@@ -102,7 +103,7 @@
 		if err != nil {
 			return reader.tell(), errOperationFailed
 		}
-		if err := call.Send(types.LogEntry{Position: offset, Line: line}); err != nil {
+		if err := ctx.SendStream().Send(types.LogEntry{Position: offset, Line: line}); err != nil {
 			return reader.tell(), err
 		}
 	}
diff --git a/services/mgmt/logreader/impl/reader.go b/services/mgmt/logreader/impl/reader.go
index 0a063b1..97d1272 100644
--- a/services/mgmt/logreader/impl/reader.go
+++ b/services/mgmt/logreader/impl/reader.go
@@ -14,7 +14,7 @@
 // - it aborts when the parent RPC is canceled.
 type followReader struct {
 	reader io.ReadSeeker
-	call   ipc.ServerCall
+	ctx    ipc.ServerContext
 	offset int64
 	follow bool
 	err    error
@@ -22,11 +22,11 @@
 }
 
 // newFollowReader is the factory for followReader.
-func newFollowReader(call ipc.ServerCall, reader io.ReadSeeker, startpos int64, follow bool) *followReader {
+func newFollowReader(ctx ipc.ServerContext, reader io.ReadSeeker, startpos int64, follow bool) *followReader {
 	_, err := reader.Seek(startpos, 0)
 	return &followReader{
 		reader: reader,
-		call:   call,
+		ctx:    ctx,
 		offset: startpos,
 		follow: follow,
 		err:    err,
@@ -43,9 +43,9 @@
 		return 0, f.err
 	}
 	for {
-		if f.call != nil {
+		if f.ctx != nil {
 			select {
-			case <-f.call.Done():
+			case <-f.ctx.Done():
 				return 0, errCanceled
 			default:
 			}
diff --git a/services/mgmt/pprof/impl/server.go b/services/mgmt/pprof/impl/server.go
index 8f2f39a..21e300d 100644
--- a/services/mgmt/pprof/impl/server.go
+++ b/services/mgmt/pprof/impl/server.go
@@ -8,12 +8,13 @@
 	"time"
 
 	"veyron.io/veyron/veyron2/ipc"
+	spprof "veyron.io/veyron/veyron2/services/mgmt/pprof"
 	"veyron.io/veyron/veyron2/verror"
 )
 
 // NewPProfService returns a new pprof service implementation.
 func NewPProfService() interface{} {
-	return &pprofService{}
+	return spprof.PProfServer(&pprofService{})
 }
 
 type pprofService struct {
@@ -39,14 +40,12 @@
 // addresses that pprof needs. Passing debug=1 adds comments translating
 // addresses to function names and line numbers, so that a programmer
 // can read the profile without tools.
-//
-// TODO(toddw): Change ipc.ServerCall into a struct stub context.
-func (pprofService) Profile(call ipc.ServerCall, name string, debug int32) error {
+func (pprofService) Profile(ctx spprof.PProfProfileContext, name string, debug int32) error {
 	profile := pprof.Lookup(name)
 	if profile == nil {
 		return verror.NoExistf("profile does not exist")
 	}
-	if err := profile.WriteTo(&streamWriter{call}, int(debug)); err != nil {
+	if err := profile.WriteTo(&streamWriter{ctx.SendStream()}, int(debug)); err != nil {
 		return verror.Convert(err)
 	}
 	return nil
@@ -54,13 +53,11 @@
 
 // CPUProfile enables CPU profiling for the requested duration and
 // streams the profile data.
-//
-// TODO(toddw): Change ipc.ServerCall into a struct stub context.
-func (pprofService) CPUProfile(call ipc.ServerCall, seconds int32) error {
+func (pprofService) CPUProfile(ctx spprof.PProfCPUProfileContext, seconds int32) error {
 	if seconds <= 0 || seconds > 3600 {
 		return verror.BadArgf("invalid number of seconds: %d", seconds)
 	}
-	if err := pprof.StartCPUProfile(&streamWriter{call}); err != nil {
+	if err := pprof.StartCPUProfile(&streamWriter{ctx.SendStream()}); err != nil {
 		return verror.Convert(err)
 	}
 	time.Sleep(time.Duration(seconds) * time.Second)
@@ -82,11 +79,13 @@
 }
 
 type streamWriter struct {
-	call ipc.ServerCall
+	sender interface {
+		Send(item []byte) error
+	}
 }
 
 func (w *streamWriter) Write(p []byte) (int, error) {
-	if err := w.call.Send(p); err != nil {
+	if err := w.sender.Send(p); err != nil {
 		return 0, err
 	}
 	return len(p), nil
diff --git a/services/mgmt/stats/impl/stats.go b/services/mgmt/stats/impl/stats.go
index 1b4b7dc..b398ee8 100644
--- a/services/mgmt/stats/impl/stats.go
+++ b/services/mgmt/stats/impl/stats.go
@@ -5,11 +5,13 @@
 import (
 	"time"
 
-	"veyron.io/veyron/veyron/lib/stats"
+	libstats "veyron.io/veyron/veyron/lib/stats"
 
 	"veyron.io/veyron/veyron2/ipc"
 	"veyron.io/veyron/veyron2/naming"
+	"veyron.io/veyron/veyron2/services/mgmt/stats"
 	"veyron.io/veyron/veyron2/services/mgmt/stats/types"
+	"veyron.io/veyron/veyron2/services/watch"
 	watchtypes "veyron.io/veyron/veyron2/services/watch/types"
 	"veyron.io/veyron/veyron2/vdl/vdlutil"
 	"veyron.io/veyron/veyron2/verror"
@@ -30,19 +32,19 @@
 // NewStatsService returns a stats server implementation. The value of watchFreq
 // is used to specify the time between WatchGlob updates.
 func NewStatsService(suffix string, watchFreq time.Duration) interface{} {
-	return &statsService{suffix, watchFreq}
+	return stats.StatsServer(&statsService{suffix, watchFreq})
 }
 
 // Glob returns the name of all objects that match pattern.
-func (i *statsService) Glob(ctx *ipc.GlobContextStub, pattern string) error {
+func (i *statsService) Glob(ctx ipc.GlobContext, pattern string) error {
 	vlog.VI(1).Infof("%v.Glob(%q)", i.suffix, pattern)
 
-	it := stats.Glob(i.suffix, pattern, time.Time{}, false)
+	it := libstats.Glob(i.suffix, pattern, time.Time{}, false)
 	for it.Advance() {
 		ctx.SendStream().Send(naming.VDLMountEntry{Name: it.Value().Key})
 	}
 	if err := it.Err(); err != nil {
-		if err == stats.ErrNotFound {
+		if err == libstats.ErrNotFound {
 			return errNotFound
 		}
 		return errOperationFailed
@@ -52,7 +54,7 @@
 
 // WatchGlob returns the name and value of the objects that match the request,
 // followed by periodic updates when values change.
-func (i *statsService) WatchGlob(call ipc.ServerCall, req watchtypes.GlobRequest) error {
+func (i *statsService) WatchGlob(ctx watch.GlobWatcherWatchGlobContext, req watchtypes.GlobRequest) error {
 	vlog.VI(1).Infof("%v.WatchGlob(%+v)", i.suffix, req)
 
 	var t time.Time
@@ -60,7 +62,7 @@
 	for {
 		prevTime := t
 		t = time.Now()
-		it := stats.Glob(i.suffix, req.Pattern, prevTime, true)
+		it := libstats.Glob(i.suffix, req.Pattern, prevTime, true)
 		changes := []watchtypes.Change{}
 		for it.Advance() {
 			v := it.Value()
@@ -72,18 +74,18 @@
 			changes = append(changes, c)
 		}
 		if err := it.Err(); err != nil {
-			if err == stats.ErrNotFound {
+			if err == libstats.ErrNotFound {
 				return errNotFound
 			}
 			return errOperationFailed
 		}
 		for _, change := range changes {
-			if err := call.Send(change); err != nil {
+			if err := ctx.SendStream().Send(change); err != nil {
 				return err
 			}
 		}
 		select {
-		case <-call.Done():
+		case <-ctx.Done():
 			break Loop
 		case <-time.After(i.watchFreq):
 		}
@@ -95,11 +97,11 @@
 func (i *statsService) Value(ctx ipc.ServerContext) (vdlutil.Any, error) {
 	vlog.VI(1).Infof("%v.Value()", i.suffix)
 
-	v, err := stats.Value(i.suffix)
+	v, err := libstats.Value(i.suffix)
 	switch err {
-	case stats.ErrNotFound:
+	case libstats.ErrNotFound:
 		return nil, errNotFound
-	case stats.ErrNoValue:
+	case libstats.ErrNoValue:
 		return nil, errNoValue
 	case nil:
 		return v, nil
diff --git a/services/mgmt/vtrace/impl/vtrace.go b/services/mgmt/vtrace/impl/vtrace.go
index 3f29f26..d389436 100644
--- a/services/mgmt/vtrace/impl/vtrace.go
+++ b/services/mgmt/vtrace/impl/vtrace.go
@@ -2,6 +2,7 @@
 
 import (
 	"veyron.io/veyron/veyron2/ipc"
+	svtrace "veyron.io/veyron/veyron2/services/mgmt/vtrace"
 	"veyron.io/veyron/veyron2/uniqueid"
 	"veyron.io/veyron/veyron2/verror2"
 	"veyron.io/veyron/veyron2/vtrace"
@@ -19,13 +20,12 @@
 	return *tr, nil
 }
 
-// TODO(toddw): Change ipc.ServerCall into a struct stub context.
-func (v *vtraceService) AllTraces(call ipc.ServerCall) error {
+func (v *vtraceService) AllTraces(ctx svtrace.StoreAllTracesContext) error {
 	// TODO(mattr): Consider changing the store to allow us to iterate through traces
 	// when there are many.
 	traces := v.store.TraceRecords()
 	for i := range traces {
-		if err := call.Send(traces[i]); err != nil {
+		if err := ctx.SendStream().Send(traces[i]); err != nil {
 			return err
 		}
 	}
@@ -33,5 +33,5 @@
 }
 
 func NewVtraceService(store vtrace.Store) interface{} {
-	return &vtraceService{store}
+	return svtrace.StoreServer(&vtraceService{store})
 }