Created
April 5, 2011 17:35
-
-
Save anonymous/904073 to your computer and use it in GitHub Desktop.
Example ESME via gen_smpp34 callback performing query_sm query against SMSC
This file contains 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
-module(querysm). | |
-include_lib("smpp34pdu/include/smpp34pdu.hrl"). | |
-behaviour(gen_esme34). | |
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, | |
handle_rx/2, handle_tx/3, terminate/2, code_change/3]). | |
-export([start/2, stop/0, sendsms/3, ping/0, querysm/2]). | |
-record(state, {host, port, system_id, password, owner, mref}). | |
start(Host, Port, SystemId, Pass) -> | |
application:start(smpp34), | |
gen_esme34:start({local, ?MODULE}, ?MODULE, [self(), Host, Port, SystemId, Pass], [{ignore_version, false}]). | |
stop() -> | |
gen_esme34:cast(?MODULE, stop). | |
querysm(MsgId, Src) -> | |
Q = #query_sm{message_id=MsgId, source_addr=Src}, | |
gen_esme34:transmit_pdu(?MODULE, Q, id()). | |
sendsms(Source, Dest, Msg) -> | |
S = #submit_sm{source_addr=Source, destination_addr=Dest, short_message=Msg}, | |
gen_esme34:transmit_pdu(?MODULE, S, id()). | |
ping() -> | |
gen_esme34:ping(?MODULE). | |
init([Owner, Host, Port, SystemId, Password]) -> | |
Mref = erlang:monitor(process, Owner), | |
{ok, {Host, Port, | |
#bind_transmitter{system_id=SystemId, password=Password}}, | |
#state{host=Host, port=Port, system_id=SystemId, password=Password, | |
owner=Owner, mref=Mref}}. | |
handle_tx({ok, Sn}, Extra, St) -> | |
error_logger:info_msg("helo|tx|~p|ok|~p~n", [Extra, Sn]), | |
{noreply, St}; | |
handle_tx({error, Reason}, Extra, St) -> | |
error_logger:info_msg("helo|tx|~p|err|~p~n", [Extra, Reason]), | |
{noreply, St}. | |
handle_rx(#pdu{sequence_number=Snum, body=#deliver_sm{source_addr=Src, destination_addr=Dst, short_message=_Msg}}=Pdu, St) -> | |
error_logger:info_msg("helo|rx|~p~n", [Pdu]), | |
Did = id(), | |
DeliverSmResp = #deliver_sm_resp{message_id=Did}, | |
SubmitSm = #submit_sm{source_addr=Dst, destination_addr=Src, short_message="Hello SMPP World"}, | |
gen_esme34:transmit_pdu(self(), SubmitSm, id()), | |
{tx, {?ESME_ROK, Snum, DeliverSmResp, Did}, St}; | |
handle_rx(Pdu, St) -> | |
error_logger:info_msg("helo|rx|~p~n", [Pdu]), | |
{noreply, St}. | |
handle_call(Req, _From, St) -> | |
{reply, {error, Req}, St}. | |
handle_cast(stop, St) -> | |
{stop, normal, St}; | |
handle_cast(_Req, St) -> | |
{noreply, St}. | |
handle_info({'DOWN', Mref, _, _, Reason}, #state{mref=Mref}=St) -> | |
{stop, Reason, St}; | |
handle_info(_Req, St) -> | |
{noreply, St}. | |
terminate(_Reason, _St) -> | |
ok. | |
code_change(_OldVsn, St, _Extra) -> | |
{noreply, St}. | |
id() -> | |
{A, B, C} = now(), | |
lists:flatten(io_lib:format("~p~p~p", [A, B, C])). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment