Class: RocketSMS::Transceiver
- Inherits:
-
Object
- Object
- RocketSMS::Transceiver
- Defined in:
- lib/rocket_sms/transceiver.rb
Instance Method Summary collapse
- #bound(transceiver) ⇒ Object
- #cleanup ⇒ Object
- #configure ⇒ Object
- #connect ⇒ Object
- #delivery_report_received(transceiver, pdu) ⇒ Object
- #dispatch ⇒ Object
- #dredis ⇒ Object
-
#initialize(id, redis_url, log_location) ⇒ Transceiver
constructor
A new instance of Transceiver.
- #log(msg, level = 'info') ⇒ Object
- #logger ⇒ Object
- #message_accepted(transceiver, mt_message_id, pdu) ⇒ Object
- #message_rejected(transceiver, mt_message_id, pdu) ⇒ Object
- #mo_received(transceiver, pdu) ⇒ Object
- #queues ⇒ Object
- #redis ⇒ Object
- #register ⇒ Object
- #send_message(message) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #stop(signal = nil) ⇒ Object
- #throughput ⇒ Object
- #unbound(transceiver) ⇒ Object
Constructor Details
#initialize(id, redis_url, log_location) ⇒ Transceiver
Returns a new instance of Transceiver.
5 6 7 8 9 10 11 12 |
# File 'lib/rocket_sms/transceiver.rb', line 5 def initialize(id, redis_url, log_location) @id, @redis_url, @log_location = id, redis_url, log_location @active = true @online = false @fast = false @settings = {} @mts = {} end |
Instance Method Details
#bound(transceiver) ⇒ Object
240 241 242 243 244 245 |
# File 'lib/rocket_sms/transceiver.rb', line 240 def bound(transceiver) log "#{@id} - Transceiver Bound" @online = true @reconnector = nil register end |
#cleanup ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rocket_sms/transceiver.rb', line 83 def cleanup redis.zrangebyscore("gateway:transceivers:#{@id}:dispatch", '-inf', '+inf') do |payloads| if payloads and !payloads.empty? op = Proc.new do |payload, iter| = Message.from_json(payload) .send_at, .expires_at = nil, nil score = (Time.now.to_f*1000).to_i redis.multi redis.zrem("gateway:transceivers:#{@id}:dispatch", payload) redis.zadd(queues[:mt][:pending], score, payload) redis.exec do |resp| iter.next end end cb = Proc.new do |responses| EM::Timer.new(3){ shutdown } end EM::Iterator.new(payloads).each(op,cb) else EM::Timer.new(3){ shutdown } end end end |
#configure ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rocket_sms/transceiver.rb', line 107 def configure return unless @active @configurator = EM::Timer.new(1){ configure } redis.multi redis.hget("gateway:transceivers:#{@id}", "throughput") redis.hget("gateway:transceivers:#{@id}", "connection") redis.exec do |response| if response and !response.flatten.include?(nil) throughput_payload = response[0] connection_payload = response[1] @settings[:throughput] = throughput_payload.to_f @settings[:connection] = MultiJson.load(connection_payload, symbolize_keys: true) else stop end end end |
#connect ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rocket_sms/transceiver.rb', line 125 def connect return unless @active if @settings and @settings[:connection] log "Connecting transceiver #{@id}." @connection = EM.connect( @settings[:connection][:host], @settings[:connection][:port], Smpp::Transceiver, @settings[:connection], self ) else EM::Timer.new(1){ connect } end end |
#delivery_report_received(transceiver, pdu) ⇒ Object
206 207 208 209 210 |
# File 'lib/rocket_sms/transceiver.rb', line 206 def delivery_report_received(transceiver, pdu) log "#{@id} - DR Received" ticket = { pdu: { source_addr: pdu.source_addr, short_message: pdu., destination_addr: pdu.destination_addr } } EM.next_tick { redis.lpush(queues[:dr],MultiJson.dump(ticket)) } end |
#dispatch ⇒ Object
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 |
# File 'lib/rocket_sms/transceiver.rb', line 141 def dispatch return unless @active interval = @fast ? ((throughput.to_f)**-1)*1.1 : 0.5 @dispatcher = EM::Timer.new(interval){ dispatch } redis.multi redis.zrange("gateway:transceivers:#{@id}:dispatch", 0, 0) redis.zremrangebyrank("gateway:transceivers:#{@id}:dispatch", 0, 0) redis.exec do |response| if response payload = response[0][0] if payload @fast = true now = Time.now.to_f = Message.from_json(payload) if .send_at > now score = (.send_at * 1000).to_i redis.zadd("gateway:transceivers:#{@id}:dispatch", score , payload) elsif .send_at <= now and now < .expires_at log "Message #{.id} detected on #{@id}. Sending." () elsif .expires_at <= now log "Message #{.id} detected on #{@id} but has expired. Retrying." .add_pass score = (( Time.now.to_f + 15 ) * 1000).to_i redis.zadd(queues[:mt][:pending], score, .to_json) @dispatcher.cancel if @dispatcher EM.next_tick{ dispatch } end else @fast = false end end end end |
#dredis ⇒ Object
18 19 20 |
# File 'lib/rocket_sms/transceiver.rb', line 18 def dredis @dredis ||= EM::Hiredis.connect(@redis_url) end |
#log(msg, level = 'info') ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rocket_sms/transceiver.rb', line 26 def log(msg, level = 'info') if EM.reactor_running? EM.defer{ logger.send(level, msg) } else logger.send(level, msg) end end |
#logger ⇒ Object
22 23 24 |
# File 'lib/rocket_sms/transceiver.rb', line 22 def logger @logger ||= Logger.new(@log_location) end |
#message_accepted(transceiver, mt_message_id, pdu) ⇒ Object
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/rocket_sms/transceiver.rb', line 212 def (transceiver, , pdu) = @mts.delete(.to_s) if log "#{@id} - Message #{.id} - Accepted" .accepted_at = Time.now.to_i EM.next_tick { redis.lpush(queues[:mt][:success],.to_json) } else log "#{@id} - Untracked MT Accepted #{}" end end |
#message_rejected(transceiver, mt_message_id, pdu) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rocket_sms/transceiver.rb', line 223 def (transceiver, , pdu) = @mts.delete(.to_s) if log "#{@id} - Message #{.id} - Rejected" .add_pass .rejected_at = Time.now.to_i if .pass <= 5 score = Time.now.to_i + 10 EM.next_tick{ redis.zadd(queues[:mt][:pending], score, .to_json) } else EM.next_tick { redis.lpush(queues[:mt][:failure],.to_json) } end else log "#{@id} - Untracked MT Rejected #{}" end end |
#mo_received(transceiver, pdu) ⇒ Object
200 201 202 203 204 |
# File 'lib/rocket_sms/transceiver.rb', line 200 def mo_received(transceiver, pdu) log "#{@id} - Message Received" ticket = { pdu: { source_addr: pdu.source_addr, short_message: pdu., destination_addr: pdu.destination_addr } } EM.next_tick { redis.lpush(queues[:mo],MultiJson.dump(ticket)) } end |
#queues ⇒ Object
34 35 36 |
# File 'lib/rocket_sms/transceiver.rb', line 34 def queues RocketSMS.queues end |
#redis ⇒ Object
14 15 16 |
# File 'lib/rocket_sms/transceiver.rb', line 14 def redis @redis ||= EM::Hiredis.connect(@redis_url) end |
#register ⇒ Object
176 177 178 179 |
# File 'lib/rocket_sms/transceiver.rb', line 176 def register stat = @online ? 'online' : 'offline' redis.hset("gateway:transceivers:#{@id}", 'status', stat) end |
#send_message(message) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rocket_sms/transceiver.rb', line 181 def () begin if @online log "Sending Message #{.id} through DID #{.sender} via #{@id}." @mts[.id.to_s] = @connection.send_mt(.id,.sender,.receiver,.body) else log "#{@id} is not connected. Pushing message #{.id} to dispatch queue." score = (.send_at * 1000).to_i redis.zadd("gateway:transceivers:#{@id}:dispatch", score , payload) end rescue Exception log "### Error Sending MT #{.id} with DID #{.sender} through Transceiver #{@id}. Retrying message." .add_pass score = Time.now.to_i + 15 redis.zadd(queues[:mt][:pending], score, .to_json) end end |
#shutdown ⇒ Object
78 79 80 81 |
# File 'lib/rocket_sms/transceiver.rb', line 78 def shutdown log "Transceiver #{@id} DOWN." EM.stop end |
#start ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rocket_sms/transceiver.rb', line 42 def start EM.threadpool_size = 128 EM.set_max_timers(100_000) EM.run do log "Starting Transceiver #{@id}" # Set quantum to 10 milliseconds to support throughputs up to 100 MTs/sec EM.set_quantum(10) # Detect transceiver configuration from Redis. configure # Connect connect # Start detecting and dispatching MTs dispatch # Trap exit-related signals Signal.trap("INT") { |signal| stop(signal) } Signal.trap("TERM") { |signal| stop(signal) } end end |
#stop(signal = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rocket_sms/transceiver.rb', line 61 def stop(signal = nil) if @kill log "#{@id} - Forcing Exit. Check your data for losses." shutdown else log "#{@id} - Stopping. Waiting for pending operations to finish." @kill = true @active = false @connection.close_connection_after_writing if @connection @dispatcher.cancel if @dispatcher @configurator.cancel if @configurator @reconnector.cancel if @reconnector redis.del("gateways:transceivers:#{@id}") cleanup end end |
#throughput ⇒ Object
38 39 40 |
# File 'lib/rocket_sms/transceiver.rb', line 38 def throughput @settings && @settings[:throughput] ||= 1.0 end |
#unbound(transceiver) ⇒ Object
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/rocket_sms/transceiver.rb', line 247 def unbound(transceiver) log "#{@id} - Transceiver Unbound" if @active log "#{@id} is not connected. Retrying in 10 seconds." @reconnector.cancel if @reconnector @reconnector = EM::Timer.new(10){ connect } end @online = false register end |