Guest User

Untitled

a guest
Feb 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. func handleConn(conn net.Conn) {
  2.  
  3. defer conn.Close()
  4.  
  5. io.WriteString(conn, "Enter a new BPM:")
  6.  
  7. scanner := bufio.NewScanner(conn)
  8.  
  9. // take in BPM from stdin and add it to blockchain after conducting necessary validation
  10. go func() {
  11. for scanner.Scan() {
  12. bpm, err := strconv.Atoi(scanner.Text())
  13. if err != nil {
  14. log.Printf("%v not a number: %v", scanner.Text(), err)
  15. continue
  16. }
  17. newBlock, err := generateBlock(Blockchain[len(Blockchain)-1], bpm)
  18. if err != nil {
  19. log.Println(err)
  20. continue
  21. }
  22. if isBlockValid(newBlock, Blockchain[len(Blockchain)-1]) {
  23. newBlockchain := append(Blockchain, newBlock)
  24. replaceChain(newBlockchain)
  25. }
  26.  
  27. bcServer <- Blockchain
  28. io.WriteString(conn, "\nEnter a new BPM:")
  29. }
  30. }()
  31.  
  32. // simulate receiving broadcast
  33. go func() {
  34. for {
  35. time.Sleep(30 * time.Second)
  36. output, err := json.Marshal(Blockchain)
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. io.WriteString(conn, string(output))
  41. }
  42. }()
  43.  
  44. for _ = range bcServer {
  45. spew.Dump(Blockchain)
  46. }
  47.  
  48. }
Add Comment
Please, Sign In to add comment