smartfox_jruby 0.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ # SmartFox Server client library for JRuby (extension requests testing).
2
+
3
+ This is a small library helping you to interact with a SmartFox server.
4
+ It's main purpose is to test the backend extension using the requests.
5
+ It requires JRuby and some jar-dependencies in a classpath.
6
+
7
+ ## What it gives
8
+
9
+ You can easily connect and interact with a SmartFox server to test your server extension requests
10
+
11
+ ```ruby
12
+ adapter = SmartfoxJruby::SfsAdapter.new
13
+ adapter.login_as("user", "password")
14
+ adapter.connect!(
15
+ :host => "localhost",
16
+ :port => 9933,
17
+ :zone => "test-zone"
18
+ )
19
+
20
+ adapter.process! {
21
+ # create extension request
22
+ request(:GetProducts, :productType => :ITEM).expect(:GetProductsOK) { |data|
23
+ puts "Whoa! I've got the following products: #{data.to_json}"
24
+
25
+ request(:BuyProduct, {:productId => data[:data].first[:id]},
26
+ :serialize_opts => {:productId_type => :long}).
27
+ expect(:BuyProductOK) { |data|
28
+ puts "Whoa! I've bought product! #{data.to_json}"
29
+ }
30
+ }
31
+ }
32
+ adapter.disconnect!
33
+
34
+ ```
35
+
36
+ ## How to install
37
+
38
+ This library uses [Doubleshot]() to process the dependencies. So you can add it as a reference by creating the
39
+ Doubleshot
40
+ file on project root level:
41
+
42
+ ```ruby
43
+ # encoding: utf-8
44
+ Doubleshot.new do |config|
45
+
46
+ config.project = "myproject"
47
+ config.version = "0.1"
48
+
49
+ config.gem 'smartfox_jruby'
50
+
51
+ config.gemspec do |spec|
52
+ spec.summary = "Test Smartfox library"
53
+ spec.description = "TODO"
54
+
55
+ spec.homepage = "http://mysite.info"
56
+ spec.author = "me"
57
+ spec.email = "me@mail.com"
58
+ spec.license = "Apache 2.0"
59
+ end
60
+
61
+ end
62
+
63
+
64
+ ```
65
+
66
+ Copyright (c) 2013 smecsia
67
+
68
+ Licensed under the Apache License, Version 2.0 (the "License");
69
+ you may not use this file except in compliance with the License.
70
+ You may obtain a copy of the License at
71
+
72
+ http://www.apache.org/licenses/LICENSE-2.0
73
+
74
+ Unless required by applicable law or agreed to in writing, software
75
+ distributed under the License is distributed on an "AS IS" BASIS,
76
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77
+ See the License for the specific language governing permissions and
78
+ limitations under the License.
@@ -1,14 +1,15 @@
1
1
  require 'rubygems'
2
2
  require 'ostruct'
3
3
  require 'pathname'
4
- Dir[Pathname.new(File.dirname(File.expand_path(__FILE__))).join("#{File.basename(__FILE__)}").join("*.rb")].each { |f| require f }
5
4
 
6
5
  ####################################################
7
6
  # Main module
8
7
  module SmartfoxJruby
9
- # configurable constants
10
- VERSION = "0.1"
11
-
12
-
8
+ MYDIR = Pathname.new(File.dirname(File.expand_path(__FILE__)))
9
+ autoload :SfsRunner, MYDIR.join('smartfox_jruby/sfs_runner')
10
+ autoload :SfsAdapter, MYDIR.join('smartfox_jruby/sfs_adapter')
11
+ autoload :SfsWorker, MYDIR.join('smartfox_jruby/sfs_worker')
12
+ autoload :SFSUtil, MYDIR.join('smartfox_jruby/common')
13
+ VERSION = "0.2.3"
13
14
  end
14
15
 
@@ -20,35 +20,38 @@ module ISFSObject
20
20
  end
21
21
  end
22
22
 
23
- module SmartfoxJruby::SFSUtil
24
- class << self
25
- def boxing(v, type)
26
- case type
27
- when :long
28
- java.lang.Long.valueOf(v)
29
- when :float
30
- java.lang.Float.valueOf(v)
31
- when :double
32
- java.lang.Double.valueOf(v)
33
- when :int
34
- java.lang.Integer.valueOf(v)
35
- when :boolean
36
- java.lang.Boolean.valueOf(v)
37
- else
38
- v
23
+ module SmartfoxJruby
24
+ module SFSUtil
25
+ class << self
26
+ def boxing(v, type)
27
+ case type
28
+ when :long
29
+ java.lang.Long.valueOf(v)
30
+ when :float
31
+ java.lang.Float.valueOf(v)
32
+ when :double
33
+ java.lang.Double.valueOf(v)
34
+ when :int
35
+ java.lang.Integer.valueOf(v)
36
+ when :boolean
37
+ java.lang.Boolean.valueOf(v)
38
+ else
39
+ v
40
+ end
39
41
  end
40
- end
41
42
 
42
- def to_java_list(value, type)
43
- list = java.util.ArrayList.new
44
- value.to_java(type).each do |v|
45
- list.add(boxing(v, type))
43
+ def to_java_list(value, type)
44
+ list = java.util.ArrayList.new
45
+ value.to_java(type).each do |v|
46
+ list.add(boxing(v, type))
47
+ end
48
+ list
46
49
  end
47
- list
48
50
  end
49
51
  end
50
52
  end
51
53
 
54
+
52
55
  def SFSObject.from_hash(hash, opts = {})
53
56
  res = SFSObject.new
54
57
  opts ||= {}
@@ -1,172 +1,174 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/sfs_worker')
2
2
 
3
- class SmartfoxJruby::SfsAdapter
4
- include IEventListener
5
- attr_reader :opts
6
- attr_reader :smart_fox
7
- attr_reader :worker
8
- attr_reader :username
9
- DEBUG = false
10
-
11
- def initialize(opts = {})
12
- @opts = opts
13
- debug "initializing sfs adapter..."
14
- @smart_fox = SmartFox.new(false)
15
- @connected = false
16
- @worker = opts[:worker]
17
- @login_as = {}
18
- @opts[:timeout] ||= 20
19
- @opts[:logger] ||= Logger.new(STDOUT)
20
- SFSEvent.constants.each do |evt|
21
- evt_value = SFSEvent.const_get(evt)
22
- debug "Registering event adapter to self for event '#{evt}' --> '#{evt_value}'..."
23
- smart_fox.add_event_listener(evt_value, self)
3
+ module SmartfoxJruby
4
+
5
+ class SfsAdapter
6
+ include IEventListener
7
+ attr_reader :opts
8
+ attr_reader :smart_fox
9
+ attr_reader :worker
10
+ attr_reader :username
11
+ DEBUG = false
12
+
13
+ def initialize(opts = {})
14
+ @opts = opts
15
+ @smart_fox = SmartFox.new(false)
16
+ @connected = false
17
+ @worker = opts[:worker]
18
+ @login_as = opts[:login_as] || {}
19
+ @opts[:timeout] ||= 20
20
+ @opts[:logger] ||= Logger.new(STDOUT)
21
+ SFSEvent.constants.each do |evt|
22
+ evt_value = SFSEvent.const_get(evt)
23
+ debug "Registering event adapter to self for event '#{evt}' --> '#{evt_value}'..."
24
+ smart_fox.add_event_listener(evt_value, self)
25
+ end
26
+ debug "initializing sfs adapter..."
24
27
  end
25
- end
26
28
 
27
- def connect!(opt = {})
28
- opts.reverse_merge!(opt)
29
- raise "host and port are required to connect SfsAdapter!" if opts[:host].blank? || opts[:port].blank?
30
- debug "connecting to smartfox at #{opts[:host]}:#{opts[:port]} ..."
31
- smart_fox.connect(opts[:host], opts[:port])
32
- end
29
+ def connect!(opt = {})
30
+ opts.reverse_merge!(opt)
31
+ raise "host and port are required to connect SfsAdapter!" if opts[:host].blank? || opts[:port].blank?
32
+ debug "connecting to smartfox at #{opts[:host]}:#{opts[:port]} ..."
33
+ smart_fox.connect(opts[:host], opts[:port])
34
+ end
33
35
 
