main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Skeleton to part 7 of the Whispering Gophers code lab.
  2. //
  3. // This program extends part 6 by adding a Peers type.
  4. // The rest of the code is left as-is, so functionally there is no change.
  5. //
  6. // However we have added a peers_test.go file, so that running
  7. // go test
  8. // from the package directory will test your implementation of the Peers type.
  9. //
  10. package main
  11. import (
  12. "bufio"
  13. "encoding/json"
  14. "flag"
  15. "fmt"
  16. "log"
  17. "net"
  18. "os"
  19. "sync"
  20. "github.com/campoy/whispering-gophers/util"
  21. )
  22. var (
  23. peerAddr = flag.String("peer", "", "peer host:port")
  24. self string
  25. )
  26. type Message struct {
  27. Addr string
  28. Body string
  29. }
  30. func main() {
  31. flag.Parse()
  32. l, err := util.Listen()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. self = l.Addr().String()
  37. log.Println("Listening on", self)
  38. go dial(*peerAddr)
  39. go readInput()
  40. for {
  41. c, err := l.Accept()
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. go serve(c)
  46. }
  47. }
  48. type Peers struct {
  49. m map[string]chan<- Message
  50. mu sync.RWMutex
  51. }
  52. // Add creates and returns a new channel for the given peer address.
  53. // If an address already exists in the registry, it returns nil.
  54. func (p *Peers) Add(addr string) <-chan Message {
  55. // TODO: Take the write lock on p.mu. Unlock it before returning (using defer).
  56. // TODO: Check if the address is already in the peers map under the key addr.
  57. // TODO: If it is, return nil.
  58. // TODO: Make a new channel of messages
  59. // TODO: Add it to the peers map
  60. // TODO: Return the newly created channel.
  61. }
  62. // Remove deletes the specified peer from the registry.
  63. func (p *Peers) Remove(addr string) {
  64. // TODO: Take the write lock on p.mu. Unlock it before returning (using defer).
  65. // TODO: Delete the peer from the peers map.
  66. }
  67. // List returns a slice of all active peer channels.
  68. func (p *Peers) List() []chan<- Message {
  69. // TODO: Take the read lock on p.mu. Unlock it before returning (using defer).
  70. // TODO: Declare a slice of chan<- Message.
  71. for /* TODO: Iterate over the map using range */ {
  72. // TODO: Append each channel into the slice.
  73. }
  74. // TODO: Return the slice.
  75. }
  76. func serve(c net.Conn) {
  77. defer c.Close()
  78. d := json.NewDecoder(c)
  79. for {
  80. var m Message
  81. err := d.Decode(&m)
  82. if err != nil {
  83. log.Println(err)
  84. return
  85. }
  86. fmt.Printf("%#v\n", m)
  87. }
  88. }
  89. var peer = make(chan Message)
  90. func readInput() {
  91. s := bufio.NewScanner(os.Stdin)
  92. for s.Scan() {
  93. m := Message{
  94. Addr: self,
  95. Body: s.Text(),
  96. }
  97. peer <- m
  98. }
  99. if err := s.Err(); err != nil {
  100. log.Fatal(err)
  101. }
  102. }
  103. func dial(addr string) {
  104. c, err := net.Dial("tcp", addr)
  105. if err != nil {
  106. log.Println(addr, err)
  107. return
  108. }
  109. defer c.Close()
  110. e := json.NewEncoder(c)
  111. for m := range peer {
  112. err := e.Encode(m)
  113. if err != nil {
  114. log.Println(addr, err)
  115. return
  116. }
  117. }
  118. }