Example #1
0
func main() {
	if len(os.Args) != 2 {
		fmt.Printf("Usage: %s host\n", os.Args[0])
		return
	}
	host := os.Args[1]
	conn, err := net.Dial("tcp", net.JoinHostPort(host, "2037"))
	muduo.PanicOnError(err)
	defer conn.Close()

	var unixtime int32
	err = binary.Read(conn, binary.BigEndian, &unixtime)
	muduo.PanicOnError(err)

	println(time.Unix(int64(unixtime), 0).String())
}
Example #2
0
func (b *backendConn) run() {
	go func() {
		defer b.conn.Close()
		for {
			resp, err := muduorpc.Decode(b.conn)
			if err != nil {
				// FIXME: reject outstandings
				close(b.requests)
			}
			b.mu.Lock()
			r, ok := b.outstanding[resp.GetId()]
			if ok {
				delete(b.outstanding, resp.GetId())
			} else {
				panic("What's wrong?")
			}
			b.mu.Unlock()
			resp.Id = &r.origId
			r.output <- resp
		}
	}()

	var nextId uint64 = 100
	for r := range b.requests {
		resp := response{r.msg.GetId(), r.output}
		var newId uint64 = nextId
		nextId++
		b.mu.Lock()
		b.outstanding[newId] = resp
		b.mu.Unlock()
		r.msg.Id = &newId
		err := muduorpc.Send(b.conn, r.msg)
		muduo.PanicOnError(err)
	}
}
Example #3
0
func newBackend(addr string) *backendConn {
	conn, err := net.Dial("tcp", addr)
	muduo.PanicOnError(err)
	return &backendConn{
		name:        addr,
		conn:        conn,
		requests:    make(chan request, 10),
		outstanding: make(map[uint64]response),
	}
}
Example #4
0
func transmit() {
	sessionMessage := SessionMessage{int32(opt.number), int32(opt.length)}
	fmt.Printf("buffer length = %d\nnumber of buffers = %d\n",
		sessionMessage.Length, sessionMessage.Number)
	total_mb := float64(sessionMessage.Number) * float64(sessionMessage.Length) / 1024.0 / 1024.0
	fmt.Printf("%.3f MiB in total\n", total_mb)

	conn, err := net.Dial("tcp", net.JoinHostPort(opt.host, strconv.Itoa(opt.port)))
	muduo.PanicOnError(err)
	// t := conn.(*net.TCPConn)
	// t.SetNoDelay(false)
	defer conn.Close()

	start := time.Now()
	err = binary.Write(conn, binary.BigEndian, &sessionMessage)
	muduo.PanicOnError(err)

	total_len := 4 + opt.length // binary.Size(int32(0)) == 4
	// println(total_len)
	payload := make([]byte, total_len)
	binary.BigEndian.PutUint32(payload, uint32(opt.length))
	for i := 0; i < opt.length; i++ {
		payload[4+i] = "0123456789ABCDEF"[i%16]
	}

	for i := 0; i < opt.number; i++ {
		var n int
		n, err = conn.Write(payload)
		muduo.PanicOnError(err)
		muduo.Check(n == len(payload), "write payload")

		var ack int32
		err = binary.Read(conn, binary.BigEndian, &ack)
		muduo.PanicOnError(err)
		muduo.Check(ack == int32(opt.length), "ack")
	}

	elapsed := time.Since(start).Seconds()
	fmt.Printf("%.3f seconds\n%.3f MiB/s\n", elapsed, total_mb/elapsed)
}
Example #5
0
func receive() {
	listener := muduo.ListenTcpOrDie(fmt.Sprintf(":%d", opt.port))
	defer listener.Close()
	println("Accepting", listener.Addr().String())
	conn, err := listener.Accept()
	muduo.PanicOnError(err)
	defer conn.Close()

	// Read header
	var sessionMessage SessionMessage
	err = binary.Read(conn, binary.BigEndian, &sessionMessage)
	muduo.PanicOnError(err)

	fmt.Printf("receive buffer length = %d\nreceive number of buffers = %d\n",
		sessionMessage.Length, sessionMessage.Number)
	total_mb := float64(sessionMessage.Number) * float64(sessionMessage.Length) / 1024.0 / 1024.0
	fmt.Printf("%.3f MiB in total\n", total_mb)

	payload := make([]byte, sessionMessage.Length)
	start := time.Now()
	for i := 0; i < int(sessionMessage.Number); i++ {
		var length int32
		err = binary.Read(conn, binary.BigEndian, &length)
		muduo.PanicOnError(err)
		muduo.Check(length == sessionMessage.Length, "read length")

		var n int
		n, err = io.ReadFull(conn, payload)
		muduo.PanicOnError(err)
		muduo.Check(n == len(payload), "read payload")

		// ack
		err = binary.Write(conn, binary.BigEndian, &length)
		muduo.PanicOnError(err)
	}

	elapsed := time.Since(start).Seconds()
	fmt.Printf("%.3f seconds\n%.3f MiB/s\n", elapsed, total_mb/elapsed)
}
Example #6
0
func main() {
	if len(os.Args) != 2 {
		fmt.Printf("Usage: %s host\n", os.Args[0])
		return
	}
	host := os.Args[1]
	conn, err := net.Dial("tcp", net.JoinHostPort(host, "2019"))
	muduo.PanicOnError(err)
	defer conn.Close()

	total, err := io.Copy(ioutil.Discard, conn)
	if err != nil {
		log.Println("discardclient:", err.Error())
	}
	log.Println("total", total)
}
Example #7
0
func NewBalancer(port int, backendS string) *balancer {
	backends := strings.Split(backendS, ",")
	if len(backends) == 0 || backends[0] == "" {
		log.Fatalln("please specify backends with -b.")
	}
	log.Printf("backends: %q\n", backends)
	s := &balancer{
		requests: make(chan request, 1024), // size ?
		backends: make([]*backendConn, len(backends)),
	}
	for i, addr := range backends {
		s.backends[i] = newBackend(addr)
		go s.backends[i].run()
	}
	l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	muduo.PanicOnError(err)
	s.listener = l
	return s
}