Transfer ownership of client socket in session_server:init/1
This commit is contained in:
@@ -12,6 +12,7 @@ start_link(Socket) ->
|
|||||||
gen_server:start_link(?MODULE, Socket, []).
|
gen_server:start_link(?MODULE, Socket, []).
|
||||||
|
|
||||||
init(Socket) ->
|
init(Socket) ->
|
||||||
|
ok = ssl:controlling_process(Socket, self()),
|
||||||
ok = ssl:setopts(Socket, [{active, true}]),
|
ok = ssl:setopts(Socket, [{active, true}]),
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
{ok, #{socket => Socket, transactions => #{}}}.
|
{ok, #{socket => Socket, transactions => #{}}}.
|
||||||
@@ -38,9 +39,7 @@ handle_info({'EXIT', Pid, Reason},
|
|||||||
TransactionId = maps:get(Pid, Transactions),
|
TransactionId = maps:get(Pid, Transactions),
|
||||||
Response = case Reason of
|
Response = case Reason of
|
||||||
{response, Value} -> Value;
|
{response, Value} -> Value;
|
||||||
_ ->
|
_ -> {error, serverError}
|
||||||
io:format("Error handling request: ~p~n", [Reason]),
|
|
||||||
{error, serverError}
|
|
||||||
end,
|
end,
|
||||||
send(Socket, TransactionId, Response),
|
send(Socket, TransactionId, Response),
|
||||||
{noreply, State#{transactions := maps:remove(Pid, Transactions)}};
|
{noreply, State#{transactions := maps:remove(Pid, Transactions)}};
|
||||||
@@ -59,25 +58,25 @@ code_change(_OldVsn, State, _Extra) ->
|
|||||||
|
|
||||||
handle_request(Request) ->
|
handle_request(Request) ->
|
||||||
timer:kill_after(500),
|
timer:kill_after(500),
|
||||||
exit(map_request(Request)).
|
exit({response, map_request(Request)}).
|
||||||
|
|
||||||
map_request({ping, 'NULL'}) ->
|
map_request({ping, 'NULL'}) ->
|
||||||
{response, {ack, 'NULL'}};
|
{ack, 'NULL'};
|
||||||
map_request({listActivities, 'NULL'}) ->
|
map_request({listActivities, 'NULL'}) ->
|
||||||
{activities, Activities} = subject_router:get_activities(),
|
{activities, Activities} = subject_router:get_activities(),
|
||||||
SortedActivities = lists:reverse(lists:keysort(3, Activities)),
|
SortedActivities = lists:reverse(lists:keysort(3, Activities)),
|
||||||
{response, {activities,
|
{activities,
|
||||||
[{'Activity', Subject, Type, round(Priority * 100)}
|
[{'Activity', Subject, Type, round(Priority * 100)}
|
||||||
|| {Subject, Type, Priority} <- SortedActivities]}};
|
|| {Subject, Type, Priority} <- SortedActivities]};
|
||||||
map_request({logSession, {'Session', Subject, Type, Timestamp, Minutes}}) ->
|
map_request({logSession, {'Session', Subject, Type, Timestamp, Minutes}}) ->
|
||||||
Session = {unicode:characters_to_list(Subject),
|
Session = {unicode:characters_to_list(Subject),
|
||||||
Type, Timestamp, Minutes},
|
Type, Timestamp, Minutes},
|
||||||
case subject_router:log_session(Session) of
|
case subject_router:log_session(Session) of
|
||||||
ok -> {response, {ack, 'NULL'}};
|
ok -> {ack, 'NULL'};
|
||||||
{error, _Error} -> {response, {error, invalidArguments}}
|
{error, _Error} -> {error, invalidArguments}
|
||||||
end;
|
end;
|
||||||
map_request(_) ->
|
map_request(_) ->
|
||||||
{response, {error, invalidArguments}}.
|
{error, invalidArguments}.
|
||||||
|
|
||||||
send(Socket, TransactionId, Response) ->
|
send(Socket, TransactionId, Response) ->
|
||||||
{ok, Encoded} = 'StudySystemProtocol':encode(
|
{ok, Encoded} = 'StudySystemProtocol':encode(
|
||||||
|
|||||||
@@ -41,8 +41,7 @@ handle_info(accept, State = #state{socket = Socket}) ->
|
|||||||
handle_info({handshake, TlsSocket}, State) ->
|
handle_info({handshake, TlsSocket}, State) ->
|
||||||
case ssl:handshake(TlsSocket, 5000) of
|
case ssl:handshake(TlsSocket, 5000) of
|
||||||
{ok, ClientSocket} ->
|
{ok, ClientSocket} ->
|
||||||
{ok, Pid} = session_sup:start_session(ClientSocket),
|
{ok, _Pid} = session_sup:start_session(ClientSocket);
|
||||||
ok = ssl:controlling_process(ClientSocket, Pid);
|
|
||||||
{error, _Reason} ->
|
{error, _Reason} ->
|
||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
|
|||||||
Reference in New Issue
Block a user