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.

128 lines
3.6 KiB

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