Remove subject IDs and improve request handling

This commit is contained in:
2025-03-01 17:22:31 +00:00
parent 9588e88b93
commit 68c55573de
9 changed files with 60 additions and 47 deletions

View File

@@ -6,7 +6,7 @@ ActivityType ::= ENUMERATED {
}
Session ::= SEQUENCE {
subjectId INTEGER,
subject UTF8String,
type ActivityType,
timestamp INTEGER,
minutes INTEGER
@@ -24,8 +24,7 @@ Request ::= SEQUENCE {
}
Activity ::= SEQUENCE {
subjectId INTEGER,
subjectName UTF8String,
subject UTF8String,
type ActivityType,
priority INTEGER
}

View File

@@ -13,7 +13,8 @@ start_link(Socket) ->
init(Socket) ->
ok = ssl:setopts(Socket, [{active, true}]),
{ok, #{socket => Socket}}.
process_flag(trap_exit, true),
{ok, #{socket => Socket, transactions => #{}}}.
handle_call(_Request, _From, State) ->
{reply, ok, State}.
@@ -21,21 +22,28 @@ handle_call(_Request, _From, State) ->
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({ssl, Socket, Data}, State) ->
handle_info({ssl, Socket, Data}, State = #{transactions := Transactions}) ->
case 'StudySystemProtocol':decode('Request', Data) of
{ok, {'Request', TransactionId, RequestBody}} ->
ResponseBody = map_request(RequestBody),
{ok, Encoded} = 'StudySystemProtocol':encode(
'Response',
{'Response', TransactionId, ResponseBody}),
ok = ssl:send(Socket, Encoded);
Pid = spawn_link(fun() -> handle_request(RequestBody) end),
NewTransactions = maps:put(Pid, TransactionId, Transactions),
{noreply, State#{transactions := NewTransactions}};
{error, {asn1, _Reason}} ->
{ok, Encoded} = 'StudySystemProtocol':encode(
'Response',
{'Response', -1, {error, invalidRequest}}),
ok = ssl:send(Socket, Encoded)
end,
{noreply, State};
send(Socket, -1, {error, invalidRequest}),
{noreply, State}
end;
handle_info({'EXIT', Pid, Reason},
State = #{socket := Socket, transactions := Transactions})
when is_map_key(Pid, Transactions) ->
TransactionId = maps:get(Pid, Transactions),
Response = case Reason of
{response, Value} -> Value;
_ ->
io:format("Request handling error: ~p~n", [Reason]),
{error, serverError}
end,
send(Socket, TransactionId, Response),
{noreply, State#{transactions := maps:remove(Pid, Transactions)}};
handle_info({ssl_closed, _Socket}, State) ->
{stop, normal, State};
handle_info({ssl_error, _Socket, _Reason}, State) ->
@@ -49,7 +57,17 @@ terminate(_Reason, #{socket := Socket}) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
handle_request(Request) ->
timer:kill_after(500),
exit(map_request(Request)).
map_request({ping, 'NULL'}) ->
{ack, 'NULL'};
{response, {ack, 'NULL'}};
map_request(_) ->
{error, invalidRequest}.
{response, {error, invalidArguments}}.
send(Socket, TransactionId, Response) ->
{ok, Encoded} = 'StudySystemProtocol':encode(
'Response',
{'Response', TransactionId, Response}),
ok = ssl:send(Socket, Encoded).

View File

@@ -11,8 +11,8 @@ start_link() ->
init([]) ->
SupFlags = #{strategy => simple_one_for_one,
intensity => 10,
period => 1},
intensity => 5,
period => 10},
ChildSpec = #{id => session_server,
start => {session_server, start_link, []},
restart => temporary,