patch to allow specifying an IP address

Joe Yandle
2008-04-18
2013-05-21
  • Joe Yandle
    Joe Yandle
    2008-04-18

    Hi everybody,

    OSERL currently does not allow users to specify an IP address when listening on a gen_smsc or gen_esme.  The following patch enables this:

    Index: gen_smsc.erl

    --- gen_smsc.erl    (revision 99669)
    +++ gen_smsc.erl    (working copy)
    @@ -360,6 +360,7 @@
              start_link/4,
              listen_start/1,
              listen_start/4,
    +         listen_start/5,
              listen_stop/1,
              call/2,
              call/3,
    @@ -573,6 +574,30 @@
    listen_start(ServerRef, Port, Count, Timers) ->
         gen_server:call(ServerRef, {listen_start, Port, Count, Timers}).

    +%% @spec listen_start(ServerRef, IpAddress, Port, Count, Timers) -> ok | {error, Reason}
    +%%    ServerRef = Name | {Name, Node} | {global, Name} | pid()
    +%%    Name = atom()
    +%%    Node = atom()
    +%%    IpAddress = ip_address()
    +%%    Port = int()
    +%%    Timers = timers()
    +%%    Count = int() | infinity
    +%%    Reason = term()
    +%%
    +%% @doc Puts the SMSC Server <tt>ServerRef</tt> to listen on <tt>IpAddress</tt>:<tt>Port</tt>
    +%%
    +%% <p><tt>Timers</tt> is a <tt>timers</tt> record as declared in
    +%% <a href="oserl.html">oserl.hrl</a>.  Accepted session will be subject to
    +%% given timers.</p>
    +%%
    +%% <p>Only <tt>Count</tt> connections are accepted.  Started sessions are NOT
    +%% linked to the SMSC.</p>
    +%%
    +%% @see listen_start/1
    +%% @end
    +listen_start(ServerRef, IpAddress, Port, Count, Timers) ->
    +    gen_server:call(ServerRef, {listen_start, IpAddress, Port, Count, Timers}).
    +
    %% @spec listen_stop(ServerRef) -> ok
    %%    ServerRef = Name | {Name, Node} | {global, Name} | pid()
    %%    Name = atom()
    @@ -883,6 +908,15 @@
             _Error ->
                 {reply, false, S}
         end;
    +handle_call({listen_start, IpAddress, Port, Count, Timers}, _From, S) ->
    +    case gen_tcp:listen(Port, [{ip, IpAddress} | ?LISTEN_OPTIONS]) of
    +        {ok, LSocket} ->
    +            Self = self(),
    +            spawn_link(fun() -> listener(Self, LSocket, Count) end),
    +            {reply, true, S#state{lsocket = LSocket, timers = Timers}};
    +        _Error ->
    +            {reply, false, S}
    +    end;
    handle_call({accept, Socket}, _From, S) ->
         {reply, gen_smsc_session:start(self(), ?MODULE, Socket, S#state.timers), S};
    handle_call({Bind, _Session, _Pdu, _IPAddr} = R, From, S)
    Index: gen_esme.erl
    ===================================================================
    --- gen_esme.erl    (revision 99669)
    +++ gen_esme.erl    (working copy)
    @@ -461,6 +461,7 @@
              start_link/4,
              listen_start/1,
              listen_start/4,
    +         listen_start/5,
              listen_stop/1,
              call/2,
              call/3,
    @@ -697,6 +698,28 @@
    listen_start(ServerRef, Port, Count, Timers) ->
         gen_server:call(ServerRef, {listen_start, Port, Count, Timers}).

    +%% @spec listen_start(ServerRef, IpAddress, Port, Count, Timers) -> ok | {error, Reason}
    +%%    ServerRef = Name | {Name, Node} | {global, Name} | pid()
    +%%    Name = atom()
    +%%    Node = atom()
    +%%    IpAddress = ip_address()
    +%%    Port = int()
    +%%    Count = int() | infinity
    +%%    Timers = timers()
    +%%    Reason = term()
    +%%
    +%% @doc Puts the ESME Server <tt>ServerRef</tt> to listen on <tt>Port</tt>
    +%%
    +%% <p><tt>Timers</tt> is a <tt>timers</tt> record as declared in
    +%% <a href="oserl.html">oserl.hrl</a>.  Accepted session will be subject to
    +%% given timers.</p>
    +%%
    +%% <p><tt>Count</tt> connections are accepted.  Started sessions are NOT
    +%% linked to the SMSC.</p>
    +%% @end
    +listen_start(ServerRef, IpAddress, Port, Count, Timers) ->
    +    gen_server:call(ServerRef, {listen_start, IpAddress, Port, Count, Timers}).
    +
    %% @spec listen_stop(ServerRef) -> ok
    %%    ServerRef = Name | {Name, Node} | {global, Name} | pid()
    %%    Name = atom()
    @@ -1417,6 +1440,15 @@
             _Error ->
                 {reply, false, S}
         end;
    +handle_call({listen_start, IpAddress, Port, Count, Timers}, _From, S) ->
    +    case gen_tcp:listen(Port, [{ip, IpAddress} | ?LISTEN_OPTS]) of
    +        {ok, LSocket} ->
    +            Self = self(),
    +            proc_lib:spawn_link(fun() -> listener(Self, LSocket, Count) end),
    +            {reply, true, S#state{lsocket = LSocket, timers = Timers}};
    +        _Error ->
    +            {reply, false, S}
    +    end;
    handle_call({accept, Socket}, _From, S) ->
         R = gen_esme_session:start(?MODULE, Socket, S#state.timers, S#state.log),
         {reply, R, S};

     
    • Thanks for the patch,

      I guess that is about time to package the last changes into version 1.3.  I will include this patch also.

      Regards,

      Quique

       
      • Joe Yandle
        Joe Yandle
        2008-05-05

        Thanks for accepting the patch.

        I sent another patch to oserl-questions@sourceforge.  This one fixes compiler warnings for obsolete guards.  Let me know if you'd prefer me to communicate via the forums rather than the mailing list, and I'll send the patch here.

        cheers,

        Joey

         
        • Thank you Joey,

          Regards,