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.

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