class OmfCommon::Comm::Local::Communicator

Public Instance Methods

broadcast_file(file_path, topic_url = nil, opts = {}, &block) click to toggle source
# File lib/omf_common/comm/local/local_communicator.rb, line 58
def broadcast_file(file_path, topic_url = nil, opts = {}, &block)
  topic_url ||= SecureRandom.uuid
  @distributed_files[topic_url] = file_path
  "bdcst:local:#{topic_url}"
end
create_topic(topic, &block) click to toggle source

Create a new pubsub topic with additional configuration

@param [String] topic Pubsub topic name

# File lib/omf_common/comm/local/local_communicator.rb, line 31
def create_topic(topic, &block)
  t = OmfCommon::Comm::Local::Topic.create(topic)
  if block
    block.call(t)
  end
  t
end
delete_topic(topic, &block) click to toggle source

Delete a pubsub topic

@param [String] topic Pubsub topic name

# File lib/omf_common/comm/local/local_communicator.rb, line 42
def delete_topic(topic, &block)
  if t = OmfCommon::CommProvider::Local::Topic.find(topic)
    t.release
  else
    warn "Attempt to delete unknown topic '#{topic}"
  end        
end
disconnect(opts = {}) click to toggle source

Shut down comms layer

# File lib/omf_common/comm/local/local_communicator.rb, line 25
def disconnect(opts = {})
end
init(opts = {}) click to toggle source

Initialize comms layer

Calls superclass method OmfCommon::Comm::init
# File lib/omf_common/comm/local/local_communicator.rb, line 19
def init(opts = {})
  @distributed_files = {}
                                super
end
on_connected(&block) click to toggle source
# File lib/omf_common/comm/local/local_communicator.rb, line 50
def on_connected(&block)
  return unless block
  
  OmfCommon.eventloop.after(0) do
    block.arity == 1 ? block.call(self) : block.call
  end
end
receive_file(topic_url, file_path = nil, opts = {}, &block) click to toggle source
# File lib/omf_common/comm/local/local_communicator.rb, line 64
def receive_file(topic_url, file_path = nil, opts = {}, &block)
  if topic_url.start_with? 'local:'
    topic_url = topic_url[6 .. -1]
  end
  file_path ||= File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname('bdcast', ''))
  OmfCommon.eventloop.after(0) do
    #puts ">>>>>> #{topic_url}::#{@distributed_files.keys}"
    unless original = @distributed_files[topic_url]
      raise "File '#{topic_url}' hasn't started broadcasting"
    end
    mime_type = `file -b --mime-type #{original}`
    `cp #{original} #{file_path}`
    unless $?.success?
      error "Couldn't copy '#{original}' to '#{file_path}'"
    end
    if block
      block.call({action: :done, mime_type: mime_type.strip, path: file_path, size: -1, received: -1})
    end
  end
  file_path
end