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
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1443afae69b50846c4ac4aa4e14c88a54e99cfa8
4
+ data.tar.gz: a9b76bb0de214c17db275392a1595bbed00e4d40
5
+ SHA512:
6
+ metadata.gz: 9ef756f4b8fd9d3b1763559cfe27d4e001a0243b3ae072b7b150f202baca1e79c1b73bcfc038cb783dc68620567a00005272d9007f80eaf274137d466941959b
7
+ data.tar.gz: f7adbd99ffe06db55ed71c07654623ddec8ee49098c0d674ad67645a0d3a0c2ecfa9c581f487be3e2c5cd077e6aca58ba4001556f41dce60d74142fcdd49e86b
data/.gitignore CHANGED
@@ -18,5 +18,5 @@ tmp
18
18
  vendor
19
19
  db
20
20
  log
21
- config
21
+ /config
22
22
 
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
3
  RSpec::Core::RakeTask.new(:spec)
4
4
  task :default => :spec
data/lib/miu.rb CHANGED
@@ -6,16 +6,16 @@ module Miu
6
6
  autoload :CLI, 'miu/cli'
7
7
  autoload :Utility, 'miu/utility'
8
8
 
9
- autoload :Socket, 'miu/socket'
10
- autoload :PubSocket, 'miu/socket'
11
- autoload :SubSocket, 'miu/socket'
12
- autoload :XPubSocket, 'miu/socket'
13
- autoload :XSubSocket, 'miu/socket'
9
+ autoload :Socket, 'miu/sockets'
10
+ autoload :PubSocket, 'miu/sockets'
11
+ autoload :SubSocket, 'miu/sockets'
12
+ autoload :XPubSocket, 'miu/sockets'
13
+ autoload :XSubSocket, 'miu/sockets'
14
14
 
15
15
  autoload :Server, 'miu/server'
16
16
  autoload :Packet, 'miu/packet'
17
- autoload :Publishable, 'miu/publishable'
18
- autoload :Subscribable, 'miu/subscribable'
17
+ autoload :Writable, 'miu/writable'
18
+ autoload :Readable, 'miu/readable'
19
19
  autoload :Publisher, 'miu/publisher'
20
20
  autoload :Subscriber, 'miu/subscriber'
21
21
  autoload :Proxy, 'miu/proxy'
@@ -23,30 +23,30 @@ module Miu
23
23
 
24
24
  autoload :Command, 'miu/command'
25
25
  autoload :Node, 'miu/node'
26
- autoload :Type, 'miu/type'
26
+ autoload :Nodes, 'miu/nodes'
27
27
  autoload :Resources, 'miu/resources'
28
28
  autoload :Messages, 'miu/messages'
29
29
 
30
30
  class << self
31
31
  def root
32
32
  require 'pathname'
33
- @root ||= Pathname.new(Dir.pwd)
33
+ Pathname.new(ENV['MIU_ROOT'] || Dir.pwd)
34
34
  end
35
35
 
36
36
  def default_port
37
37
  Integer(ENV['MIU_DEFAULT_PORT']) rescue 22200
38
38
  end
39
39
 
40
- def default_god_port
41
- default_port
40
+ def default_pub_port
41
+ default_port + 0
42
42
  end
43
43
 
44
- def default_pub_port
44
+ def default_sub_port
45
45
  default_port + 1
46
46
  end
47
47
 
48
- def default_sub_port
49
- default_port + 2
48
+ def default_god_port
49
+ default_port + 3
50
50
  end
51
51
 
52
52
  def default_god_config
@@ -6,7 +6,7 @@ module Miu
6
6
  class CLI < CLIBase
7
7
  class << self
8
8
  def source_root
9
- File.expand_path('../../templates', __FILE__)
9
+ File.expand_path('../templates', __FILE__)
10
10
  end
11
11
 
12
12
  def destination_root
@@ -22,9 +22,7 @@ module Miu
22
22
 
23
23
  desc 'init', 'Generates a miu configuration files'
24
24
  def init
25
- inside 'config' do
26
- template 'miu.god'
27
- end
25
+ directory 'config'
28
26
  empty_directory 'log'
29
27
  empty_directory 'tmp/pids'
30
28
  end
@@ -38,7 +36,7 @@ module Miu
38
36
  print_table table, :indent => 2, :truncate => true
39
37
  end
40
38
 
41
- desc 'start', 'Start miu server'
39
+ desc 'server', 'Start miu server'
42
40
  option 'pub-host', :type => :string, :default => '127.0.0.1', :desc => 'pub host'
43
41
  option 'pub-port', :type => :numeric, :default => Miu.default_pub_port, :desc => 'pub port'
44
42
  option 'sub-host', :type => :string, :default => '127.0.0.1', :desc => 'sub host'
@@ -46,7 +44,7 @@ module Miu
46
44
  option 'bridge-host', :type => :string, :default => '127.0.0.1', :desc => 'bridge host'
47
45
  option 'bridge-port', :type => :numeric, :desc => 'bridge port'
48
46
  option 'verbose', :type => :boolean, :default => false, :desc => 'verbose output', :aliases => '-V'
49
- def start
47
+ def server
50
48
  server = Miu::Server.new Miu::Utility.optionify_keys(options)
51
49
  server.run
52
50
  end
@@ -54,10 +52,9 @@ module Miu
54
52
  desc 'cat TAG ROOM TEXT', 'Okaka kakeyoune'
55
53
  option 'host', :type => :string, :default => '127.0.0.1', :desc => 'miu sub host'
56
54
  option 'port', :type => :numeric, :default => Miu.default_sub_port, :desc => 'miu sub port'
57
- option 'network', :type => :string, :default => 'debug', :desc => 'miu network name'
55
+ option 'network', :type => :string, :default => 'cat', :desc => 'miu network name'
58
56
  def cat(tag, room, text)
59
57
  require 'miu/messages'
60
- require 'json'
61
58
  publisher = Miu::Publisher.new :host => options[:host], :port => options[:port]
62
59
  message = Miu::Messages::Text.new do |m|
63
60
  m.network.name = options[:network]
@@ -73,30 +70,74 @@ module Miu
73
70
  Miu::Logger.exception e
74
71
  end
75
72
 
76
- desc 'supervise', 'Supervise miu and nodes'
73
+ desc 'supervise', 'Supervise nodes'
77
74
  def supervise(*args)
78
- args.unshift "-c #{Miu.default_god_config}"
79
- run_god *args
75
+ args.unshift(
76
+ 'god',
77
+ "-c #{Miu.root.join(Miu.default_god_config)}",
78
+ "-l #{Miu.root.join('log/god.log')}",
79
+ '--no-syslog',
80
+ '--no-events',
81
+ )
82
+ run args.join(' '), :verbose => false
80
83
  end
81
84
 
82
- desc 'terminate', 'Terminate miu and nodes'
85
+ desc 'terminate', 'Terminate nodes'
83
86
  def terminate(*args)
84
- args.unshift "-p #{Miu.default_god_port} terminate"
87
+ args.unshift 'terminate'
88
+ run_god *args
89
+ end
90
+
91
+ desc 'start [NODE]', 'Start specified node or all nodes'
92
+ def start(node = :all, *args)
93
+ args.unshift 'start', node
94
+ run_god *args
95
+ end
96
+
97
+ desc 'stop [NODE]', 'Stop specified node or all nodes'
98
+ def stop(node = :all, *args)
99
+ args.unshift 'stop', node
100
+ run_god *args
101
+ end
102
+
103
+ desc 'restart [NODE]', 'Restart specified node or all nodes'
104
+ def restart(node = :all, *args)
105
+ args.unshift 'restart', node
106
+ run_god *args
107
+ end
108
+
109
+ desc 'monitor [NODE]', 'Monitor specified node or all nodes'
110
+ def monitor(node = :all, *args)
111
+ args.unshift 'monitor', node
112
+ run_god *args
113
+ end
114
+
115
+ desc 'unmonitor [NODE]', 'Unmonitor specified node or all nodes'
116
+ def unmonitor(node = :all, *args)
117
+ args.unshift 'unmonitor', node
118
+ run_god *args
119
+ end
120
+
121
+ desc 'status [NODE]', 'Show status of the specified node or all nodes'
122
+ def status(node = :all, *args)
123
+ args.unshift 'status', node
85
124
  run_god *args
86
125
  end
87
126
 
88
- desc 'god [ARGS]', 'Miu is a god'
89
- def god(*args)
90
- args.unshift "-p #{Miu.default_god_port}"
127
+ desc 'reload', 'Reload config file'
128
+ def reload(*args)
129
+ args.unshift "load #{Miu.root.join(Miu.default_god_config)}"
91
130
  run_god *args
92
131
  end
93
132
 
94
133
  private
95
134
 
96
135
  def run_god(*args)
97
- require 'god'
98
- args.unshift 'god'
99
- run args.join(' ')
136
+ args.unshift(
137
+ 'god',
138
+ "-p #{Miu.default_god_port}",
139
+ )
140
+ run args.join(' '), :verbose => false
100
141
  end
101
142
  end
102
143
  end
@@ -1,9 +1,10 @@
1
1
  require 'miu'
2
- require 'miu/cli_base'
3
2
 
4
3
  module Miu
5
4
  class Command
6
5
  def self.new(name, node, options = {}, &block)
6
+ require 'miu/cli_base'
7
+
7
8
  Class.new Miu::CLIBase do
8
9
  attr_accessor :node
9
10
  @node = node
@@ -22,15 +23,17 @@ module Miu
22
23
  end
23
24
 
24
25
  def add_miu_pub_options(name)
26
+ tag = ['miu', 'input', name].select { |s| s && !s.empty? }.join('.') + '.'
25
27
  option 'pub-host', :type => :string, :default => '127.0.0.1', :desc => 'miu pub host'
26
28
  option 'pub-port', :type => :numeric, :default => Miu.default_sub_port, :desc => 'miu pub port'
27
- option 'pub-tag', :type => :string, :default => "miu.input.#{name}.", :desc => 'miu pub tag'
29
+ option 'pub-tag', :type => :string, :default => tag, :desc => 'miu pub tag'
28
30
  end
29
31
 
30
32
  def add_miu_sub_options(name)
33
+ tag = ['miu', 'output', name].select { |s| s && !s.empty? }.join('.') + '.'
31
34
  option 'sub-host', :type => :string, :default => '127.0.0.1', :desc => 'miu sub host'
32
35
  option 'sub-port', :type => :numeric, :default => Miu.default_pub_port, :desc => 'miu sub port'
33
- option 'sub-tag', :type => :string, :default => "miu.output.#{name}.", :desc => 'miu sub tag'
36
+ option 'sub-tag', :type => :string, :default => tag, :desc => 'miu sub tag'
34
37
  end
35
38
 
36
39
  def add_miu_pub_sub_options(name)
@@ -39,6 +42,30 @@ module Miu
39
42
  end
40
43
  end
41
44
 
45
+ unless respond_to? :no_commands
46
+ class << self
47
+ alias_method :no_commands, :no_tasks
48
+ end
49
+ end
50
+
51
+ no_commands do
52
+ def invoke_command(command, *args)
53
+ Miu.logger.level = ::Logger::DEBUG if options['verbose'] && Miu.logger
54
+ super
55
+ end
56
+
57
+ def config(data = nil, &block)
58
+ data = block.call if !data && block
59
+ append_to_file Miu.root.join(Miu.default_god_config), "\n#{data}"
60
+ end
61
+ end
62
+
63
+ class_option :force, :type => :boolean, :group => :runtime, :desc => 'Overwrite files that already exist'
64
+ class_option :pretend, :type => :boolean, :group => :runtime, :desc => 'Run but do not make any changes'
65
+ class_option :verbose, :type => :boolean, :group => :runtime, :desc => 'Verbose status output'
66
+ class_option :quiet, :type => :boolean, :group => :runtime, :desc => 'Suppress status output'
67
+ class_option :skip, :type => :boolean, :group => :runtime, :desc => 'Skip files that already exist'
68
+
42
69
  namespace name
43
70
  class_eval &block if block
44
71
  end
@@ -0,0 +1,45 @@
1
+ require 'miu'
2
+ require 'god/sugar'
3
+ require 'god/task'
4
+ require 'god/watch'
5
+
6
+ module Miu
7
+ class << self
8
+ def dump_cli_options(options)
9
+ options.map do |k, v|
10
+ v = case v
11
+ when Array
12
+ v.map { |x| "'#{x}'" }.join(' ')
13
+ else
14
+ v.to_s
15
+ end
16
+ "#{k}=#{v}"
17
+ end.join(' ')
18
+ end
19
+
20
+ def watch(name)
21
+ God.watch do |w|
22
+ w.dir = Miu.root
23
+ w.log = Miu.root.join("log/#{name}.log").to_s
24
+ w.name = name
25
+ w.group = 'all'
26
+
27
+ yield w if block_given?
28
+
29
+ [:start, :stop, :restart].each do |action|
30
+ value = w.public_send action
31
+ if value.is_a?(Array)
32
+ options = Miu::Utility.extract_options! value
33
+ value << dump_cli_options(options)
34
+ w.public_send "#{action}=", value.join(' ')
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ if $load_god
43
+ God.port = Miu.default_god_port
44
+ God.pid_file_directory = Miu.root.join('tmp/pids').to_s
45
+ end
@@ -1,4 +1,4 @@
1
- require 'miu/socket'
1
+ require 'miu/sockets'
2
2
  require 'miu/proxy'
