summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-08-06 22:22:02 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-08-06 22:22:02 +0900
commit3d580b40e398487e3c6c4edbd50b0d7ad29514b1 (patch)
tree0c66681d62fdd4613436307e96edf5c5c64955d9
parentc767321a5a02bc8c6baeee10f706a5e8e875f8e7 (diff)
downloadplum-3d580b40e398487e3c6c4edbd50b0d7ad29514b1.tar.gz
extract event emitter
-rw-r--r--lib/plum.rb1
-rw-r--r--lib/plum/event_emitter.rb19
-rw-r--r--lib/plum/server_connection.rb13
-rw-r--r--lib/plum/stream.rb13
4 files changed, 22 insertions, 24 deletions
diff --git a/lib/plum.rb b/lib/plum.rb
index d37a22c..59e6a9e 100644
--- a/lib/plum.rb
+++ b/lib/plum.rb
@@ -3,6 +3,7 @@ require "socket"
require "plum/version"
require "plum/error"
require "plum/binary_string"
+require "plum/event_emitter"
require "plum/hpack/constants"
require "plum/hpack/huffman"
require "plum/hpack/context"
diff --git a/lib/plum/event_emitter.rb b/lib/plum/event_emitter.rb
new file mode 100644
index 0000000..feebfa1
--- /dev/null
+++ b/lib/plum/event_emitter.rb
@@ -0,0 +1,19 @@
+module Plum
+ module EventEmitter
+ # Registers an event handler to specified event. An event can have multiple handlers.
+ # @param name [String] The name of event.
+ # @yield Gives event-specific parameters.
+ def on(name, &blk)
+ callbacks[name] << blk
+ end
+
+ private
+ def callback(name, *args)
+ callbacks[name].each {|cb| cb.call(*args) }
+ end
+
+ def callbacks
+ @callbacks ||= Hash.new {|hash, key| hash[key] = [] }
+ end
+ end
+end
diff --git a/lib/plum/server_connection.rb b/lib/plum/server_connection.rb
index 18df556..12bbe4f 100644
--- a/lib/plum/server_connection.rb
+++ b/lib/plum/server_connection.rb
@@ -2,6 +2,7 @@ using Plum::BinaryString
module Plum
class ServerConnection
+ include EventEmitter
include FlowControl
include ServerConnectionHelper
@@ -24,7 +25,6 @@ module Plum
@socket = socket
@local_settings = Hash.new {|hash, key| DEFAULT_SETTINGS[key] }.merge!(local_settings)
@remote_settings = Hash.new {|hash, key| DEFAULT_SETTINGS[key] }
- @callbacks = Hash.new {|hash, key| hash[key] = [] }
@buffer = "".force_encoding(Encoding::BINARY)
@streams = {}
@state = :waiting_connetion_preface
@@ -34,13 +34,6 @@ module Plum
recv: @local_settings[:initial_window_size])
end
- # Registers an event handler to specified event. An event can have multiple handlers.
- # @param name [String] The name of event.
- # @yield Gives event-specific parameters.
- def on(name, &blk)
- @callbacks[name] << blk
- end
-
# Starts communication with the peer. It blocks until the socket is closed, or reaches EOF.
def start
settings(@local_settings)
@@ -107,10 +100,6 @@ module Plum
end
private
- def callback(name, *args)
- @callbacks[name].each {|cb| cb.call(*args) }
- end
-
def send_immediately(frame)
callback(:send_frame, frame)
@socket.write(frame.assemble)
diff --git a/lib/plum/stream.rb b/lib/plum/stream.rb
index ad0bfe6..67e2550 100644
--- a/lib/plum/stream.rb
+++ b/lib/plum/stream.rb
@@ -2,6 +2,7 @@ using Plum::BinaryString
module Plum
class Stream
+ include EventEmitter
include FlowControl
include StreamHelper
@@ -14,7 +15,6 @@ module Plum
@id = id
@state = state
@continuation = []
- @callbacks = Hash.new {|hash, key| hash[key] = [] }
initialize_flow_control(send: @connection.remote_settings[:initial_window_size],
recv: @connection.local_settings[:initial_window_size])
@@ -69,18 +69,7 @@ module Plum
payload: data)
end
- # Registers an event handler to specified event. An event can have multiple handlers.
- # @param name [String] The name of event.
- # @yield Gives event-specific parameters.
- def on(name, &blk)
- @callbacks[name] << blk
- end
-
private
- def callback(name, *args)
- @callbacks[name].each {|cb| cb.call(*args) }
- end
-
def send_immediately(frame)
callback(:send_frame, frame)
@connection.send(frame)