events.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package simulations
  17. import (
  18. "fmt"
  19. "time"
  20. )
  21. // EventType is the type of event emitted by a simulation network
  22. type EventType string
  23. const (
  24. // EventTypeNode is the type of event emitted when a node is either
  25. // created, started or stopped
  26. EventTypeNode EventType = "node"
  27. // EventTypeConn is the type of event emitted when a connection is
  28. // is either established or dropped between two nodes
  29. EventTypeConn EventType = "conn"
  30. // EventTypeMsg is the type of event emitted when a p2p message it
  31. // sent between two nodes
  32. EventTypeMsg EventType = "msg"
  33. )
  34. // Event is an event emitted by a simulation network
  35. type Event struct {
  36. // Type is the type of the event
  37. Type EventType `json:"type"`
  38. // Time is the time the event happened
  39. Time time.Time `json:"time"`
  40. // Control indicates whether the event is the result of a controlled
  41. // action in the network
  42. Control bool `json:"control"`
  43. // Node is set if the type is EventTypeNode
  44. Node *Node `json:"node,omitempty"`
  45. // Conn is set if the type is EventTypeConn
  46. Conn *Conn `json:"conn,omitempty"`
  47. // Msg is set if the type is EventTypeMsg
  48. Msg *Msg `json:"msg,omitempty"`
  49. //Optionally provide data (currently for simulation frontends only)
  50. Data interface{} `json:"data"`
  51. }
  52. // NewEvent creates a new event for the given object which should be either a
  53. // Node, Conn or Msg.
  54. //
  55. // The object is copied so that the event represents the state of the object
  56. // when NewEvent is called.
  57. func NewEvent(v interface{}) *Event {
  58. event := &Event{Time: time.Now()}
  59. switch v := v.(type) {
  60. case *Node:
  61. event.Type = EventTypeNode
  62. event.Node = v.copy()
  63. case *Conn:
  64. event.Type = EventTypeConn
  65. conn := *v
  66. event.Conn = &conn
  67. case *Msg:
  68. event.Type = EventTypeMsg
  69. msg := *v
  70. event.Msg = &msg
  71. default:
  72. panic(fmt.Sprintf("invalid event type: %T", v))
  73. }
  74. return event
  75. }
  76. // ControlEvent creates a new control event
  77. func ControlEvent(v interface{}) *Event {
  78. event := NewEvent(v)
  79. event.Control = true
  80. return event
  81. }
  82. // String returns the string representation of the event
  83. func (e *Event) String() string {
  84. switch e.Type {
  85. case EventTypeNode:
  86. return fmt.Sprintf("<node-event> id: %s up: %t", e.Node.ID().TerminalString(), e.Node.Up())
  87. case EventTypeConn:
  88. return fmt.Sprintf("<conn-event> nodes: %s->%s up: %t", e.Conn.One.TerminalString(), e.Conn.Other.TerminalString(), e.Conn.Up)
  89. case EventTypeMsg:
  90. return fmt.Sprintf("<msg-event> nodes: %s->%s proto: %s, code: %d, received: %t", e.Msg.One.TerminalString(), e.Msg.Other.TerminalString(), e.Msg.Protocol, e.Msg.Code, e.Msg.Received)
  91. default:
  92. return ""
  93. }
  94. }