func LoopMessage() { for { var msg Message msg = Message{ ID: util.RandomID(), Addr: self, Body: "進捗どうですか?", } Broadcast(msg) time.Sleep(200 * time.Millisecond) msg = Message{ ID: util.RandomID(), Addr: self, Body: "進捗ダメです", } Broadcast(msg) time.Sleep(200 * time.Millisecond) } }
func Broadcast(msg Message) { if *wildFlag { msg.ID = util.RandomID() } for _, ch := range peers.List() { select { case ch <- msg: // Send message into a channel default: // Drop message } } }
func ReadInput() { scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { msg := Message{ ID: util.RandomID(), Addr: self, Body: scanner.Text(), } Broadcast(msg) } if err := scanner.Err(); err != nil { log.Fatal(err) } }
func readInput() { s := bufio.NewScanner(os.Stdin) for s.Scan() { m := Message{ ID: util.RandomID(), Addr: self, Body: s.Text(), } Seen(m.ID) broadcast(m) } if err := s.Err(); err != nil { log.Fatal(err) } }
func readInput() { r := bufio.NewReader(os.Stdin) for { s, err := r.ReadString('\n') if err != nil { log.Fatal(err) } m := Message{ ID: util.RandomID(), Addr: self, Body: s[:len(s)-1], } Seen(m.ID) broadcast(m) } }
func (l *Logger) WriteTo(w io.Writer) error { id := util.RandomID() ch := make(chan []byte) l.mu.Lock() l.m[id] = ch l.mu.Unlock() defer func() { l.mu.Lock() delete(l.m, id) l.mu.Unlock() }() for b := range ch { _, err := w.Write(b) if err != nil { return err } } return nil }
// Read the information we got from our TCP connection func readInput() { // Create a new scanner s := bufio.NewScanner(os.Stdin) // While we are scanning the message for s.Scan() { // Initialize our message struct, getting the information out of it m := Message{ ID: util.RandomID(), Addr: self, Body: s.Text(), } // Add the ID of the message to a list of seen peers Seen(m.ID) // Broadcast the message out to all of the other peers broadcast(m) } if err := s.Err(); err != nil { log.Fatal(err) } }
func main() { id := util.RandomID() fmt.Println(id) }