miu 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -1
- data/Rakefile +1 -1
- data/lib/miu.rb +14 -14
- data/lib/miu/cli.rb +60 -19
- data/lib/miu/command.rb +30 -3
- data/lib/miu/dsl.rb +45 -0
- data/lib/miu/forwarder.rb +1 -1
- data/lib/miu/logger.rb +1 -0
- data/lib/miu/messages.rb +3 -0
- data/lib/miu/messages/base.rb +4 -7
- data/lib/miu/messages/enter.rb +16 -0
- data/lib/miu/messages/leave.rb +16 -0
- data/lib/miu/packet.rb +4 -0
- data/lib/miu/proxy.rb +3 -14
- data/lib/miu/publisher.rb +3 -3
- data/lib/miu/{subscribable.rb → readable.rb} +1 -1
- data/lib/miu/resources.rb +2 -0
- data/lib/miu/resources/base.rb +2 -2
- data/lib/miu/resources/content.rb +2 -1
- data/lib/miu/resources/enter_content.rb +22 -0
- data/lib/miu/resources/leave_content.rb +22 -0
- data/lib/miu/resources/network.rb +1 -1
- data/lib/miu/resources/room.rb +1 -1
- data/lib/miu/resources/text_content.rb +3 -3
- data/lib/miu/resources/user.rb +1 -1
- data/lib/miu/server.rb +14 -7
- data/lib/miu/{socket.rb → sockets.rb} +59 -4
- data/lib/miu/subscriber.rb +3 -3
- data/lib/miu/templates/config/miu.god +7 -0
- data/lib/miu/version.rb +1 -1
- data/lib/miu/{publishable.rb → writable.rb} +1 -1
- data/miu.gemspec +21 -20
- data/spec/miu/dsl_spec.rb +31 -0
- data/spec/miu/messages/base_spec.rb +2 -2
- data/spec/miu/messages/enter_spec.rb +35 -0
- data/spec/miu/messages/leave_spec.rb +35 -0
- data/spec/miu/nodes_spec.rb +4 -0
- data/spec/miu/publisher_spec.rb +0 -9
- data/spec/miu/{subscribable_spec.rb → readable_spec.rb} +2 -2
- data/spec/miu/sockets_spec.rb +97 -0
- data/spec/miu/subscriber_spec.rb +0 -9
- data/spec/miu/{publishable_spec.rb → writable_spec.rb} +2 -2
- data/spec/spec_helper.rb +2 -0
- data/spec/support/socket.rb +36 -0
- metadata +51 -55
data/lib/miu/publisher.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require 'miu/
|
2
|
-
require 'miu/
|
1
|
+
require 'miu/sockets'
|
2
|
+
require 'miu/writable'
|
3
3
|
require 'miu/utility'
|
4
4
|
|
5
5
|
module Miu
|
@@ -12,7 +12,7 @@ module Miu
|
|
12
12
|
socket = options[:socket] || PubSocket
|
13
13
|
|
14
14
|
klass = Class.new(socket, &block)
|
15
|
-
klass.send :include,
|
15
|
+
klass.send :include, Writable
|
16
16
|
klass.send :include, self
|
17
17
|
|
18
18
|
klass.new.tap do |pub|
|
data/lib/miu/resources.rb
CHANGED
@@ -6,5 +6,7 @@ module Miu
|
|
6
6
|
autoload :User, 'miu/resources/user'
|
7
7
|
autoload :Content, 'miu/resources/content'
|
8
8
|
autoload :TextContent, 'miu/resources/text_content'
|
9
|
+
autoload :EnterContent, 'miu/resources/enter_content'
|
10
|
+
autoload :LeaveContent, 'miu/resources/leave_content'
|
9
11
|
end
|
10
12
|
end
|
data/lib/miu/resources/base.rb
CHANGED
@@ -3,13 +3,14 @@ require 'miu/resources'
|
|
3
3
|
module Miu
|
4
4
|
module Resources
|
5
5
|
class Content < Base
|
6
|
+
attr_accessor :raw
|
6
7
|
attr_accessor :meta
|
7
8
|
|
8
9
|
def initialize(options = {})
|
9
10
|
@meta = options[:meta] || {}
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
+
def to_h
|
13
14
|
super.merge({:meta => @meta})
|
14
15
|
end
|
15
16
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'miu/resources'
|
2
|
+
|
3
|
+
module Miu
|
4
|
+
module Resources
|
5
|
+
class EnterContent < Content
|
6
|
+
attr_accessor :room, :user
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@room = Miu::Utility.adapt(Room, options[:room] || {})
|
10
|
+
@user = Miu::Utility.adapt(User, options[:user] || {})
|
11
|
+
super options
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_h
|
15
|
+
super.merge({
|
16
|
+
:room => @room.to_h,
|
17
|
+
:user => @user.to_h,
|
18
|
+
})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'miu/resources'
|
2
|
+
|
3
|
+
module Miu
|
4
|
+
module Resources
|
5
|
+
class LeaveContent < Content
|
6
|
+
attr_accessor :room, :user
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@room = Miu::Utility.adapt(Room, options[:room] || {})
|
10
|
+
@user = Miu::Utility.adapt(User, options[:user] || {})
|
11
|
+
super options
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_h
|
15
|
+
super.merge({
|
16
|
+
:room => @room.to_h,
|
17
|
+
:user => @user.to_h,
|
18
|
+
})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/miu/resources/room.rb
CHANGED
data/lib/miu/resources/user.rb
CHANGED
data/lib/miu/server.rb
CHANGED
@@ -15,13 +15,7 @@ module Miu
|
|
15
15
|
Logger.info "Publish on #{@options[:pub_host]}:#{@options[:pub_port]}"
|
16
16
|
Logger.info "Subscribe on #{@options[:sub_host]}:#{@options[:sub_port]}"
|
17
17
|
|
18
|
-
|
19
|
-
trap(sig) do
|
20
|
-
Logger.info "Quit"
|
21
|
-
close
|
22
|
-
exit
|
23
|
-
end
|
24
|
-
end
|
18
|
+
register_signal_handlers
|
25
19
|
|
26
20
|
@forwarder = Forwarder.new @options
|
27
21
|
@forwarder.run
|
@@ -32,5 +26,18 @@ module Miu
|
|
32
26
|
def close
|
33
27
|
@forwarder.close
|
34
28
|
end
|
29
|
+
|
30
|
+
def register_signal_handlers
|
31
|
+
%w(INT TERM HUP QUIT).each do |sig|
|
32
|
+
trap(sig) do
|
33
|
+
close
|
34
|
+
exit
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
at_exit do
|
39
|
+
Logger.info 'Quit...'
|
40
|
+
end
|
41
|
+
end
|
35
42
|
end
|
36
43
|
end
|
@@ -5,10 +5,13 @@ require 'forwardable'
|
|
5
5
|
module Miu
|
6
6
|
class Socket
|
7
7
|
class << self
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
def inherited(base)
|
9
|
+
base.socket_type socket_type
|
10
|
+
end
|
11
|
+
|
12
|
+
def socket_type(type = nil)
|
13
|
+
@socket_type = type if type
|
14
|
+
@socket_type
|
12
15
|
end
|
13
16
|
|
14
17
|
def build_address(*args)
|
@@ -26,6 +29,10 @@ module Miu
|
|
26
29
|
@linger = 0
|
27
30
|
end
|
28
31
|
|
32
|
+
def socket_type
|
33
|
+
@type ||= self.class.socket_type.to_s.upcase.to_sym
|
34
|
+
end
|
35
|
+
|
29
36
|
def bind(address)
|
30
37
|
error_wrapper { @socket.bind address }
|
31
38
|
end
|
@@ -43,6 +50,10 @@ module Miu
|
|
43
50
|
@socket.close
|
44
51
|
end
|
45
52
|
|
53
|
+
def to_io
|
54
|
+
@socket
|
55
|
+
end
|
56
|
+
|
46
57
|
protected
|
47
58
|
|
48
59
|
def error_wrapper(source = nil, &block)
|
@@ -89,6 +100,8 @@ module Miu
|
|
89
100
|
alias_method :<<, :write
|
90
101
|
end
|
91
102
|
|
103
|
+
|
104
|
+
# pub/sub
|
92
105
|
class PubSocket < Socket
|
93
106
|
include WritableSocket
|
94
107
|
socket_type :pub
|
@@ -107,6 +120,48 @@ module Miu
|
|
107
120
|
end
|
108
121
|
end
|
109
122
|
|
123
|
+
|
124
|
+
# req/rep
|
125
|
+
class ReqSocket < Socket
|
126
|
+
include ReadableSocket
|
127
|
+
include WritableSocket
|
128
|
+
socket_type :req
|
129
|
+
end
|
130
|
+
|
131
|
+
class RepSocket < Socket
|
132
|
+
include ReadableSocket
|
133
|
+
include WritableSocket
|
134
|
+
socket_type :rep
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
# dealer/router
|
139
|
+
class DealerSocket < Socket
|
140
|
+
include ReadableSocket
|
141
|
+
include WritableSocket
|
142
|
+
socket_type :dealer
|
143
|
+
end
|
144
|
+
|
145
|
+
class RouterSocket < Socket
|
146
|
+
include ReadableSocket
|
147
|
+
include WritableSocket
|
148
|
+
socket_type :router
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
# push/pull
|
153
|
+
class PushSocket < Socket
|
154
|
+
include WritableSocket
|
155
|
+
socket_type :push
|
156
|
+
end
|
157
|
+
|
158
|
+
class PullSocket < Socket
|
159
|
+
include ReadableSocket
|
160
|
+
socket_type :pull
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
# xpub/xsub
|
110
165
|
class XPubSocket < PubSocket
|
111
166
|
socket_type :xpub
|
112
167
|
end
|
data/lib/miu/subscriber.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require 'miu/
|
2
|
-
require 'miu/
|
1
|
+
require 'miu/sockets'
|
2
|
+
require 'miu/readable'
|
3
3
|
require 'miu/utility'
|
4
4
|
|
5
5
|
module Miu
|
@@ -12,7 +12,7 @@ module Miu
|
|
12
12
|
socket = options[:socket] || SubSocket
|
13
13
|
|
14
14
|
klass = Class.new(socket, &block)
|
15
|
-
klass.send :include,
|
15
|
+
klass.send :include, Readable
|
16
16
|
klass.send :include, self
|
17
17
|
|
18
18
|
klass.new.tap do |sub|
|
data/lib/miu/version.rb
CHANGED
data/miu.gemspec
CHANGED
@@ -1,27 +1,28 @@
|
|
1
|
-
#
|
1
|
+
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'miu/version'
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'miu'
|
8
|
+
spec.version = Miu::VERSION
|
9
|
+
spec.authors = ['mashiro']
|
10
|
+
spec.email = ['mail@mashiro.org']
|
11
|
+
spec.description = %q{Miu message hub}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = 'https://github.com/yuijo/miu'
|
14
|
+
spec.license = 'MIT'
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
spec.add_dependency 'thor', '>= 0.18.1'
|
22
|
+
spec.add_dependency 'god', '>= 0.13.2'
|
23
|
+
spec.add_dependency 'ffi-rzmq', '>= 1.0.0'
|
24
|
+
spec.add_dependency 'msgpack', '>= 0.5.4'
|
25
|
+
spec.add_development_dependency 'rake', '>= 10.0.4'
|
26
|
+
spec.add_development_dependency 'rspec', '>= 2.13.0'
|
27
|
+
spec.add_development_dependency 'celluloid-zmq', '>= 0.13.0'
|
27
28
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'miu/dsl'
|
3
|
+
|
4
|
+
describe Miu do
|
5
|
+
describe 'dump_cli_options' do
|
6
|
+
let(:options) { {'--string' => 'string', '--array' => ['foo', 'bar buzz']} }
|
7
|
+
let(:str) { Miu.dump_cli_options(options) }
|
8
|
+
it { expect(str).to eq "--string=string --array='foo' 'bar buzz'" }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'watch' do
|
12
|
+
let(:watch) do
|
13
|
+
Class.new do
|
14
|
+
attr_accessor :dir, :log, :name, :group
|
15
|
+
attr_accessor :start, :stop, :restart
|
16
|
+
end.new
|
17
|
+
end
|
18
|
+
|
19
|
+
before do
|
20
|
+
God.stub(:watch).and_yield(watch)
|
21
|
+
Miu.watch 'test' do |w|
|
22
|
+
expect(w).to eq watch
|
23
|
+
w.start = 'foo'
|
24
|
+
w.stop = 'bar', {:buzz => 123}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it { expect(watch.start).to be_instance_of String }
|
29
|
+
it { expect(watch.stop).to be_instance_of String }
|
30
|
+
end
|
31
|
+
end
|
@@ -32,8 +32,8 @@ describe Miu::Messages::Base do
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
describe '#
|
36
|
-
let(:hash) { Miu::Messages::Base.new(:type => 'test').
|
35
|
+
describe '#to_h' do
|
36
|
+
let(:hash) { Miu::Messages::Base.new(:type => 'test').to_h }
|
37
37
|
|
38
38
|
it { expect(hash).to be_instance_of ::Hash }
|
39
39
|
it { expect(hash).to have_key :network }
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Miu::Messages::Enter do
|
4
|
+
describe '#initialize' do
|
5
|
+
context 'no args' do
|
6
|
+
before do
|
7
|
+
@msg = Miu::Messages::Enter.new
|
8
|
+
end
|
9
|
+
subject { @msg }
|
10
|
+
|
11
|
+
its(:id) { should be_instance_of String }
|
12
|
+
its(:time) { should be_instance_of Fixnum }
|
13
|
+
its(:network) { should be_instance_of Miu::Resources::Network }
|
14
|
+
its(:type) { should eq 'enter' }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'with args' do
|
18
|
+
before do
|
19
|
+
@msg = Miu::Messages::Enter.new({
|
20
|
+
:id => 123,
|
21
|
+
:time => 123,
|
22
|
+
:network => {:name => 'test'},
|
23
|
+
:content => {},
|
24
|
+
})
|
25
|
+
end
|
26
|
+
subject { @msg }
|
27
|
+
|
28
|
+
its(:id) { should eq 123 }
|
29
|
+
its(:time) { should eq 123 }
|
30
|
+
its(:network) { should be_instance_of Miu::Resources::Network }
|
31
|
+
its(:type) { should eq 'enter' }
|
32
|
+
its(:content) { should be_instance_of Miu::Resources::EnterContent }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|