34
- def dispatch(event)
35
- debug "got event #{event.type}: #{event}"
36
- callback = "on_#{event.type.try(:underscore)}_event"
37
- if respond_to?(callback, true)
38
- send(callback, event)
39
- else
40
- debug "Unknown event caught #{event.type} (No method '#{callback}' in #{self})"
36
+ def dispatch(event)
37
+ debug "got event #{event.type}: #{event}"
38
+ callback = "on_#{event.type.try(:underscore)}_event"
39
+ if respond_to?(callback, true)
40
+ send(callback, event)
41
+ else
42
+ debug "Unknown event caught #{event.type} (No method '#{callback}' in #{self})"
43
+ end
41
44
  end
42
- end
43
45
 
44
- def disconnect!
45
- smart_fox.disconnect
46
- end
46
+ def disconnect!
47
+ smart_fox.disconnect
48
+ end
47
49
 
48
- def process!(wrk = nil, &block)
49
- wait_with_timeout(@opts[:timeout]) { connected? }
50
- @worker = wrk || opts[:worker]
51
- if block_given? && connected?
52
- instance_eval(&block)
53
- else
54
- raise "Worker is null!" if @worker.blank?
55
- raise "Not connected!" unless connected?
50
+ def process!(wrk = nil, &block)
51
+ wait_with_timeout(@opts[:timeout]) { connected? }
52
+ @worker = wrk || opts[:worker]
53
+ if block_given? && connected?
54
+ instance_eval(&block)
55
+ else
56
+ raise "Worker is null!" if @worker.blank?
57
+ raise "Not connected!" unless connected?
58
+ end
59
+ worker.perform!
56
60
  end
57
- worker.perform!
58
- end
59
61
 
60
- def connected?
61
- @connected
62
- end
62
+ def connected?
63
+ @connected
64
+ end
63
65
 
64
- def on_connect(&block)
65
- @on_connect ||= []
66
- @on_connect << block if block_given?
67
- end
66
+ def on_connect(&block)
67
+ @on_connect ||= []
68
+ @on_connect << block if block_given?
69
+ end
68
70
 
69
- def on_login(&block)
70
- @on_login ||= []
71
- @on_login << block if block_given?
72
- end
71
+ def on_login(&block)
72
+ @on_login ||= []
73
+ @on_login << block if block_given?
74
+ end
73
75
 
74
- def login_as(username, password, params = {})
75
- params = input_params.to_sfsobject if params.is_a?(Hash)
76
- @login_as ||= {:username => username, :password => password, :params => params}
77
- end
76
+ def login_as(username, password, params = {})
77
+ params = params.to_sfsobject if params.is_a?(Hash)
78
+ @login_as = {:username => username, :password => password, :params => params}
79
+ end
78
80
 
79
- private
81
+ private
80
82
 
81
- ###############
82
- ### HELPERS ###
83
+ ###############
84
+ ### HELPERS ###
83
85
 
84
- def request(*args)
85
- worker.request(*args)
86
- end
86
+ def request(*args)
87
+ worker.request(*args)
88
+ end
87
89
 
88
- def worker(opts = {})
89
- @worker ||= SfsWorker::Worker.new(@smart_fox, opts)
90
- @worker
91
- end
90
+ def worker(opts = {})
91
+ @worker ||= SfsWorker::Worker.new(@smart_fox, opts)
92
+ @worker
93
+ end
92
94
 
93
- def sfs_send(req)
94
- smart_fox.send(req)
95
- end
95
+ def sfs_send(req)
96
+ smart_fox.send(req)
97
+ end
96
98
 
97
- #########################
98
- ### EVENTS PROCESSORS ###
99
+ #########################
100
+ ### EVENTS PROCESSORS ###
99
101
 
100
- def on_extension_response_event(event)
101
- debug "extension_response #{event.arguments.get("cmd")}"
102
- @worker.response(event.arguments.get("cmd"), event.arguments.get("params").try(:to_hash) || {}) unless @worker.blank?
103
- end
102
+ def on_extension_response_event(event)
103
+ debug "extension_response #{event.arguments.get("cmd")}"
104
+ @worker.response(event.arguments.get("cmd"), event.arguments.get("params").try(:to_hash) || {}) unless @worker.blank?
105
+ end
104
106
 
