github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/Godeps/_workspace/src/google.golang.org/grpc/interop/server/server.go (about)

     1  /*
     2   *
     3   * Copyright 2014, Google Inc.
     4   * All rights reserved.
     5   *
     6   * Redistribution and use in source and binary forms, with or without
     7   * modification, are permitted provided that the following conditions are
     8   * met:
     9   *
    10   *     * Redistributions of source code must retain the above copyright
    11   * notice, this list of conditions and the following disclaimer.
    12   *     * Redistributions in binary form must reproduce the above
    13   * copyright notice, this list of conditions and the following disclaimer
    14   * in the documentation and/or other materials provided with the
    15   * distribution.
    16   *     * Neither the name of Google Inc. nor the names of its
    17   * contributors may be used to endorse or promote products derived from
    18   * this software without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    24   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    25   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    26   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    28   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    29   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    30   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31   *
    32   */
    33  
    34  package main
    35  
    36  import (
    37  	"flag"
    38  	"fmt"
    39  	"io"
    40  	"net"
    41  	"strconv"
    42  	"time"
    43  
    44  	"github.com/coreos/rkt/Godeps/_workspace/src/github.com/golang/protobuf/proto"
    45  	"github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/net/context"
    46  	"github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc"
    47  	"github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/credentials"
    48  	"github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/grpclog"
    49  	testpb "github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/interop/grpc_testing"
    50  )
    51  
    52  var (
    53  	useTLS   = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
    54  	certFile = flag.String("tls_cert_file", "testdata/server1.pem", "The TLS cert file")
    55  	keyFile  = flag.String("tls_key_file", "testdata/server1.key", "The TLS key file")
    56  	port     = flag.Int("port", 10000, "The server port")
    57  )
    58  
    59  type testServer struct {
    60  }
    61  
    62  func (s *testServer) EmptyCall(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
    63  	return new(testpb.Empty), nil
    64  }
    65  
    66  func newPayload(t testpb.PayloadType, size int32) (*testpb.Payload, error) {
    67  	if size < 0 {
    68  		return nil, fmt.Errorf("requested a response with invalid length %d", size)
    69  	}
    70  	body := make([]byte, size)
    71  	switch t {
    72  	case testpb.PayloadType_COMPRESSABLE:
    73  	case testpb.PayloadType_UNCOMPRESSABLE:
    74  		return nil, fmt.Errorf("payloadType UNCOMPRESSABLE is not supported")
    75  	default:
    76  		return nil, fmt.Errorf("unsupported payload type: %d", t)
    77  	}
    78  	return &testpb.Payload{
    79  		Type: t.Enum(),
    80  		Body: body,
    81  	}, nil
    82  }
    83  
    84  func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
    85  	pl, err := newPayload(in.GetResponseType(), in.GetResponseSize())
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	return &testpb.SimpleResponse{
    90  		Payload: pl,
    91  	}, nil
    92  }
    93  
    94  func (s *testServer) StreamingOutputCall(args *testpb.StreamingOutputCallRequest, stream testpb.TestService_StreamingOutputCallServer) error {
    95  	cs := args.GetResponseParameters()
    96  	for _, c := range cs {
    97  		if us := c.GetIntervalUs(); us > 0 {
    98  			time.Sleep(time.Duration(us) * time.Microsecond)
    99  		}
   100  		pl, err := newPayload(args.GetResponseType(), c.GetSize())
   101  		if err != nil {
   102  			return err
   103  		}
   104  		if err := stream.Send(&testpb.StreamingOutputCallResponse{
   105  			Payload: pl,
   106  		}); err != nil {
   107  			return err
   108  		}
   109  	}
   110  	return nil
   111  }
   112  
   113  func (s *testServer) StreamingInputCall(stream testpb.TestService_StreamingInputCallServer) error {
   114  	var sum int
   115  	for {
   116  		in, err := stream.Recv()
   117  		if err == io.EOF {
   118  			return stream.SendAndClose(&testpb.StreamingInputCallResponse{
   119  				AggregatedPayloadSize: proto.Int32(int32(sum)),
   120  			})
   121  		}
   122  		if err != nil {
   123  			return err
   124  		}
   125  		p := in.GetPayload().GetBody()
   126  		sum += len(p)
   127  	}
   128  }
   129  
   130  func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServer) error {
   131  	for {
   132  		in, err := stream.Recv()
   133  		if err == io.EOF {
   134  			// read done.
   135  			return nil
   136  		}
   137  		if err != nil {
   138  			return err
   139  		}
   140  		cs := in.GetResponseParameters()
   141  		for _, c := range cs {
   142  			if us := c.GetIntervalUs(); us > 0 {
   143  				time.Sleep(time.Duration(us) * time.Microsecond)
   144  			}
   145  			pl, err := newPayload(in.GetResponseType(), c.GetSize())
   146  			if err != nil {
   147  				return err
   148  			}
   149  			if err := stream.Send(&testpb.StreamingOutputCallResponse{
   150  				Payload: pl,
   151  			}); err != nil {
   152  				return err
   153  			}
   154  		}
   155  	}
   156  }
   157  
   158  func (s *testServer) HalfDuplexCall(stream testpb.TestService_HalfDuplexCallServer) error {
   159  	var msgBuf []*testpb.StreamingOutputCallRequest
   160  	for {
   161  		in, err := stream.Recv()
   162  		if err == io.EOF {
   163  			// read done.
   164  			break
   165  		}
   166  		if err != nil {
   167  			return err
   168  		}
   169  		msgBuf = append(msgBuf, in)
   170  	}
   171  	for _, m := range msgBuf {
   172  		cs := m.GetResponseParameters()
   173  		for _, c := range cs {
   174  			if us := c.GetIntervalUs(); us > 0 {
   175  				time.Sleep(time.Duration(us) * time.Microsecond)
   176  			}
   177  			pl, err := newPayload(m.GetResponseType(), c.GetSize())
   178  			if err != nil {
   179  				return err
   180  			}
   181  			if err := stream.Send(&testpb.StreamingOutputCallResponse{
   182  				Payload: pl,
   183  			}); err != nil {
   184  				return err
   185  			}
   186  		}
   187  	}
   188  	return nil
   189  }
   190  
   191  func main() {
   192  	flag.Parse()
   193  	p := strconv.Itoa(*port)
   194  	lis, err := net.Listen("tcp", ":"+p)
   195  	if err != nil {
   196  		grpclog.Fatalf("failed to listen: %v", err)
   197  	}
   198  	var opts []grpc.ServerOption
   199  	if *useTLS {
   200  		creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
   201  		if err != nil {
   202  			grpclog.Fatalf("Failed to generate credentials %v", err)
   203  		}
   204  		opts = []grpc.ServerOption{grpc.Creds(creds)}
   205  	}
   206  	server := grpc.NewServer(opts...)
   207  	testpb.RegisterTestServiceServer(server, &testServer{})
   208  	server.Serve(lis)
   209  }