TensorFlow Serving C++ API Documentation
evhttp_connection.h
1 /* Copyright 2018 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_SERVING_UTIL_NET_HTTP_CLIENT_TEST_CLIENT_INTERNAL_EVHTTP_CONNECTION_H_
17 #define TENSORFLOW_SERVING_UTIL_NET_HTTP_CLIENT_TEST_CLIENT_INTERNAL_EVHTTP_CONNECTION_H_
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/strings/string_view.h"
26 #include "absl/synchronization/notification.h"
27 #include "libevent/include/event2/buffer.h"
28 #include "libevent/include/event2/bufferevent.h"
29 #include "libevent/include/event2/event.h"
30 #include "libevent/include/event2/http.h"
31 #include "libevent/include/event2/keyvalq_struct.h"
32 #include "libevent/include/event2/util.h"
33 
34 // TODO(wenboz): move EventExecutor to net_http/common
35 #include "tensorflow_serving/util/net_http/client/test_client/public/httpclient_interface.h"
36 #include "tensorflow_serving/util/net_http/server/public/httpserver_interface.h"
37 
38 namespace tensorflow {
39 namespace serving {
40 namespace net_http {
41 
42 // The following types may be moved to an API interface in future.
43 
45  public:
46  TestEvHTTPConnection() = default;
47 
48  ~TestEvHTTPConnection() override;
49 
50  TestEvHTTPConnection(const TestEvHTTPConnection& other) = delete;
51  TestEvHTTPConnection& operator=(const TestEvHTTPConnection& other) = delete;
52 
53  // Terminates the connection.
54  void Terminate() override;
55 
56  // Returns a new connection given an absolute URL.
57  // Always treat the URL scheme as "http" for now.
58  // Returns nullptr if any error
59  static std::unique_ptr<TestEvHTTPConnection> Connect(absl::string_view url);
60 
61  // Returns a new connection to the specified host:port.
62  // Returns nullptr if any error
63  static std::unique_ptr<TestEvHTTPConnection> Connect(absl::string_view host,
64  int port);
65 
66  // Returns a new connection to the specified port of localhost.
67  // Returns nullptr if any error
68  static std::unique_ptr<TestEvHTTPConnection> ConnectLocal(int port) {
69  return Connect("localhost", port);
70  }
71 
72  // Sends a request and blocks the caller till a response is received
73  // or any error has happened.
74  // Returns false if any error.
75  bool BlockingSendRequest(const TestClientRequest& request,
76  TestClientResponse* response) override;
77 
78  // Sends a request and returns immediately. The response will be handled
79  // asynchronously via the response->done callback.
80  // Returns false if any error in sending the request, or if the executor
81  // has not been configured.
82  bool SendRequest(const TestClientRequest& request,
83  TestClientResponse* response) override;
84 
85  // Sets the executor for processing requests asynchronously.
86  void SetExecutor(std::unique_ptr<EventExecutor> executor) override;
87 
88  private:
89  struct event_base* ev_base_;
90  struct evhttp_uri* http_uri_;
91  struct evhttp_connection* evcon_;
92 
93  std::unique_ptr<EventExecutor> executor_;
94 
95  std::unique_ptr<absl::Notification> loop_exit_;
96 };
97 
98 } // namespace net_http
99 } // namespace serving
100 } // namespace tensorflow
101 
102 #endif // TENSORFLOW_SERVING_UTIL_NET_HTTP_CLIENT_TEST_CLIENT_INTERNAL_EVHTTP_CONNECTION_H_