Module: BBFS::ContentServer

Defined in:
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/remote_content.rb,
lib/content_server/content_receiver.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.runObject



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
# File 'lib/content_server.rb', line 33

def run
  all_threads = []

  # # # # # # # # # # # #
  # Initialize/Start monitoring
  monitoring_events = Queue.new
  fm = FileMonitoring::FileMonitoring.new
  fm.set_config_path(Params['monitoring_config_path'])
  fm.set_event_queue(monitoring_events)
  # Start monitoring and writing changes to queue
  all_threads << Thread.new do
    fm.monitor_files
  end

  # # # # # # # # # # # # # # # # # # # # # # # # #
  # Initialize/Start backup server content data listener
  #backup_server_content_data = nil
  #backup_server_content_data_queue = Queue.new
  #content_data_receiver = ContentDataReceiver.new(
  #    backup_server_content_data_queue,
  #    Params['remote_listening_port'])
  # Start listening to backup server
  #all_threads << Thread.new do
  #  content_data_receiver.run
  #end

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

  # # # # # # # # # # # # # # # # # # # # # #
  # Initialize/Start content data comparator
  copy_files_events = Queue.new
  local_dynamic_content_data = ContentData::DynamicContentData.new
  all_threads << Thread.new do
  #  backup_server_content_data = ContentData::ContentData.new
  #  local_server_content_data = nil
    while true do

      # Note: This thread should be the only consumer of local_server_content_data_queue
      Log.info '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)
  #
  #    # Note: This thread should be the only consumer of backup_server_content_data_queue
  #    # Note: The server will wait in the first time on pop until backup sends it's content data
  #    while backup_server_content_data_queue.size > 0
  #      Log.info 'Waiting on backup server content data.'
  #      backup_server_content_data = backup_server_content_data_queue.pop
  #    end

  #    Log.info 'Updating file copy queue.'
  #    Log.debug1 "local_server_content_data #{local_server_content_data}."
  #    Log.debug1 "backup_server_content_data #{backup_server_content_data}."
  #    # Remove backup content data from local server
  #    content_to_copy = ContentData::ContentData.remove(backup_server_content_data, local_server_content_data)
  #    content_to_copy = local_server_content_data
  #    # Add copy instruction in case content is not empty
  #    Log.debug1 "Content to copy: #{content_to_copy}"
  #    copy_files_events.push([:COPY_MESSAGE, content_to_copy]) unless content_to_copy.empty?
    end
  end

  remote_content_client = RemoteContentClient.new(local_dynamic_content_data,
                                                  Params['remote_content_port'])
  all_threads << remote_content_client.tcp_thread

  # # # # # # # # # # # # # # # #
  # Start copying files on demand
  copy_server = FileCopyServer.new(copy_files_events, Params['backup_file_listening_port'])
  all_threads.concat(copy_server.run())

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

.run_backup_serverObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/content_server.rb', line 117

def run_backup_server
  all_threads = []

  # # # # # # # # # # # #
  # Initialize/Start monitoring
  monitoring_events = Queue.new
  fm = FileMonitoring::FileMonitoring.new
  fm.set_config_path(Params['monitoring_config_path'])
  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
  local_server_content_data_queue = Queue.new
  queue_indexer = QueueIndexer.new(monitoring_events,
                                   local_server_content_data_queue,
                                   Params['content_data_path'])
  # Start indexing on demand and write changes to queue
  all_threads << queue_indexer.run

  # # # # # # # # # # # # # # # # # # # # # # # # # # #
  # Initialize/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.info '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

  content_server_dynamic_content_data = ContentData::DynamicContentData.new
  remote_content = ContentServer::RemoteContent.new(content_server_dynamic_content_data,
                                                    Params['remote_server'],
                                                    Params['remote_content_port'],
                                                    Params['backup_destination_folder'])
  all_threads.concat(remote_content.run())

  file_copy_client = FileCopyClient.new(Params['remote_server'],
                                           Params['backup_file_listening_port'],
                                           dynamic_content_data)
  all_threads.concat(file_copy_client.threads)

  # Each
  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)
      Log.debug2("Files to send? #{!diff.empty?}")
      file_copy_client.request_copy(diff) unless diff.empty?
    end
  end


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