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.

155 lines
3.2 KiB

  1. package peer
  2. import (
  3. . "github.com/tendermint/tendermint/binary"
  4. "github.com/tendermint/tendermint/merkle"
  5. "sync"
  6. "io"
  7. )
  8. /* Client */
  9. type Client struct {
  10. listener *Listener
  11. addrBook AddrBook
  12. strategies map[String]*FilterStrategy
  13. targetNumPeers int
  14. peersMtx sync.Mutex
  15. peers merkle.Tree // addr -> *Peer
  16. filtersMtx sync.Mutex
  17. filters merkle.Tree // channelName -> Filter (objects that I know of)
  18. }
  19. func NewClient(protocol string, laddr string) *Client {
  20. // XXX set the handler
  21. listener := NewListener(protocol, laddr, nil)
  22. c := &Client{
  23. listener: listener,
  24. peers: merkle.NewIAVLTree(nil),
  25. filters: merkle.NewIAVLTree(nil),
  26. }
  27. return c
  28. }
  29. func (c *Client) Start() (<-chan *IncomingMsg) {
  30. return nil
  31. }
  32. func (c *Client) Stop() {
  33. c.listener.Close()
  34. }
  35. func (c *Client) LocalAddress() *NetAddress {
  36. return c.listener.LocalAddress()
  37. }
  38. func (c *Client) ConnectTo(addr *NetAddress) (*Peer, error) {
  39. conn, err := addr.Dial()
  40. if err != nil { return nil, err }
  41. peer := NewPeer(conn)
  42. // lock
  43. c.peersMtx.Lock()
  44. c.peers.Put(addr, peer)
  45. c.peersMtx.Unlock()
  46. // unlock
  47. return peer, nil
  48. }
  49. func (c *Client) Broadcast(channel String, msg Binary) {
  50. for v := range c.peersCopy().Values() {
  51. peer, ok := v.(*Peer)
  52. if !ok { panic("Expected peer but got something else") }
  53. peer.Queue(channel, msg)
  54. }
  55. }
  56. // Updates the client's filter for a channel & broadcasts it.
  57. func (c *Client) UpdateFilter(channel String, filter Filter) {
  58. c.filtersMtx.Lock()
  59. c.filters.Put(channel, filter)
  60. c.filtersMtx.Unlock()
  61. c.Broadcast("", &NewFilterMsg{
  62. Channel: channel,
  63. Filter: filter,
  64. })
  65. }
  66. func (c *Client) peersCopy() merkle.Tree {
  67. c.peersMtx.Lock(); defer c.peersMtx.Unlock()
  68. return c.peers.Copy()
  69. }
  70. /* Channel */
  71. type Channel struct {
  72. Name String
  73. Filter Filter
  74. //Stats Stats
  75. }
  76. /* Peer */
  77. type Peer struct {
  78. Conn *Connection
  79. Channels map[String]*Channel
  80. }
  81. func NewPeer(conn *Connection) *Peer {
  82. return &Peer{
  83. Conn: conn,
  84. Channels: nil,
  85. }
  86. }
  87. // Must be quick and nonblocking.
  88. func (p *Peer) Queue(channel String, msg Binary) {}
  89. func (p *Peer) WriteTo(w io.Writer) (n int64, err error) {
  90. return 0, nil // TODO
  91. }
  92. /* IncomingMsg */
  93. type IncomingMsg struct {
  94. SPeer *Peer
  95. SChan *Channel
  96. Time Time
  97. Msg Binary
  98. }
  99. /* Filter
  100. A Filter could be a bloom filter for lossy filtering, or could be a lossless filter.
  101. Either way, it's used to keep track of what a peer knows of.
  102. */
  103. type Filter interface {
  104. Binary
  105. Add(ByteSlice)
  106. Has(ByteSlice) bool
  107. }
  108. /* FilterStrategy
  109. Defines how filters are generated per peer, and whether they need to get refreshed occasionally.
  110. */
  111. type FilterStrategy interface {
  112. LoadFilter(ByteSlice) Filter
  113. }
  114. /* NewFilterMsg */
  115. type NewFilterMsg struct {
  116. Channel String
  117. Filter Filter
  118. }
  119. func (m *NewFilterMsg) WriteTo(w io.Writer) (int64, error) {
  120. return 0, nil // TODO
  121. }