Class: BBFS::ContentServer::RemoteContent

Inherits:
Object
  • Object
show all
Defined in:
lib/content_server/remote_content.rb

Overview

TODO(kolman): Use only one tcp/ip socket by utilizing one NQueue for many queues!

Instance Method Summary collapse

Constructor Details

#initialize(dynamic_content_data, host, port, local_backup_folder) ⇒ RemoteContent

Returns a new instance of RemoteContent.



16
17
18
19
20
21
22
# File 'lib/content_server/remote_content.rb', line 16

def initialize(dynamic_content_data, host, port, local_backup_folder)
  @dynamic_content_data = dynamic_content_data
  @remote_tcp = Networking::TCPClient.new(host, port, method(:receive_content))
  @last_update_timestamp = nil
  @content_server_content_data_path = File.join(local_backup_folder, 'remote',
                                                host + '_' + port.to_s)
end

Instance Method Details

#receive_content(message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/content_server/remote_content.rb', line 24

def receive_content(message)
  Log.debug1("Remote content data received: #{message.to_s}")
  ref = @dynamic_content_data.last_content_data
  @dynamic_content_data.update(message)

  max_time_span = Params['max_content_timeout']
  if !@last_update_timestamp.nil?
    max_time_span = Time.now.to_i - @last_update_timestamp
  end

  @last_update_timestamp = Time.now.to_i

  if ref != message || max_time_span >= Params['max_content_timeout']
    Log.debug2("Remote content data changed or max time span is large, writing.")
    Log.debug3("max_time_span: #{max_time_span}")
    write_to = File.join(@content_server_content_data_path,
                         @last_update_timestamp.to_s + '.cd')
    FileUtils.makedirs(@content_server_content_data_path) unless \
        File.directory?(@content_server_content_data_path)
    count = File.open(write_to, 'wb') { |f| f.write(message.to_s) }
  else
    Log.debug2("No need to write remote content data, it has not changed.")
  end
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/content_server/remote_content.rb', line 49

def run()
  threads = []
  threads << @remote_tcp.tcp_thread if @remote_tcp != nil
  threads << Thread.new do
    loop do
      # if need content data
      if @last_update_timestamp.nil?
        sleep_time_span = Params['remote_content_timeout']
      else
        sleep_time_span = Time.now.to_i - @last_update_timestamp
      end

      if sleep_time_span >= Params['remote_content_timeout']
        # Send ping!
        Log.debug2('Sending remote contend request.')
        bytes_written = @remote_tcp.send_obj(nil)
        Log.debug3("Bytes written #{bytes_written}.")
      end

      sleep_time_span = Time.now.to_i - @last_update_timestamp \
          unless @last_update_timestamp.nil?
      Log.debug2("sleep_time_span: #{sleep_time_span}")
      sleep(sleep_time_span) if sleep_time_span > 0
    end
  end
end