amf_socket 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- amf_socket (0.1.4)
4
+ amf_socket (0.2.0)
5
5
  RocketAMF (>= 0.2.1)
6
6
  eventmachine (>= 0.12.10)
7
7
 
@@ -9,7 +9,7 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  RocketAMF (0.2.1)
12
- eventmachine (1.0.0.rc.4)
12
+ eventmachine (0.12.10)
13
13
 
14
14
  PLATFORMS
15
15
  ruby
data/README.md CHANGED
@@ -23,22 +23,36 @@ Or install it yourself as:
23
23
  def post_init
24
24
  super
25
25
 
26
- puts 'Sending a request'
27
- send_request('hello', :foo => 'bar') do |response|
28
- puts "received a response to my request: #{response.result}"
26
+ puts 'Connected.'
27
+
28
+ puts 'Sending a request.'
29
+ send_request('hello', :foo => 'bar') do |request|
30
+ request.succeeded do |response|
31
+ puts "Request received a response. result=#{response.result}"
32
+ end
33
+
34
+ request.failed do |reason|
35
+ puts "Request failed to receive a response. reason=#{reason}"
36
+ end
29
37
  end
30
38
 
31
- puts 'Sending a message'
32
- send_message('hey there', ['hello', 'world'])
39
+ puts 'Sending a message.'
40
+ send_message('hey there', :bar => ['hello', 'world'])
33
41
  end
34
42
 
35
43
  def receive_request(request)
36
- puts "received a request: #{request.command}"
44
+ puts "Received a request. command=#{request.command}, params=#{request.params}"
37
45
  request.reply('Here is my response')
38
46
  end
39
47
 
40
48
  def receive_message(message)
41
- puts "received a message: #{message.command}"
49
+ puts "Received a message. command=#{message.command}, params=#{message.params}"
50
+ end
51
+
52
+ def unbind
53
+ super
54
+
55
+ puts 'Disconnected.'
42
56
  end
43
57
  end
44
58
 
data/Rakefile CHANGED
@@ -37,22 +37,36 @@ task :harness => :environment do
37
37
  def post_init
38
38
  super
39
39
 
40
- puts 'Sending a request'
41
- send_request('hello', :foo => 'bar') do |response|
42
- puts "received a response to my request: #{response.result}"
40
+ puts 'Connected.'
41
+
42
+ puts 'Sending a request.'
43
+ send_request('hello', :foo => 'bar') do |request|
44
+ request.succeeded do |response|
45
+ puts "Request received a response. result=#{response.result}"
46
+ end
47
+
48
+ request.failed do |reason|
49
+ puts "Request failed to receive a response. reason=#{reason}"
50
+ end
43
51
  end
44
52
 
45
- puts 'Sending a message'
46
- send_message('hey there', ['hello', 'world'])
53
+ puts 'Sending a message.'
54
+ send_message('hey there', :bar => ['hello', 'world'])
47
55
  end
48
56
 
49
57
  def receive_request(request)
50
- puts "received a request: #{request.command}"
58
+ puts "Received a request. command=#{request.command}, params=#{request.params}"
51
59
  request.reply('Here is my response')
52
60
  end
53
61
 
54
62
  def receive_message(message)
55
- puts "received a message: #{message.command}"
63
+ puts "Received a message. command=#{message.command}, params=#{message.params}"
64
+ end
65
+
66
+ def unbind
67
+ super
68
+
69
+ puts 'Disconnected.'
56
70
  end
57
71
  end
58
72
 
data/amf_socket.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["chad@remesch.com"]
7
7
  gem.description = %q{Ruby implementation of AMF Socket (https://github.com/chadrem/amf_socket)}
8
8
  gem.summary = %q{AMF Socket is a bi-directional RPC system for Adobe Flash (Actionscript) programs.}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/chadrem/observation"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -1,32 +1,16 @@
1
1
  class AmfSocket::AmfRpcConnection < AmfSocket::AmfConnection
2
2
  def send_request(command, params = {}, &block)
3
- message_id = SecureRandom.hex
4
-
5
- object = {}
6
- object[:type] = 'rpcRequest'
7
- object[:request] = {}
8
- object[:request][:messageId] = message_id
9
- object[:request][:command] = command
10
- object[:request][:params] = params
11
-
12
- @sent_requests[message_id] = [object, block]
13
-
14
- send_object(object)
3
+ request = AmfSocket::RpcRequest.new(command, params)
4
+ block.call(request)
5
+ @sent_requests[request.message_id] = request
6
+ send_object(request.to_hash)
15
7
  end
16
8
 
17
9
  def send_message(command, params = {})
18
- object = {}
19
- object[:type] = 'rpcMessage'
20
- object[:message] = {}
21
- object[:message][:messageId] = SecureRandom.hex
22
- object[:message][:command] = command
23
- object[:message][:params] = params
24
-
25
- send_object(object)
10
+ message = AmfSocket::RpcMessage.new(command, params)
11
+ send_object(message.to_hash)
26
12
  end
27
13
 
28
- private
29
-
30
14
  #
31
15
  # EM Callbacks.
32
16
  #
@@ -42,21 +26,31 @@ class AmfSocket::AmfRpcConnection < AmfSocket::AmfConnection
42
26
 
43
27
  case object[:type]
44
28
  when 'rpcRequest'
45
- request = AmfSocket::RpcRequest.new(object, self)
29
+ request = AmfSocket::RpcReceivedRequest.new(object, self)
46
30
  receive_request(request)
47
31
  when 'rpcResponse'
48
32
  receive_response(object)
49
33
  when 'rpcMessage'
50
- message = AmfSocket::RpcMessage.new(object, self)
34
+ message = AmfSocket::RpcReceivedMessage.new(object, self)
51
35
  receive_message(message)
52
36
  else
53
37
  raise AmfSocket::InvalidObject
54
38
  end
55
- rescue AmfSocket::InvalidObject => e
39
+ rescue AmfSocket::InvalidObject
56
40
  close_connection
57
41
  end
58
42
  end
59
43
 
44
+ def unbind
45
+ @sent_requests.each do |message_id, request|
46
+ if request.failed_callback.is_a?(Proc)
47
+ request.failed_callback.call('disconnected')
48
+ end
49
+ end
50
+
51
+ @sent_requests.clear
52
+ end
53
+
60
54
  #
61
55
  # AMF RPC Connection Callbacks.
62
56
  #
@@ -72,14 +66,13 @@ class AmfSocket::AmfRpcConnection < AmfSocket::AmfConnection
72
66
 
73
67
  def receive_response(response_object)
74
68
  raise AmfSocket::InvalidObject unless (message_id = response_object[:response][:messageId])
75
- raise AmfSocket::InvalidObject unless (sent_request = @sent_requests[message_id])
76
-
77
- request_object = sent_request[0]
78
- block = sent_request[1]
79
- response = AmfSocket::RpcResponse.new(request_object, response_object, self)
69
+ raise AmfSocket::InvalidObject unless (request = @sent_requests[message_id])
80
70
 
71
+ response = AmfSocket::RpcResponse.new(request, response_object)
81
72
  @sent_requests.delete(message_id)
82
73
 
83
- block.call(response)
74
+ if request.succeeded_callback.is_a?(Proc)
75
+ request.succeeded_callback.call(response)
76
+ end
84
77
  end
85
78
  end
@@ -1,4 +1,5 @@
1
1
  module AmfSocket
2
2
  class InvalidObject < RuntimeError; end
3
+ class InvalidArg < RuntimeError; end
3
4
  class AlreadyReplied < RuntimeError; end
4
5
  end
@@ -1,35 +1,26 @@
1
1
  class AmfSocket::RpcMessage
2
- attr_reader :connection
2
+ attr_reader :message_id
3
+ attr_reader :command
4
+ attr_reader :params
3
5
 
4
- def initialize(object, connection)
5
- raise AmfSocket::InvalidObject unless validate_object(object)
6
+ def initialize(command, params = {})
7
+ raise AmfSocket::InvalidArg.new('Command must be a String.') unless command.is_a?(String)
8
+ raise AmfSocket::InvalidArg.new('Params must be a Hash.') unless params.is_a?(Hash)
6
9
 
7
- @message_obj = object[:message]
8
- @connection = connection
10
+ @command = command
11
+ @params = params
12
+ @message_id = SecureRandom.hex
9
13
  end
10
14
 
11
- def message_id
12
- return @message_obj[:messageId]
13
- end
14
-
15
- def command
16
- return @message_obj[:command]
17
- end
18
-
19
- def params
20
- return @message_obj[:params]
21
- end
22
-
23
- private
15
+ def to_hash
16
+ object = {}
24
17
 
25
- def validate_object(object)
26
- return false unless object.is_a?(Hash)
27
- return false unless object[:type] == 'rpcMessage'
28
- return false unless object[:message].is_a?(Hash)
29
- return false unless object[:message][:command].is_a?(String)
30
- return false unless object[:message][:params].is_a?(Hash)
31
- return false unless object[:message][:messageId].is_a?(String)
18
+ object[:type] = 'rpcMessage'
19
+ object[:message] = {}
20
+ object[:message][:messageId] = message_id
21
+ object[:message][:command] = command
22
+ object[:message][:params] = params
32
23
 
33
- return true
24
+ return object
34
25
  end
35
26
  end
@@ -0,0 +1,35 @@
1
+ class AmfSocket::RpcReceivedMessage
2
+ attr_reader :connection
3
+
4
+ def initialize(object, connection)
5
+ raise AmfSocket::InvalidObject unless validate_object(object)
6
+
7
+ @message_obj = object[:message]
8
+ @connection = connection
9
+ end
10
+
11
+ def message_id
12
+ return @message_obj[:messageId]
13
+ end
14
+
15
+ def command
16
+ return @message_obj[:command]
17
+ end
18
+
19
+ def params
20
+ return @message_obj[:params]
21
+ end
22
+
23
+ private
24
+
25
+ def validate_object(object)
26
+ return false unless object.is_a?(Hash)
27
+ return false unless object[:type] == 'rpcMessage'
28
+ return false unless object[:message].is_a?(Hash)
29
+ return false unless object[:message][:command].is_a?(String)
30
+ return false unless object[:message][:params].is_a?(Hash)
31
+ return false unless object[:message][:messageId].is_a?(String)
32
+
33
+ return true
34
+ end
35
+ end
@@ -0,0 +1,51 @@
1
+ class AmfSocket::RpcReceivedRequest
2
+ attr_reader :connection
3
+ attr_reader :state # Valid states: :initialized, replied.
4
+
5
+ def initialize(object, connection)
6
+ raise AmfSocket::InvalidObject unless validate_object(object)
7
+
8
+ @request_obj = object[:request]
9
+ @connection = connection
10
+ @state = :initialized
11
+ end
12
+
13
+ def message_id
14
+ return @request_obj[:messageId]
15
+ end
16
+
17
+ def command
18
+ return @request_obj[:command]
19
+ end
20
+
21
+ def params
22
+ return @request_obj[:params]
23
+ end
24
+
25
+ def reply(result)
26
+ return false if @state == :replied
27
+
28
+ object = {}
29
+ object[:type] = 'rpcResponse'
30
+ object[:response] = {}
31
+ object[:response][:messageId] = message_id
32
+ object[:response][:result] = result
33
+
34
+ @connection.send_object(object)
35
+
36
+ return true
37
+ end
38
+
39
+ private
40
+
41
+ def validate_object(object)
42
+ return false unless object.is_a?(Hash)
43
+ return false unless object[:type] == 'rpcRequest'
44
+ return false unless object[:request].is_a?(Hash)
45
+ return false unless object[:request][:command].is_a?(String)
46
+ return false unless object[:request][:params].is_a?(Hash)
47
+ return false unless object[:request][:messageId].is_a?(String)
48
+
49
+ return true
50
+ end
51
+ end
@@ -1,51 +1,40 @@
1
1
  class AmfSocket::RpcRequest
2
- attr_reader :connection
3
- attr_reader :state # Valid states: :initialized, replied.
2
+ attr_reader :state # Valid states: :initialized, :sent, :responded.
3
+ attr_reader :message_id
4
+ attr_reader :command
5
+ attr_reader :params
4
6
 
5
- def initialize(object, connection)
6
- raise AmfSocket::InvalidObject unless validate_object(object)
7
+ attr_accessor :succeeded_callback
8
+ attr_accessor :failed_callback
9
+ attr_accessor :timeout
7
10
 
8
- @request_obj = object[:request]
9
- @connection = connection
10
- @state = :initialized
11
- end
11
+ def initialize(command, params = {})
12
+ raise AmfSocket::InvalidArg.new('Command must be a String.') unless command.is_a?(String)
13
+ raise AmfSocket::InvalidArg.new('Params must be a Hash.') unless params.is_a?(Hash)
12
14
 
13
- def message_id
14
- return @request_obj[:messageId]
15
+ @command = command
16
+ @params = params
17
+ @state = :initialized
18
+ @message_id = SecureRandom.hex
15
19
  end
16
20
 
17
- def command
18
- return @request_obj[:command]
21
+ def succeeded(&block)
22
+ @succeeded_callback = block
19
23
  end
20
24
 
21
- def params
22
- return @request_obj[:params]
25
+ def failed(&block)
26
+ @failed_callback = block
23
27
  end
24
28
 
25
- def reply(result)
26
- return false if @state == :replied
27
-
29
+ def to_hash
28
30
  object = {}
29
- object[:type] = 'rpcResponse'
30
- object[:response] = {}
31
- object[:response][:messageId] = message_id
32
- object[:response][:result] = result
33
-
34
- @connection.send_object(object)
35
-
36
- return true
37
- end
38
-
39
- private
40
31
 
41
- def validate_object(object)
42
- return false unless object.is_a?(Hash)
43
- return false unless object[:type] == 'rpcRequest'
44
- return false unless object[:request].is_a?(Hash)
45
- return false unless object[:request][:command].is_a?(String)
46
- return false unless object[:request][:params].is_a?(Hash)
47
- return false unless object[:request][:messageId].is_a?(String)
32
+ object[:type] = 'rpcRequest'
33
+ object[:request] = {}
34
+ object[:request][:messageId] = message_id
35
+ object[:request][:command] = command
36
+ object[:request][:params] = params
48
37
 
49
- return true
38
+ return object
50
39
  end
51
40
  end
@@ -1,21 +1,12 @@
1
1
  class AmfSocket::RpcResponse
2
- attr_reader :connection
3
- attr_reader :message_id
4
- attr_reader :command
5
- attr_reader :params
2
+ attr_reader :request
6
3
  attr_reader :result
7
4
 
8
- def initialize(request_object, response_object, connection)
5
+ def initialize(request, response_object)
9
6
  raise AmfSocket::InvalidObject unless validate_object(response_object)
10
7
 
11
- req = request_object[:request]
12
- res = response_object[:response]
13
-
14
- @connection = connection
15
- @message_id = req[:messageId]
16
- @command = req[:command]
17
- @params = req[:params]
18
- @result = res[:result]
8
+ @result = response_object[:response][:result]
9
+ @request = request
19
10
  end
20
11
 
21
12
  private
@@ -1,3 +1,3 @@
1
1
  module AmfSocket
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/amf_socket.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'eventmachine'
2
2
  require 'rocketamf'
3
+ require 'securerandom'
3
4
 
4
5
  module AmfSocket; end
5
6
 
@@ -7,7 +8,9 @@ require 'amf_socket/version'
7
8
  require 'amf_socket/amf_connection'
8
9
  require 'amf_socket/amf_rpc_connection'
9
10
  require 'amf_socket/rpc_request'
10
- require 'amf_socket/rpc_message'
11
11
  require 'amf_socket/rpc_response'
12
+ require 'amf_socket/rpc_message'
13
+ require 'amf_socket/rpc_received_request'
14
+ require 'amf_socket/rpc_received_message'
12
15
  require 'amf_socket/serializer'
13
16
  require 'amf_socket/exceptions'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amf_socket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-14 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -62,11 +62,13 @@ files:
62
62
  - lib/amf_socket/amf_rpc_connection.rb
63
63
  - lib/amf_socket/exceptions.rb
64
64
  - lib/amf_socket/rpc_message.rb
65
+ - lib/amf_socket/rpc_received_message.rb
66
+ - lib/amf_socket/rpc_received_request.rb
65
67
  - lib/amf_socket/rpc_request.rb
66
68
  - lib/amf_socket/rpc_response.rb
67
69
  - lib/amf_socket/serializer.rb
68
70
  - lib/amf_socket/version.rb
69
- homepage: ''
71
+ homepage: https://github.com/chadrem/observation
70
72
  licenses: []
71
73
  post_install_message:
72
74
  rdoc_options: []
@@ -80,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
82
  version: '0'
81
83
  segments:
82
84
  - 0
83
- hash: 3888358199244684483
85
+ hash: -337065738793524363
84
86
  required_rubygems_version: !ruby/object:Gem::Requirement
85
87
  none: false
86
88
  requirements:
@@ -89,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
91
  version: '0'
90
92
  segments:
91
93
  - 0
92
- hash: 3888358199244684483
94
+ hash: -337065738793524363
93
95
  requirements: []
94
96
  rubyforge_project:
95
97
  rubygems_version: 1.8.24