rubyfox-client 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm jruby@rubyfox-client --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubyfox-client.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ = Rubyfox::Client
2
+
3
+ Ruby binding for SmartFox's client.
4
+ http://docs2x.smartfoxserver.com/api-docs/javadoc/client/
5
+
6
+ Gem[https://rubygems.org/gems/rubyfox-client] |
7
+ Source[https://github.com/neopoly/rubyfox-client] |
8
+ Documentation[http://rubydoc.info/github/neopoly/rubyfox-client/master/file/README.rdoc]
9
+
10
+ == Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'rubyfox-client'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install rubyfox-client
23
+
24
+ == Usage
25
+
26
+ See examples/ for usage.
27
+
28
+ == TODO
29
+
30
+ * Tests!
31
+
32
+ == Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc 'Default: run unit tests.'
4
+ task :default => :test
5
+
6
+ # Test
7
+ require 'rake/testtask'
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ t.verbose = true
12
+ end
13
+
14
+ # RDoc
15
+ require 'rdoc/task'
16
+ RDoc::Task.new do |rdoc|
17
+ rdoc.title = "Rubyfox Client"
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.main = 'README.rdoc'
20
+ rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
21
+ end
@@ -0,0 +1,44 @@
1
+ require 'java'
2
+ require 'rubyfox/client'
3
+
4
+ unless ARGV.size == 3
5
+ abort "usage: #{$0} username password zone"
6
+ end
7
+
8
+ unless ENV['SF_DIR']
9
+ abort "Point SF_DIR to your SmartFox installation"
10
+ end
11
+
12
+ Rubyfox::Client.require_libs(ENV['SF_DIR'] + "/lib")
13
+ Rubyfox::Client.boot!
14
+
15
+ client = Rubyfox::Client.new(:debug => true)
16
+
17
+ client.on_event :connection do
18
+ client.send :login, *ARGV
19
+ end
20
+
21
+ client.on_event :connection_attempt_http, :connection_resume, :connection_retry do |event|
22
+ p :problems => event
23
+ end
24
+
25
+ client.on_event :login do |event|
26
+ p :login => event
27
+ client.send_extension "KeepAlive"
28
+ end
29
+
30
+ client.on_event :login_error, :connection_lost, :logout do |event|
31
+ p event
32
+ client.exit
33
+ end
34
+
35
+ client.on_extension "KeepAlive" do |request|
36
+ p request
37
+ next_in = request.params.get_int("next_in")
38
+ Thread.start do
39
+ sleep next_in
40
+ client.send_extension "KeepAlive"
41
+ end
42
+ end
43
+
44
+ client.connect
@@ -0,0 +1,32 @@
1
+ module Rubyfox
2
+ module Client
3
+ class Config
4
+ def initialize(options={})
5
+ @options = options
6
+ end
7
+
8
+ def host
9
+ @options[:host] || "127.0.0.1"
10
+ end
11
+
12
+ def port
13
+ @options[:port] || 9933
14
+ end
15
+
16
+ def debug?
17
+ @options[:debug] || false
18
+ end
19
+
20
+ def udp?
21
+ false
22
+ end
23
+
24
+ def bluebox?
25
+ false
26
+ end
27
+
28
+ def bluebox_port
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubyfox/client/java'
2
+
3
+ module Rubyfox
4
+ module Client
5
+ Event = Java::SFSEvent
6
+
7
+ class Event
8
+ def self.types(&block)
9
+ constants
10
+ end
11
+
12
+ def self.[](name)
13
+ const_get(name.to_s.upcase)
14
+ end
15
+
16
+ def method_missing(name, *args, &block)
17
+ self[name]
18
+ end
19
+
20
+ def inspect
21
+ "#{super}: #{type}(#{arguments.inspect})"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ require 'rubyfox/client/event'
2
+
3
+ module Rubyfox
4
+ module Client
5
+ class EventHandler
6
+ def initialize(smartfox)
7
+ @handler = Hash.new { |hash, type| hash[type] = [] }
8
+ @smartfox = smartfox
9
+ end
10
+
11
+ def register
12
+ Event.types.each do |type|
13
+ @smartfox.add_event_listener Event[type], self
14
+ end
15
+ end
16
+
17
+ def unregister
18
+ @smartfox.remove_all_event_listeners
19
+ end
20
+
21
+ def add(*names, &block)
22
+ names.each do |name|
23
+ type = Event[name]
24
+ @handler[type] << block
25
+ end
26
+ end
27
+
28
+ def remove(*names)
29
+ names.each do |name|
30
+ type = Event[name]
31
+ @handler[type].clear
32
+ end
33
+ end
34
+
35
+ def dispatch(event)
36
+ type = event.type
37
+
38
+ @handler[type].each do |handler|
39
+ handler.call(event)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,43 @@
1
+ module Rubyfox
2
+ module Client
3
+ class ExtensionHandler
4
+ Request = Struct.new(:command, :params, :room, :packet_id)
5
+
6
+ def initialize(event_handler)
7
+ @handler = Hash.new { |hash, type| hash[type] = [] }
8
+ @event_handler = event_handler
9
+ end
10
+
11
+ def register
12
+ @event_handler.add(:extension_response) do |event|
13
+ request = Request.new(*event.arguments.values_at("cmd", "params", "sourceRoom", "packetId"))
14
+ dispatch(request)
15
+ end
16
+ end
17
+
18
+ def unregister
19
+ @event_handler.remove(:extension_response)
20
+ end
21
+
22
+ def add(*names, &block)
23
+ names.each do |name|
24
+ @handler[name.to_s] << block
25
+ end
26
+ end
27
+
28
+ def remove(*names)
29
+ names.each do |name|
30
+ @handler[name.to_s].clear
31
+ end
32
+ end
33
+
34
+ def dispatch(request)
35
+ command = request.command
36
+
37
+ @handler[command].each do |handler|
38
+ handler.call(request)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ require 'java'
2
+
3
+ module Rubyfox
4
+ module Client
5
+ module Java
6
+ SmartFox = ::Java.sfs2x.client.SmartFox
7
+ Requests = ::Java.sfs2x.client.requests
8
+ SFSEvent = ::Java.sfs2x.client.core.SFSEvent
9
+ System = ::Java.java.lang.System
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'rubyfox/client/java'
3
+
4
+ module Rubyfox
5
+ module Client
6
+ Requests = Java::Requests
7
+
8
+ module Requests
9
+ def self.[](*args)
10
+ arg = args.shift
11
+ case arg
12
+ when Java::Requests::BaseRequest
13
+ arg
14
+ else
15
+ name = arg.to_s.camelcase
16
+ name += "Request" unless name.end_with?("Request")
17
+ request_klass = Requests.__send__(name)
18
+ request_klass.new(*args)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,60 @@
1
+ require 'rubyfox/client/java'
2
+ require 'rubyfox/client/config'
3
+ require 'rubyfox/client/requests'
4
+ require 'rubyfox/client/event_handler'
5
+ require 'rubyfox/client/extension_handler'
6
+
7
+ module Rubyfox
8
+ module Client
9
+ class Transport
10
+ def initialize(config)
11
+ @config = config
12
+ @smartfox = Java::SmartFox.new(@config.debug?)
13
+ @event_handler = EventHandler.new(@smartfox)
14
+ @extension_handler = ExtensionHandler.new(@event_handler)
15
+ end
16
+
17
+ def connect
18
+ @event_handler.register
19
+ @extension_handler.register
20
+ @smartfox.connect(@config.host, @config.port)
21
+ end
22
+
23
+ def disconnect
24
+ @smartfox.disconnect
25
+ @extension_handler.unregister
26
+ @event_handler.unregister
27
+ end
28
+
29
+ def exit(ret=0)
30
+ disconnect
31
+ Java::System.exit(ret)
32
+ end
33
+
34
+ def send(request, *args)
35
+ request = Requests[request, *args]
36
+ @smartfox.send(request)
37
+ end
38
+
39
+ def send_extension(command, params=nil, room=nil)
40
+ send :extension, command.to_s, params, room
41
+ end
42
+
43
+ def on_extension(*commands, &block)
44
+ @extension_handler.add(*commands, &block)
45
+ end
46
+
47
+ def remove_extension(*commands)
48
+ @extension.remove(*commands)
49
+ end
50
+
51
+ def on_event(*names, &block)
52
+ @event_handler.add(*names, &block)
53
+ end
54
+
55
+ def remove_event(*names)
56
+ @event_handler.remove(*names)
57
+ end
58
+ end
59
+ end
60
+ end
Binary file
@@ -0,0 +1,5 @@
1
+ module Rubyfox
2
+ module Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ module Rubyfox
2
+ module Client
3
+ def self.new(*args)
4
+ config = Config.new(*args)
5
+ Transport.new(config)
6
+ end
7
+
8
+ def self.boot!(vendor_dir=File.expand_path('client/vendor', File.dirname(__FILE__)))
9
+ require_libs vendor_dir
10
+
11
+ require 'rubyfox/client/transport'
12
+ require 'rubyfox/client/event_handler'
13
+ end
14
+
15
+ def self.require_libs(dir)
16
+ glob = File.join(dir, '*.jar')
17
+ libs = Dir[glob].to_a
18
+ if libs.empty?
19
+ abort "No libs to require for #{glob}"
20
+ end
21
+ libs.each { |lib| require lib }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubyfox/client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rubyfox-client"
8
+ gem.version = Rubyfox::Client::VERSION
9
+ gem.platform = Gem::Platform::JAVA
10
+ gem.authors = ["Peter Suschlik", "Jakob Holderbaum"]
11
+ gem.email = ["ps@neopoly.de", "jh@neopoly.de"]
12
+ gem.description = %q{Ruby bindings for SmartFox's client.}
13
+ gem.summary = %q{}
14
+ gem.homepage = "https://github.com/neopoly/rubyfox-client"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'activesupport'
22
+
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rdoc'
25
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyfox-client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: java
7
+ authors:
8
+ - Peter Suschlik
9
+ - Jakob Holderbaum
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ version_requirements: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: !binary |-
22
+ MA==
23
+ none: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: !binary |-
29
+ MA==
30
+ none: false
31
+ prerelease: false
32
+ type: :runtime
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: !binary |-
40
+ MA==
41
+ none: false
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: !binary |-
47
+ MA==
48
+ none: false
49
+ prerelease: false
50
+ type: :development
51
+ - !ruby/object:Gem::Dependency
52
+ name: rdoc
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: !binary |-
58
+ MA==
59
+ none: false
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: !binary |-
65
+ MA==
66
+ none: false
67
+ prerelease: false
68
+ type: :development
69
+ description: Ruby bindings for SmartFox's client.
70
+ email:
71
+ - ps@neopoly.de
72
+ - jh@neopoly.de
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rvmrc
79
+ - Gemfile
80
+ - README.rdoc
81
+ - Rakefile
82
+ - examples/login_tester.rb
83
+ - lib/rubyfox/client.rb
84
+ - lib/rubyfox/client/config.rb
85
+ - lib/rubyfox/client/event.rb
86
+ - lib/rubyfox/client/event_handler.rb
87
+ - lib/rubyfox/client/extension_handler.rb
88
+ - lib/rubyfox/client/java.rb
89
+ - lib/rubyfox/client/requests.rb
90
+ - lib/rubyfox/client/transport.rb
91
+ - lib/rubyfox/client/vendor/SFS2X_API_Java.jar
92
+ - lib/rubyfox/client/vendor/jdom.jar
93
+ - lib/rubyfox/client/vendor/netty-3.2.2.Final.jar
94
+ - lib/rubyfox/client/vendor/sfs2x-client-core.jar
95
+ - lib/rubyfox/client/vendor/slf4j-api-1.6.1.jar
96
+ - lib/rubyfox/client/version.rb
97
+ - rubyfox-client.gemspec
98
+ homepage: https://github.com/neopoly/rubyfox-client
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ hash: 2
111
+ version: !binary |-
112
+ MA==
113
+ none: false
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ segments:
119
+ - 0
120
+ hash: 2
121
+ version: !binary |-
122
+ MA==
123
+ none: false
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.24
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: ''
130
+ test_files: []