--- /dev/null
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package rpc
+
+/*
+ Some HTML presented at http://machine:port/debug/rpc
+ Lists services, their methods, and some statistics, still rudimentary.
+*/
+
+import (
+ "fmt";
+ "gob";
+ "http";
+ "io";
+ "log";
+ "os";
+ "rpc";
+ "sort";
+ "template";
+)
+
+const debugText =
+ `<html>
+ <body>
+ <title>Services</title>
+ {.repeated section @}
+ <hr>
+ Service {name}
+ <hr>
+ <table>
+ <th align=center>Method</th><th align=center>Calls</th>
+ {.repeated section meth}
+ <tr>
+ <td align=left font=fixed>{name}({.section m}{argType}, {replyType}) os.Error</td>
+ <td align=center>{numCalls}</td>{.end}
+ </tr>
+ {.end}
+ </table>
+ {.end}
+ </body>
+ </html>`
+
+var debug *template.Template
+
+type debugMethod struct {
+ m *methodType;
+ name string;
+}
+
+type methodArray []debugMethod
+
+type debugService struct {
+ s *service;
+ name string;
+ meth methodArray;
+}
+
+type serviceArray []debugService
+
+func (s serviceArray) Len() int { return len(s) }
+func (s serviceArray) Less(i, j int) bool { return s[i].name < s[j].name }
+func (s serviceArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+func (m methodArray) Len() int { return len(m) }
+func (m methodArray) Less(i, j int) bool { return m[i].name < m[j].name }
+func (m methodArray) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
+
+// Runs at /debug/rpc
+func debugHTTP(c *http.Conn, req *http.Request) {
+ var err os.Error;
+ if debug == nil {
+ debug, err = template.Parse(debugText, nil);
+ if err != nil {
+ fmt.Fprintln(c, "rpc can't create debug HTML template:", err.String());
+ return;
+ }
+ }
+ // Build a sorted version of the data.
+ var services = make(serviceArray, len(server.serviceMap));
+ i := 0;
+ server.Lock();
+ for sname, service := range server.serviceMap {
+ services[i] = debugService{service, sname, make(methodArray, len(service.method))};
+ j := 0;
+ for mname, method := range service.method {
+ services[i].meth[j] = debugMethod{method, mname};
+ j++;
+ }
+ sort.Sort(services[i].meth);
+ i++;
+ }
+ server.Unlock();
+ sort.Sort(services);
+ err = debug.Execute(services, c);
+ if err != nil {
+ fmt.Fprintln(c, "rpc: error executing template:", err.String());
+ }
+}
var typeOfOsError = reflect.Typeof(unusedError).(*reflect.PtrType).Elem()
type methodType struct {
+ sync.Mutex; // protects counters
method reflect.Method;
argType *reflect.PtrType;
replyType *reflect.PtrType;
+ numCalls uint;
}
type service struct {
}
type serverType struct {
+ sync.Mutex; // protects the serviceMap
serviceMap map[string] *service;
}
// This variable is a global whose "public" methods are really private methods
// called from the global functions of this package: rpc.Register, rpc.ServeConn, etc.
// For example, rpc.Register() calls server.add().
-var server = &serverType{ make(map[string] *service) }
+var server = &serverType{ serviceMap: make(map[string] *service) }
// Is this a publicly vislble - upper case - name?
func isPublic(name string) bool {
}
func (server *serverType) register(rcvr interface{}) os.Error {
+ server.Lock();
+ defer server.Unlock();
if server.serviceMap == nil {
server.serviceMap = make(map[string] *service);
}
log.Stderr("method", mname, "returns", returnType.String(), "not os.Error");
continue;
}
- s.method[mname] = &methodType{method, argType, replyType};
+ s.method[mname] = &methodType{method: method, argType: argType, replyType: replyType};
}
if len(s.method) == 0 {
sending.Unlock();
}
-func (s *service) call(sending *sync.Mutex, function *reflect.FuncValue, req *Request, argv, replyv reflect.Value, enc *gob.Encoder) {
+func (s *service) call(sending *sync.Mutex, mtype *methodType, req *Request, argv, replyv reflect.Value, enc *gob.Encoder) {
+ mtype.Lock();
+ mtype.numCalls++;
+ mtype.Unlock();
+ function := mtype.method.Func;
// Invoke the method, providing a new value for the reply.
returnValues := function.Call([]reflect.Value{s.rcvr, argv, replyv});
// The return value for the method is an os.Error.
continue;
}
// Look up the request.
+ server.Lock();
service, ok := server.serviceMap[serviceMethod[0]];
+ server.Unlock();
if !ok {
s := "rpc: can't find service " + req.ServiceMethod;
sendResponse(sending, req, invalidRequest, enc, s);
sendResponse(sending, req, replyv.Interface(), enc, err.String());
continue;
}
- go service.call(sending, method.Func, req, argv, replyv, enc);
+ go service.call(sending, mtype, req, argv, replyv, enc);
}
conn.Close();
}
// Can connect to RPC service using HTTP CONNECT to rpcPath.
var rpcPath string = "/_goRPC_"
+var debugPath string = "/debug/rpc"
var connected = "200 Connected to Go RPC"
func serveHTTP(c *http.Conn, req *http.Request) {
server.input(conn);
}
+func debugHTTP(c *http.Conn, req *http.Request)
+
// HandleHTTP registers an HTTP handler for RPC messages.
// It is still necessary to invoke http.Serve(), typically in a go statement.
func HandleHTTP() {
http.Handle(rpcPath, http.HandlerFunc(serveHTTP));
+ http.Handle(debugPath, http.HandlerFunc(debugHTTP));
}