smartfox_jruby 0.2.1-java → 0.2.2-java

Sign up to get free protection for your applications and to get access to all the features.
data/Doubleshot CHANGED
@@ -1,21 +1,27 @@
1
1
  # encoding: utf-8
2
2
  require 'pathname'
3
- require Pathname.new(File.dirname(File.expand_path(__FILE__))).join('lib/smartfox_jruby.rb')
3
+ require File.join(File.dirname(File.expand_path(__FILE__)),"lib", "smartfox_jruby")
4
+
4
5
  Doubleshot.new do |config|
5
6
 
6
7
  config.project = "smartfox_jruby"
7
8
  config.version = SmartfoxJruby::VERSION
8
9
 
9
- config.gem 'activesupport', '~> 3.2.8'
10
- config.gem 'json'
11
- config.gem 'rspec', '~> 2.10.0'
12
-
13
- config.mvn_repository 'http://repo1.maven.org/maven2'
14
- config.mvn_repository 'http://artifactory-innovations.i-free.ru/artifactory/ext-release-local'
15
-
10
+ config.mvn_repository 'http://maven.smecsia.me/'
16
11
  config.jar 'com.smartfox2x.client:sfs2x-client-core:jar:1.0.4'
17
12
  config.jar 'com.smartfox2x.client:sfs2x-api-java:jar:1.0.4'
18
13
  config.jar 'org.slf4j:slf4j-log4j12:jar:1.5.10'
14
+ config.jar 'io.netty:netty:jar:3.5.3.Final'
15
+ config.jar 'commons-beanutils:commons-beanutils:jar:1.7.0'
16
+ config.jar 'net.sf.ezmorph:ezmorph:jar:1.0.6'
17
+ config.jar 'net.sf.json-lib:json-lib:jar:jdk5:2.2.3'
18
+ config.jar 'commons-lang:commons-lang:jar:2.5'
19
+ config.jar 'commons-collections:commons-collections:jar:3.2.1'
20
+ config.jar 'commons-logging:commons-logging:jar:1.1.1'
21
+
22
+ config.gem 'activesupport', '~> 3.2.8'
23
+ config.gem 'json'
24
+ config.gem 'rspec', '~> 2.10.0'
19
25
 
20
26
  config.gemspec do |spec|
21
27
  spec.summary = "Smartfox client for jruby"
@@ -29,4 +35,4 @@ Doubleshot.new do |config|
29
35
  spec.license = "Apache 2.0"
30
36
  end
31
37
 
32
- end
38
+ end
@@ -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 = opts[: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
-
@@ -3,120 +3,123 @@ require File.expand_path(File.dirname(__FILE__) + '/common')
3
3
  java_import java.lang.Runtime
4
4
  java_import java.lang.System
5
5
 
6
- class SmartfoxJruby::SfsRunner
7
-
8
- def initialize(home_dir, opts = {})
9
- @home_dir = home_dir
10
- @opts = opts
11
- @launched = false
12
- @fault = false
13
- @pid = nil
14
- end
6
+ module SmartfoxJruby
7
+ class SfsRunner
8
+
9
+ def initialize(home_dir, opts = {})
10
+ @home_dir = home_dir
11
+ @opts = opts
12
+ @launched = false
13
+ @fault = false
14
+ @pid = nil
15
+ end
15
16
 
16
- def run!
17
- Thread.new do
18
- begin
19
- info "Changing dir to #{work_dir}"
20
- Dir.chdir(work_dir) do
21
- info "Running cmd #{cmd}"
22
- IO.popen(cmd) do |output|
23
- @pid = output.pid
24
- info "Running child with pid=#{output.pid}..."
25
- output.each do |line|
26
- debug(line.gsub(/\n/, ""))
27
- @launched = true if line =~ /SmartFoxServer 2X \(.+\) READY!/
17
+ def run!
18
+ Thread.new do
19
+ begin
20
+ info "Changing dir to #{work_dir}"
21
+ Dir.chdir(work_dir) do
22
+ info "Running cmd #{cmd}"
23
+ IO.popen(cmd) do |output|
24
+ @pid = output.pid
25
+ info "Running child with pid=#{output.pid}..."
26
+ output.each do |line|
27
+ debug(line.gsub(/\n/, ""))
28
+ @launched = true if line =~ /SmartFoxServer 2X \(.+\) READY!/
29
+ end
28
30
  end
31
+ @fault = true
29
32
  end
30
- @fault = true
33
+ rescue Exception => e
34
+ error "#{e}"
31
35
  end
32
- rescue Exception => e
33
- error "#{e}"
34
36
  end
35
37
  end
36
- end
37
38
 
38
- def kill!
39
- info "Checking running processes: #{pids.join(",")}"
40
- pids.each { |pid|
41
- pid = pid.try(:strip).try(:to_i)
42
- info "Killing the process with pid=#{pid}..."
43
- Process.kill("KILL", pid) if Process.alive?(pid)
44
- wait_with_timeout(5) { !Process.alive?(pid) } rescue ""
45
- }
46
- end
39
+ def kill!
40
+ info "Checking running processes: #{pids.join(",")}"
41
+ pids.each { |pid|
42
+ pid = pid.try(:strip).try(:to_i)
43
+ info "Killing the process with pid=#{pid}..."
44
+ Process.kill("KILL", pid) if Process.alive?(pid)
45
+ wait_with_timeout(5) { !Process.alive?(pid) } rescue ""
46
+ }
47
+ end
47
48
 
48
- def run_and_wait!(opts = {})
49
- run!
50
- wait_until_launched_or_fault(opts[:timeout])
51
- end
49
+ def run_and_wait!(opts = {})
50
+ run!
51
+ wait_until_launched_or_fault(opts[:timeout])
52
+ end
52
53
 
53
54
 
54
- def kill_and_wait!(opts = {})
55
- kill!
56
- wait_until_terminated(opts[:timeout])
57
- end
55
+ def kill_and_wait!(opts = {})
56
+ kill!
57
+ wait_until_terminated(opts[:timeout])
58
+ end
58
59
 
59
- def running?
60
- !pids.blank? && pids.map { |pid| Process.alive?(pid.to_i) }.include?(true)
61
- end
60
+ def running?
61
+ !pids.blank? && pids.map { |pid| Process.alive?(pid.to_i) }.include?(true)
62
+ end
62
63
 
63
- def launched?
64
- @launched
65
- end
64
+ def launched?
65
+ @launched
66
+ end
66
67
 
67
- def fault?
68
- @fault
69
- end
68
+ def fault?
69
+ @fault
70
+ end
70
71
 
71
- def wait_until_terminated(timeout = nil)
72
- wait_with_timeout(timeout) { !running? }
73
- end
72
+ def wait_until_terminated(timeout = nil)
73
+ wait_with_timeout(timeout) { !running? }
74
+ end
74
75
 
75
- def wait_until_launched_or_fault(timeout = nil)
76
- wait_with_timeout(timeout) { launched? or fault? }
77
- end
76
+ def wait_until_launched_or_fault(timeout = nil)
77
+ wait_with_timeout(timeout) { launched? or fault? }
78
+ end
78
79
 
79
- private
80
+ private
80
81
 
81
- def pids
82
- pids_out = `pgrep -f "#{work_dir}"`
83
- (pids_out.try(:split, "\n")) || []
84
- end
82
+ def pids
83
+ pids_out = `pgrep -f "#{work_dir}"`
84
+ (pids_out.try(:split, "\n")) || []
85
+ end
85
86
 
86
- def error(msg)
87
- @opts[:logger].error(msg) unless @opts[:logger].blank?
88
- end
87
+ def error(msg)
88
+ @opts[:logger].error(msg) unless @opts[:logger].blank?
89
+ end
89
90
 
90
- def debug(msg)
91
- @opts[:logger].debug(msg) unless @opts[:logger].blank?
92
- end
91
+ def debug(msg)
92
+ @opts[:logger].debug(msg) unless @opts[:logger].blank?
93
+ end
93
94
 
94
- def info(msg)
95
- @opts[:logger].info(msg) unless @opts[:logger].blank?
96
- end
95
+ def info(msg)
96
+ @opts[:logger].info(msg) unless @opts[:logger].blank?
97
+ end
97
98
 
98
- def work_dir
99
- Pathname.new(@home_dir).join("SFS2X").to_s
100
- end
99
+ def work_dir
100
+ Pathname.new(@home_dir).join("SFS2X").to_s
101
+ end
101
102
 
102
- def env
103
- %W[].to_java :string
104
- end
103
+ def env
104
+ %W[].to_java :string
105
+ end
105
106
 
106
- def cmd
107
- %Q[#{java_bin} -cp #{classpath} #{java_opts} -Dsmartfox.work_dir="#{work_dir}" com.smartfoxserver.v2.Main]
108
- end
107
+ def cmd
108
+ %Q[#{java_bin} -cp #{classpath} #{java_opts} -Dsmartfox.work_dir="#{work_dir}" com.smartfoxserver.v2.Main]
109
+ end
109
110
 
110
- def java_bin
111
- Pathname.new(System.getProperty("java.home")).join("bin").join("java")
112
- end
111
+ def java_bin
112
+ Pathname.new(System.getProperty("java.home")).join("bin").join("java")
113
+ end
113
114
 
114
- def java_opts
115
- %Q[-XX:MaxPermSize=512m -Xms128m -Xms1024m]
116
- end
115
+ def java_opts
116
+ %Q[-XX:MaxPermSize=512m -Xms128m -Xms1024m]
117
+ end
118
+
119
+ def classpath
120
+ %Q[aimy:./:lib/*:lib/Jetty/*:extensions/__lib__/*]
121
+ end
117
122
 
118
- def classpath
119
- %Q[aimy:./:lib/*:lib/Jetty/*:extensions/__lib__/*]
120
123
  end
121
124
 
122
125
  end
@@ -1,256 +1,258 @@
1
1
  require 'thread'
2
2
  require File.expand_path(File.dirname(__FILE__) + '/common')
3
3
 
4
- module SmartfoxJruby::SfsWorker
5
- class Processor
6
- attr_accessor :opts
7
- attr_accessor :name
8
- attr_accessor :chained
9
- attr_accessor :current
10
- attr_accessor :blocks
11
-
12
- def initialize(name, opts = {}, &block)
13
- @blocks = [block]
14
- @name = name
15
- @current = self
16
- @chained = nil
17
- @opts = opts
18
- end
4
+ module SmartfoxJruby
5
+ module SfsWorker
6
+ class Processor
7
+ attr_accessor :opts
8
+ attr_accessor :name
9
+ attr_accessor :chained
10
+ attr_accessor :current
11
+ attr_accessor :blocks
12
+
13
+ def initialize(name, opts = {}, &block)
14
+ @blocks = [block]
15
+ @name = name
16
+ @current = self
17
+ @chained = nil
18
+ @opts = opts
19
+ end
19
20
 
20
- def chain(name, &block)
21
- link.chained = Processor.new(name, @opts, &block)
22
- self
23
- end
21
+ def chain(name, &block)
22
+ link.chained = Processor.new(name, @opts, &block)
23
+ self
24
+ end
24
25
 
25
- def link
26
- p = current
27
- p = p.chained while (!p.try(:chained).blank?)
28
- p
29
- end
26
+ def link
27
+ p = current
28
+ p = p.chained while (!p.try(:chained).blank?)
29
+ p
30
+ end
30
31
 
31
- def append(&block)
32
- unless link.blank?
33
- link.blocks << block
34
- else
35
- current.blocks << block
32
+ def append(&block)
33
+ unless link.blank?
34
+ link.blocks << block
35
+ else
36
+ current.blocks << block
37
+ end
38
+ self
36
39
  end
37
- self
38
- end
39
40
 
40
- def completed?
41
- current.blank?
42
- end
41
+ def completed?
42
+ current.blank?
43
+ end
43
44
 
44
- def mandatory?
45
- @opts[:mandatory].nil? || @opts[:mandatory]
46
- end
45
+ def mandatory?
46
+ @opts[:mandatory].nil? || @opts[:mandatory]
47
+ end
47
48
 
48
- def event(name, data = {})
49
- if @current.name.to_s == name.to_s
50
- @current.blocks.each { |b| b.call(data) }
51
- @current = (@current.chained.blank?) ? nil : @current.chained
52
- return true
49
+ def event(name, data = {})
50
+ if @current.name.to_s == name.to_s
51
+ @current.blocks.each { |b| b.call(data) }
52
+ @current = (@current.chained.blank?) ? nil : @current.chained
53
+ return true
54
+ end
55
+ false
53
56
  end
54
- false
55
- end
56
57
 
57
- def to_s
58
- "Proc[#{current.try(:name)}] --> #{current.try(:chained)}"
58
+ def to_s
59
+ "Proc[#{current.try(:name)}] --> #{current.try(:chained)}"
60
+ end
59
61
  end
60
- end
61
62
 
62
- class Request
63
- attr_reader :name
64
- attr_reader :data
63
+ class Request
64
+ attr_reader :name
65
+ attr_reader :data
65
66
 
66
- def initialize(name, data = {})
67
- @name = name
68
- @data = data
69
- end
67
+ def initialize(name, data = {})
68
+ @name = name
69
+ @data = data
70
+ end
70
71
 
71
- def to_extension_request
72
- ExtensionRequest.new(@name.to_s, @data)
73
- end
72
+ def to_extension_request
73
+ ExtensionRequest.new(@name.to_s, @data)
74
+ end
74
75
 
75
- def to_s
76
- "Req[#{@name}]#{data.to_json}"
76
+ def to_s
77
+ "Req[#{@name}]#{data.to_json}"
78
+ end
77
79
  end
78
- end
79
80
 
80
- class Response < Request
81
- def to_s
82
- "Resp[#{@name}]#{data.to_json}"
81
+ class Response < Request
82
+ def to_s
83
+ "Resp[#{@name}]#{data.to_json}"
84
+ end
83
85
  end
84
- end
85
86
 
86
- class ContextWorker
87
+ class ContextWorker
87
88
 
88
- def request(name, data = {})
89
- @worker.request(name, data, :context => @context)
90
- end
89
+ def request(name, data = {})
90
+ @worker.request(name, data, :context => @context)
91
+ end
91
92
 
92
- def append_processor(opts = {}, &block)
93
- @worker.append_processor(opts.merge(:context => @context), &block)
94
- end
93
+ def append_processor(opts = {}, &block)
94
+ @worker.append_processor(opts.merge(:context => @context), &block)
95
+ end
95
96
 
96
- def expect(name, opts={}, &block)
97
- @worker.expect(name, opts.merge(:context => @context), &block)
98
- end
97
+ def expect(name, opts={}, &block)
98
+ @worker.expect(name, opts.merge(:context => @context), &block)
99
+ end
99
100
 
100
- def initialize(context, worker)
101
- @context = context
102
- @worker = worker
101
+ def initialize(context, worker)
102
+ @context = context
103
+ @worker = worker
104
+ end
103
105
  end
104
- end
105
106
 
106
- class Worker
107
- attr_reader :send_queue
108
- attr_reader :events_queue
109
- attr_reader :processors
110
- attr_reader :smart_fox
111
- attr_reader :opts
112
-
113
- def initialize(smart_fox, opts = {})
114
- @send_queue = []
115
- @processors = []
116
- @events_queue = []
117
- @opts = opts
118
- @opts[:timeout] ||= 20
119
- @smart_fox = smart_fox
120
- @mutex = Mutex.new
121
- @send_qm = Mutex.new
122
- end
107
+ class Worker
108
+ attr_reader :send_queue
109
+ attr_reader :events_queue
110
+ attr_reader :processors
111
+ attr_reader :smart_fox
112
+ attr_reader :opts
113
+
114
+ def initialize(smart_fox, opts = {})
115
+ @send_queue = []
116
+ @processors = []
117
+ @events_queue = []
118
+ @opts = opts
119
+ @opts[:timeout] ||= 20
120
+ @smart_fox = smart_fox
121
+ @mutex = Mutex.new
122
+ @send_qm = Mutex.new
123
+ end
123
124
 
124
- def append_processor(opts = {}, &block)
125
- debug "appending processor with context #{opts[:context]}"
126
- if opts[:context]
127
- context = opts[:context].append(&block)
128
- else
129
- context = processors.last.append(&block)
125
+ def append_processor(opts = {}, &block)
126
+ debug "appending processor with context #{opts[:context]}"
127
+ if opts[:context]
128
+ context = opts[:context].append(&block)
129
+ else
130
+ context = processors.last.append(&block)
131
+ end
132
+ ContextWorker.new(context, self)
130
133
  end
131
- ContextWorker.new(context, self)
132
- end
133
134
 
134
- def request(name, data = {}, opts = {})
135
- debug "create request #{name} with context #{opts[:context]}"
136
- data = data.to_sfsobject(opts[:serialize_opts]) if data.is_a?(Hash)
137
- req = Request.new(name, data)
138
- if !opts[:context].blank? && opts[:context].is_a?(Processor)
139
- debug "appending #{req} to processor #{opts[:context]}"
140
- context = opts[:context].append {
135
+ def request(name, data = {}, opts = {})
136
+ debug "create request #{name} with context #{opts[:context]}"
137
+ data = data.to_sfsobject(opts[:serialize_opts]) if data.is_a?(Hash)
138
+ req = Request.new(name, data)
139
+ if !opts[:context].blank? && opts[:context].is_a?(Processor)
140
+ debug "appending #{req} to processor #{opts[:context]}"
141
+ context = opts[:context].append {
142
+ @send_qm.synchronize { send_queue << req }
143
+ }
144
+ else
145
+ debug "adding #{req} to send_queue \n #{dump_state}"
146
+ context = req
141
147
  @send_qm.synchronize { send_queue << req }
142
- }
143
- else
144
- debug "adding #{req} to send_queue \n #{dump_state}"
145
- context = req
146
- @send_qm.synchronize { send_queue << req }
148
+ end
149
+ ContextWorker.new(context, self)
147
150
  end
148
- ContextWorker.new(context, self)
149
- end
150
151
 
151
- def expect(name, opts = {}, &block)
152
- debug "Expecting to get response #{name} with context #{opts[:context]}"
153
- unless opts[:context].blank?
154
- if opts[:context].is_a?(Processor)
155
- context = opts[:context].chain(name, &block)
156
- elsif opts[:context].is_a?(Request)
157
- context = Processor.new(name, :context => opts[:context], &block)
152
+ def expect(name, opts = {}, &block)
153
+ debug "Expecting to get response #{name} with context #{opts[:context]}"
154
+ unless opts[:context].blank?
155
+ if opts[:context].is_a?(Processor)
156
+ context = opts[:context].chain(name, &block)
157
+ elsif opts[:context].is_a?(Request)
158
+ context = Processor.new(name, :context => opts[:context], &block)
159
+ @mutex.synchronize { processors << context }
160
+ end
161
+ else
162
+ context = Processor.new(name, &block)
158
163
  @mutex.synchronize { processors << context }
159
164
  end
160
- else
161
- context = Processor.new(name, &block)
162
- @mutex.synchronize { processors << context }
165
+ ContextWorker.new(context, self)
163
166
  end
164
- ContextWorker.new(context, self)
165
- end
166
167
 
167
- def response(name, data = {})
168
- info "Got response #{name} (#{data.to_json})..."
169
- @mutex.synchronize { events_queue << Response.new(name, data) }
170
- end
168
+ def response(name, data = {})
169
+ info "Got response #{name} (#{data.to_json})..."
170
+ @mutex.synchronize { events_queue << Response.new(name, data) }
171
+ end
171
172
 
172
- def perform!
173
- while !all_events_caught?
174
- while !send_queue.blank?
175
- req = nil
176
- @send_qm.synchronize { req = send_queue.shift }
177
- debug "sending request #{req.name}..."
178
- smart_fox.send(req.to_extension_request) unless smart_fox.blank?
179
- @last_act = Time.now.to_i
173
+ def perform!
174
+ while !all_events_caught?
175
+ while !send_queue.blank?
176
+ req = nil
177
+ @send_qm.synchronize { req = send_queue.shift }
178
+ debug "sending request #{req.name}..."
179
+ smart_fox.send(req.to_extension_request) unless smart_fox.blank?
180
+ @last_act = Time.now.to_i
181
+ end
182
+ process_events
183
+ check_timeouts
180
184
  end
181
- process_events
182
- check_timeouts
183
185
  end
184
- end
185
186
 
186
- def all_events_caught?
187
- processors.blank? || processors.collect { |p| p.mandatory? }.blank?
188
- end
187
+ def all_events_caught?
188
+ processors.blank? || processors.collect { |p| p.mandatory? }.blank?
189
+ end
189
190
 
190
- def wait_all_events_caught
191
- debug "Waiting all events being caught..."
192
- begin
193
- wait_with_timeout { all_events_caught? }
194
- rescue WaitTimeoutException => e
195
- raise "Failed to catch all the events:"+ dump_state
191
+ def wait_all_events_caught
192
+ debug "Waiting all events being caught..."
193
+ begin
194
+ wait_with_timeout { all_events_caught? }
195
+ rescue WaitTimeoutException => e
196
+ raise "Failed to catch all the events:"+ dump_state
197
+ end
196
198
  end
197
- end
198
199
 
199
- private
200
+ private
200
201
 
201
- def dump_state
202
- "events_queue = \n\t-#{events_queue.join("\n\t-")}" +
203
- " \n processors = \n\t-#{processors.join("\n\t-")}" +
204
- " \n send_queue = \n\t-#{send_queue.join("\n\t-")}"
205
- end
202
+ def dump_state
203
+ "events_queue = \n\t-#{events_queue.join("\n\t-")}" +
204
+ " \n processors = \n\t-#{processors.join("\n\t-")}" +
205
+ " \n send_queue = \n\t-#{send_queue.join("\n\t-")}"
206
+ end
206
207
 
207
- def check_timeouts
208
- if !@last_act.blank? && Time.now.to_i > @last_act + opts[:timeout]
209
- raise "Worker timeout! Latest interaction was #{Time.now.to_i - @last_act} sec ago!\n #{dump_state}"
208
+ def check_timeouts
209
+ if !@last_act.blank? && Time.now.to_i > @last_act + opts[:timeout]
210
+ raise "Worker timeout! Latest interaction was #{Time.now.to_i - @last_act} sec ago!\n #{dump_state}"
211
+ end
210
212
  end
211
- end
212
213
 
213
- def process_events
214
- wait_with_timeout { !events_queue.blank? } rescue nil
215
- ei = 0
216
- debug "Processing events..."
217
- while ei < events_queue.size
218
- event = events_queue[ei]
219
- pi = 0
220
- debug "Processing event #{event.name}..."
221
- while pi < processors.size
222
- processor = processors[pi]
223
- debug "Trying processor #{processor.name}..."
224
- if processor.event(event.name, event.data)
225
- @last_act = Time.now.to_i
226
- debug "Processor found #{processor}"
227
- @mutex.synchronize { events_queue.delete_at(ei) }
228
- ei -= 1
229
- if processor.completed?
230
- debug "Processor completed #{processor.name}."
231
- @mutex.synchronize { processors.delete_at(pi) }
232
- pi -= 1
214
+ def process_events
215
+ wait_with_timeout { !events_queue.blank? } rescue nil
216
+ ei = 0
217
+ debug "Processing events..."
218
+ while ei < events_queue.size
219
+ event = events_queue[ei]
220
+ pi = 0
221
+ debug "Processing event #{event.name}..."
222
+ while pi < processors.size
223
+ processor = processors[pi]
224
+ debug "Trying processor #{processor.name}..."
225
+ if processor.event(event.name, event.data)
226
+ @last_act = Time.now.to_i
227
+ debug "Processor found #{processor}"
228
+ @mutex.synchronize { events_queue.delete_at(ei) }
229
+ ei -= 1
230
+ if processor.completed?
231
+ debug "Processor completed #{processor.name}."
232
+ @mutex.synchronize { processors.delete_at(pi) }
233
+ pi -= 1
234
+ end
233
235
  end
236
+ pi += 1
234
237
  end
235
- pi += 1
238
+ ei += 1
236
239
  end
237
- ei += 1
240
+ debug "Events processing finished. #{dump_state}"
238
241
  end
239
- debug "Events processing finished. #{dump_state}"
240
- end
241
242
 
242
- def debug(msg)
243
- puts "[#{time}] DEBUG #{msg}" if opts[:debug]
244
- end
243
+ def debug(msg)
244
+ puts "[#{time}] DEBUG #{msg}" if opts[:debug]
245
+ end
245
246
 
246
- def info(msg)
247
- puts "[#{time}] INFO #{msg}"
248
- end
247
+ def info(msg)
248
+ puts "[#{time}] INFO #{msg}"
249
+ end
249
250
 
250
- def time
251
- Time.now.strftime("%Y-%m-%d %H:%M:%S")
251
+ def time
252
+ Time.now.strftime("%Y-%m-%d %H:%M:%S")
253
+ end
252
254
  end
253
255
  end
254
- end
255
256
 
257
+ end
256
258
 
@@ -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.2.1"
11
-
8
+ autoload :SfsRunner, 'smartfox_jruby/sfs_runner'
9
+ autoload :SfsAdapter, 'smartfox_jruby/sfs_adapter'
10
+ autoload :SfsWorker, 'smartfox_jruby/sfs_worker'
11
+ autoload :SFSUtil, 'smartfox_jruby/common'
12
+ VERSION = "0.2.2"
12
13
 
13
14
  end
14
15
 
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: smartfox_jruby
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.2
6
6
  platform: java
7
7
  authors:
8
8
  - Ilya Sadykov
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-10 00:00:00.000000000 Z
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -113,9 +113,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: !binary |-
115
115
  MA==
116
- segments:
117
- - 0
118
- hash: 2
119
116
  none: false
120
117
  required_rubygems_version: !ruby/object:Gem::Requirement
121
118
  requirements: