Class: Device::Transaction::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/device/transaction/download.rb

Constant Summary collapse

ERL_VERSION_MAGIC =
131
ERL_NIL_EXT =
'j'
ERL_SMALL_TUPLE_EXT =
'h'
ERL_ATOM_EXT =
'd'
ERL_BINARY_EXT =
'm'
ERL_INTEGER_EXT =
'b'
ERL_CONTENT_TYPE =
"application/x-erlang-binary"
MAXATOMLEN =
255
PARAMS_FILE =
"params.dat"
SUCCESS =
0
FILE_NOT_CHANGE =
1
FILE_NOT_FOUND =
2
SERIAL_NUMBER_NOT_FOUND =
3
COMMUNICATION_ERROR =
-1
MAPREDUCE_RESPONSE_ERROR =
-2
IO_ERROR =
-3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serial, path, version) ⇒ Download

Returns a new instance of Download.



52
53
54
55
56
# File 'lib/device/transaction/download.rb', line 52

def initialize(serial, path, version)
  @serial  = serial
  @path    = path
  @version = version
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



50
51
52
# File 'lib/device/transaction/download.rb', line 50

def buffer
  @buffer
end

#crcObject

Returns the value of attribute crc.



50
51
52
# File 'lib/device/transaction/download.rb', line 50

def crc
  @crc
end

#first_packetObject

Returns the value of attribute first_packet.



50
51
52
# File 'lib/device/transaction/download.rb', line 50

def first_packet
  @first_packet
end

#pathObject

Returns the value of attribute path.



50
51
52
# File 'lib/device/transaction/download.rb', line 50

def path
  @path
end

#requestObject

Returns the value of attribute request.



50
51
52
# File 'lib/device/transaction/download.rb', line 50

def request
  @request
end

#socketObject

Returns the value of attribute socket.



50
51
52
# File 'lib/device/transaction/download.rb', line 50

def socket
  @socket
end

Class Method Details

.check(ret) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/device/transaction/download.rb', line 23

def self.check(ret)
  case ret
  when SERIAL_NUMBER_NOT_FOUND
  when FILE_NOT_FOUND
  when FILE_NOT_CHANGE
    return true
  when SUCCESS
    return true
  when COMMUNICATION_ERROR
  when MAPREDUCE_RESPONSE_ERROR
  when IO_ERROR
  end
  false
end

.request_file(remote_path, local_path, crc = nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/device/transaction/download.rb', line 38

def self.request_file(remote_path, local_path, crc = nil)
  download = Device::Transaction::Download.new(Device::System.serial, "", DaFunk::VERSION)
  download.perform(Device::Network.socket,
                   Device::Setting.company_name,
                   remote_path, local_path, Device::System.app,
                   Device::Setting.logical_number, crc)
end

.request_param_file(file = PARAMS_FILE) ⇒ Object



46
47
48
# File 'lib/device/transaction/download.rb', line 46

def self.request_param_file(file = PARAMS_FILE)
  request_file("#{Device::Setting.logical_number}_#{PARAMS_FILE}", file)
end

Instance Method Details

#generate_crc(local_path) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/device/transaction/download.rb', line 141

def generate_crc(local_path)
  if File.exists?(local_path)
    file = File.open(local_path)
    Device::Crypto.crc16_hex(file.read)
  else
    ""
  end
ensure
  file.close if file
end

#perform(socket_proc, company_name, remote_path, filepath, current_app, logical_number, file_crc = nil, socket_call = true) ⇒ Object

0: Success -1: Commnucation error -2: Mapreduce response error -3: IO Error



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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/device/transaction/download.rb', line 62

def perform(socket_proc, company_name, remote_path, filepath, current_app, logical_number, file_crc = nil, socket_call = true)
  if socket_call
    @socket, @buffer, @request, @first_packet = socket_proc.call, "", "", ""
  else
    @socket, @buffer, @request, @first_packet = socket_proc, "", "", ""
  end
  @crc = file_crc ? file_crc : generate_crc(filepath)
  key = "#{company_name}_#{remote_path}"

  ei_encode_version                # version
  ei_encode_list_header(3)         # mapreduce with 3 tuples
  ei_encode_tuple_header(2)        # tuple 1 inputs
  ei_encode_atom("inputs")         # atom inputs
  ei_encode_list_header(1)         # list inputs
  ei_encode_tuple_header(2)        # tuple contendo 2 elementos binarios, bucket and key
  ei_encode_binary("assets")       # elemento binario bucket
  ei_encode_binary(key)            # elemento binario key
  ei_encode_list_header(0)         # fim da list do inputs

  ei_encode_tuple_header(2)        # tuple 2: query
  ei_encode_atom("query")          # atom query
  ei_encode_list_header(1)         # list da query
  ei_encode_tuple_header(4)        # tuple contendo 4 elementos, { map, {modfun,Module,Function}, args, true }
  ei_encode_atom("map")            # primeiro elemento, atom type
  ei_encode_tuple_header(3)        # segundo elemento, nova tuple contendo 3 atoms
  ei_encode_atom("modfun")         # primeiro atom do segundo elemento, modfun
  ei_encode_atom("walk")           # segundo atom do segundo elemento, Module
  ei_encode_atom("get_asset")      # terceiro atom do segundo elemento, Function

  ei_encode_list_header(7)         # terceiro elemento, uma list com parametros do walk
  ei_encode_binary(@serial)        # elemento binario serialterminal
  ei_encode_binary(@version)       # elemento binario versao walk
  ei_encode_binary(remote_path)      # elemento binario nomeaplicativo
  ei_encode_binary(@crc)            # elemento binario crc aplicativo
  ei_encode_binary("")             # elemento binario buffer do posxml
  ei_encode_binary(logical_number) # elemento binario numero do terminal
  ei_encode_binary(current_app)    # elemento binario nome do aplicativo que esta sendo executado
  ei_encode_list_header(0)         # fim da list com parametros do walk

  ei_encode_atom("true")           # quarto elemento, atom true
  ei_encode_list_header(0)         # fim da list da query

  ei_encode_tuple_header(2)        # tuple 3: timeout
  ei_encode_atom("timeout")        # atom timeout
  ei_encode_long(5000)             # integer timeout
  ei_encode_list_header(0)         # fim da list do mapreduce contendo 3 tuples

  mount_request                    # add request to protocol buffers message

  return COMMUNICATION_ERROR unless @socket

  # Send Request
  socket.write(@request)

  return COMMUNICATION_ERROR if (response_size = get_response_size(socket.read(4))) <= 0

  return MAPREDUCE_RESPONSE_ERROR unless @first_packet = get_binary_term_beginning(response_size)

  return_code = @first_packet[7].to_s.unpack("C*").first
  file_size   = @first_packet[9..12].to_s.unpack("N*").first

  if return_code != FILE_NOT_CHANGE
    return IO_ERROR if (partial_download_to_store(filepath, response_size, file_size) < 0)
  end

  # receive 6A
  @socket.read(1) if response_size > 1024
  if socket_call
    @socket.close unless @socket.closed?
  end

  return_code
rescue SocketError
  return COMMUNICATION_ERROR
rescue => e
  ContextLog.exception(e, e.backtrace)
  return IO_ERROR
end