miu 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -1
  3. data/Rakefile +1 -1
  4. data/lib/miu.rb +14 -14
  5. data/lib/miu/cli.rb +60 -19
  6. data/lib/miu/command.rb +30 -3
  7. data/lib/miu/dsl.rb +45 -0
  8. data/lib/miu/forwarder.rb +1 -1
  9. data/lib/miu/logger.rb +1 -0
  10. data/lib/miu/messages.rb +3 -0
  11. data/lib/miu/messages/base.rb +4 -7
  12. data/lib/miu/messages/enter.rb +16 -0
  13. data/lib/miu/messages/leave.rb +16 -0
  14. data/lib/miu/packet.rb +4 -0
  15. data/lib/miu/proxy.rb +3 -14
  16. data/lib/miu/publisher.rb +3 -3
  17. data/lib/miu/{subscribable.rb → readable.rb} +1 -1
  18. data/lib/miu/resources.rb +2 -0
  19. data/lib/miu/resources/base.rb +2 -2
  20. data/lib/miu/resources/content.rb +2 -1
  21. data/lib/miu/resources/enter_content.rb +22 -0
  22. data/lib/miu/resources/leave_content.rb +22 -0
  23. data/lib/miu/resources/network.rb +1 -1
  24. data/lib/miu/resources/room.rb +1 -1
  25. data/lib/miu/resources/text_content.rb +3 -3
  26. data/lib/miu/resources/user.rb +1 -1
  27. data/lib/miu/server.rb +14 -7
  28. data/lib/miu/{socket.rb → sockets.rb} +59 -4
  29. data/lib/miu/subscriber.rb +3 -3
  30. data/lib/miu/templates/config/miu.god +7 -0
  31. data/lib/miu/version.rb +1 -1
  32. data/lib/miu/{publishable.rb → writable.rb} +1 -1
  33. data/miu.gemspec +21 -20
  34. data/spec/miu/dsl_spec.rb +31 -0
  35. data/spec/miu/messages/base_spec.rb +2 -2
  36. data/spec/miu/messages/enter_spec.rb +35 -0
  37. data/spec/miu/messages/leave_spec.rb +35 -0
  38. data/spec/miu/nodes_spec.rb +4 -0
  39. data/spec/miu/publisher_spec.rb +0 -9
  40. data/spec/miu/{subscribable_spec.rb → readable_spec.rb} +2 -2
  41. data/spec/miu/sockets_spec.rb +97 -0
  42. data/spec/miu/subscriber_spec.rb +0 -9
  43. data/spec/miu/{publishable_spec.rb → writable_spec.rb} +2 -2
  44. data/spec/spec_helper.rb +2 -0
  45. data/spec/support/socket.rb +36 -0
  46. metadata +51 -55
@@ -1,5 +1,5 @@
1
- require 'miu/socket'
2
- require 'miu/publishable'
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, Publishable
15
+ klass.send :include, Writable
16
16
  klass.send :include, self
17
17
 
18
18
  klass.new.tap do |pub|
@@ -2,7 +2,7 @@ require 'miu/packet'
2
2
  require 'miu/messages'
3
3
 
4
4
  module Miu
5
- module Subscribable
5
+ module Readable
6
6
  def self.included(base)
7
7
  base.class_eval do
8
8
  def read_with_packet
@@ -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
@@ -3,12 +3,12 @@ require 'msgpack'
3
3
  module Miu
4
4
  module Resources
5
5
  class Base
6
- def to_hash
6
+ def to_h
7
7
  {}
8
8
  end
9
9
 
10
10
  def to_msgpack(*args)
11
- to_hash.to_msgpack(*args)
11
+ to_h.to_msgpack(*args)
12
12
  end
13
13
  end
14
14
  end
@@ -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 to_hash
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
@@ -9,7 +9,7 @@ module Miu
9
9
  @name = options[:name]
10
10
  end
11
11
 
12
- def to_hash
12
+ def to_h
13
13
  super.merge({:name => @name})
14
14
  end
15
15
  end
@@ -9,7 +9,7 @@ module Miu
9
9
  @name = options[:name]
10
10
  end
11
11
 
12
- def to_hash
12
+ def to_h
13
13
  super.merge({:name => @name})
14
14
  end
15
15
  end
@@ -12,10 +12,10 @@ module Miu
12
12
  super options
13
13
  end
14
14
 
15
- def to_hash
15
+ def to_h
16
16
  super.merge({
17
- :room => @room.to_hash,
18
- :user => @user.to_hash,
17
+ :room => @room.to_h,
18
+ :user => @user.to_h,
19
19
  :text => @text
20
20
  })
21
21
  end
@@ -9,7 +9,7 @@ module Miu
9
9
  @name = options[:name]
10
10
  end
11
11
 
12
- def to_hash
12
+ def to_h
13
13
  super.merge({:name => @name})
14
14
  end
15
15
  end
@@ -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
- [:INT, :TERM].each do |sig|
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 socket_type(type)
9
- class_eval <<-EOS
10
- def socket_type; :#{type.to_s.upcase}; end
11
- EOS
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
@@ -1,5 +1,5 @@
1
- require 'miu/socket'
2
- require 'miu/subscribable'
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, Subscribable
15
+ klass.send :include, Readable
16
16
  klass.send :include, self
17
17
 
18
18
  klass.new.tap do |sub|
@@ -0,0 +1,7 @@
1
+ # coding: utf-8 vim: ft=ruby
2
+ require 'miu/dsl'
3
+
4
+ Miu.watch 'server' do |w|
5
+ w.start = 'miu server'
6
+ w.keepalive
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Miu
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'miu/packet'
2
2
 
3
3
  module Miu
4
- module Publishable
4
+ module Writable
5
5
  def self.included(base)
6
6
  base.class_eval do
7
7
  def write_with_packet(tag, message)
@@ -1,27 +1,28 @@
1
- # -*- encoding: utf-8 -*-
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 |gem|
7
- gem.name = "miu"
8
- gem.version = Miu::VERSION
9
- gem.authors = ["mashiro"]
10
- gem.email = ["mail@mashiro.org"]
11
- gem.description = %q{miu miu}
12
- gem.summary = %q{miu miu}
13
- gem.homepage = ""
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
- gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
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
- gem.add_dependency 'thor', '>= 0.18.1'
21
- gem.add_dependency 'god', '>= 0.13.2'
22
- gem.add_dependency 'ffi-rzmq', '>= 1.0.0'
23
- gem.add_dependency 'msgpack', '>= 0.5.4'
24
- gem.add_development_dependency 'rake', '>= 10.0.4'
25
- gem.add_development_dependency 'rspec', '>= 2.13.0'
26
- gem.add_development_dependency 'celluloid-zmq', '>= 0.13.0'
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 '#to_hash' do
36
- let(:hash) { Miu::Messages::Base.new(:type => 'test').to_hash }
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