Module: ContentServer

Defined in:
lib/content_server/backup_server.rb,
lib/content_server.rb,
lib/content_server/version.rb,
lib/content_server/queue_copy.rb,
lib/content_server/file_streamer.rb,
lib/content_server/queue_indexer.rb,
lib/content_server/content_server.rb,
lib/content_server/remote_content.rb,
lib/content_server/content_receiver.rb

Overview

Content server. Monitors files, index local files, listen to backup server content, copy changes and new files to backup server.

Defined Under Namespace

Classes: ContentDataReceiver, ContentDataSender, FileCopyClient, FileCopyServer, FileReceiver, FileStreamer, QueueIndexer, RemoteContentClient, RemoteContentServer, Stream

Constant Summary collapse

VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

.run_backup_serverObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/content_server/backup_server.rb', line 29

def run_backup_server
  Log.info('Start backup server')
  Thread.abort_on_exception = true
  all_threads = []

  @process_variables = ThreadSafeHash::ThreadSafeHash.new
  @process_variables.set('server_name', 'backup_server')

  # # # # # # # # # # # #
  # Initialize/Start monitoring
  Log.info('Start monitoring following directories:')
  Params['monitoring_paths'].each {|path|
    Log.info("  Path:'#{path['path']}'")
  }
  monitoring_events = Queue.new
  fm = FileMonitoring::FileMonitoring.new
  fm.set_event_queue(monitoring_events)
  # Start monitoring and writing changes to queue
  all_threads << Thread.new do
    fm.monitor_files
  end

  # # # # # # # # # # # # # #
  # Initialize/Start local indexer
  Log.debug1('Start indexer')
  local_server_content_data_queue = Queue.new
  queue_indexer = QueueIndexer.new(monitoring_events,
                                   local_server_content_data_queue,
                                   Params['local_content_data_path'])
  # Start indexing on demand and write changes to queue
  all_threads << queue_indexer.run

  # # # # # # # # # # # # # # # # # # # # # # # # # # #
  # Initialize/Start backup server content data sender
  Log.debug1('Start backup server content data sender')
  dynamic_content_data = ContentData::DynamicContentData.new
  #content_data_sender = ContentDataSender.new(
  #    Params['remote_server'],
  #    Params['remote_listening_port'])
  # Start sending to backup server
  all_threads << Thread.new do
    while true do
      Log.debug1 'Waiting on local server content data queue.'
      cd = local_server_content_data_queue.pop
      #    content_data_sender.send_content_data(cd)
      dynamic_content_data.update(cd)
    end
  end
  Params['backup_destination_folder'] = File.expand_path(Params['monitoring_paths'][0]['path'])
  Log.info("backup_destination_folder is:#{Params['backup_destination_folder']}")
  content_server_dynamic_content_data = ContentData::DynamicContentData.new
  remote_content = ContentServer::RemoteContentClient.new(content_server_dynamic_content_data,
                                                          Params['content_server_hostname'],
                                                          Params['content_server_data_port'],
                                                          Params['backup_destination_folder'])
  all_threads.concat(remote_content.run())

  file_copy_client = FileCopyClient.new(Params['content_server_hostname'],
                                        Params['content_server_files_port'],
                                        dynamic_content_data,
                                        @process_variables)
  all_threads.concat(file_copy_client.threads)

  # Each
  Log.info('Start remote and local contents comparator')
  all_threads << Thread.new do
    loop do
      sleep(Params['backup_check_delay'])
      local_cd = dynamic_content_data.last_content_data()
      remote_cd = content_server_dynamic_content_data.last_content_data()
      diff = ContentData::ContentData.remove(local_cd, remote_cd)
      #file_copy_client.request_copy(diff) unless diff.empty?
      if !diff.empty?
        Log.info('Start sync check. Backup and remote contents need a sync:')
        Log.debug2("Backup content:\n#{local_cd}")
        Log.debug2("Remote content:\n#{remote_cd}")
        Log.info("Missing contents:\n#{diff}")
        Log.info('Requesting copy files')
        file_copy_client.request_copy(diff)
      else
        Log.info("Start sync check. Local and remote contents are equal. No sync required.")
      end
    end
  end

  if Params['enable_monitoring']
    mon = Monitoring::Monitoring.new(@process_variables)
    Log.add_consumer(mon)
    all_threads << mon.thread
    monitoring_info = MonitoringInfo::MonitoringInfo.new(@process_variables)
  end

  all_threads.each { |t| t.abort_on_exception = true }
  all_threads.each { |t| t.join }
  # Should never reach this line.
end

.run_content_serverObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/content_server/content_server.rb', line 26

def run_content_server
  Log.info('Content server start')
  all_threads = []

  @process_variables = ThreadSafeHash::ThreadSafeHash.new
  @process_variables.set('server_name', 'content_server')

  # # # # # # # # # # # #
  # Initialize/Start monitoring
  Log.info('Start monitoring following directories:')
  Params['monitoring_paths'].each {|path|
    Log.info("  Path:'#{path['path']}'")
  }
  monitoring_events = Queue.new
  fm = FileMonitoring::FileMonitoring.new
  fm.set_event_queue(monitoring_events)
  # Start monitoring and writing changes to queue
  all_threads << Thread.new do
    fm.monitor_files
  end

  # # # # # # # # # # # # # #
  # Initialize/Start local indexer
  Log.debug1('Start indexer')
  local_server_content_data_queue = Queue.new
  queue_indexer = QueueIndexer.new(monitoring_events,
                                   local_server_content_data_queue,
                                   Params['local_content_data_path'])
  # Start indexing on demand and write changes to queue
  all_threads << queue_indexer.run

  # # # # # # # # # # # # # # # # # # # # # #
  # Initialize/Start content data comparator
  Log.debug1('Start content data comparator')
  copy_files_events = Queue.new
  local_dynamic_content_data = ContentData::DynamicContentData.new
  all_threads << Thread.new do
    while true do
      # Note: This thread should be the only consumer of local_server_content_data_queue
      Log.debug1 'Waiting on local server content data.'
      local_server_content_data = local_server_content_data_queue.pop
      local_dynamic_content_data.update(local_server_content_data)
    end
  end

  remote_content_client = RemoteContentServer.new(local_dynamic_content_data,
                                                  Params['local_content_data_port'])
  all_threads << remote_content_client.tcp_thread

  # # # # # # # # # # # # # # # #
  # Start copying files on demand
  Log.debug1('Start copy data on demand')
  copy_server = FileCopyServer.new(copy_files_events, Params['local_files_port'])
  all_threads.concat(copy_server.run())

  if Params['enable_monitoring']
    mon = Monitoring::Monitoring.new(@process_variables)
    Log.add_consumer(mon)
    all_threads << mon.thread
    monitoring_info = MonitoringInfo::MonitoringInfo.new(@process_variables)
  end

  # Finalize server threads.
  all_threads.each { |t| t.abort_on_exception = true }
  all_threads.each { |t| t.join }
  # Should never reach this line.
end