wildcloud-router 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -0
- data/LICENSE +174 -0
- data/README.md +7 -0
- data/bin/wildcloud-router +27 -0
- data/lib/wildcloud/router.rb +13 -0
- data/lib/wildcloud/router/client.rb +128 -0
- data/lib/wildcloud/router/configuration.rb +30 -0
- data/lib/wildcloud/router/core.rb +106 -0
- data/lib/wildcloud/router/logger.rb +38 -0
- data/lib/wildcloud/router/proxy.rb +86 -0
- data/lib/wildcloud/router/server.rb +34 -0
- data/lib/wildcloud/router/version.rb +19 -0
- metadata +103 -0
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright 2011 Marek Jelen
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
$: << File.expand_path('../../lib', __FILE__)
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
|
21
|
+
require 'eventmachine'
|
22
|
+
|
23
|
+
require 'wildcloud/router/server'
|
24
|
+
|
25
|
+
EventMachine.run do
|
26
|
+
Wildcloud::Router::Server.start
|
27
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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.
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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
|
+
require 'http/parser'
|
16
|
+
|
17
|
+
module Wildcloud
|
18
|
+
module Router
|
19
|
+
module Client
|
20
|
+
|
21
|
+
def initialize(proxy)
|
22
|
+
@proxy = proxy
|
23
|
+
@closed = false
|
24
|
+
@parser = Http::Parser.new(self)
|
25
|
+
@buffer = []
|
26
|
+
@time = Time.now
|
27
|
+
end
|
28
|
+
|
29
|
+
def closed?
|
30
|
+
@closed
|
31
|
+
end
|
32
|
+
|
33
|
+
def post_init
|
34
|
+
send_line("#{@proxy.request_method} #{@proxy.request_url} HTTP/#{@proxy.request_version}")
|
35
|
+
@proxy.request_headers.each do |name, value|
|
36
|
+
send_line("#{name}: #{value}")
|
37
|
+
end
|
38
|
+
send_line('')
|
39
|
+
send_data(@proxy.buffer)
|
40
|
+
EventMachine.enable_proxy(@proxy, self)
|
41
|
+
end
|
42
|
+
|
43
|
+
def receive_data(data)
|
44
|
+
@parser << data
|
45
|
+
rescue Http::Parser::Error => error
|
46
|
+
Router.logger.debug('Proxy client') { "Error #{error}" }
|
47
|
+
end
|
48
|
+
|
49
|
+
def on_headers_complete(headers)
|
50
|
+
@proxy.send_line("HTTP/#{@parser.http_version.join('.')} #{@parser.status_code} #{CODE[@parser.status_code]}")
|
51
|
+
headers.each do |name, value|
|
52
|
+
@proxy.send_line("#{name}: #{value}")
|
53
|
+
end
|
54
|
+
@proxy.send_line('')
|
55
|
+
EventMachine.enable_proxy(self, @proxy)
|
56
|
+
end
|
57
|
+
|
58
|
+
def unbind
|
59
|
+
@closed = true
|
60
|
+
@proxy.close_connection(true) if @proxy and !@proxy.closed?
|
61
|
+
end
|
62
|
+
|
63
|
+
def on_body(chunk)
|
64
|
+
@proxy.send_data(chunk)
|
65
|
+
end
|
66
|
+
|
67
|
+
def send_line(data)
|
68
|
+
send_data("#{data}\r\n")
|
69
|
+
end
|
70
|
+
|
71
|
+
# Borrowed from em-http-request
|
72
|
+
CODE = {
|
73
|
+
100 => 'Continue',
|
74
|
+
101 => 'Switching Protocols',
|
75
|
+
102 => 'Processing',
|
76
|
+
200 => 'OK',
|
77
|
+
201 => 'Created',
|
78
|
+
202 => 'Accepted',
|
79
|
+
203 => 'Non-Authoritative Information',
|
80
|
+
204 => 'No Content',
|
81
|
+
205 => 'Reset Content',
|
82
|
+
206 => 'Partial Content',
|
83
|
+
207 => 'Multi-Status',
|
84
|
+
226 => 'IM Used',
|
85
|
+
300 => 'Multiple Choices',
|
86
|
+
301 => 'Moved Permanently',
|
87
|
+
302 => 'Found',
|
88
|
+
303 => 'See Other',
|
89
|
+
304 => 'Not Modified',
|
90
|
+
305 => 'Use Proxy',
|
91
|
+
306 => 'Reserved',
|
92
|
+
307 => 'Temporary Redirect',
|
93
|
+
400 => 'Bad Request',
|
94
|
+
401 => 'Unauthorized',
|
95
|
+
402 => 'Payment Required',
|
96
|
+
403 => 'Forbidden',
|
97
|
+
404 => 'Not Found',
|
98
|
+
405 => 'Method Not Allowed',
|
99
|
+
406 => 'Not Acceptable',
|
100
|
+
407 => 'Proxy Authentication Required',
|
101
|
+
408 => 'Request Timeout',
|
102
|
+
409 => 'Conflict',
|
103
|
+
410 => 'Gone',
|
104
|
+
411 => 'Length Required',
|
105
|
+
412 => 'Precondition Failed',
|
106
|
+
413 => 'Request Entity Too Large',
|
107
|
+
414 => 'Request-URI Too Long',
|
108
|
+
415 => 'Unsupported Media Type',
|
109
|
+
416 => 'Requested Range Not Satisfiable',
|
110
|
+
417 => 'Expectation Failed',
|
111
|
+
422 => 'Unprocessable Entity',
|
112
|
+
423 => 'Locked',
|
113
|
+
424 => 'Failed Dependency',
|
114
|
+
426 => 'Upgrade Required',
|
115
|
+
500 => 'Internal Server Error',
|
116
|
+
501 => 'Not Implemented',
|
117
|
+
502 => 'Bad Gateway',
|
118
|
+
503 => 'Service Unavailable',
|
119
|
+
504 => 'Gateway Timeout',
|
120
|
+
505 => 'HTTP Version Not Supported',
|
121
|
+
506 => 'Variant Also Negotiates',
|
122
|
+
507 => 'Insufficient Storage',
|
123
|
+
510 => 'Not Extended'
|
124
|
+
}
|
125
|
+
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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
|
+
require 'yaml'
|
15
|
+
|
16
|
+
require 'wildcloud/router/logger'
|
17
|
+
|
18
|
+
module Wildcloud
|
19
|
+
module Router
|
20
|
+
def self.configuration
|
21
|
+
return @configuration if @configuration
|
22
|
+
file = '/etc/wildcloud/router.yml'
|
23
|
+
unless File.exists?(file)
|
24
|
+
file = './router.yml'
|
25
|
+
end
|
26
|
+
Router.logger.info('Config') { "Loading from file #{file}" }
|
27
|
+
@configuration = YAML.load_file(file)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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
|
+
require 'amqp'
|
16
|
+
require 'singleton'
|
17
|
+
|
18
|
+
module Wildcloud
|
19
|
+
module Router
|
20
|
+
class Core
|
21
|
+
|
22
|
+
include Singleton
|
23
|
+
|
24
|
+
attr_reader :amqp
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@counter = {}
|
28
|
+
@routes = {}
|
29
|
+
|
30
|
+
Router.logger.info('Core') { "Starting" }
|
31
|
+
connect_amqp
|
32
|
+
|
33
|
+
Router.logger.info('Core') { "Requesting synchronization" }
|
34
|
+
publish({ :node => Router.configuration["node"]["name"], :type => :sync })
|
35
|
+
|
36
|
+
@queue.subscribe do |metadata, message|
|
37
|
+
handle(metadata, message)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def connect_amqp
|
42
|
+
Router.logger.info('Core') { "Connecting to broker" }
|
43
|
+
@amqp = AMQP.connect(Router.configuration["amqp"])
|
44
|
+
@channel = AMQP::Channel.new(@amqp)
|
45
|
+
# Communication infrastructure
|
46
|
+
@topic = @channel.topic('wildcloud.router')
|
47
|
+
@queue = @channel.queue("wildcloud.router.#{Router.configuration["node"]["name"]}")
|
48
|
+
@queue.bind(@topic, :routing_key => "nodes")
|
49
|
+
@queue.bind(@topic, :routing_key => "node.#{Router.configuration["node"]["name"]}")
|
50
|
+
end
|
51
|
+
|
52
|
+
def handle(metadata, message)
|
53
|
+
Router.logger.debug('Core') { "Got message: #{message}" }
|
54
|
+
message = ::JSON.parse(message)
|
55
|
+
method = "handle_#{message["type"]}".to_sym
|
56
|
+
if respond_to?(method)
|
57
|
+
send(method, message)
|
58
|
+
else
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def handle_sync(data)
|
64
|
+
@routes = data['routes']
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse_target(target)
|
68
|
+
target = target.split(':')
|
69
|
+
if target.size == 1
|
70
|
+
{ "socket" => target[0] }
|
71
|
+
else
|
72
|
+
{ "address" => target[0], "port" => target[1] }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def handle_add_route(data)
|
77
|
+
host = data['host']
|
78
|
+
( @routes[host] ||= [] ) << parse_target(data['target'])
|
79
|
+
Router.logger.debug('Core') { "Routes: #{@routes.inspect}" }
|
80
|
+
end
|
81
|
+
|
82
|
+
def handle_remove_route(data)
|
83
|
+
host = data['host']
|
84
|
+
return unless @routes[host]
|
85
|
+
@routes[host].delete(parse_target(data['target']))
|
86
|
+
Router.logger.debug('Core') { "Routes: #{@routes.inspect}" }
|
87
|
+
end
|
88
|
+
|
89
|
+
def resolve(host)
|
90
|
+
host << ':80' unless host.index(':')
|
91
|
+
return nil unless @routes[host]
|
92
|
+
@counter[host] ||= 0
|
93
|
+
res = @routes[host][@counter[host]]
|
94
|
+
@counter[host] += 1
|
95
|
+
@counter[host] = 0 if @counter[host] == @routes[host].size
|
96
|
+
res
|
97
|
+
end
|
98
|
+
|
99
|
+
def publish(message)
|
100
|
+
Router.logger.debug('Core') { "Publishing #{message.inspect}" }
|
101
|
+
@topic.publish(::JSON.dump(message), :routing_key => 'master')
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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
|
+
require 'wildcloud/logger'
|
16
|
+
require 'wildcloud/logger/middleware/console'
|
17
|
+
require 'wildcloud/logger/middleware/amqp'
|
18
|
+
require 'wildcloud/logger/middleware/json'
|
19
|
+
|
20
|
+
require 'json'
|
21
|
+
|
22
|
+
module Wildcloud
|
23
|
+
module Router
|
24
|
+
|
25
|
+
def self.logger
|
26
|
+
unless @logger
|
27
|
+
@logger = Wildcloud::Logger::Logger.new
|
28
|
+
@logger.application = 'wildcloud.router'
|
29
|
+
@logger.add(Wildcloud::Logger::Middleware::Console)
|
30
|
+
@logger.add(Wildcloud::Logger::Middleware::Json)
|
31
|
+
@topic = AMQP::Channel.new(Core.instance.amqp).topic('wildcloud.logger')
|
32
|
+
@logger.add(Wildcloud::Logger::Middleware::Amqp, :exchange => @topic, :routing_key => 'wildcloud.router')
|
33
|
+
end
|
34
|
+
@logger
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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
|
+
require 'http/parser'
|
16
|
+
|
17
|
+
require 'wildcloud/router/client'
|
18
|
+
|
19
|
+
module Wildcloud
|
20
|
+
module Router
|
21
|
+
module Proxy
|
22
|
+
|
23
|
+
attr_reader :core, :request_headers, :buffer, :request_version, :request_method, :request_url
|
24
|
+
|
25
|
+
def initialize(core)
|
26
|
+
@core = core
|
27
|
+
@parser = Http::Parser.new(self)
|
28
|
+
@buffer = []
|
29
|
+
@closed = false
|
30
|
+
@time = Time.now
|
31
|
+
@request_headers = nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def post_init
|
35
|
+
end
|
36
|
+
|
37
|
+
def closed?
|
38
|
+
@closed
|
39
|
+
end
|
40
|
+
|
41
|
+
def receive_data(data)
|
42
|
+
offset = @parser << data
|
43
|
+
if @request_headers
|
44
|
+
@buffer = data[offset..-1]
|
45
|
+
if @target["socket"]
|
46
|
+
@client = EventMachine.connect(@target["socket"], Router::Client, self)
|
47
|
+
else
|
48
|
+
@client = EventMachine.connect(@target["address"], @target["port"], Router::Client, self)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
rescue HTTP::Parser::Error => error
|
52
|
+
Router.logger.debug('Proxy') { "Error during parsing #{error.message}" }
|
53
|
+
end
|
54
|
+
|
55
|
+
def unbind
|
56
|
+
@closed = true
|
57
|
+
@client.close_connection(true) if @client and !@client.closed?
|
58
|
+
end
|
59
|
+
|
60
|
+
def on_headers_complete(headers)
|
61
|
+
@request_headers = headers
|
62
|
+
@request_version = @parser.http_version.join('.')
|
63
|
+
@request_method = @parser.http_method
|
64
|
+
@request_url = @parser.request_url
|
65
|
+
return bad_request unless @request_headers['Host']
|
66
|
+
@target = @core.resolve(@request_headers['Host'])
|
67
|
+
return bad_request unless @target
|
68
|
+
:stop
|
69
|
+
end
|
70
|
+
|
71
|
+
def bad_request
|
72
|
+
close_connection(true)
|
73
|
+
:stop
|
74
|
+
end
|
75
|
+
|
76
|
+
def on_body(chunk)
|
77
|
+
@buffer << chunk
|
78
|
+
end
|
79
|
+
|
80
|
+
def send_line(data)
|
81
|
+
send_data("#{data}\r\n")
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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
|
+
require 'wildcloud/router/configuration'
|
16
|
+
require 'wildcloud/router/logger'
|
17
|
+
require 'wildcloud/router/core'
|
18
|
+
require 'wildcloud/router/proxy'
|
19
|
+
|
20
|
+
module Wildcloud
|
21
|
+
module Router
|
22
|
+
class Server
|
23
|
+
|
24
|
+
def self.start
|
25
|
+
Router.logger.info('Server') { 'Starting' }
|
26
|
+
addr = Router.configuration['http']['address']
|
27
|
+
port = Router.configuration['http']['port']
|
28
|
+
EventMachine.start_server(addr, port, Router::Proxy, Wildcloud::Router::Core.instance)
|
29
|
+
Router.logger.info('Server') { 'Started' }
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Copyright 2011 Marek Jelen
|
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
|
+
module Wildcloud
|
16
|
+
module Router
|
17
|
+
VERSION = '0.0.2' unless const_defined?(:VERSION)
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wildcloud-router
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marek Jelen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: amqp
|
16
|
+
requirement: &2161097640 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - =
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.4
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2161097640
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: http_parser.rb
|
27
|
+
requirement: &2161097140 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - =
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.5.3
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2161097140
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: json
|
38
|
+
requirement: &2161096640 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - =
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.6.4
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2161096640
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: wildcloud-logger
|
49
|
+
requirement: &2161096160 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - =
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.0.1
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2161096160
|
58
|
+
description: Routes requests across the platform
|
59
|
+
email:
|
60
|
+
- marek@jelen.biz
|
61
|
+
executables:
|
62
|
+
- wildcloud-router
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- bin/wildcloud-router
|
67
|
+
- lib/wildcloud/router/client.rb
|
68
|
+
- lib/wildcloud/router/configuration.rb
|
69
|
+
- lib/wildcloud/router/core.rb
|
70
|
+
- lib/wildcloud/router/logger.rb
|
71
|
+
- lib/wildcloud/router/proxy.rb
|
72
|
+
- lib/wildcloud/router/server.rb
|
73
|
+
- lib/wildcloud/router/version.rb
|
74
|
+
- lib/wildcloud/router.rb
|
75
|
+
- LICENSE
|
76
|
+
- README.md
|
77
|
+
- CHANGELOG.md
|
78
|
+
homepage: http://github.com/wildcloud
|
79
|
+
licenses:
|
80
|
+
- Apache2
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.6
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.8.10
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Routing component of Wildcloud
|
103
|
+
test_files: []
|