Module: Nanite::FileStreaming

Included in:
Agent
Defined in:
lib/nanite/streaming.rb

Overview

Nanite actors can transfer files to each other.

Options

filename : you guessed it, name of the file! domain : part of the routing key used to locate receiver(s) destination : is a name of the file as it gonna be stored at the destination meta :

File streaming is done in chunks. When file streaming starts, Nanite::FileStart packet is sent, followed by one or more (usually more ;)) Nanite::FileChunk packets each 16384 (16K) in size. Once file streaming is done, Nanite::FileEnd packet is sent.

16K is a packet size because on certain UNIX-like operating systems, you cannot read/write more than that in one operation via socket.

Domains

Streaming happens using a topic exchange called ‘file broadcast’, with keys formatted as “nanite.filepeer.DOMAIN”. Domain variable in the key lets senders and receivers find each other in the cluster. Default domain is ‘global’.

Domains also serve as a way to register a callback Nanite agent executes once file streaming is completed. If a callback with name of domain is registered, it is called.

Callbacks are registered by passing a block to subscribe_to_files method.

Defined Under Namespace

Classes: FileState

Instance Method Summary collapse

Instance Method Details

#broadcast_data(filename, io, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nanite/streaming.rb', line 40

def broadcast_data(filename, io, options = {})
  domain   = options[:domain] || 'global'
  filename = File.basename(filename)
  dest     = options[:destination] || filename
  sent = 0

    begin
      file_push = Nanite::FileStart.new(filename, dest, Identity.generate)
      amq.topic('file broadcast').publish(serializer.dump(file_push), :key => "nanite.filepeer.#{domain}")
      res = Nanite::FileChunk.new(file_push.token)
      while chunk = io.read(16384)
        res.chunk = chunk
        amq.topic('file broadcast').publish(serializer.dump(res), :key => "nanite.filepeer.#{domain}")
        sent += chunk.length
      end
      fend = Nanite::FileEnd.new(file_push.token, options[:meta])
      amq.topic('file broadcast').publish(serializer.dump(fend), :key => "nanite.filepeer.#{domain}")
      ""
    ensure
      io.close
    end

    sent
end

#broadcast_file(filename, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/nanite/streaming.rb', line 30

def broadcast_file(filename, options = {})
  if File.exist?(filename)
    File.open(filename, 'rb') do |file|
       broadcast_data(filename, file, options)
    end
  else
    return "file not found"
  end
end

#subscribe_to_files(domain = 'global', write = false, &blk) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nanite/streaming.rb', line 110

def subscribe_to_files(domain='global', write=false, &blk)
  Nanite::Log.info "subscribing to file broadcasts for #{domain}"
  @files ||= {}
  amq.queue("files#{domain}").bind(amq.topic('file broadcast'), :key => "nanite.filepeer.#{domain}").subscribe do |packet|
    case msg = serializer.load(packet)
    when FileStart
      @files[msg.token] = FileState.new(msg.token, msg.dest, domain, write, blk)
    when FileChunk, FileEnd
      if file = @files[msg.token]
        file.handle_packet(msg)
      end
    end
  end
end