Data correctly extracted.
This commit is contained in:
parent
fb6457660b
commit
1ec145efa8
2 changed files with 50 additions and 23 deletions
|
@ -6,23 +6,43 @@ connect() ->
|
||||||
{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
|
{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
|
||||||
Socket.
|
Socket.
|
||||||
|
|
||||||
|
read(Socket) ->
|
||||||
|
Content = receive_content(Socket),
|
||||||
|
Headers = extract_headers(Content),
|
||||||
|
ContentSize = dict:fetch("Content-Size", Headers),
|
||||||
|
ContentSizeI = list_to_integer(lists:nth(1, ContentSize)),
|
||||||
|
Data = receive_data(Socket, ContentSizeI, ""),
|
||||||
|
io:format("Headers: ~s~n", [Content]),
|
||||||
|
io:format("Data: ~s~n", [Data]),
|
||||||
|
received_command(Headers, Data).
|
||||||
|
|
||||||
read(Message) ->
|
receive_content(Socket) ->
|
||||||
case Message of
|
receive_content_(0, "", Socket).
|
||||||
{ok, M} ->
|
|
||||||
io:format("Message: ~n~n~s~n", [M]),
|
receive_content_(Amount, Headers, Socket) ->
|
||||||
HeaderList = string:tokens(M, "\n"),
|
{ok, Char1} = gen_tcp:recv(Socket, 1),
|
||||||
Headers = extract_headers(HeaderList),
|
case Char1 of
|
||||||
Data = extract_data(HeaderList),
|
"\n" -> case Amount of
|
||||||
io:format("Data: ~s~n", [Data]),
|
1 -> Headers;
|
||||||
received_command(Headers, Data)
|
_ -> receive_content_(Amount + 1,
|
||||||
|
Headers ++ Char1,
|
||||||
|
Socket)
|
||||||
|
end;
|
||||||
|
_ -> receive_content_(0, Headers ++ Char1, Socket)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
receive_data(_, 0, Headers) ->
|
||||||
|
Headers;
|
||||||
|
receive_data(Socket, ContentSize, Headers) ->
|
||||||
|
{ok, Char} = gen_tcp:recv(Socket, 1),
|
||||||
|
receive_data(Socket, ContentSize - 1, Headers ++ Char).
|
||||||
|
|
||||||
received_command(Headers, Data) ->
|
received_command(Headers, Data) ->
|
||||||
{ok, CommandList} = dict:find("Client-Command", Headers),
|
{ok, CommandList} = dict:find("Client-Command", Headers),
|
||||||
Command = lists:nth(1, CommandList),
|
Command = lists:nth(1, CommandList),
|
||||||
case Command of
|
case Command of
|
||||||
"hello" ->
|
"hello" ->
|
||||||
|
io:format("Set game token~n"),
|
||||||
pong_bot:set_game_token(Data),
|
pong_bot:set_game_token(Data),
|
||||||
send_command("ready", "");
|
send_command("ready", "");
|
||||||
"defined" ->
|
"defined" ->
|
||||||
|
@ -33,7 +53,9 @@ received_command(Headers, Data) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
make_message(ServerOrGame, Command, Args) ->
|
make_message(ServerOrGame, Command, Args) ->
|
||||||
|
io:format("Before fetch gametoken~n"),
|
||||||
GameToken = pong_bot:get_game_token(),
|
GameToken = pong_bot:get_game_token(),
|
||||||
|
io:format("After fetch gametoken~n"),
|
||||||
StrGameToken = string:concat("Token: ", GameToken),
|
StrGameToken = string:concat("Token: ", GameToken),
|
||||||
StrGameTokenln = string:concat(StrGameToken, "\n"),
|
StrGameTokenln = string:concat(StrGameToken, "\n"),
|
||||||
StrCommand = string:concat("-Command: ", Command),
|
StrCommand = string:concat("-Command: ", Command),
|
||||||
|
@ -65,20 +87,21 @@ list_concat([E|ES],Ret) ->
|
||||||
|
|
||||||
|
|
||||||
extract_headers(Source) ->
|
extract_headers(Source) ->
|
||||||
key_value_strings_to_dict(Source).
|
HeaderList = string:tokens(Source, "\n"),
|
||||||
|
key_value_strings_to_dict(HeaderList).
|
||||||
|
|
||||||
|
|
||||||
extract_data([]) ->
|
%extract_data([]) ->
|
||||||
[];
|
% [];
|
||||||
extract_data([E|ES]) ->
|
%extract_data([E|ES]) ->
|
||||||
io:format("~n~s~n", [E]),
|
% io:format("~n~s~n~n~n~n", [E]),
|
||||||
KeyValueList = key_value_string_to_list(E),
|
% KeyValueList = key_value_string_to_list(E),
|
||||||
case length(KeyValueList) of
|
% case length(KeyValueList) of
|
||||||
2 ->
|
% 2 ->
|
||||||
extract_data(ES);
|
% extract_data(ES);
|
||||||
_ ->
|
% _ ->
|
||||||
E
|
% E
|
||||||
end.
|
% end.
|
||||||
|
|
||||||
|
|
||||||
%%%Low-level internals.%%%
|
%%%Low-level internals.%%%
|
||||||
|
|
|
@ -13,11 +13,15 @@ start_link() ->
|
||||||
spawn(fun() -> game_loop() end ).
|
spawn(fun() -> game_loop() end ).
|
||||||
|
|
||||||
communication_loop(Socket) ->
|
communication_loop(Socket) ->
|
||||||
A = gen_tcp:recv(Socket, 0),
|
ggs_network:read(Socket),
|
||||||
ggs_network:read(A),
|
|
||||||
communication_loop(Socket).
|
communication_loop(Socket).
|
||||||
|
|
||||||
|
|
||||||
|
% A = gen_tcp:recv(Socket, 0),
|
||||||
|
% ggs_network:read(A),
|
||||||
|
%communication_loop(Socket).
|
||||||
|
|
||||||
|
|
||||||
peek_socket() ->
|
peek_socket() ->
|
||||||
gen_server:call({global, pong_bot}, socket).
|
gen_server:call({global, pong_bot}, socket).
|
||||||
|
|
||||||
|
|
Reference in a new issue