smartfox 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +15 -11
- data/VERSION +1 -1
- data/lib/smartfox/client.rb +62 -41
- metadata +8 -34
- data/.gitignore +0 -21
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ begin
|
|
10
10
|
gem.email = "self@richardpenwell.me"
|
11
11
|
gem.homepage = "http://github.com/penwellr/smartfox"
|
12
12
|
gem.authors = ["Richard Penwell"]
|
13
|
-
gem.add_development_dependency "rspec", "
|
13
|
+
gem.add_development_dependency "rspec", "~> 2.0"
|
14
14
|
gem.add_dependency 'json'
|
15
15
|
gem.add_dependency 'builder'
|
16
16
|
gem.add_dependency 'libxml-ruby'
|
@@ -22,17 +22,21 @@ rescue LoadError
|
|
22
22
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
spec
|
28
|
-
|
29
|
-
|
25
|
+
begin
|
26
|
+
require 'rspec/core/rake_task'
|
27
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
33
|
+
spec.libs << 'lib' << 'spec'
|
34
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
35
|
+
spec.rcov = true
|
36
|
+
spec.rcov_opts = ['--exclude', "features,kernel,load-diff-lcs\.rb,instance_exec\.rb,lib/spec.rb,lib/spec/runner.rb,^spec/*,bin/spec,examples,/gems,/Library/Ruby,\.autotest,#{ENV['GEM_HOME']}"]
|
37
|
+
end
|
38
|
+
rescue
|
39
|
+
puts "Rspec ~> 2.0 (or a dependency) is not available. Install it with: gem install rspec"
|
36
40
|
end
|
37
41
|
|
38
42
|
task :spec => :check_dependencies
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/smartfox/client.rb
CHANGED
@@ -6,16 +6,17 @@ class SmartFox::Client
|
|
6
6
|
class ConnectionFailureError < SmartFox::SmartFoxError; end
|
7
7
|
class ApiIncompatibleError < SmartFox::SmartFoxError; end
|
8
8
|
class TransportTimeoutError < SmartFox::SmartFoxError; end
|
9
|
-
|
9
|
+
|
10
10
|
attr_reader :connected, :room_list, :buddy_list, :server, :port
|
11
|
-
attr_reader :current_room, :users
|
11
|
+
attr_reader :current_room, :users
|
12
12
|
alias_method :connected?, :connected
|
13
|
+
attr_accessor :user_id, :user_name
|
13
14
|
|
14
15
|
CLIENT_VERSION = "1.5.8"
|
15
16
|
CONNECTION_TIMEOUT = 5
|
16
17
|
TRANSPORTS = [ SmartFox::Socket::Connection, SmartFox::BlueBox::Connection ]
|
17
18
|
EVENTS = [ :connected, :logged_in, :rooms_updated ]
|
18
|
-
|
19
|
+
|
19
20
|
HEADER_SYSTEM = 'sys'
|
20
21
|
HEADER_EXTENDED = 'xt'
|
21
22
|
|
@@ -24,12 +25,13 @@ class SmartFox::Client
|
|
24
25
|
ACTION_API_OBSOLETE = 'apiKO'
|
25
26
|
ACTION_LOGIN = 'login'
|
26
27
|
ACTION_LOGIN_OK = 'logOK'
|
27
|
-
ACTION_AUTO_JOIN = 'autoJoin'
|
28
|
+
ACTION_AUTO_JOIN = 'autoJoin'
|
29
|
+
ACTION_JOIN_ROOM = 'joinRoom'
|
28
30
|
ACTION_UPDATE_ROOMS = 'getRmList'
|
29
31
|
ACTION_ROOM_LIST = 'rmList'
|
30
32
|
ACTION_JOIN_OK = 'joinOK'
|
31
33
|
ACTION_JOIN_FAIL = 'joinKO'
|
32
|
-
|
34
|
+
|
33
35
|
EXTENDED_RESPONSE = 'xtRes'
|
34
36
|
|
35
37
|
def initialize(options = {})
|
@@ -52,11 +54,11 @@ class SmartFox::Client
|
|
52
54
|
started_at = Time.now
|
53
55
|
@transport = transport_class.new(self)
|
54
56
|
@transport.connect
|
55
|
-
|
57
|
+
|
56
58
|
while not connected? and Time.now <= started_at + CONNECTION_TIMEOUT
|
57
59
|
Thread.pass
|
58
60
|
end
|
59
|
-
|
61
|
+
|
60
62
|
return @transport if connected?
|
61
63
|
rescue
|
62
64
|
end
|
@@ -64,52 +66,66 @@ class SmartFox::Client
|
|
64
66
|
raise ConnectionFailureError.new "Could not negotiate any transport with server."
|
65
67
|
end
|
66
68
|
end
|
67
|
-
|
69
|
+
|
68
70
|
def disconnect
|
69
71
|
if @connected
|
70
72
|
@transport.disconnect
|
71
73
|
@connected = false
|
72
74
|
end
|
73
|
-
end
|
74
|
-
|
75
|
+
end
|
76
|
+
|
75
77
|
def extended_command(action, data = nil, room = -1, options = {})
|
76
78
|
options[:format] ||= :xml
|
77
|
-
|
79
|
+
|
78
80
|
case options[:format]
|
79
|
-
|
81
|
+
when :xml
|
80
82
|
send_packet(HEADER_EXTENDED, action, room) { |x| x.cdata!(data || '') }
|
81
83
|
end
|
82
84
|
end
|
83
|
-
|
85
|
+
|
84
86
|
def add_handler(event, &proc)
|
85
87
|
@events[event.to_sym] = [] unless @events[event.to_sym]
|
86
88
|
@events[event.to_sym] << proc
|
87
89
|
end
|
88
|
-
|
90
|
+
|
89
91
|
def refresh_rooms
|
90
92
|
send_packet(HEADER_SYSTEM, ACTION_UPDATE_ROOMS)
|
91
93
|
end
|
92
|
-
|
94
|
+
|
93
95
|
def connect_succeeded
|
94
96
|
send_packet(HEADER_SYSTEM, ACTION_VERSION_CHECK, 0) { |x| x.ver(:v => CLIENT_VERSION.delete('.')) }
|
95
97
|
end
|
96
|
-
|
98
|
+
|
97
99
|
def auto_join
|
98
100
|
send_packet(HEADER_SYSTEM, ACTION_AUTO_JOIN)
|
101
|
+
end
|
102
|
+
|
103
|
+
def join_room(room)
|
104
|
+
#CHECK: activeRoomId == -1 no room has already been entered
|
105
|
+
if(@current_room == -1 || @current_room == nil)
|
106
|
+
leave_current_room = "0"
|
107
|
+
room_to_leave = -1
|
108
|
+
else
|
109
|
+
leave_current_room = "1"
|
110
|
+
room_to_leave = @current_room
|
111
|
+
end
|
112
|
+
send_packet(HEADER_SYSTEM, ACTION_JOIN_ROOM, room.id) { |x|
|
113
|
+
x.room(:id=>room.id,:pwd=>'',:spec=>'0',:leave=>leave_current_room,:old=>room_to_leave)
|
114
|
+
}
|
99
115
|
end
|
100
|
-
|
116
|
+
|
101
117
|
def packet_recieved(data)
|
102
118
|
begin
|
103
119
|
SmartFox::Logger.debug "SmartFox::Client#packet_recieved('#{data}')"
|
104
|
-
packet = SmartFox::Packet.parse(data)
|
120
|
+
packet = SmartFox::Packet.parse(data)
|
105
121
|
case packet.header
|
106
|
-
|
122
|
+
when HEADER_SYSTEM
|
107
123
|
if @connected
|
108
124
|
handle_system_packet(packet)
|
109
125
|
else
|
110
126
|
complete_login(packet)
|
111
127
|
end
|
112
|
-
|
128
|
+
when HEADER_EXTENDED
|
113
129
|
raise_event :extended_response, self, packet.data
|
114
130
|
end
|
115
131
|
rescue => e
|
@@ -117,7 +133,7 @@ class SmartFox::Client
|
|
117
133
|
SmartFox::Logger.exception e
|
118
134
|
end
|
119
135
|
end
|
120
|
-
|
136
|
+
|
121
137
|
def login(zone, username, password = nil)
|
122
138
|
send_packet(HEADER_SYSTEM, ACTION_LOGIN) do |packet|
|
123
139
|
packet.login(:z => zone) do |login|
|
@@ -127,20 +143,21 @@ class SmartFox::Client
|
|
127
143
|
end
|
128
144
|
Thread.pass
|
129
145
|
end
|
130
|
-
|
146
|
+
|
131
147
|
def send_extended(extension, action, options)
|
148
|
+
SmartFox::Logger.debug "send_extended #{extension},#{action},#{options}"
|
132
149
|
options[:format] ||= :xml
|
133
|
-
options[:room] ||= (current_room ? current_room.id : 0)
|
150
|
+
options[:room] ||= (@current_room ? @current_room.id : 0)
|
134
151
|
options[:parameters] ||= {}
|
135
|
-
|
152
|
+
|
136
153
|
case options[:format]
|
137
|
-
|
138
|
-
|
154
|
+
when :xml
|
155
|
+
|
139
156
|
when :json
|
140
157
|
send_extended_json_packet(extension, action, options[:room], options[:parameters])
|
141
158
|
end
|
142
159
|
end
|
143
|
-
|
160
|
+
|
144
161
|
def parse_user(node)
|
145
162
|
if user = @users[node['i'].to_i]
|
146
163
|
user.parse(node)
|
@@ -148,7 +165,7 @@ class SmartFox::Client
|
|
148
165
|
@users[node['i'].to_i] = SmartFox::User.parse(node)
|
149
166
|
end
|
150
167
|
end
|
151
|
-
|
168
|
+
|
152
169
|
private
|
153
170
|
def send_packet(header, action, room_id = -1)
|
154
171
|
xml = Builder::XmlMarkup.new()
|
@@ -163,13 +180,13 @@ class SmartFox::Client
|
|
163
180
|
SmartFox::Logger.debug "SmartFox::Client#send_packet -> #{packet}"
|
164
181
|
@transport.send_data(packet + "\0")
|
165
182
|
end
|
166
|
-
|
183
|
+
|
167
184
|
def send_extended_json_packet(name, action, room, object)
|
168
185
|
packet = { :t => 'xt', :b => { :x => name, :c => action, :r => room, :p => object } }
|
169
186
|
SmartFox::Logger.debug "SmartFox::Client#send_extended_json_packet -> #{packet.to_json}"
|
170
187
|
@transport.send_data(packet.to_json + "\0")
|
171
188
|
end
|
172
|
-
|
189
|
+
|
173
190
|
def raise_event(event_name, *params)
|
174
191
|
event = @events[event_name.to_sym]
|
175
192
|
return unless event
|
@@ -177,7 +194,7 @@ class SmartFox::Client
|
|
177
194
|
event_handler.call(*params)
|
178
195
|
end
|
179
196
|
end
|
180
|
-
|
197
|
+
|
181
198
|
def complete_login(connect_response)
|
182
199
|
if connect_response.header == HEADER_SYSTEM and connect_response.action == ACTION_API_OK
|
183
200
|
@connected = true
|
@@ -187,32 +204,36 @@ class SmartFox::Client
|
|
187
204
|
raise ApiIncompatibleError if connect_response.action == ACTION_API_OBSOLETE
|
188
205
|
raise ConnectionFailureError.new "Did not recieve an expected response from the server."
|
189
206
|
end
|
190
|
-
end
|
191
|
-
|
207
|
+
end
|
208
|
+
|
192
209
|
def handle_system_packet(packet)
|
193
|
-
|
194
|
-
|
210
|
+
SmartFox::Logger.debug "packet.action=#{packet.action}"
|
211
|
+
case packet.action
|
212
|
+
when ACTION_LOGIN_OK
|
213
|
+
SmartFox::Logger.debug "packet.data = #{packet.data}"
|
214
|
+
SmartFox::Logger.debug "packet.data.first = #{packet.data.first}"
|
195
215
|
@username = packet.data.first['n']
|
196
216
|
@moderator = packet.data.first['mod'] != "0"
|
197
217
|
@user_id = packet.data.first['id'].to_i
|
198
218
|
SmartFox::Logger.info "SmartFox::Client logged in as #{@username} (ID:#{@user_id})"
|
199
219
|
raise_event(:logged_in, self)
|
200
|
-
|
220
|
+
when ACTION_ROOM_LIST
|
201
221
|
@rooms.clear
|
202
222
|
packet.data.first.children.each do |room|
|
203
223
|
room_object = SmartFox::Room.parse(self, room)
|
204
224
|
@rooms[room_object.id] = room_object
|
225
|
+
@room_list=@rooms
|
205
226
|
end
|
206
|
-
|
227
|
+
|
207
228
|
raise_event(:rooms_updated, self, @rooms)
|
208
|
-
|
229
|
+
when ACTION_JOIN_OK
|
209
230
|
@current_room = @rooms[packet.room]
|
210
231
|
SmartFox::Logger.info "SmartFox::Client joined room #{packet.room}"
|
211
232
|
SmartFox::Logger.debug "SmartFox::Client#handle_system_packet ACTION_JOIN_OK data => #{packet.data}"
|
212
233
|
@current_room.joined
|
213
234
|
@current_room.parse_users(packet.data.find{|n| n.name == 'uLs'})
|
214
235
|
raise_event(:room_joined, self, @current_room)
|
215
|
-
|
236
|
+
when ACTION_JOIN_FAIL
|
216
237
|
raise_event(:room_join_failed, self)
|
217
238
|
else
|
218
239
|
if packet.room > 0
|
@@ -220,6 +241,6 @@ class SmartFox::Client
|
|
220
241
|
end
|
221
242
|
end
|
222
243
|
end
|
223
|
-
|
224
|
-
|
244
|
+
|
245
|
+
|
225
246
|
end
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smartfox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 3
|
9
|
-
- 1
|
10
|
-
version: 0.3.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.4.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Richard Penwell
|
@@ -15,7 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date:
|
13
|
+
date: 2011-02-28 00:00:00 -08:00
|
19
14
|
default_executable:
|
20
15
|
dependencies:
|
21
16
|
- !ruby/object:Gem::Dependency
|
@@ -24,14 +19,9 @@ dependencies:
|
|
24
19
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
20
|
none: false
|
26
21
|
requirements:
|
27
|
-
- -
|
22
|
+
- - ~>
|
28
23
|
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 1
|
32
|
-
- 2
|
33
|
-
- 9
|
34
|
-
version: 1.2.9
|
24
|
+
version: "2.0"
|
35
25
|
type: :development
|
36
26
|
version_requirements: *id001
|
37
27
|
- !ruby/object:Gem::Dependency
|
@@ -42,9 +32,6 @@ dependencies:
|
|
42
32
|
requirements:
|
43
33
|
- - ">="
|
44
34
|
- !ruby/object:Gem::Version
|
45
|
-
hash: 3
|
46
|
-
segments:
|
47
|
-
- 0
|
48
35
|
version: "0"
|
49
36
|
type: :runtime
|
50
37
|
version_requirements: *id002
|
@@ -56,9 +43,6 @@ dependencies:
|
|
56
43
|
requirements:
|
57
44
|
- - ">="
|
58
45
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 3
|
60
|
-
segments:
|
61
|
-
- 0
|
62
46
|
version: "0"
|
63
47
|
type: :runtime
|
64
48
|
version_requirements: *id003
|
@@ -70,9 +54,6 @@ dependencies:
|
|
70
54
|
requirements:
|
71
55
|
- - ">="
|
72
56
|
- !ruby/object:Gem::Version
|
73
|
-
hash: 3
|
74
|
-
segments:
|
75
|
-
- 0
|
76
57
|
version: "0"
|
77
58
|
type: :runtime
|
78
59
|
version_requirements: *id004
|
@@ -87,7 +68,6 @@ extra_rdoc_files:
|
|
87
68
|
- README.rdoc
|
88
69
|
files:
|
89
70
|
- .document
|
90
|
-
- .gitignore
|
91
71
|
- LICENSE
|
92
72
|
- README.rdoc
|
93
73
|
- Rakefile
|
@@ -113,8 +93,8 @@ homepage: http://github.com/penwellr/smartfox
|
|
113
93
|
licenses: []
|
114
94
|
|
115
95
|
post_install_message:
|
116
|
-
rdoc_options:
|
117
|
-
|
96
|
+
rdoc_options: []
|
97
|
+
|
118
98
|
require_paths:
|
119
99
|
- lib
|
120
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -122,23 +102,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
102
|
requirements:
|
123
103
|
- - ">="
|
124
104
|
- !ruby/object:Gem::Version
|
125
|
-
hash: 3
|
126
|
-
segments:
|
127
|
-
- 0
|
128
105
|
version: "0"
|
129
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
107
|
none: false
|
131
108
|
requirements:
|
132
109
|
- - ">="
|
133
110
|
- !ruby/object:Gem::Version
|
134
|
-
hash: 3
|
135
|
-
segments:
|
136
|
-
- 0
|
137
111
|
version: "0"
|
138
112
|
requirements: []
|
139
113
|
|
140
114
|
rubyforge_project:
|
141
|
-
rubygems_version: 1.
|
115
|
+
rubygems_version: 1.5.2
|
142
116
|
signing_key:
|
143
117
|
specification_version: 3
|
144
118
|
summary: Client library for SmartFoxServer
|