Class: ContentServer::RemoteContentClient

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) ⇒ RemoteContentClient

Returns a new instance of RemoteContentClient.



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

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_fetch_timestamp = nil
  @last_save_timestamp = nil
  @content_server_content_data_path = File.join(local_backup_folder, 'remote',
                                                host + '_' + port.to_s)
  Log.debug3("Initialized RemoteContentClient: host:#{host}   port:#{port}  local_backup_folder:#{local_backup_folder}")
end

Instance Method Details

#receive_content(message) ⇒ Object



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 25

def receive_content(message)
  Log.debug1("Backup server received Remote content data:#{message.to_s}")
  Log.info("Backup server received Remote content data")
  @dynamic_content_data.update(message)
  @last_fetch_timestamp = Time.now.to_i

  save_time_span = Params['remote_content_save_timeout']
  if !@last_save_timestamp.nil?
    save_time_span = Time.now.to_i - @last_save_timestamp
  end

  if save_time_span >= Params['remote_content_save_timeout']
    @last_save_timestamp = Time.now.to_i
    write_to = File.join(@content_server_content_data_path,
                         @last_save_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) }
    Log.debug1("Written content data to file:#{write_to}.")
  else
    Log.debug1("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
# File 'lib/content_server/remote_content.rb', line 49

def run()
  Log.debug1("Running remote content client.")
  threads = []
  threads << @remote_tcp.tcp_thread if @remote_tcp != nil
  threads << Thread.new do
    Log.debug1("New thread.")
    loop do
      # if need content data
      sleep_time_span = Params['remote_content_save_timeout']
      if !@last_fetch_timestamp.nil?
        sleep_time_span = Time.now.to_i - @last_fetch_timestamp
      end
      Log.debug1("sleep_time_span: #{sleep_time_span}")
      if sleep_time_span >= Params['remote_content_save_timeout']
        # Send ping!
        bytes_written = @remote_tcp.send_obj(nil)
        Log.info("sending ping request for remote content data!")
      end
      sleep(sleep_time_span) if sleep_time_span > 0
    end
  end
end