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.

125 lines
3.5 KiB

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