Permalink
Browse files

Add SSL support.

  • Loading branch information...
1 parent 0ff8610 commit bed3d87cbed5e67d1c281ebdccd2e83650628843 @jlouis jlouis committed Jul 6, 2012
Showing with 48 additions and 18 deletions.
  1. +23 −14 src/pony_client.erl
  2. +25 −4 src/pony_core_app.erl
View
@@ -72,19 +72,15 @@ handle_cast(Msg, State) ->
handle_info({tcp_closed, S},
#state { socket = {_, S} } = State) ->
{stop, normal, State};
+handle_info({ssl_closed, S},
+ #state { socket = {_, S} } = State) ->
+ {stop, normal, State};
handle_info({tcp, S, Chunk},
- #state { socket = {_, S} = Socket,
- cont = Cont } = State) ->
- ack_socket(Socket),
- case process_stream_chunk(Chunk, Cont) of
- {ok, NewCont} ->
- {noreply, State#state { cont = NewCont }};
- {msgs, Msgs, NewCont} ->
- {noreply,
- lists:foldl(fun handle_message/2,
- State#state { cont = NewCont },
- Msgs)}
- end;
+ #state { socket = {_, S}} = State) ->
+ handle_transport_packet(Chunk, State);
+handle_info({ssl, S, Chunk},
+ #state { socket = {_, S}} = State) ->
+ handle_transport_packet(Chunk, State);
handle_info(timeout, #state { synchronized = no,
listener = Listener,
socket = Socket } = State) ->
@@ -105,6 +101,19 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
+handle_transport_packet(Chunk, #state { cont = Cont,
+ socket = Socket } = State) ->
+ ack_socket(Socket),
+ case process_stream_chunk(Chunk, Cont) of
+ {ok, NewCont} ->
+ {noreply, State#state { cont = NewCont }};
+ {msgs, Msgs, NewCont} ->
+ {noreply,
+ lists:foldl(fun handle_message/2,
+ State#state { cont = NewCont },
+ Msgs)}
+ end.
+
ack_socket({Transport, Socket}) ->
Transport:setopts(Socket, [{active, once}]).
@@ -242,9 +251,9 @@ out({Transport, Socket}, Data) ->
out({Transport, Socket}, Format, Params) ->
Transport:send(Socket, [io_lib:format(Format, Params), <<"\r\n">>]).
-lookup_hostname({_Transport, Socket}) ->
+lookup_hostname({Transport, Socket}) ->
%% @todo should probably be a service on its own
- {ok, {Address, _Port}} = inet:peername(Socket),
+ {ok, {Address, _Port}} = Transport:peername(Socket),
{ok, #hostent { h_name = Hostname }} = inet:gethostbyaddr(Address),
{ok, Hostname}.
View
@@ -10,12 +10,33 @@
%% ===================================================================
start(_StartType, _StartArgs) ->
+ {ok, Port} = application:get_env(pony_core, port),
+ {ok, SSLPort} = application:get_env(pony_core, ssl_port),
{ok, Pid} = pony_core_sup:start_link(),
- ranch:start_listener(pony, 10,
- ranch_tcp, [{port, 6667}, raw, binary, {active, false}],
- pony_client, []),
+ case Port of
+ undefined ->
+ ok;
+ P when is_integer(P) ->
+ ranch:start_listener(pony_tcp, 10,
+ ranch_tcp, [{port, P}, inet,
+ raw, binary, {active, false}],
+ pony_client, [])
+ end,
+ case SSLPort of
+ undefined -> ok;
+ SP when is_integer(SP) ->
+ {ok, SKey} = application:get_env(pony_core, ssl_key),
+ {ok, Cert} = application:get_env(pony_core, ssl_cert),
+ ranch:start_listener(pony_ssl, 10,
+ ranch_ssl, [{port, SP}, inet,
+ {keyfile, SKey},
+ {certfile, Cert},
+ raw, binary, {active, false}],
+ pony_client, [])
+ end,
{ok, Pid}.
stop(_State) ->
- ranch:stop_listener(pony),
+ ranch:stop_listener(pony_tcp),
+ ranch:stop_listener(pony_ssl),
ok.

0 comments on commit bed3d87

Please sign in to comment.