Sha256: 171210e2d1aed1d20654e2f50982f713c7727d244520610deb8c9f7f5ff8ca21

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require "xmpp4r/client"
require "drb"

module Saber
  class Server
    def self.start
      # register on_finished event.
      Retort::Service.call("system.method.set_key", "event.download.finished", "saber-download", "execute= /usr/bin/saber-drb_add, $d.get_hash=, $d.get_custom1=")

      s = Server.new
      DRbServer.start(s)
      s.start

      Thread.list.each {|v| v.join}
    end

    attr_reader :client

    def initialize
      @client = Jabber::Client.new(Jabber::JID.new(Rc.server.xmpp.jid))
    end

    def start
      client.connect Rc.server.xmpp.host, Rc.server.xmpp.port
      client.auth Rc.server.xmpp.password
      client.send Jabber::Presence.new
      Saber.ui.say ">> Connected to xmpp with #{Rc.server.xmpp.jid}"
    end

    def send(files)
      $log.debug %~::send::\n #{files.join("\n")}~
      client.send Jabber::Message.new(Rc.client.xmpp.jid, files.join("\n"))
    end
  end

  class DRbServer
    class << self
      def start(saber_server)
        DRbServer.new(saber_server).start
      end
    end

    attr_reader :saber_server

    # @params [Server] server
    def initialize(saber_server)
      @saber_server = saber_server
    end

    def start
      DRb.start_service Rc.drb_uri, self
      Saber.ui.say ">> DRbSever listening on #{Rc.drb_uri}"
    end

    # drb. add a complete torrent.
    def add(*names)
      $log.debug "::DRbServer add:: #{names.inspect}"
      files = build_files(*names) 

      saber_server.send(files)
    end

  private

    # ["filea", "foo/filea", "foo/fileb"]
    def build_files(*names)
      Pa.ls2_r(*names, :base_dir => Rc.server.download, :file => true, :include => true) { |path| not Pa.directory?(path) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saber-0.0.6 lib/saber/server.rb