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.

126 lines
3.3 KiB

  1. package main
  2. import (
  3. "errors"
  4. "net/http"
  5. rpc "github.com/tendermint/go-rpc/server"
  6. )
  7. func startRPC(listenAddr string, m *Monitor) {
  8. routes := routes(m)
  9. // serve http and ws
  10. mux := http.NewServeMux()
  11. wm := rpc.NewWebsocketManager(routes, nil) // TODO: evsw
  12. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  13. rpc.RegisterRPCFuncs(mux, routes)
  14. if _, err := rpc.StartHTTPServer(listenAddr, mux); err != nil {
  15. panic(err)
  16. }
  17. }
  18. func routes(m *Monitor) map[string]*rpc.RPCFunc {
  19. return map[string]*rpc.RPCFunc{
  20. "status": rpc.NewRPCFunc(RPCStatus(m), ""),
  21. "status/network": rpc.NewRPCFunc(RPCNetworkStatus(m), ""),
  22. "status/node": rpc.NewRPCFunc(RPCNodeStatus(m), "name"),
  23. "monitor": rpc.NewRPCFunc(RPCMonitor(m), "endpoint"),
  24. "unmonitor": rpc.NewRPCFunc(RPCUnmonitor(m), "endpoint"),
  25. // "start_meter": rpc.NewRPCFunc(network.StartMeter, "chainID,valID,event"),
  26. // "stop_meter": rpc.NewRPCFunc(network.StopMeter, "chainID,valID,event"),
  27. // "meter": rpc.NewRPCFunc(GetMeterResult(network), "chainID,valID,event"),
  28. }
  29. }
  30. // RPCStatus returns common statistics for the network and statistics per node.
  31. func RPCStatus(m *Monitor) interface{} {
  32. return func() (networkAndNodes, error) {
  33. values := make([]*Node, len(m.Nodes))
  34. i := 0
  35. for _, v := range m.Nodes {
  36. values[i] = v
  37. i++
  38. }
  39. return networkAndNodes{m.Network, values}, nil
  40. }
  41. }
  42. // RPCNetworkStatus returns common statistics for the network.
  43. func RPCNetworkStatus(m *Monitor) interface{} {
  44. return func() (*Network, error) {
  45. return m.Network, nil
  46. }
  47. }
  48. // RPCNodeStatus returns statistics for the given node.
  49. func RPCNodeStatus(m *Monitor) interface{} {
  50. return func(name string) (*Node, error) {
  51. if n, ok := m.Nodes[name]; ok {
  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.
  58. func RPCMonitor(m *Monitor) interface{} {
  59. return func(endpoint string) (*Node, error) {
  60. n := NewNode(endpoint)
  61. if err := m.Monitor(n); err != nil {
  62. return nil, err
  63. }
  64. return n, nil
  65. }
  66. }
  67. // RPCUnmonitor removes the given endpoint from under the monitor.
  68. func RPCUnmonitor(m *Monitor) interface{} {
  69. return func(endpoint string) (bool, error) {
  70. if n, ok := m.Nodes[endpoint]; ok {
  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 *Network `json:"network"`
  107. Nodes []*Node `json:"nodes"`
  108. }