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.

124 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. return networkAndNodes{m.Network, m.Nodes}, nil
  35. }
  36. }
  37. // RPCNetworkStatus returns common statistics for the network.
  38. func RPCNetworkStatus(m *monitor.Monitor) interface{} {
  39. return func() (*monitor.Network, error) {
  40. return m.Network, nil
  41. }
  42. }
  43. // RPCNodeStatus returns statistics for the given node.
  44. func RPCNodeStatus(m *monitor.Monitor) interface{} {
  45. return func(name string) (*monitor.Node, error) {
  46. if i, n := m.NodeByName(name); i != -1 {
  47. return n, nil
  48. }
  49. return nil, errors.New("Cannot find node with that name")
  50. }
  51. }
  52. // RPCMonitor allows to dynamically add a endpoint to under the monitor. Safe
  53. // to call multiple times.
  54. func RPCMonitor(m *monitor.Monitor) interface{} {
  55. return func(endpoint string) (*monitor.Node, error) {
  56. i, n := m.NodeByName(endpoint)
  57. if i == -1 {
  58. n = monitor.NewNode(endpoint)
  59. if err := m.Monitor(n); err != nil {
  60. return nil, err
  61. }
  62. }
  63. return n, nil
  64. }
  65. }
  66. // RPCUnmonitor removes the given endpoint from under the monitor.
  67. func RPCUnmonitor(m *monitor.Monitor) interface{} {
  68. return func(endpoint string) (bool, error) {
  69. if i, n := m.NodeByName(endpoint); i != -1 {
  70. m.Unmonitor(n)
  71. return true, nil
  72. }
  73. return false, errors.New("Cannot find node with that name")
  74. }
  75. }
  76. // func (tn *TendermintNetwork) StartMeter(chainID, valID, eventID string) error {
  77. // tn.mtx.Lock()
  78. // defer tn.mtx.Unlock()
  79. // val, err := tn.getChainVal(chainID, valID)
  80. // if err != nil {
  81. // return err
  82. // }
  83. // return val.EventMeter().Subscribe(eventID, nil)
  84. // }
  85. // func (tn *TendermintNetwork) StopMeter(chainID, valID, eventID string) error {
  86. // tn.mtx.Lock()
  87. // defer tn.mtx.Unlock()
  88. // val, err := tn.getChainVal(chainID, valID)
  89. // if err != nil {
  90. // return err
  91. // }
  92. // return val.EventMeter().Unsubscribe(eventID)
  93. // }
  94. // func (tn *TendermintNetwork) GetMeter(chainID, valID, eventID string) (*eventmeter.EventMetric, error) {
  95. // tn.mtx.Lock()
  96. // defer tn.mtx.Unlock()
  97. // val, err := tn.getChainVal(chainID, valID)
  98. // if err != nil {
  99. // return nil, err
  100. // }
  101. // return val.EventMeter().GetMetric(eventID)
  102. // }
  103. //--> types
  104. type networkAndNodes struct {
  105. Network *monitor.Network `json:"network"`
  106. Nodes []*monitor.Node `json:"nodes"`
  107. }