105
- def on_connection_event(event)
106
- debug "on_connection #{event}"
107
- @on_connect.each { |block|
108
- block.call(event) if block.is_a?(Proc)
109
- } unless @on_connect.blank?
110
- sfs_send(LoginRequest.new(@login_as[:username], @login_as[:password], opts[:zone],
111
- (@login_as[:params] || {}).to_sfsobject));
112
- end
107
+ def on_connection_event(event)
108
+ debug "on_connection #{event}"
109
+ @on_connect.each { |block|
110
+ block.call(event) if block.is_a?(Proc)
111
+ } unless @on_connect.blank?
112
+ sfs_send(LoginRequest.new(@login_as[:username], @login_as[:password], opts[:zone],
113
+ (@login_as[:params] || {}.to_sfsobject)));
114
+ end
113
115
 
114
- def on_login_event(event)
115
- args = event.arguments.get("data").to_hash
116
- user = event.arguments.get("user")
117
- debug("on_login, args=#{args.to_json}")
118
- unless user.blank?
119
- info "connected as #{user.name}"
120
- @username = user.name
121
- @connected = true
122
- @on_login.each { |block|
123
- block.call(@username) if block.is_a?(Proc)
124
- } unless @on_login.blank?
125
- else
126
- raise "login error '#{args[Aimy::Param.ERROR_MESSAGE]}'"
116
+ def on_login_event(event)
117
+ args = event.arguments.get("data").to_hash
118
+ user = event.arguments.get("user")
119
+ debug("on_login, args=#{args.to_json}")
120
+ unless user.blank?
121
+ info "connected as #{user.name}"
122
+ @username = user.name
123
+ @connected = true
124
+ @on_login.each { |block|
125
+ block.call(@username) if block.is_a?(Proc)
126
+ } unless @on_login.blank?
127
+ else
128
+ raise "login error '#{args[Aimy::Param.ERROR_MESSAGE]}'"
129
+ end
127
130
  end
128
- end
129
131
 
130
- def on_login_error_event(event)
131
- error("error while logging-in: #{event.arguments}")
132
- end
132
+ def on_login_error_event(event)
133
+ error("error while logging-in: #{event.arguments}")
134
+ end
133
135
 
134
- def on_connection_lost_event(event)
135
- # nothing to do
136
- end
136
+ def on_connection_lost_event(event)
137
+ # nothing to do
138
+ end
137
139
 
138
- def on_handshake_event(event)
139
- # nothing to do
140
- end
140
+ def on_handshake_event(event)
141
+ # nothing to do
142
+ end
141
143
 
142
144
 
143
- #######################
144
- ### SERVICE HELPERS ###
145
+ #######################
146
+ ### SERVICE HELPERS ###
145
147
 
146
- def logger
147
- opts[:logger]
148
- end
148
+ def logger
149
+ opts[:logger]
150
+ end
149
151
 
150
- def debug_enabled?
151
- opts[:debug] || DEBUG
152
- end
152
+ def debug_enabled?
153
+ opts[:debug] || DEBUG
154
+ end
153
155
 
154
- def debug(msg)
155
- logger.debug "#{msg}" if debug_enabled?
156
- end
156
+ def debug(msg)
157
+ logger.debug "#{msg}" if debug_enabled?
158
+ end
157
159
 
158
- def info(msg)
159
- logger.info "#{msg}"
160
- end
160
+ def info(msg)
161
+ logger.info "#{msg}"
162
+ end
161
163
 
162
- def log(msg)
163
- logger.info "#{msg}"
164
- end
164
+ def log(msg)
165
+ logger.info "#{msg}"
166
+ end
165
167
 
166
- def error(msg)
167
- logger.error "#{msg}"
168
- end
168
+ def error(msg)
169
+ logger.error "#{msg}"
170
+ end
169
171
 
172
+ end
170
173
 
171
174
  end
172
-