-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse.rb
71 lines (64 loc) · 1.53 KB
/
parse.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
require 'socket'
require 'redis'
require 'json'
def update_plane(planes, updates, id, info)
if(planes[id].length < info.length)
planes[id].replace(info)
else
for i in 2..planes[id].length do
if(info[i] != '' && info[i] != nil && planes[id][i] != info[i])
planes[id][i] = info[i]
updates[id] = true
end
end
end
end
def update_redis(redis, planes, updates)
planes.each do |key, value|
complete = true
[11,14,15].each do |item|
complete = false if value[item] == '' || value[item] == nil
end
if complete && updates[value[4].to_sym]
redis.set(key.to_s, value.to_json, {:ex => 360})
updates[value[4].to_sym] = false
puts "updated redis"
end
#puts key.to_s+" : "+value.to_s if complete
end
end
def prune_hash(redis, planes, updates)
planes.delete_if do |key, value|
result = redis.get(key)
if result == nil
updates.delete(key)
puts "Pruned key: #{key}"
return true
end
return false
end
end
s = TCPSocket.new 'localhost', 30003
planes = Hash.new
updates = Hash.new
redis = Redis.new
times = 0
while line = s.gets
line.strip!
parts = line.split(',')
next if parts[1] == '8'
if !planes.has_key?(parts[4].to_sym) then
planes[parts[4].to_sym] = parts
updates[parts[4].to_sym] = false
else
update_plane(planes, updates, parts[4].to_sym, parts)
end
update_redis(redis, planes, updates)
if times >= 10000
puts "Hash pruning"
prune_hash(redis, planes, updates)
times = 0
end
times +=1
end
s.close