gen_server working.
This commit is contained in:
parent
9986a801aa
commit
7f95d96cda
2 changed files with 43 additions and 27 deletions
|
@ -1,12 +1,12 @@
|
|||
-module(ggs_network).
|
||||
-export([connect/0,append_key_value_strings_to_dict/2,key_value_string_to_list/1]).
|
||||
-export([read/1]).
|
||||
|
||||
connect() ->
|
||||
{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
|
||||
A = gen_tcp:recv(Socket,0),
|
||||
read(A),
|
||||
Socket.
|
||||
|
||||
|
||||
read(Message) ->
|
||||
case Message of
|
||||
{ok, M} ->
|
||||
|
@ -22,9 +22,10 @@ received_command(Headers, Data) ->
|
|||
case Command of
|
||||
"hello" ->
|
||||
io:format("Received command 'hello'~n"),
|
||||
io:format("Game token: ~s~n", [Data]),
|
||||
pong_bot:set_game_token(Data),
|
||||
%gen_server:cast({global, pong_bot}, {game_token, Data}),
|
||||
send_command("Ready", "");
|
||||
send_command("'ready'", "");
|
||||
%pong_bot:ggsNetworkReady(); Unneccessary
|
||||
"defined" ->
|
||||
ok;
|
||||
|
@ -34,10 +35,7 @@ received_command(Headers, Data) ->
|
|||
end.
|
||||
|
||||
make_message(ServerOrGame, Command, Args) ->
|
||||
io:format("Make message~n"),
|
||||
GameToken = pong_bot:get_game_token(),
|
||||
% GameToken = gen_server:call({global, pong_bot}, game_token),
|
||||
io:format("Make message2~n"),
|
||||
StrGameToken = string:concat("Token: ", GameToken),
|
||||
StrGameTokenln = string:concat(StrGameToken, "\n"),
|
||||
StrCommand = string:concat("-Command: ", Command),
|
||||
|
@ -47,14 +45,10 @@ make_message(ServerOrGame, Command, Args) ->
|
|||
StrContentLengthln = string:concat(StrContentLength, "\n\n"),
|
||||
StrTokenCommand = string:concat(StrGameTokenln, StrFullCommand),
|
||||
Message = string:concat(StrTokenCommand, StrContentLengthln),
|
||||
|
||||
MessageWithArgs = string:concat(Message, list_concat(Args,[])),
|
||||
io:format("Make message: ~n~n~s",[MessageWithArgs]),
|
||||
MessageWithArgs.
|
||||
|
||||
%define(SourceCode) ->
|
||||
% write(make_message("Server", "define", SourceCode)).
|
||||
|
||||
|
||||
send_command(Command, Args) ->
|
||||
write(make_message("Client", Command, Args)).
|
||||
|
||||
|
|
|
@ -3,27 +3,36 @@
|
|||
-export([start_link/0]).
|
||||
-export([init/1, handle_call/3, handle_cast/2]).
|
||||
-export([ggsNetworkReceivedCommandWithArgs/2,set_game_token/1,get_game_token/0]).
|
||||
-export([view/0, peek_socket/0]).
|
||||
|
||||
|
||||
start_link() ->
|
||||
gen_server:start_link({global, pong_bot}, pong_bot, [], []).
|
||||
gen_server:start_link({global, pong_bot}, pong_bot, [], []),
|
||||
Socket = peek_socket(),
|
||||
A = gen_tcp:recv(Socket,0),
|
||||
ggs_network:read(A).
|
||||
|
||||
peek_socket() ->
|
||||
gen_server:call({global, pong_bot}, socket).
|
||||
|
||||
|
||||
init(_Args) ->
|
||||
io:format("State initialization.~n"),
|
||||
Player1 = new_pos(),
|
||||
Player2 = new_pos(),
|
||||
Ball = new_pos(),
|
||||
Paused = true,
|
||||
SendStart = false,
|
||||
GGSNetwork = ggs_network:connect(), %Localhost is set internally inside
|
||||
%ggs_network.
|
||||
Socket = ggs_network:connect(), %Localhost is set internally inside ggs_network.
|
||||
State1 = dict:new(),
|
||||
State2 = dict:store(player1, Player1, State1),
|
||||
State3 = dict:store(player2, Player2, State2),
|
||||
State4 = dict:store(ball, Ball, State3),
|
||||
State5 = dict:store(paused, Paused, State4),
|
||||
State6 = dict:store(send_start, SendStart, State5),
|
||||
State = dict:store(ggs_network, GGSNetwork, State6),
|
||||
State.
|
||||
State = dict:store(socket, Socket, State6),
|
||||
io:format("End State initialization.~n"),
|
||||
{ok, State}.
|
||||
|
||||
new_pos() ->
|
||||
{0, 0}.
|
||||
|
@ -48,6 +57,7 @@ ggsNetworkReceivedCommandWithArgs(Command, Args) ->
|
|||
end.
|
||||
|
||||
welcome(Who_am_I) ->
|
||||
io:format("Welcome begin~n"),
|
||||
case Who_am_I of
|
||||
1 ->
|
||||
Me = gen_server:call(pong_bot, player1),
|
||||
|
@ -130,6 +140,9 @@ set_game_token(GameToken) ->
|
|||
get_game_token() ->
|
||||
gen_server:call({global, pong_bot}, game_token).
|
||||
|
||||
view() ->
|
||||
gen_server:call({global, pong_bot}, game_token).
|
||||
|
||||
handle_call(player1, _From, State) ->
|
||||
Player1 = dict:fetch(player1, State),
|
||||
{reply, Player1, State};
|
||||
|
@ -143,9 +156,22 @@ handle_call(player2_y, _From, State) ->
|
|||
{reply, Y, State};
|
||||
|
||||
handle_call(game_token, _From, State) ->
|
||||
io:format("Handle call game_token~n"),
|
||||
GameToken = dict:fetch(game_token, State),
|
||||
{reply, GameToken, State}.
|
||||
{reply, GameToken, State};
|
||||
|
||||
handle_call(view, _From, State) ->
|
||||
io:format("View the state.~n"),
|
||||
% StateFromList = lists:nth(1, State)
|
||||
{reply, State, State};
|
||||
|
||||
handle_call(socket, _From, State) ->
|
||||
Socket = dict:fetch(socket, State),
|
||||
%Socket = lists:nth(1, SocketInList),
|
||||
{reply, Socket, State}.
|
||||
|
||||
handle_cast({game_token, GameToken}, State) ->
|
||||
NewState = dict:store(game_token, GameToken, State),
|
||||
{noreply, NewState};
|
||||
|
||||
handle_cast({me, Me}, State) ->
|
||||
NewState = dict:store(me, Me, State),
|
||||
|
@ -174,9 +200,5 @@ handle_cast({paused, Paused}, State) ->
|
|||
handle_cast({new_rouned, Paused, SendStart}, State) ->
|
||||
State1 = dict:store(paused, Paused, State),
|
||||
NewState = dict:store(send_start, SendStart, State1),
|
||||
{noreply, NewState};
|
||||
|
||||
handle_cast({game_token, Token}, State) ->
|
||||
io:format("Handle cast game_token~n"),
|
||||
NewState = dict:store(game_token, Token, State),
|
||||
{noreply, NewState}.
|
||||
|
||||
|
|
Reference in a new issue