Skip to content

Instantly share code, notes, and snippets.

@Bek
Created December 29, 2011 00:11
Show Gist options
  • Save Bek/1530609 to your computer and use it in GitHub Desktop.
Save Bek/1530609 to your computer and use it in GitHub Desktop.
module MessageSender
def self.redis
@redis ||= EM::Hiredis.connect
end
def self.next
if UcellGateway.connection_exists == true
redis.blpop("ucell-out", 0) do |item| #blpop on redis return an array with key(ucell-out) and value
if item[1]
message_hashed = JSON.parse(item[1])
UcellGateway.send_mt(message_hashed["from"],
message_hashed["number"],
message_hashed["message"])
end
EM.next_tick(&method(:next))
end
else
EM.next_tick(&method(:next))
end
end
end
class UcellGateway
include MessageFilter
include MessageSender
# MT id counter.
@@mt_id = 0
# expose SMPP transceiver's send_mt method
def self.send_mt(*args)
@@mt_id += 1
@@tx.send_mt(@@mt_id, *args)
end
# new method: checks if the connection is made
def self.connection_exists
@@tx.state == :bound
end
def logger
Smpp::Base.logger
end
def start(config)
# File.open(PIDFILE, 'w') { |f| f << Process.pid }
# The transceiver sends MT messages to the SMSC. It needs a storage with Hash-like
# semantics to map SMSC message IDs to your own message IDs.
pdr_storage = {}
# Run EventMachine in loop so we can reconnect when the SMSC drops our connection.
puts "Connecting to SMSC..."
loop do
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
)
MessageSender.next # gets the messages from redis list and sends at each click of the EventMachine
end
puts "Disconnected. Reconnecting in 5 seconds.."
sleep 5
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment