changed player to gen_server
This commit is contained in:
parent
da67b0a977
commit
135caff0e9
7 changed files with 1673 additions and 1445 deletions
1
Makefile
1
Makefile
|
@ -26,6 +26,7 @@ endif
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BEAMDIR)/*.beam ;
|
rm -rf $(BEAMDIR)/*.beam ;
|
||||||
|
rm -rf $(SRCDIR)/*.beam ;
|
||||||
rm -rf erl_crush.dump ;
|
rm -rf erl_crush.dump ;
|
||||||
echo "==> clean ggs" ;
|
echo "==> clean ggs" ;
|
||||||
$(MAKE) -C erlang_js/ clean
|
$(MAKE) -C erlang_js/ clean
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
@synthesize asyncSocket, delegate, gameToken, currentHeaders;
|
@synthesize asyncSocket, delegate, gameToken, currentHeaders;
|
||||||
|
|
||||||
- (id)initWithDelegate:(id<GGSDelegate>)_delegate {
|
- (id)initWithDelegate:(id<GGSDelegate>)_delegate {
|
||||||
if (self = [super init]) {
|
if ((self = [super init])) {
|
||||||
delegate = _delegate;
|
delegate = _delegate;
|
||||||
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
|
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -25,11 +25,10 @@
|
||||||
%% identifying the player.
|
%% identifying the player.
|
||||||
%% @spec start_link(Socket::socket()) -> {ok, Pid} | {error, Reason}
|
%% @spec start_link(Socket::socket()) -> {ok, Pid} | {error, Reason}
|
||||||
start(Socket) ->
|
start(Socket) ->
|
||||||
erlang:display("start_link"),
|
|
||||||
gen_server:start(?MODULE, [Socket], []).
|
gen_server:start(?MODULE, [Socket], []).
|
||||||
|
|
||||||
init([Socket]) ->
|
init([Socket]) ->
|
||||||
{ok, Protocol} = ggs_protocol:start_link(Socket, self()),
|
{ok, Protocol} = ggs_protocol:start_link(),
|
||||||
{ok, Token} = ggs_coordinator:join_lobby(),
|
{ok, Token} = ggs_coordinator:join_lobby(),
|
||||||
|
|
||||||
case ggs_coordinator:join_table("1337") of
|
case ggs_coordinator:join_table("1337") of
|
||||||
|
@ -75,20 +74,24 @@ stop(Player) ->
|
||||||
gen_server:cast(Player, stop).
|
gen_server:cast(Player, stop).
|
||||||
|
|
||||||
%% Internals
|
%% Internals
|
||||||
|
handle_call({notify, Message}, _From, #state { protocol = Protocol, socket = Socket } = State) ->
|
||||||
handle_call({notify, Message}, _From, #state { protocol = Protocol } = State) ->
|
gen_tcp:send(Socket, ggs_protocol:create_message(Protocol, Message)),
|
||||||
ggs_protocol:send_command(Protocol, Message),
|
{noreply, State};
|
||||||
{noreplay, State};
|
|
||||||
|
|
||||||
handle_call({game_cmd, Command, _Headers, Data}, _From, #state { table = Table } = State) ->
|
handle_call({game_cmd, Command, _Headers, Data}, _From, #state { table = Table } = State) ->
|
||||||
ggs_table:notify(Table, self(), {game, Command, Data}),
|
ggs_table:notify(Table, self(), {game, Command, Data}),
|
||||||
{noreplay, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_call({srv_cmd, "define", _Headers, Data}, _From, #state { table = Table } = State) ->
|
handle_call({srv_cmd, "define", _Headers, Data}, _From, #state { table = Table } = State) ->
|
||||||
ggs_table:notify(Table, self(), {server, define, Data}),
|
ggs_table:notify(Table, self(), {server, define, Data}),
|
||||||
{noreplay, State};
|
{noreply, State};
|
||||||
|
|
||||||
|
|
||||||
handle_call(_Request, _From, St) -> {stop, unimplemented, St}.
|
handle_call(_Request, _From, St) -> {stop, unimplemented, St}.
|
||||||
|
|
||||||
|
handle_cast({tcp, _Socket, Data}, #state { protocol = Protocol } = _State) ->
|
||||||
|
ggs_protocol:parse(Protocol, Data);
|
||||||
|
|
||||||
handle_cast(_Request, St) -> {stop, unimplemented, St}.
|
handle_cast(_Request, St) -> {stop, unimplemented, St}.
|
||||||
|
|
||||||
handle_info(_Info, St) -> {stop, unimplemented, St}.
|
handle_info(_Info, St) -> {stop, unimplemented, St}.
|
||||||
|
|
|
@ -1,121 +1,62 @@
|
||||||
%%% @doc This module handles TCP incomming and outcommint.
|
%% Parse a string formatted with the GGS protocol using
|
||||||
|
%% an FSM. Each char is put into the FSM, which incrementally
|
||||||
|
%% builds a list of strings which represent the complete
|
||||||
|
%% message.
|
||||||
|
|
||||||
-module(ggs_protocol).
|
-module(ggs_protocol).
|
||||||
-export([start_link/2,stop/1]).
|
-export([parse/2, getToken/1, create_message/4,
|
||||||
-behaviour(gen_server).
|
expect_headers/2, expect_data_section/2,
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
expect_headers/3, expect_data_section/3]).
|
||||||
terminate/2, code_change/3]).
|
|
||||||
%% Old
|
|
||||||
-export([parse/1, getToken/1, create_message/4, send_command/2]).
|
|
||||||
|
|
||||||
-vsn(1.0).
|
%% tests
|
||||||
|
-export([to_dictionary/2]).
|
||||||
|
|
||||||
-record(state, {
|
% gen_fsm callbacks..
|
||||||
player,
|
-export([init/1, handle_info/2, terminate/2, code_change/3, start_link/0]).
|
||||||
socket,
|
|
||||||
header_string,
|
|
||||||
header_list,
|
|
||||||
body,
|
|
||||||
content_length}).
|
|
||||||
|
|
||||||
start_link(Socket, Player) ->
|
|
||||||
gen_server:start_link(?MODULE, {Socket, Player}, []).
|
|
||||||
|
|
||||||
stop(Protocol) ->
|
|
||||||
gen_server:cast(Protocol, stop).
|
|
||||||
|
|
||||||
send_command(Protocol, {Command, Data}) ->
|
|
||||||
gen_server:cast(Protocol, {send, Command, Data}).
|
|
||||||
|
|
||||||
init({Socket, Player}) ->
|
|
||||||
erlang:port_connect(Socket, self()),
|
|
||||||
State = #state{
|
|
||||||
socket = Socket,
|
|
||||||
player = Player,
|
|
||||||
header_list = [],
|
|
||||||
header_string = "",
|
|
||||||
body = "",
|
|
||||||
content_length = -1
|
|
||||||
},
|
|
||||||
{ok, State}.
|
|
||||||
|
|
||||||
handle_cast({tcp, _Socket, Data}, State) ->
|
|
||||||
case State#state.content_length of
|
|
||||||
-1 -> % its a header
|
|
||||||
TmpHeader = State#state.header_string ++ Data,
|
|
||||||
case string:str(TmpHeader, "\n\n") of
|
|
||||||
0 -> % still in header
|
|
||||||
{reply, ok, State # state {header_string = TmpHeader}};
|
|
||||||
_ -> % we left the header
|
|
||||||
{Header, Body} = parse(TmpHeader),
|
|
||||||
{_, ContentLengthString} = lists:keyfind(content_len, 1, Header), % find Content-Length
|
|
||||||
{ContentLength, []} = string:to_integer(ContentLengthString),
|
|
||||||
{reply, ok, State#state{
|
|
||||||
header_list = Header,
|
|
||||||
header_string = "",
|
|
||||||
body = Body,
|
|
||||||
content_length = ContentLength}}
|
|
||||||
end;
|
|
||||||
Length -> % its a body
|
|
||||||
LBody = string:len(State#state.body),
|
|
||||||
LData = string:len(Data),
|
|
||||||
NewLength = LBody + LData,
|
|
||||||
if
|
|
||||||
NewLength < Length -> % not enough data
|
|
||||||
Body = State#state.body ++ Data,
|
|
||||||
{reply, ok, State#state {body = Body}};
|
|
||||||
NewLength > Length -> % too much data
|
|
||||||
EndOfMessagePos = LBody + LData - Length,
|
|
||||||
Body = State#state.body ++ string:substr(Data, 0, EndOfMessagePos),
|
|
||||||
NextHeader = string:substr(Data, EndOfMessagePos, LData),
|
|
||||||
Message = prettify(State#state.header_list, Body),
|
|
||||||
gen_player:notify_game(State#state.player, Message),
|
|
||||||
{reply, ok, State#state {
|
|
||||||
header_string = NextHeader,
|
|
||||||
header_list = [],
|
|
||||||
body = "",
|
|
||||||
content_length = -1}};
|
|
||||||
NewLength == Length -> % end of message
|
|
||||||
Message = prettify(State#state.header_list, State#state.body ++ Data),
|
|
||||||
gen_player:notify_game(State#state.player, Message),
|
|
||||||
{reply, ok, State#state {
|
|
||||||
header_string = "",
|
|
||||||
header_list = [],
|
|
||||||
body = "",
|
|
||||||
content_length = -1}}
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
|
|
||||||
handle_cast({send, Command, Data}, State) ->
|
|
||||||
Message = create_message(Command, "text", "text", Data),
|
|
||||||
gen_tcp:send(State#state.socket, Message),
|
|
||||||
{noreply, State};
|
|
||||||
|
|
||||||
handle_cast(_Request, St) -> {stop, unimplemented, St}.
|
|
||||||
handle_call(_Request, _From, St) -> {stop, unimplemented, St}.
|
|
||||||
|
|
||||||
handle_info(_Info, St) -> {stop, unimplemented, St}.
|
|
||||||
|
|
||||||
|
|
||||||
terminate(_Reason, _St) -> ok.
|
-define(SERVER, ?MODULE).
|
||||||
code_change(_OldVsn, St, _Extra) -> {ok, St}.
|
|
||||||
|
|
||||||
|
%% JONTES TESTS
|
||||||
|
%test() ->
|
||||||
|
% start_link(),
|
||||||
|
% parse("Token: %s\nServer-Command: define\nContent-Type: text\nContent-Length: 9\n\nHELLOWORLDToken: %s\nServer-Command: define\nContent-Type: text\nContent-Length: 9\n\nHELLOWORLDToken: %s\nServer-Command: define\n"),
|
||||||
|
% to_dictionary(["Hello: world", "Hi: there!"], []).
|
||||||
|
%% END TESTS
|
||||||
|
|
||||||
|
|
||||||
%% API Functions
|
%% API Functions
|
||||||
parse(Data) ->
|
parse(Protocol, Data) ->
|
||||||
do_parse(Data, []).
|
lists:foreach(fun(X) ->
|
||||||
|
gen_fsm:sync_send_event(Protocol, {char, X})
|
||||||
|
end, Data).
|
||||||
|
|
||||||
|
|
||||||
|
start_link() ->
|
||||||
|
gen_fsm:start_link({local, ?SERVER}, ?MODULE, [], []).
|
||||||
|
|
||||||
|
% Start state: {[""],0}, meaning:
|
||||||
|
% - Start with no strings parsed
|
||||||
|
% - Start with a data-section-lengths of 0
|
||||||
|
init([]) ->
|
||||||
|
{ok,expect_headers,{[""], 0}}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
getToken(Parsed) ->
|
getToken(Parsed) ->
|
||||||
|
|
||||||
case lists:keyfind(token, 1, Parsed) of
|
case lists:keyfind(token, 1, Parsed) of
|
||||||
{_, Value} ->
|
{_, Value} ->
|
||||||
Value;
|
Value;
|
||||||
false ->
|
false ->
|
||||||
false
|
false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
create_message({Command, Data}) ->
|
||||||
|
create_message(Command, "text", "text", Data).
|
||||||
|
|
||||||
|
%% Assemble a message which can b
|
||||||
|
%e used as a reply to a client
|
||||||
create_message(Cmd, Enc, Acc, Data) ->
|
create_message(Cmd, Enc, Acc, Data) ->
|
||||||
Length = integer_to_list(string:len(Data)),
|
Length = integer_to_list(string:len(Data)),
|
||||||
Msg = "Client-Command: " ++ Cmd ++ "\n" ++
|
Msg = "Client-Command: " ++ Cmd ++ "\n" ++
|
||||||
|
@ -127,41 +68,64 @@ create_message(Cmd, Enc, Acc, Data) ->
|
||||||
Data,
|
Data,
|
||||||
Msg.
|
Msg.
|
||||||
|
|
||||||
%% Internal helpers
|
%%% Transitions
|
||||||
do_parse(Data, Headers) ->
|
expect_headers(_Event, State) ->
|
||||||
NewLinePos = string:chr(Data, $\n),
|
{next_state, expect_headers, State}.
|
||||||
Line = string:substr(Data, 1, NewLinePos-1),
|
expect_data_section(_Event, State) ->
|
||||||
Tokens = re:split(Line, ": ", [{return, list}]),
|
{next_state, expect_data_section, State}.
|
||||||
case handle(Tokens) of
|
|
||||||
{Command, more} ->
|
|
||||||
do_parse(string:substr(Data, NewLinePos+1), Headers ++ [Command]);
|
%%% End transitions
|
||||||
{separator, data_next} ->
|
expect_headers({char, $\n}, _From, {Strings, Remains}) ->
|
||||||
{Headers, Data}
|
[LastMessage|_] = Strings,
|
||||||
|
case LastMessage of
|
||||||
|
"" -> % We have a data section.. Last line should thus be the content length.
|
||||||
|
[LastMessage, SecondLast | _] = Strings,
|
||||||
|
case re:split(SecondLast, ": ", [{return, list}]) of
|
||||||
|
["Content-Length", X] ->
|
||||||
|
{Int,_} = string:to_integer(X),
|
||||||
|
{reply, ok, expect_data_section, {[""|Strings], Int}};
|
||||||
|
Other -> ok
|
||||||
|
end;
|
||||||
|
Other ->
|
||||||
|
{reply,ok,expect_headers, {[""|Strings], Remains}}
|
||||||
|
end;
|
||||||
|
|
||||||
|
expect_headers({char, Char}, _From, {[Current|Rest], Remains}) ->
|
||||||
|
NewCurrent = Current ++ [Char],
|
||||||
|
{reply,ok, expect_headers, {[NewCurrent|Rest], Remains}}.
|
||||||
|
|
||||||
|
|
||||||
|
expect_data_section({char, Char}, From, {Strings, Remains}) ->
|
||||||
|
case Remains of
|
||||||
|
0 ->
|
||||||
|
[LastMsg,_|Rest] = Strings,
|
||||||
|
NewMsg = LastMsg ++ [Char],
|
||||||
|
Result = [NewMsg|Rest],
|
||||||
|
{Pid,_} = From,
|
||||||
|
erlang:display(From),
|
||||||
|
Pid ! (prettify(to_dictionary(Rest, []), NewMsg)),
|
||||||
|
{reply, ok, expect_headers, {[""], 0}};
|
||||||
|
Other -> [LastMsg|Rest] = Strings,
|
||||||
|
NewMsg = LastMsg ++ [Char],
|
||||||
|
{reply, ok, expect_data_section, {[NewMsg|Rest], Remains-1}}
|
||||||
end.
|
end.
|
||||||
|
handle_call(_Msg, _From, State) ->
|
||||||
handle([[]]) ->
|
{noreply, State}.
|
||||||
{separator, data_next};
|
handle_info(_Msg, State) ->
|
||||||
handle(["Server-Command", Param]) ->
|
{noreply, State}.
|
||||||
{{srv_cmd, Param}, more};
|
terminate(_Reason, _State) ->
|
||||||
handle(["Game-Command", Param]) ->
|
ok.
|
||||||
{{game_cmd, Param}, more};
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
handle(["Content-Length", Param]) ->
|
{ok, State}.
|
||||||
{{content_len, Param}, more};
|
|
||||||
handle(["Token", Param]) ->
|
|
||||||
{{token, Param}, more};
|
|
||||||
handle(["Content-Type", Param]) ->
|
|
||||||
{{content_type, Param}, more}.
|
|
||||||
|
|
||||||
%handle_data(Data, Length) ->
|
|
||||||
% {data, string:substr(Data,1,Length)}.
|
|
||||||
|
|
||||||
|
|
||||||
prettify(Args, Data) ->
|
prettify(Args, Data) ->
|
||||||
case lists:keyfind(srv_cmd, 1, Args) of
|
case lists:keyfind("Server-Command", 1, Args) of
|
||||||
{_, Value} ->
|
{_, Value} ->
|
||||||
{srv_cmd, Value, Args, Data};
|
{srv_cmd, Value, Args, Data};
|
||||||
_Other ->
|
_Other ->
|
||||||
case lists:keyfind(game_cmd, 1, Args) of
|
case lists:keyfind("Game-Command", 1, Args) of
|
||||||
{_, Value} ->
|
{_, Value} ->
|
||||||
{game_cmd, Value, Args, Data};
|
{game_cmd, Value, Args, Data};
|
||||||
_ ->
|
_ ->
|
||||||
|
@ -169,3 +133,9 @@ prettify(Args, Data) ->
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
to_dictionary([], Dict) ->
|
||||||
|
Dict;
|
||||||
|
to_dictionary([S|Strings], Dict) ->
|
||||||
|
[First, Snd] = re:split(S, ": ", [{return, list}]),
|
||||||
|
to_dictionary(Strings, [{First, Snd}|Dict]).
|
||||||
|
|
||||||
|
|
146
src/ggs_protocol.erl.orig
Normal file
146
src/ggs_protocol.erl.orig
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
%% Parse a string formatted with the GGS protocol using
|
||||||
|
%% an FSM. Each char is put into the FSM, which incrementally
|
||||||
|
%% builds a list of strings which represent the complete
|
||||||
|
%% message.
|
||||||
|
|
||||||
|
-module(ggs_protocol).
|
||||||
|
-export([parse/1, getToken/1, create_message/4,
|
||||||
|
expect_headers/2, expect_data_section/2,
|
||||||
|
expect_headers/3, expect_data_section/3]).
|
||||||
|
|
||||||
|
%% tests
|
||||||
|
-export([test/0, to_dictionary/2]).
|
||||||
|
|
||||||
|
% gen_fsm callbacks..
|
||||||
|
-export([init/1, handle_info/2, terminate/2, code_change/3, start_link/0]).
|
||||||
|
|
||||||
|
|
||||||
|
-define(SERVER, ?MODULE).
|
||||||
|
|
||||||
|
%% JONTES TESTS
|
||||||
|
test() ->
|
||||||
|
start_link(),
|
||||||
|
parse("Token: %s\nServer-Command: define\nContent-Type: text\nContent-Length: 9\n\nHELLOWORLDToken: %s\nServer-Command: define\nContent-Type: text\nContent-Length: 9\n\nHELLOWORLDToken: %s\nServer-Command: define\n"),
|
||||||
|
to_dictionary(["Hello: world", "Hi: there!"], []).
|
||||||
|
%% END TESTS
|
||||||
|
|
||||||
|
|
||||||
|
%% API Functions
|
||||||
|
parse(Data) ->
|
||||||
|
lists:foreach(fun(X) ->
|
||||||
|
gen_fsm:sync_send_event(?SERVER, {char, X})
|
||||||
|
end, Data).
|
||||||
|
|
||||||
|
|
||||||
|
start_link() ->
|
||||||
|
gen_fsm:start_link({local, ?SERVER}, ?MODULE, [], []).
|
||||||
|
|
||||||
|
% Start state: {[""],0}, meaning:
|
||||||
|
% - Start with no strings parsed
|
||||||
|
% - Start with a data-section-lengths of 0
|
||||||
|
init([]) ->
|
||||||
|
{ok,expect_headers,{[""], 0}}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
getToken(Parsed) ->
|
||||||
|
|
||||||
|
case lists:keyfind(token, 1, Parsed) of
|
||||||
|
{_, Value} ->
|
||||||
|
Value;
|
||||||
|
false ->
|
||||||
|
false
|
||||||
|
end.
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
=======
|
||||||
|
|
||||||
|
|
||||||
|
create_message(Protocol, {Command, Data}) {
|
||||||
|
create_message(Command, "text", "text", Data),
|
||||||
|
}
|
||||||
|
>>>>>>> Stashed changes
|
||||||
|
|
||||||
|
%% Assemble a message which can b
|
||||||
|
%e used as a reply to a client
|
||||||
|
create_message(Cmd, Enc, Acc, Data) ->
|
||||||
|
Length = integer_to_list(string:len(Data)),
|
||||||
|
Msg = "Client-Command: " ++ Cmd ++ "\n" ++
|
||||||
|
"Client-Encoding: " ++ Enc ++ "\n" ++
|
||||||
|
"Content-Size: " ++ Length ++ "\n" ++
|
||||||
|
"GGS-Version: 1.0\n" ++
|
||||||
|
"Accept: " ++ Acc ++ "\n" ++
|
||||||
|
"\n" ++
|
||||||
|
Data,
|
||||||
|
Msg.
|
||||||
|
|
||||||
|
%%% Transitions
|
||||||
|
expect_headers(_Event, State) ->
|
||||||
|
{next_state, expect_headers, State}.
|
||||||
|
expect_data_section(_Event, State) ->
|
||||||
|
{next_state, expect_data_section, State}.
|
||||||
|
|
||||||
|
|
||||||
|
%%% End transitions
|
||||||
|
expect_headers({char, $\n}, _From, {Strings, Remains}) ->
|
||||||
|
[LastMessage|_] = Strings,
|
||||||
|
case LastMessage of
|
||||||
|
"" -> % We have a data section.. Last line should thus be the content length.
|
||||||
|
[LastMessage, SecondLast | _] = Strings,
|
||||||
|
case re:split(SecondLast, ": ", [{return, list}]) of
|
||||||
|
["Content-Length", X] ->
|
||||||
|
{Int,_} = string:to_integer(X),
|
||||||
|
{reply, ok, expect_data_section, {[""|Strings], Int}};
|
||||||
|
Other -> ok
|
||||||
|
end;
|
||||||
|
Other ->
|
||||||
|
{reply,ok,expect_headers, {[""|Strings], Remains}}
|
||||||
|
end;
|
||||||
|
|
||||||
|
expect_headers({char, Char}, _From, {[Current|Rest], Remains}) ->
|
||||||
|
NewCurrent = Current ++ [Char],
|
||||||
|
{reply,ok, expect_headers, {[NewCurrent|Rest], Remains}}.
|
||||||
|
|
||||||
|
|
||||||
|
expect_data_section({char, Char}, From, {Strings, Remains}) ->
|
||||||
|
case Remains of
|
||||||
|
0 ->
|
||||||
|
[LastMsg,_|Rest] = Strings,
|
||||||
|
NewMsg = LastMsg ++ [Char],
|
||||||
|
Result = [NewMsg|Rest],
|
||||||
|
{Pid,_} = From,
|
||||||
|
erlang:display(From),
|
||||||
|
Pid ! (prettify(to_dictionary(Rest, []), NewMsg)),
|
||||||
|
{reply, ok, expect_headers, {[""], 0}};
|
||||||
|
Other -> [LastMsg|Rest] = Strings,
|
||||||
|
NewMsg = LastMsg ++ [Char],
|
||||||
|
{reply, ok, expect_data_section, {[NewMsg|Rest], Remains-1}}
|
||||||
|
end.
|
||||||
|
handle_call(_Msg, _From, State) ->
|
||||||
|
{noreply, State}.
|
||||||
|
handle_info(_Msg, State) ->
|
||||||
|
{noreply, State}.
|
||||||
|
terminate(_Reason, _State) ->
|
||||||
|
ok.
|
||||||
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
|
{ok, State}.
|
||||||
|
|
||||||
|
|
||||||
|
prettify(Args, Data) ->
|
||||||
|
case lists:keyfind("Server-Command", 1, Args) of
|
||||||
|
{_, Value} ->
|
||||||
|
{srv_cmd, Value, Args, Data};
|
||||||
|
_Other ->
|
||||||
|
case lists:keyfind("Game-Command", 1, Args) of
|
||||||
|
{_, Value} ->
|
||||||
|
{game_cmd, Value, Args, Data};
|
||||||
|
_ ->
|
||||||
|
ok
|
||||||
|
end
|
||||||
|
end.
|
||||||
|
|
||||||
|
to_dictionary([], Dict) ->
|
||||||
|
Dict;
|
||||||
|
to_dictionary([S|Strings], Dict) ->
|
||||||
|
[First, Snd] = re:split(S, ": ", [{return, list}]),
|
||||||
|
to_dictionary(Strings, [{First, Snd}|Dict]).
|
||||||
|
|
|
@ -55,7 +55,7 @@ init([Port]) ->
|
||||||
{ok, State#state{lsock = LSock}, 0}
|
{ok, State#state{lsock = LSock}, 0}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_call({backup_state, OldState}, _From, State) ->
|
handle_call({backup_state, OldState}, _From, _State) ->
|
||||||
io:format("Received old state from backup~n"),
|
io:format("Received old state from backup~n"),
|
||||||
{noreply, OldState}.
|
{noreply, OldState}.
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ handle_info(timeout, #state{lsock = LSock} = State) ->
|
||||||
{ok, _Sock} = gen_tcp:accept(LSock),
|
{ok, _Sock} = gen_tcp:accept(LSock),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info(Other, State) ->
|
handle_info(Other, _State) ->
|
||||||
erlang:display(Other).
|
erlang:display(Other).
|
||||||
|
|
||||||
terminate(_Reason, _State) ->
|
terminate(_Reason, _State) ->
|
||||||
|
@ -106,13 +106,14 @@ handle_cast({srv_cmd, "call", Headers, Data}, State) ->
|
||||||
|
|
||||||
% Set the new state to the reference generated, and JSVM associated
|
% Set the new state to the reference generated, and JSVM associated
|
||||||
%handle_cast({server, hello, Headers}, State) ->
|
%handle_cast({server, hello, Headers}, State) ->
|
||||||
handle_cast({srv_cmd, "hello", Headers, Data}, State) ->
|
handle_cast({srv_cmd, "hello", Headers, _Data}, State) ->
|
||||||
GameToken = case proplist:get_value(game_token, Headers) of ->
|
GameToken = case proplist:get_value(game_token, Headers) of
|
||||||
undefined -> getNewToken();
|
undefined -> getNewToken();
|
||||||
GT -> GT;
|
GT -> GT
|
||||||
end,
|
end,
|
||||||
ClientToken = getNewToken(),
|
ClientToken = getNewToken(),
|
||||||
OldMap = State#state.client_vm_map,
|
OldMap = State#state.client_vm_map,
|
||||||
|
GameVM = getJSVM(ClientToken, State),
|
||||||
NewState = State#state{client_vm_map = OldMap ++ [{ClientToken, GameVM, GameToken}]},
|
NewState = State#state{client_vm_map = OldMap ++ [{ClientToken, GameVM, GameToken}]},
|
||||||
gen_server:cast(ggs_backup, {set_backup, NewState}),
|
gen_server:cast(ggs_backup, {set_backup, NewState}),
|
||||||
{noreply, NewState}.
|
{noreply, NewState}.
|
||||||
|
|
Reference in a new issue