-
-
Save raykrueger/1534951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
require 'openssl' | |
class UcellGateway | |
#dunno what this is, but it's yours :) | |
include MessageFilter | |
def initialize | |
@logger = Logger.new | |
Smpp::Base.logger = @logger | |
end | |
# expose SMPP transceiver's send_mt method | |
def send_mt(from, number, message) | |
# we actually use a UUID, but this is easy | |
mt_id = OpenSSL::Random.random_bytes(16).unpack('H*').first | |
options = { | |
:data_coding => 0, | |
:source_addr_ton => 3, | |
:source_addr_npi => 0, | |
:dest_addr_ton => 1, | |
:dest_addr_npi => 1, | |
:priority_flag => 1 | |
} | |
@tx.send_mt(mt_id, from, number, message, options) | |
end | |
def logger | |
@logger | |
end | |
def start(config) | |
logger.info "Connecting to SMSC..." | |
EventMachine::run do | |
@tx = EventMachine::connect( | |
config[:host], | |
config[:port], | |
Smpp::Transceiver, | |
config, | |
self # delegate that will receive callbacks on MOs and DRs and other events | |
) | |
end | |
end | |
def redis | |
@redis ||= EM::Hiredis.connect | |
end | |
def bound | |
@bound = true | |
process_messages | |
end | |
def unbound | |
@bound = false | |
EventMachine.stop_event_loop | |
end | |
def message_accepted(transceiver, mt_message_id, pdu) | |
logger.info "Delegate: message_accepted: id #{mt_message_id} smsc ref id: #{pdu.message_id}" | |
end | |
def message_rejected(transceiver, mt_message_id, pdu) | |
logger.info "Delegate: message_rejected: id #{mt_message_id} smsc ref id: #{pdu.message_id}" | |
end | |
def process_messages | |
while @bound | |
#only wait 3 seconds so we can check if we're still bound | |
redis.blpop("ucell-out", 3) do |item| #blpop on redis return an array with key(ucell-out) and value | |
if item[1] | |
message_hashed = JSON.parse(item[1]) | |
send_mt(message_hashed["from"], | |
message_hashed["number"], | |
message_hashed["message"]) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment