tamashii 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 971b0a636366074f7e051af483166d7056753fcf
4
- data.tar.gz: c75d1df8a9e5c487b1c989170e7aaf09594e315b
3
+ metadata.gz: f7740a43e6b86d1673461a03ec8af4a48279b3b2
4
+ data.tar.gz: 0e2320ea7184d79a380d223ff8b7a02bb402c58e
5
5
  SHA512:
6
- metadata.gz: '08bd487845177364886ecf70e5cb745446a1801cc6264b3dc4c477cc717ddfa172e00ba4b4edc97a0aba8054d2f151599a2f58e79c6965bda2a7e0f2ba28c7d3'
7
- data.tar.gz: 3098ada65a89ee03fcce8ae3c1a99572e20fc70fcd51f282ddb233df58ec7f4ea658c935cf612e56f39e340bc3e73692fb522428d8f967f29f938beed870f9ad
6
+ metadata.gz: 7e989819b700f4a999aa452f7ca08878bc4361c7354a055ebc605ea87a7390f2e649b06f052ee47ccc7d57fa7666b1d7c3680211872bb9e8afac96a929913345
7
+ data.tar.gz: 1f463edf6b95e70adffd37c4ec6dfa78dd32d1ea070013f88c726a71cac112c2a6b5bbfe0f5b3ad9647601284824815c4959a22d37a6cbb0f7a4ad240e81ffb8
@@ -21,6 +21,7 @@ rspec:
21
21
  stage: test
22
22
  script:
23
23
  - bundle exec rspec -p
24
+ - bundle exec codeclimate-test-reporter
24
25
 
25
26
  rubocop:
26
27
  stage: test
@@ -1,5 +1,5 @@
1
1
  sudo: false
2
+ cache: bundler
2
3
  language: ruby
3
4
  rvm:
4
5
  - 2.4.1
5
- before_install: gem install bundler -v 1.14.6
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Tamashii
1
+ Tamashii [![Gem Version](https://badge.fury.io/rb/tamashii.svg)](https://badge.fury.io/rb/tamashii) [![Build Status](https://travis-ci.org/5xRuby/tamashii.svg?branch=master)](https://travis-ci.org/5xRuby/tamashii) [![Test Coverage](https://codeclimate.com/github/5xRuby/tamashii/badges/coverage.svg)](https://codeclimate.com/github/5xRuby/tamashii/coverage) [![Code Climate](https://codeclimate.com/github/5xRuby/tamashii/badges/gpa.svg)](https://codeclimate.com/github/5xRuby/tamashii)
2
+ ===
3
+
2
4
 
3
5
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tamashii`. To experiment with that code, run `bin/console` for an interactive prompt.
4
6
 
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tamashii
4
+ # :nodoc:
5
+ module Configurable
6
+ # :nodoc:
7
+ module ClassMethods
8
+ def register(key, default, &_block)
9
+ @defaults ||= {}
10
+ return @defaults[key.to_sym] = yield if block_given?
11
+ @defaults[key.to_sym] = default
12
+ end
13
+
14
+ def exist?(key)
15
+ @defaults.key?(key.to_sym) || @defaults.key?(key.to_s[0..-2].to_sym)
16
+ end
17
+
18
+ def default_value(key)
19
+ @defaults[key.to_sym]
20
+ end
21
+ end
22
+
23
+ def self.included(klass)
24
+ klass.extend ClassMethods
25
+ end
26
+
27
+ def config(key, value = nil, &_block)
28
+ @configs ||= {}
29
+ return unless self.class.exist?(key)
30
+ return @configs[key.to_sym] || self.class.default_value(key) if value.nil?
31
+ return @configs[key.to_sym] = yield if block_given?
32
+ @configs[key.to_sym] = value
33
+ end
34
+
35
+ def respond_to_missing?(name)
36
+ self.class.exist?(name)
37
+ end
38
+
39
+ def method_missing(name, *args, &block)
40
+ return super unless self.class.exist?(name)
41
+ return config(name.to_sym) unless name.to_s.end_with?('=')
42
+ config(name.to_s[0..-2].to_sym, args.first, &block)
43
+ end
44
+ end
45
+ end
@@ -18,6 +18,13 @@ module Tamashii
18
18
  autoload :Client, 'tamashii/server/client'
19
19
  autoload :Connection, 'tamashii/server/connection'
20
20
  autoload :Subscription, 'tamashii/server/subscription'
21
+ autoload :Config, 'tamashii/server/config'
22
+
23
+ def self.config(&block)
24
+ @config ||= Config.new
25
+ return instance_exec(@config, &block) if block_given?
26
+ @config
27
+ end
21
28
 
22
29
  def self.logger
23
30
  # TODO: Add config to set logger path
@@ -17,7 +17,7 @@ module Tamashii
17
17
 
18
18
  def pubsub
19
19
  @pubsub || mutex.synchronize do
20
- @pubsub ||= Subscription::Redis.new(self)
20
+ @pubsub ||= Server.config.pubsub_class.new(self)
21
21
  end
22
22
  end
23
23
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tamashii/configurable'
4
+
5
+ module Tamashii
6
+ module Server
7
+ # :nodoc:
8
+ class Config
9
+ include Tamashii::Configurable
10
+
11
+ register :connection_class, Connection::Base
12
+ register :pubsub_class, Subscription::Redis
13
+ end
14
+ end
15
+ end
@@ -4,6 +4,7 @@ module Tamashii
4
4
  module Server
5
5
  # :nodoc:
6
6
  module Connection
7
+ autoload :Base, 'tamashii/server/connection/base'
7
8
  autoload :ClientSocket, 'tamashii/server/connection/client_socket'
8
9
  autoload :StreamEventLoop, 'tamashii/server/connection/stream_event_loop'
9
10
  autoload :Stream, 'tamashii/server/connection/stream'
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tamashii
4
+ module Server
5
+ module Connection
6
+ # :nodoc:
7
+ class Base
8
+ def initialize(server, env, event_loop)
9
+ @server = server
10
+ @env = env
11
+ @event_loop = event_loop
12
+
13
+ @socket = nil
14
+ end
15
+
16
+ def init
17
+ @socket = ClientSocket.new(@server, self, @env, @event_loop)
18
+ @socket.rack_response
19
+ end
20
+
21
+ def on_open
22
+ end
23
+
24
+ def on_message(data)
25
+ @server.pubsub.broadcast(data)
26
+ end
27
+
28
+ def on_error
29
+ end
30
+
31
+ def on_close
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -20,16 +20,23 @@ module Tamashii
20
20
  false
21
21
  end
22
22
 
23
+ CONNECTING = 0
24
+ OPEN = 1
25
+ CLOSING = 2
26
+ CLOSED = 3
27
+
23
28
  attr_reader :env, :url
24
29
  attr_accessor :id
25
30
 
26
31
  # TODO: Support define protocols
27
- def initialize(server, env, event_loop)
32
+ def initialize(server, conn, env, event_loop)
28
33
  @server = server
34
+ @conn = conn
29
35
  @env = env
30
36
  @event_loop = event_loop
31
37
 
32
38
  @id ||= env['REMOTE_ADDR']
39
+ @state = CONNECTING
33
40
 
34
41
  @url = ClientSocket.determine_url(@env)
35
42
  @driver = setup_driver
@@ -100,25 +107,38 @@ module Tamashii
100
107
  end
101
108
 
102
109
  def open
103
- Server::Client.register(self)
110
+ return unless @state == CONNECTING
111
+ @state = OPEN
112
+ @conn.on_open
113
+ Client.register(self)
104
114
  end
105
115
 
106
116
  def receive_message(data)
107
- @server.pubsub.broadcast(data)
117
+ return unless @state == OPEN
118
+ @conn.on_message(data)
108
119
  end
109
120
 
110
121
  def emit_error(message)
122
+ return if @state >= CLOSING
111
123
  Server.logger.error("Client #{id} has some error: #{message}")
124
+ @conn.on_error(message)
112
125
  end
113
126
 
114
127
  def begin_close(_reason, _code)
128
+ # TODO: Define reason and code
129
+ return if @state == CLOSED
130
+ @state = CLOSING
131
+
115
132
  Server.logger.info("Close connection to #{id}")
133
+ @conn.on_close
116
134
  Client.unregister(self)
117
135
  finialize_close
118
136
  end
119
137
 
120
138
  def finialize_close
121
- # TODO: Processing close
139
+ return if @state == CLOSED
140
+ @state = CLOSED
141
+
122
142
  @stream.close
123
143
  end
124
144
  end
@@ -19,7 +19,8 @@ module Tamashii
19
19
  private
20
20
 
21
21
  def start_websocket(env)
22
- Connection::ClientSocket.new(@server, env, @event_loop).rack_response
22
+ conn = Server.config.connection_class.new(@server, env, @event_loop)
23
+ conn.init
23
24
  end
24
25
 
25
26
  def start_http(_)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tamashii
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -45,4 +45,5 @@ Gem::Specification.new do |spec|
45
45
  spec.add_development_dependency "simplecov"
46
46
  spec.add_development_dependency "guard"
47
47
  spec.add_development_dependency "guard-rspec"
48
+ spec.add_development_dependency "codeclimate-test-reporter"
48
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tamashii
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 蒼時弦也
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2017-05-16 00:00:00.000000000 Z
13
+ date: 2017-05-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: concurrent-ruby
@@ -208,6 +208,20 @@ dependencies:
208
208
  - - ">="
209
209
  - !ruby/object:Gem::Version
210
210
  version: '0'
211
+ - !ruby/object:Gem::Dependency
212
+ name: codeclimate-test-reporter
213
+ requirement: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ type: :development
219
+ prerelease: false
220
+ version_requirements: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
211
225
  description: The WebSocket framework implement inspired by ActionCable
212
226
  email:
213
227
  - elct9620@frost.tw
@@ -230,10 +244,13 @@ files:
230
244
  - bin/console
231
245
  - bin/setup
232
246
  - lib/tamashii.rb
247
+ - lib/tamashii/configurable.rb
233
248
  - lib/tamashii/server.rb
234
249
  - lib/tamashii/server/base.rb
235
250
  - lib/tamashii/server/client.rb
251
+ - lib/tamashii/server/config.rb
236
252
  - lib/tamashii/server/connection.rb
253
+ - lib/tamashii/server/connection/base.rb
237
254
  - lib/tamashii/server/connection/client_socket.rb
238
255
  - lib/tamashii/server/connection/stream.rb
239
256
  - lib/tamashii/server/connection/stream_event_loop.rb