Exemplo n.º 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
		}
	}
	// }
}
Exemplo n.º 2
0
func doUpload(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: Uploading", addr)

	io.WriteString(conn, "upload")

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