(stream pb.Echo_BidirectionalStreamingEchoServer)
| 154 | } |
| 155 | |
| 156 | func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error { |
| 157 | fmt.Printf("--- BidirectionalStreamingEcho ---\n") |
| 158 | // Create trailer in defer to record function return time. |
| 159 | defer func() { |
| 160 | trailer := metadata.Pairs("timestamp", time.Now().Format(timestampFormat)) |
| 161 | stream.SetTrailer(trailer) |
| 162 | }() |
| 163 | |
| 164 | // Read metadata from client. |
| 165 | md, ok := metadata.FromIncomingContext(stream.Context()) |
| 166 | if !ok { |
| 167 | return status.Errorf(codes.DataLoss, "BidirectionalStreamingEcho: failed to get metadata") |
| 168 | } |
| 169 | |
| 170 | if t, ok := md["timestamp"]; ok { |
| 171 | fmt.Printf("timestamp from metadata:\n") |
| 172 | for i, e := range t { |
| 173 | fmt.Printf(" %d. %s\n", i, e) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Create and send header. |
| 178 | header := metadata.New(map[string]string{"location": "MTV", "timestamp": time.Now().Format(timestampFormat)}) |
| 179 | stream.SendHeader(header) |
| 180 | |
| 181 | // Read requests and send responses. |
| 182 | for { |
| 183 | in, err := stream.Recv() |
| 184 | if err == io.EOF { |
| 185 | return nil |
| 186 | } |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | fmt.Printf("request received %v, sending echo\n", in) |
| 191 | if err := stream.Send(&pb.EchoResponse{Message: in.Message}); err != nil { |
| 192 | return err |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func main() { |
| 198 | flag.Parse() |
nothing calls this directly
no test coverage detected