3
3
  require 'ffi-rzmq'
4
4
 
@@ -33,6 +33,7 @@ module Miu
33
33
  attr_accessor :default_logger
34
34
  attr_accessor :logger
35
35
  end
36
+
36
37
  self.default_logger = ::Logger.new(STDERR)
37
38
  self.logger = self.default_logger
38
39
  self.logger.level = ::Logger::INFO
@@ -19,3 +19,6 @@ end
19
19
  require 'miu/messages/base'
20
20
  require 'miu/messages/unknown'
21
21
  require 'miu/messages/text'
22
+ require 'miu/messages/enter'
23
+ require 'miu/messages/leave'
24
+
@@ -17,20 +17,17 @@ module Miu
17
17
  yield self if block_given?
18
18
  end
19
19
 
20
- def to_hash
20
+ def to_h
21
21
  {
22
- :network => @network.to_hash,
22
+ :network => @network.to_h,
23
23
  :type => @type,
24
- :content => @content ? @content.to_hash : {}
24
+ :content => @content ? @content.to_h : {}
25
25
  }
26
26
  end
27
27
 
28
28
  def to_msgpack(*args)
29
- to_hash.to_msgpack(*args)
29
+ to_h.to_msgpack(*args)
30
30
  end
31
31
  end
32
-
33
- class Unknown < Base
34
- end
35
32
  end
36
33
  end
@@ -0,0 +1,16 @@
1
+ require 'miu/resources'
2
+ require 'miu/messages/base'
3
+
4
+ module Miu
5
+ module Messages
6
+ class Enter < Base
7
+ def initialize(options = {})
8
+ options[:type] ||= 'enter'
9
+ options[:content] = Miu::Utility.adapt(Resources::EnterContent, options[:content] || {})
10
+ super
11
+ end
12
+ end
13
+
14
+ register :enter, Enter
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'miu/resources'
2
+ require 'miu/messages/base'
3
+
4
+ module Miu
5
+ module Messages
6
+ class Leave < Base
7
+ def initialize(options = {})
8
+ options[:type] ||= 'leave'
9
+ options[:content] = Miu::Utility.adapt(Resources::LeaveContent, options[:content] || {})
10
+ super
11
+ end
12
+ end
13
+
14
+ register :leave, Leave
15
+ end
16
+ end
@@ -22,6 +22,10 @@ module Miu
22
22
  raise PacketLoadError, e
23
23
  end
24
24
 
25
+ def to_s
26
+ "<#{tag}> #{data.to_h}"
27
+ end
28
+
25
29
  def inspect
26
30
  inspection = [:tag, :data].map do |name|
27
31
  "#{name}: #{__send__(name).inspect}"
@@ -1,4 +1,4 @@
1
- require 'miu/socket'
1
+ require 'miu/sockets'
2
2
  require 'ffi-rzmq'
3
3
 
4
4
  module Miu
@@ -9,8 +9,8 @@ module Miu
9
9
  PROXY_TO = '@__proxy_to__'
10
10
 
11
11
  def initialize(frontends, backends)
12
- @frontends = Array(frontends).map { |s| unwrap s }
13
- @backends = Array(backends).map { |s| unwrap s }
12
+ @frontends = Array(frontends).map { |s| s.to_io rescue s }
13
+ @backends = Array(backends).map { |s| s.to_io rescue s }
14
14
 
15
15
  @frontends.each { |s| s.instance_variable_set PROXY_TO, @backends }
16
16
  @backends.each { |s| s.instance_variable_set PROXY_TO, @frontends }
@@ -42,16 +42,5 @@ module Miu
42
42
  end
43
43
  end
44
44
  end
45
-
46
- private
47
-
48
- def unwrap(socket)
49
- case socket
50
- when Miu::Socket
51
- socket.socket
52
- else
53
- socket
54
- end
55
- end
56
45
  end
57
46
  end