Class: FileProcessingJob::Server::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/fpj/server.rb

Constant Summary collapse

@@q =
EM::Queue.new()

Constants inherited from Connection

Connection::TOKEN

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#initialize, logger, #logger, #receive_data, #receive_request, #send_message

Constructor Details

This class inherits a constructor from FileProcessingJob::Connection

Class Method Details

.configObject



153
154
155
# File 'lib/fpj/server.rb', line 153

def self.config
  FileProcessingJob::Server.config
end

.push(filename) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fpj/server.rb', line 141

def self.push(filename)
  if (filename.nil?)
    #ignore
  elsif (File.size(File.join(config.inbox_directory, filename)) == 0)
    logger.error "Moving 0 byte file #{filename} to errors directory"
    FileUtils.move(File.join(config.inbox_directory, filename), File.join(config.error_directory, filename))
  else
    FileProcessingJob.logger.debug "#{filename} discovered in inbox"
    @@q.push(filename)
  end
end

Instance Method Details

#bindObject



202
203
204
# File 'lib/fpj/server.rb', line 202

def bind
  logger.info "client connected to the server"
end

#configObject



157
158
159
# File 'lib/fpj/server.rb', line 157

def config 
  @config ||= FileProcessingJob::Server.config
end

#receive_task(data) ⇒ Object

inbound client connection



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/fpj/server.rb', line 176

def receive_task data
  data = data.strip
  logger.debug("received command: #{data}")
  case (data)
    when "next"
      @@q.pop {|filename| send_file(filename) }
    when /^processed (.*)$/
      filename = $1
      logger.debug("finished processing #{filename}")
      if (File.exists?(File.join(self.config.processed_directory, filename)))
        logger.error "file #{filename} already exists in processed directory"
      else
        FileUtils.move(File.join(self.config.processing_directory, filename), File.join(self.config.processed_directory, filename))
        logger.debug "finished processing #{filename}"
      end
      close_connection
    when /^error (.*)$/
      filename = $1
      FileUtils.move(File.join(self.config.processing_directory, filename), File.join(self.config.error_directory, filename))
      logger.error "Unable to stream file #{filename} to client. The file can be found in the 'error' directory"
      close_connection
    else
      puts "unknown command: '#{data}'"
    end
end

#send_file(filename) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fpj/server.rb', line 161

def send_file(filename)
  logger.debug "Sending #{filename} to client"

  send_message("filename: #{filename}")

  FileUtils.move(File.join(self.config.inbox_directory, filename), File.join(self.config.processing_directory, filename))
  logger.debug "Sending file #{filename} to client"
  streamer = EventMachine::FileStreamer.new(self, File.join(self.config.processing_directory, filename))
  streamer.callback{
    send_data TOKEN
    logger.debug "\tsuccessfully sent #{filename}"
  }
end

#unbindObject



206
207
208
# File 'lib/fpj/server.rb', line 206

def unbind
  logger.info "client disconnected from the server"
end