libgo: update to go1.9
[official-gcc.git] / libgo / go / net / rpc / debug.go
bloba1d799ff19a1424258dd18123664a6476c343293
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package rpc
7 /*
8 Some HTML presented at http://machine:port/debug/rpc
9 Lists services, their methods, and some statistics, still rudimentary.
12 import (
13 "fmt"
14 "html/template"
15 "net/http"
16 "sort"
19 const debugText = `<html>
20 <body>
21 <title>Services</title>
22 {{range .}}
23 <hr>
24 Service {{.Name}}
25 <hr>
26 <table>
27 <th align=center>Method</th><th align=center>Calls</th>
28 {{range .Method}}
29 <tr>
30 <td align=left font=fixed>{{.Name}}({{.Type.ArgType}}, {{.Type.ReplyType}}) error</td>
31 <td align=center>{{.Type.NumCalls}}</td>
32 </tr>
33 {{end}}
34 </table>
35 {{end}}
36 </body>
37 </html>`
39 var debug = template.Must(template.New("RPC debug").Parse(debugText))
41 // If set, print log statements for internal and I/O errors.
42 var debugLog = false
44 type debugMethod struct {
45 Type *methodType
46 Name string
49 type methodArray []debugMethod
51 type debugService struct {
52 Service *service
53 Name string
54 Method methodArray
57 type serviceArray []debugService
59 func (s serviceArray) Len() int { return len(s) }
60 func (s serviceArray) Less(i, j int) bool { return s[i].Name < s[j].Name }
61 func (s serviceArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
63 func (m methodArray) Len() int { return len(m) }
64 func (m methodArray) Less(i, j int) bool { return m[i].Name < m[j].Name }
65 func (m methodArray) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
67 type debugHTTP struct {
68 *Server
71 // Runs at /debug/rpc
72 func (server debugHTTP) ServeHTTP(w http.ResponseWriter, req *http.Request) {
73 // Build a sorted version of the data.
74 var services serviceArray
75 server.serviceMap.Range(func(snamei, svci interface{}) bool {
76 svc := svci.(*service)
77 ds := debugService{svc, snamei.(string), make(methodArray, 0, len(svc.method))}
78 for mname, method := range svc.method {
79 ds.Method = append(ds.Method, debugMethod{method, mname})
81 sort.Sort(ds.Method)
82 services = append(services, ds)
83 return true
85 sort.Sort(services)
86 err := debug.Execute(w, services)
87 if err != nil {
88 fmt.Fprintln(w, "rpc: error executing template:", err.Error())