blob: 54676a1180ed257d82678ca09fe3fc7b9d2e9e77 [file] [log] [blame]
David Reiss5ddabb82010-10-06 17:09:37 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include "TEvhttpClientChannel.h"
21#include <evhttp.h>
22
23namespace apache { namespace thrift { namespace async {
24
25
26TEvhttpClientChannel::TEvhttpClientChannel(
27 const std::string& host,
28 const std::string& path,
29 const char* address,
30 int port,
31 struct event_base* eb)
32 : host_(host)
33 , path_(path)
34 , recvBuf_(NULL)
35 , conn_(NULL)
36{
37 conn_ = evhttp_connection_new(address, port);
38 if (conn_ == NULL) {
39 abort(); // XXX
40 }
41 evhttp_connection_set_base(conn_, eb);
42}
43
44
45TEvhttpClientChannel::~TEvhttpClientChannel() {
46 if (conn_ != NULL) {
47 evhttp_connection_free(conn_);
48 }
49}
50
51
52bool TEvhttpClientChannel::sendAndRecvMessage(
53 const VoidCallback& cob,
54 apache::thrift::transport::TMemoryBuffer* sendBuf,
55 apache::thrift::transport::TMemoryBuffer* recvBuf) {
56 cob_ = cob;
57 recvBuf_ = recvBuf;
58
59 struct evhttp_request* req = evhttp_request_new(response, this);
60 if (req == NULL) {
61 abort(); // XXX
62 }
63
64 int rv;
65
66 rv = evhttp_add_header(req->output_headers, "Host", host_.c_str());
67 if (rv != 0) {
68 abort(); // XXX
69 }
70
71 rv = evhttp_add_header(req->output_headers, "Content-Type", "application/x-thrift");
72 if (rv != 0) {
73 abort(); // XXX
74 }
75
76 uint8_t* obuf;
77 uint32_t sz;
78 sendBuf->getBuffer(&obuf, &sz);
79 rv = evbuffer_add(req->output_buffer, obuf, sz);
80 if (rv != 0) {
81 abort(); // XXX
82 }
83
84 rv = evhttp_make_request(conn_, req, EVHTTP_REQ_POST, path_.c_str());
85 if (rv != 0) {
86 abort(); // XXX
87 }
88
89 return true;
90}
91
92
93bool TEvhttpClientChannel::sendMessage(
94 const VoidCallback& cob, apache::thrift::transport::TMemoryBuffer* message) {
95 abort(); // XXX
96}
97
98
99bool TEvhttpClientChannel::recvMessage(
100 const VoidCallback& cob, apache::thrift::transport::TMemoryBuffer* message) {
101 abort(); // XXX
102}
103
104
105void TEvhttpClientChannel::finish(struct evhttp_request* req) {
106 if (req == NULL) {
107 return cob_();
108 } else if (req->response_code != 200) {
109 return cob_();
110 }
111 recvBuf_->resetBuffer(
112 EVBUFFER_DATA(req->input_buffer),
113 EVBUFFER_LENGTH(req->input_buffer));
114 return cob_();
115}
116
117
118/* static */ void TEvhttpClientChannel::response(struct evhttp_request* req, void* arg) {
119 TEvhttpClientChannel* self = (TEvhttpClientChannel*)arg;
120 self->finish(req);
121}
122
123
124}}} // apache::thrift::async