Class: ContentServer::RemoteContentClient

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

Overview

Remote Content (client) - Periodically requests remote content (content server) from Remote Content Server. Stores locally the remote content data structure. Remote content client protocol based on time. Each n seconds it requests content from Remote Content Client connects to server (tcp/ip) and waits for request. remote_content_save_timeout is pre defined in config-backup_server.yml

Instance Method Summary collapse

Constructor Details

#initialize(host, port, local_backup_folder) ⇒ RemoteContentClient

initializes class variables

Parameters:

  • host (host)

    host address

  • port (port)

    port TCP/IP port

  • local_backup_folder (local_backup_folder)

    pre defined folder for backup



23
24
25
26
27
28
29
30
31
# File 'lib/content_server/remote_content.rb', line 23

def initialize(host, port, local_backup_folder)
  @remote_tcp = Networking::TCPClient.new(host, port, method(:receive_content))
  @last_fetch_timestamp = nil
  @last_content_data_id = nil
  @content_server_content_data_path = File.join(local_backup_folder, 'remote',
                                                host + '_' + port.to_s)
  Log.debug3("Initialized RemoteContentClient: host:%s   port:%s  local_backup_folder:%s",
             host, port, local_backup_folder)
end

Instance Method Details

#receive_content(message) ⇒ Object

Receives content data, and writes it to file Unchanged data will not be saved

Parameters:

  • message (Message)

    ContentData object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/content_server/remote_content.rb', line 36

def receive_content(message)
  Log.info("Backup server received Remote content data")
  @last_fetch_timestamp = Time.now.to_i

  # Update remote content data and write to file if changed ContentData received
  if(message.unique_id != @last_content_data_id)
    path = File.join(@content_server_content_data_path, @last_fetch_timestamp.to_s + '.cd')
    FileUtils.makedirs(@content_server_content_data_path) unless \
          File.directory?(@content_server_content_data_path)
    $remote_content_data_lock.synchronize{
      $remote_content_data = message
      $remote_content_data.to_file(path)
      @last_content_data_id = message.unique_id   # save last content data ID
    }
    Log.debug1("Written content data to file:%s.", path)
  else
    Log.debug1("No need to write remote content data, it has not changed.")
  end
end

#runObject

Runs Remote content client



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/content_server/remote_content.rb', line 57

def run()
  Log.debug1("Running remote content client.")
  threads = []
  threads << @remote_tcp.tcp_thread if @remote_tcp != nil
  threads << Thread.new do
    loop do
      # if need content data
      sleep_time_span = Params['remote_content_save_timeout']
      if @last_fetch_timestamp
        sleep_time_span = Time.now.to_i - @last_fetch_timestamp
      end
      Log.debug1("sleep_time_span: %s", sleep_time_span)
      if sleep_time_span >= Params['remote_content_save_timeout']
        # Send ping!
        @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