events.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  50. // NewEvent creates a new event for the given object which should be either a
  51. // Node, Conn or Msg.
  52. //
  53. // The object is copied so that the event represents the state of the object
  54. // when NewEvent is called.
  55. func NewEvent(v interface{}) *Event {
  56. event := &Event{Time: time.Now()}
  57. switch v := v.(type) {
  58. case *Node:
  59. event.Type = EventTypeNode
  60. node := *v
  61. event.Node = &node
  62. case *Conn:
  63. event.Type = EventTypeConn
  64. conn := *v
  65. event.Conn = &conn
  66. case *Msg:
  67. event.Type = EventTypeMsg
  68. msg := *v
  69. event.Msg = &msg
  70. default:
  71. panic(fmt.Sprintf("invalid event type: %T", v))
  72. }
  73. return event
  74. }
  75. // ControlEvent creates a new control event
  76. func ControlEvent(v interface{}) *Event {
  77. event := NewEvent(v)
  78. event.Control = true
  79. return event
  80. }
  81. // String returns the string representation of the event
  82. func (e *Event) String() string {
  83. switch e.Type {
  84. case EventTypeNode:
  85. return fmt.Sprintf("<node-event> id: %s up: %t", e.Node.ID().TerminalString(), e.Node.Up)
  86. case EventTypeConn:
  87. return fmt.Sprintf("<conn-event> nodes: %s->%s up: %t", e.Conn.One.TerminalString(), e.Conn.Other.TerminalString(), e.Conn.Up)
  88. case EventTypeMsg:
  89. 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)
  90. default:
  91. return ""
  92. }
  93. }