Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func handleConn(conn net.Conn) {
- defer conn.Close()
- io.WriteString(conn, "Enter a new BPM:")
- scanner := bufio.NewScanner(conn)
- // take in BPM from stdin and add it to blockchain after conducting necessary validation
- go func() {
- for scanner.Scan() {
- bpm, err := strconv.Atoi(scanner.Text())
- if err != nil {
- log.Printf("%v not a number: %v", scanner.Text(), err)
- continue
- }
- newBlock, err := generateBlock(Blockchain[len(Blockchain)-1], bpm)
- if err != nil {
- log.Println(err)
- continue
- }
- if isBlockValid(newBlock, Blockchain[len(Blockchain)-1]) {
- newBlockchain := append(Blockchain, newBlock)
- replaceChain(newBlockchain)
- }
- bcServer <- Blockchain
- io.WriteString(conn, "\nEnter a new BPM:")
- }
- }()
- // simulate receiving broadcast
- go func() {
- for {
- time.Sleep(30 * time.Second)
- output, err := json.Marshal(Blockchain)
- if err != nil {
- log.Fatal(err)
- }
- io.WriteString(conn, string(output))
- }
- }()
- for _ = range bcServer {
- spew.Dump(Blockchain)
- }
- }
Add Comment
Please, Sign In to add comment