You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.5 KiB

  1. package main
  2. import (
  3. "errors"
  4. "net/http"
  5. rpc "github.com/tendermint/go-rpc/server"
  6. monitor "github.com/tendermint/tools/tm-monitor/monitor"
  7. )
  8. func startRPC(listenAddr string, m *monitor.Monitor) {
  9. routes := routes(m)
  10. // serve http and ws
  11. mux := http.NewServeMux()
  12. wm := rpc.NewWebsocketManager(routes, nil) // TODO: evsw
  13. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  14. rpc.RegisterRPCFuncs(mux, routes)
  15. if _, err := rpc.StartHTTPServer(listenAddr, mux); err != nil {
  16. panic(err)
  17. }
  18. }
  19. func routes(m *monitor.Monitor) map[string]*rpc.RPCFunc {
  20. return map[string]*rpc.RPCFunc{
  21. "status": rpc.NewRPCFunc(RPCStatus(m), ""),
  22. "status/network": rpc.NewRPCFunc(RPCNetworkStatus(m), ""),
  23. "status/node": rpc.NewRPCFunc(RPCNodeStatus(m), "name"),
  24. "monitor": rpc.NewRPCFunc(RPCMonitor(m), "endpoint"),
  25. "unmonitor": rpc.NewRPCFunc(RPCUnmonitor(m), "endpoint"),
  26. // "start_meter": rpc.NewRPCFunc(network.StartMeter, "chainID,valID,event"),
  27. // "stop_meter": rpc.NewRPCFunc(network.StopMeter, "chainID,valID,event"),
  28. // "meter": rpc.NewRPCFunc(GetMeterResult(network), "chainID,valID,event"),
  29. }
  30. }
  31. // RPCStatus returns common statistics for the network and statistics per node.
  32. func RPCStatus(m *monitor.Monitor) interface{} {
  33. return func() (networkAndNodes, error) {
  34. values := make([]*monitor.Node, len(m.Nodes))
  35. i := 0
  36. for _, v := range m.Nodes {
  37. values[i] = v
  38. i++
  39. }
  40. return networkAndNodes{m.Network, values}, nil
  41. }
  42. }
  43. // RPCNetworkStatus returns common statistics for the network.
  44. func RPCNetworkStatus(m *monitor.Monitor) interface{} {
  45. return func() (*monitor.Network, error) {
  46. return m.Network, nil
  47. }
  48. }
  49. // RPCNodeStatus returns statistics for the given node.
  50. func RPCNodeStatus(m *monitor.Monitor) interface{} {
  51. return func(name string) (*monitor.Node, error) {
  52. if n, ok := m.Nodes[name]; ok {
  53. return n, nil
  54. }
  55. return nil, errors.New("Cannot find node with that name")
  56. }
  57. }
  58. // RPCMonitor allows to dynamically add a endpoint to under the monitor.
  59. func RPCMonitor(m *monitor.Monitor) interface{} {
  60. return func(endpoint string) (*monitor.Node, error) {
  61. n := monitor.NewNode(endpoint)
  62. if err := m.Monitor(n); err != nil {
  63. return nil, err
  64. }
  65. return n, nil
  66. }
  67. }
  68. // RPCUnmonitor removes the given endpoint from under the monitor.
  69. func RPCUnmonitor(m *monitor.Monitor) interface{} {
  70. return func(endpoint string) (bool, error) {
  71. if n, ok := m.Nodes[endpoint]; ok {
  72. m.Unmonitor(n)
  73. return true, nil
  74. }
  75. return false, errors.New("Cannot find node with that name")
  76. }
  77. }
  78. // func (tn *TendermintNetwork) StartMeter(chainID, valID, eventID string) error {
  79. // tn.mtx.Lock()
  80. // defer tn.mtx.Unlock()
  81. // val, err := tn.getChainVal(chainID, valID)
  82. // if err != nil {
  83. // return err
  84. // }
  85. // return val.EventMeter().Subscribe(eventID, nil)
  86. // }
  87. // func (tn *TendermintNetwork) StopMeter(chainID, valID, eventID string) error {
  88. // tn.mtx.Lock()
  89. // defer tn.mtx.Unlock()
  90. // val, err := tn.getChainVal(chainID, valID)
  91. // if err != nil {
  92. // return err
  93. // }
  94. // return val.EventMeter().Unsubscribe(eventID)
  95. // }
  96. // func (tn *TendermintNetwork) GetMeter(chainID, valID, eventID string) (*eventmeter.EventMetric, error) {
  97. // tn.mtx.Lock()
  98. // defer tn.mtx.Unlock()
  99. // val, err := tn.getChainVal(chainID, valID)
  100. // if err != nil {
  101. // return nil, err
  102. // }
  103. // return val.EventMeter().GetMetric(eventID)
  104. // }
  105. //--> types
  106. type networkAndNodes struct {
  107. Network *monitor.Network `json:"network"`
  108. Nodes []*monitor.Node `json:"nodes"`
  109. }