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