github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/api/rpc/liteserver/server.go (about)

     1  package liteserver
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net"
     7  	"time"
     8  
     9  	pb "github.com/laof/lite-speed-test/api/rpc/lite"
    10  	"github.com/laof/lite-speed-test/web"
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  type server struct {
    15  	pb.TestProxyServer
    16  }
    17  
    18  // stream
    19  func (s *server) StartTest(req *pb.TestRequest, stream pb.TestProxy_StartTestServer) error {
    20  	// check data
    21  	links, err := web.ParseLinks(req.Subscription)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	groupName := req.GroupName
    26  	if len(groupName) < 1 {
    27  		groupName = "Default"
    28  	}
    29  	concurrency := req.Concurrency
    30  	if concurrency < 1 {
    31  		concurrency = 1
    32  	}
    33  	timeout := time.Duration(req.Timeout)
    34  	if timeout < 15 {
    35  		timeout = 15
    36  	}
    37  	speedTestMode := "all"
    38  	if req.SpeedTestMode == pb.SpeedTestMode_pingonly {
    39  		speedTestMode = "pingonly"
    40  	} else if req.SpeedTestMode == pb.SpeedTestMode_speedonly {
    41  		speedTestMode = "speedonly"
    42  	}
    43  	sortMethod := "none"
    44  	if req.SortMethod == pb.SortMethod_ping {
    45  		sortMethod = "ping"
    46  	} else if req.SortMethod == pb.SortMethod_rping {
    47  		sortMethod = "rping"
    48  	} else if req.SortMethod == pb.SortMethod_rping {
    49  		sortMethod = "rping"
    50  	}
    51  	// config
    52  	p := web.ProfileTest{
    53  		Writer:      nil,
    54  		MessageType: web.ALLTEST,
    55  		Links:       links,
    56  		Options: &web.ProfileTestOptions{
    57  			GroupName:     groupName,
    58  			SpeedTestMode: speedTestMode,
    59  			PingMethod:    "googleping",
    60  			SortMethod:    sortMethod,
    61  			Concurrency:   int(concurrency),
    62  			TestMode:      2,
    63  			Timeout:       timeout * time.Second,
    64  			Language:      "en",
    65  			FontSize:      24,
    66  		},
    67  	}
    68  
    69  	nodeChan, err := p.TestAll(stream.Context(), nil)
    70  	count := 0
    71  	linkCount := len(links)
    72  	for count < linkCount {
    73  		node := <-nodeChan
    74  		reply := pb.TestReply{
    75  			Id:        int32(node.Id),
    76  			GroupName: node.Group,
    77  			Remarks:   node.Remarks,
    78  			Protocol:  node.Protocol,
    79  			Ping:      node.Ping,
    80  			AvgSpeed:  node.AvgSpeed,
    81  			MaxSpeed:  node.MaxSpeed,
    82  			IsOk:      node.IsOk,
    83  			Traffic:   node.Traffic,
    84  			Link:      node.Link,
    85  		}
    86  		if err := stream.Send(&reply); err != nil {
    87  			return err
    88  		}
    89  		count += 1
    90  	}
    91  	return nil
    92  }
    93  
    94  func StartServer(port uint16) error {
    95  	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
    96  	if err != nil {
    97  		return err
    98  	}
    99  	log.Printf("start grpc server at %s", lis.Addr().String())
   100  	s := grpc.NewServer()
   101  	pb.RegisterTestProxyServer(s, &server{})
   102  	if err := s.Serve(lis); err != nil {
   103  		return err
   104  	}
   105  	return nil
   106  }