Beispiel #1
0
func (s server) handleConn(c net.Conn) {
	client := fmt.Sprintf("Client %v", c.RemoteAddr())
	logrus.Printf("%s: Connected", client)

	defer c.Close()

	stats := newLogger(client)
	defer close(stats)

	// for {
	var op [256]byte
	n, err := c.Read(op[:])
	if err != nil {
		logrus.Errorf("%s: failure: %v", client, err)
		return
	}

	switch string(op[:n]) {
	case "bye":
		logrus.Printf("%s: goodbye", client)
		return
	case "upload":
		if err := speedtest.Consume(c, s.buffers, s.buffers, stats); err != nil {
			logrus.Errorf("%s: failed upload: %v", client, err)
			return
		}
	case "download":
		if err := speedtest.Provide(c, s.buffers, s.buffers, stats); err != nil {
			logrus.Errorf("%s: failed download: %v", client, err)
			return
		}
	}
	// }
}
Beispiel #2
0
func doDownload(buffers chan []byte, addr string) {
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		logrus.Fatalf("Cannot connect to %v: %v", addr, err)
	}
	logrus.Printf("Connected to %s: Downloading", addr)

	io.WriteString(conn, "download")

	stats := newLogger(fmt.Sprintf("Server %v", addr))
	if err := speedtest.Consume(conn, buffers, buffers, stats); err != nil {
		logrus.Fatal(err)
	}
	stats <- speedtest.Stats{} // flush the last entry.
	close(stats)
	logrus.Print("Done")
}