github.com/godevsig/adaptiveservice@v0.9.23/examples/echo/client/client.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sync"
     7  	"time"
     8  
     9  	as "github.com/godevsig/adaptiveservice"
    10  	echo "github.com/godevsig/adaptiveservice/examples/echo/server"
    11  )
    12  
    13  // Run runs the client.
    14  func Run(cmd string, opts []as.Option) {
    15  	c := as.NewClient(opts...).SetDeepCopy()
    16  	conn := <-c.Discover(echo.Publisher, echo.ServiceEcho)
    17  	if conn == nil {
    18  		fmt.Println(as.ErrServiceNotFound(echo.Publisher, echo.ServiceEcho))
    19  		os.Exit(1)
    20  	}
    21  	defer conn.Close()
    22  
    23  	if cmd == "timeout" {
    24  		stream := conn.NewStream()
    25  		fmt.Println("No timeout by default")
    26  		if err := stream.SendRecv(echo.MessageTimeout{}, nil); err != nil {
    27  			fmt.Println(err)
    28  			return
    29  		}
    30  		fmt.Println("Recv OK")
    31  
    32  		fmt.Println("Set timeout to 15s")
    33  		stream.SetRecvTimeout(15 * time.Second)
    34  		if err := stream.SendRecv(echo.MessageTimeout{}, nil); err != nil {
    35  			fmt.Println(err)
    36  			return
    37  		}
    38  		fmt.Println("Recv OK")
    39  
    40  		fmt.Println("Set timeout to 3s")
    41  		stream.SetRecvTimeout(3 * time.Second)
    42  		if err := stream.SendRecv(echo.MessageTimeout{}, nil); err != nil {
    43  			fmt.Println(err)
    44  		} else {
    45  			fmt.Println("Some error happend")
    46  		}
    47  		if err := stream.SendRecv(echo.MessageTimeout{}, nil); err != nil {
    48  			fmt.Println(err)
    49  		} else {
    50  			fmt.Println("Some error happend")
    51  		}
    52  
    53  		fmt.Println("Set back to no timeout")
    54  		stream.SetRecvTimeout(0)
    55  		if err := stream.SendRecv(echo.MessageTimeout{}, nil); err != nil {
    56  			fmt.Println(err)
    57  			return
    58  		}
    59  		fmt.Println("Recv OK")
    60  
    61  		return
    62  	}
    63  
    64  	if cmd == "whoelse" {
    65  		go func() {
    66  			eventStream := conn.NewStream()
    67  			if err := eventStream.Send(echo.SubWhoElseEvent{}); err != nil {
    68  				fmt.Println(err)
    69  				return
    70  			}
    71  			for {
    72  				var addr string
    73  				if err := eventStream.Recv(&addr); err != nil {
    74  					fmt.Println(err)
    75  					return
    76  				}
    77  				fmt.Printf("event: new client %s\n", addr)
    78  			}
    79  		}()
    80  
    81  		for i := 0; i < 200; i++ {
    82  			var whoelse string
    83  			if err := conn.SendRecv(echo.WhoElse{}, &whoelse); err != nil {
    84  				fmt.Println(err)
    85  				return
    86  			}
    87  			fmt.Printf("clients: %s\n", whoelse)
    88  			time.Sleep(3 * time.Second)
    89  		}
    90  		return
    91  	}
    92  
    93  	var rep echo.MessageReply
    94  	req := echo.MessageRequest{
    95  		Msg: "ni hao",
    96  		Num: 0,
    97  	}
    98  	for i := 0; i < 9; i++ {
    99  		req.Num += 100
   100  		if err := conn.SendRecv(&req, &rep); err != nil {
   101  			fmt.Println(err)
   102  			return
   103  		}
   104  		fmt.Printf("%v ==> %v, %s\n", req, rep.MessageRequest, rep.Signature)
   105  		//time.Sleep(time.Second)
   106  	}
   107  
   108  	var wg sync.WaitGroup
   109  	for i := 0; i < 100; i++ {
   110  		wg.Add(1)
   111  		go func(i int) {
   112  			defer wg.Done()
   113  			stream := conn.NewStream()
   114  			req := echo.MessageRequest{
   115  				Msg: "ni hao",
   116  				Num: 100 * int32(i),
   117  			}
   118  			var rep echo.MessageReply
   119  			for i := 0; i < 9; i++ {
   120  				req.Num += 10
   121  				if err := stream.SendRecv(&req, &rep); err != nil {
   122  					fmt.Println(err)
   123  					return
   124  				}
   125  				if req.Num+1 != rep.Num {
   126  					panic("wrong number")
   127  				}
   128  				fmt.Printf("%v ==> %v, %s\n", req, rep.MessageRequest, rep.Signature)
   129  				//time.Sleep(time.Second)
   130  			}
   131  		}(i)
   132  	}
   133  	wg.Wait()
   134  }