-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.hpp
More file actions
157 lines (120 loc) · 4.08 KB
/
proxy.hpp
File metadata and controls
157 lines (120 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright (c) 2013 Andrey Goryachev <andrey.goryachev@gmail.com>
Copyright (c) 2011-2013 Other contributors as noted in the AUTHORS file.
This file is part of Cocaine.
Cocaine is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Cocaine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef COCAINE_NATIVE_PROXY_HPP
#define COCAINE_NATIVE_PROXY_HPP
#include "service_pool.hpp"
#include <cocaine/framework/services/app.hpp>
#include <thevoid/server.hpp>
namespace cocaine { namespace proxy {
class proxy :
public ioremap::thevoid::server<proxy>
{
public:
struct on_enqueue;
struct response_stream :
public cocaine::framework::basic_stream<std::string>,
public std::enable_shared_from_this<response_stream>
{
response_stream(const std::shared_ptr<on_enqueue>& req);
virtual
void
write(std::string&& chunk);
virtual
void
error(const std::exception_ptr& e) {
std::unique_lock<std::mutex> guard(m_access_mutex);
error(e, guard);
}
virtual
void
close() {
std::unique_lock<std::mutex> guard(m_access_mutex);
close(boost::system::error_code(), guard);
}
virtual
bool
closed() const {
return m_closed;
}
private:
void
write_headers(std::string&& packed, const std::unique_lock<std::mutex>&);
void
write_body(std::string&& packed, const std::unique_lock<std::mutex>&);
void
error(const std::exception_ptr& e, const std::unique_lock<std::mutex>&);
void
close(const boost::system::error_code& ec, const std::unique_lock<std::mutex>&);
void
on_error(const boost::system::error_code &err);
private:
std::weak_ptr<cocaine::framework::logger_t> m_logger;
std::shared_ptr<on_enqueue> m_request;
bool m_chunked;
size_t m_content_length;
bool m_body;
std::atomic<bool> m_closed;
std::mutex m_access_mutex;
};
friend struct response_stream;
struct on_enqueue :
public ioremap::thevoid::simple_request_stream<proxy>,
public std::enable_shared_from_this<on_enqueue>
{
friend struct response_stream;
virtual
void
on_request(const ioremap::thevoid::http_request &req,
const boost::asio::const_buffer &buffer);
const std::string&
app() const {
return m_application;
}
const std::string&
event() const {
return m_event;
}
private:
std::string m_application;
std::string m_event;
};
friend struct on_enqueue;
struct on_ping :
public ioremap::thevoid::simple_request_stream<proxy>
{
virtual
void
on_request(const ioremap::thevoid::http_request &req,
const boost::asio::const_buffer &buffer);
};
public:
bool
initialize(const rapidjson::Value &config);
~proxy();
std::map<std::string, std::string>
get_statistics() const;
private:
typedef std::map<std::string, std::shared_ptr<service_pool<cocaine::framework::app_service_t>>>
clients_map_t;
size_t m_pool_size;
unsigned int m_reconnect_timeout;
unsigned int m_request_timeout;
std::shared_ptr<cocaine::framework::service_manager_t> m_service_manager;
clients_map_t m_services;
mutable std::mutex m_services_mutex;
};
}} // namespace cocaine::proxy
#endif // COCAINE_NATIVE_PROXY_HPP