1#!/usr/bin/env ruby
2#
3# Copyright (C) 2020 Denver Gingerich <denver@ossguy.com>
4#
5# This file is part of sgx-bwmsgsv2.
6#
7# sgx-bwmsgsv2 is free software: you can redistribute it and/or modify it under
8# the terms of the GNU Affero General Public License as published by the Free
9# Software Foundation, either version 3 of the License, or (at your option) any
10# later version.
11#
12# sgx-bwmsgsv2 is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
15# details.
16#
17# You should have received a copy of the GNU Affero General Public License along
18# with sgx-bwmsgsv2. If not, see <http://www.gnu.org/licenses/>.
19
20require 'redis'
21require 'hiredis'
22require 'net/http'
23require 'json'
24require 'time'
25
26$stdout.sync = true
27
28puts "Redis queue to SGX HTTP request translator - for Bandwidth API V2 SGX\n"\
29 "==>> last commit of this version is " + `git rev-parse HEAD` + "\n"
30
31if ARGV.size != 4
32 puts "Usage: r2s-bwmsgsv2.rb <redis_input_queue_key> "\
33 "<redis_pending_queue_key> <sgx_hostname> <sgx_http_port>"
34 exit 0
35end
36
37["INT", "TERM"].each do |sig|
38 trap(sig) do
39 puts "Translator has terminated due to SIG#{sig}."
40 exit 0
41 end
42end
43
44t = Time.now
45puts "LOG %d.%09d: starting...\n\n" % [t.to_i, t.nsec]
46
47redis = Redis.new(:driver => :hiredis)
48
49while true
50 timestamps_plus_json_blob = redis.brpoplpush(ARGV[0], ARGV[1])
51 # TODO: add timestamping here, and MUST include a ./tai call with it
52
53 # TODO: print less stuff in here
54 puts "TODO - got some stuff: " + timestamps_plus_json_blob
55
56 # add some timestamps to timestamps_plus_json_blob (our own)
57
58 # TODO: fix so sending timestamps_plus_json_blob to SGX, and via POST
59 response = Net::HTTP.get_response(
60 URI("https://#{ARGV[2]}:#{ARGV[3]}/r2tst.php")
61 )
62
63 puts "TODO - response.code = #{response.code} and body #{response.body}"
64
65 # if SGX gives back anything but response.code 200, retry 5 times-ish
66 # if SGX failed to respond after retries, email or other notify
67 # and back off with even longer retries (can't do anything else really)
68
69 # if got HTTP 200, then:
70
71 # redis.set(...) with expiration of 3 days, including timestamps
72
73 # confirm ARGV[1] key has one element and e == timestamps_plus_json_blob
74 # if so, then rpop ARGV[1] and discard
75 # if not, then email or other notify, and probably exit with failure
76end