Class: FileServe
- Defined in:
- lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/fileserve.rb
Instance Method Summary collapse
- #cleaner ⇒ Object
- #command(from, cmd, arg) ⇒ Object
-
#initialize(conf) ⇒ FileServe
constructor
A new instance of FileServe.
- #presence ⇒ Object
- #register_handlers ⇒ Object
- #say(to, text) ⇒ Object
Constructor Details
#initialize(conf) ⇒ FileServe
Returns a new instance of FileServe.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 177 def initialize(conf) @uploads = 0 @downloads = 0 @transfers = [] @transfers_lock = Mutex.new @client = Jabber::Client.new Jabber::JID.new(conf['jabber']['jid']) @client.connect @client.auth(conf['jabber']['password']) @ft = Jabber::FileTransfer::Helper.new(@client) Jabber::Version::SimpleResponder.new(@client, "XMPP4R FileServe example", Jabber::XMPP4R_VERSION, IO.popen('uname -sr').readlines.to_s.strip) register_handlers @directory = conf['directory'] @directory.gsub!(/\/+$/, '') @socksserver = Jabber::Bytestreams::SOCKS5BytestreamsServer.new(conf['socks']['port']) conf['socks']['addresses'].each { |addr| @socksserver.add_address(addr) } @proxies = [] conf['socks']['proxies'].collect { |jid| puts "Querying proxy #{jid}..." begin @proxies.push(Jabber::Bytestreams::SOCKS5Bytestreams::query_streamhost(@client, jid)) rescue puts "Error: #{$!}" end } Thread.new { presence } Thread.new { cleaner } # Panic reboot ;-) @client.on_exception { initialize(conf) } end |
Instance Method Details
#cleaner ⇒ Object
331 332 333 334 335 336 337 338 339 340 |
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 331 def cleaner loop { @transfers_lock.synchronize { @transfers.delete_if { |t| t.done? } } sleep 1 } end |
#command(from, cmd, arg) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 283 def command(from, cmd, arg) say = lambda { |text| say(from, text) } socksconf = lambda { |stream| stream.add_streamhost(@socksserver) @proxies.each { |sh| stream.add_streamhost(sh) } } case cmd when 'get' arg.gsub!(/\//, '') arg.gsub!(/^\.+/, '') transfer = Download.new(@ft, from, "#{@directory}/#{arg}", say, socksconf) @downloads += 1 @transfers_lock.synchronize { @transfers.push(transfer) } when 'ls' text = "" Dir.foreach(@directory) { |file| next if file =~ /^\./ path = "#{@directory}/#{file}" text += "#{file} (#{human_readable File.size(path)})\n" if File.file?(path) } say.call(text.strip) when 'stat' @transfers_lock.synchronize { text = "#{@transfers.size} transfers:\n" @transfers.each { |t| text += "#{t.filename} (#{t.peer}): #{(t.bytes * 100) / t.filesize}% (#{human_readable t.stats}/s)\n" } } say.call(text.strip) when 'help' say.call "Download a file: get <filename>\nList directory contents: ls\nLook who is currently wasting bandwidth: stat\nUpload a file, simply send this file" else say.call "Unknown command: #{cmd}, try help" end end |
#presence ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 219 def presence rate_upload = 0 rate_download = 0 old_status = nil loop { # Update the @rate_* variables @transfers_lock.synchronize { @transfers.each { |t| if t.kind_of?(Upload) and t.stats > rate_upload rate_upload = t.stats elsif t.kind_of?(Download) and t.stats > rate_download rate_download = t.stats end } } status = "Attempted #{@downloads} downloads (max. #{human_readable rate_download}/s) and #{@uploads} uploads (max. #{human_readable rate_upload}/s)" @client.send(Jabber::Presence.new(:chat, status)) if status != old_status old_status = status sleep 1 } end |
#register_handlers ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/fileserve.rb', line 245 def register_handlers @client. { |msg| if msg.type == :chat and msg.body and msg.from != '[email protected]/rails' puts "<#{msg.from}> #{msg.body.strip}" cmd, arg = msg.body.split(/ /, 2) command(msg.from, cmd, arg) end } @ft.add_incoming_callback { |iq,file| say = lambda { |text| say(iq.from, text) } puts "Incoming file transfer from #{iq.from}: #{file.fname} (#{file.size / 1024} KB)" filename = file.fname.split(/\//).last filename.gsub!(/^\.+/, '') puts "Range: #{file.range != nil}" transfer = Upload.new(@ft, iq, "#{@directory}/#{filename}", file.size, file.range != nil, say) @uploads += 1 @transfers_lock.synchronize { @transfers.push(transfer) } } roster = Jabber::Roster::Helper.new(@client) roster.add_subscription_request_callback { |item,presence| roster.accept_subscription(presence.from) } end |