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}]),
|
||||
Socket.
|
||||
|
||||
|
||||
read(Message) ->
|
||||
case Message of
|
||||
{ok, M} ->
|
||||
io:format("Message: ~n~n~s~n", [M]),
|
||||
HeaderList = string:tokens(M, "\n"),
|
||||
Headers = extract_headers(HeaderList),
|
||||
Data = extract_data(HeaderList),
|
||||
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)
|
||||
received_command(Headers, Data).
|
||||
|
||||
receive_content(Socket) ->
|
||||
receive_content_(0, "", Socket).
|
||||
|
||||
receive_content_(Amount, Headers, Socket) ->
|
||||
{ok, Char1} = gen_tcp:recv(Socket, 1),
|
||||
case Char1 of
|
||||
"\n" -> case Amount of
|
||||
1 -> Headers;
|
||||
_ -> receive_content_(Amount + 1,
|
||||
Headers ++ Char1,
|
||||
Socket)
|
||||
end;
|
||||
_ -> receive_content_(0, Headers ++ Char1, Socket)
|
||||
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) ->
|
||||
{ok, CommandList} = dict:find("Client-Command", Headers),
|
||||
Command = lists:nth(1, CommandList),
|
||||
case Command of
|
||||
"hello" ->
|
||||
io:format("Set game token~n"),
|
||||
pong_bot:set_game_token(Data),
|
||||
send_command("ready", "");
|
||||
"defined" ->
|
||||
|
@ -33,7 +53,9 @@ received_command(Headers, Data) ->
|
|||
end.
|
||||
|
||||
make_message(ServerOrGame, Command, Args) ->
|
||||
io:format("Before fetch gametoken~n"),
|
||||
GameToken = pong_bot:get_game_token(),
|
||||
io:format("After fetch gametoken~n"),
|
||||
StrGameToken = string:concat("Token: ", GameToken),
|
||||
StrGameTokenln = string:concat(StrGameToken, "\n"),
|
||||
StrCommand = string:concat("-Command: ", Command),
|
||||
|
@ -65,20 +87,21 @@ list_concat([E|ES],Ret) ->
|
|||
|
||||
|
||||
extract_headers(Source) ->
|
||||
key_value_strings_to_dict(Source).
|
||||
HeaderList = string:tokens(Source, "\n"),
|
||||
key_value_strings_to_dict(HeaderList).
|
||||
|
||||
|
||||
extract_data([]) ->
|
||||
[];
|
||||
extract_data([E|ES]) ->
|
||||
io:format("~n~s~n", [E]),
|
||||
KeyValueList = key_value_string_to_list(E),
|
||||
case length(KeyValueList) of
|
||||
2 ->
|
||||
extract_data(ES);
|
||||
_ ->
|
||||
E
|
||||
end.
|
||||
%extract_data([]) ->
|
||||
% [];
|
||||
%extract_data([E|ES]) ->
|
||||
% io:format("~n~s~n~n~n~n", [E]),
|
||||
% KeyValueList = key_value_string_to_list(E),
|
||||
% case length(KeyValueList) of
|
||||
% 2 ->
|
||||
% extract_data(ES);
|
||||
% _ ->
|
||||
% E
|
||||
% end.
|
||||
|
||||
|
||||
%%%Low-level internals.%%%
|
||||
|
|
|
@ -13,11 +13,15 @@ start_link() ->
|
|||
spawn(fun() -> game_loop() end ).
|
||||
|
||||
communication_loop(Socket) ->
|
||||
A = gen_tcp:recv(Socket, 0),
|
||||
ggs_network:read(A),
|
||||
ggs_network:read(Socket),
|
||||
communication_loop(Socket).
|
||||
|
||||
|
||||
% A = gen_tcp:recv(Socket, 0),
|
||||
% ggs_network:read(A),
|
||||
%communication_loop(Socket).
|
||||
|
||||
|
||||
peek_socket() ->
|
||||
gen_server:call({global, pong_bot}, socket).
|
||||
|
||||
|
|
Reference in a new issue