Protocol halfway ported

This commit is contained in:
Jonatan Pålsson 2011-02-14 17:18:24 +01:00
parent 6aad821279
commit 973f2fcf98
4 changed files with 66 additions and 56 deletions

@ -1 +1 @@
Subproject commit cbac148c440a93db44bad767a43c6d8619f6871f Subproject commit 5350ed21606606dbee5ecb07e974f2abb9106270

View file

@ -11,7 +11,7 @@ s.connect((HOST, PORT))
print "Saying hello to server" print "Saying hello to server"
s.send( s.send(
"Command: hello\n\ "Server-Command: hello\n\
Content-Type: text\n\ Content-Type: text\n\
Content-Length: 0\n\ Content-Length: 0\n\
\n\ \n\
@ -27,7 +27,7 @@ print "Data: ", ' '.join(data.split(" ")[1:])
print "Defining a function called myFun" print "Defining a function called myFun"
s.send( s.send(
"Token: %s\n\ "Token: %s\n\
Command: define\n\ Server-Command: define\n\
Content-Type: text\n\ Content-Type: text\n\
Content-Length: 42\n\ Content-Length: 42\n\
\n\ \n\
@ -42,7 +42,7 @@ print "Data: ", ' '.join(data.split(" ")[1:])
print "Calling myFun" print "Calling myFun"
s.send( s.send(
"Token: %s\n\ "Token: %s\n\
Command: call\n\ Server-Command: call\n\
Content-Type: text\n\ Content-Type: text\n\
Content-Length: 6\n\ Content-Length: 6\n\
\n\ \n\
@ -63,7 +63,7 @@ s.connect((HOST, PORT))
print "Calling myFun" print "Calling myFun"
s.send( s.send(
"Token: %s\n\ "Token: %s\n\
Command: call\n\ Server-Command: call\n\
Content-Type: text\n\ Content-Type: text\n\
Content-Length: 6\n\ Content-Length: 6\n\
\n\ \n\

View file

@ -2,38 +2,48 @@
-export([parse/1]). -export([parse/1]).
parse(Data) -> parse(Data) ->
Message =string:tokens(Data, "\n"), Parsed = do_parse(Data, []),
% Turn "A: B" pairs into "{A, B}" tuples, for searching. prettify(Parsed).
MsgKV = lists:map((fun(Str) ->
list_to_tuple(string:tokens(Str, ": ")) end do_parse(Data, ParsedMessage) ->
), Message), NewLinePos = string:chr(Data, $\n),
% Hacky way to build a tuple, filter out not_found later on Line = string:substr(Data, 1, NewLinePos-1),
Processed = { Tokens = re:split(Line, ": ", [{return, list}]),
case lists:keysearch("Command", 1, MsgKV) of case handle(Tokens) of
{value,{_, "define"}} -> {Command, more} ->
define; do_parse(string:substr(Data, NewLinePos+1), ParsedMessage ++ [Command]);
{value,{_, "call"}} -> {separator, data_next} ->
call; {_, Value} = lists:keyfind(content_len, 1, ParsedMessage),
{value,{_, "hello"}} -> {ContentLength, []} = string:to_integer(Value),
hello; {ParsedMessage, handle_data(string:substr(Data, NewLinePos+1), ContentLength)}
false -> end.
not_found
end, handle([[]]) ->
case lists:keysearch("Token", 1, MsgKV) of {separator, data_next};
{value,{_, Value}} -> handle(["Server-Command", Param]) ->
Value; {{srv_cmd, Param}, more};
false -> handle(["Content-Length", Param]) ->
not_found {{content_len, Param}, more};
end, handle(["Token", Param]) ->
case lists:keysearch("Content-Length", 1, MsgKV) of {{token, Param}, more};
{value,{_, Value}} -> handle(["Content-Type", Param]) ->
{Length, _} = string:to_integer(Value), {{content_type, Param}, more}.
[_|Cont] = re:split(Data, "\n\n",[{return,list}]),
Content = string:join(Cont, "\n\n"), handle_data(Data, Length) ->
Payload = string:substr(Content,1,Length), {data, string:substr(Data,1,Length)}.
Payload;
false ->
not_found %% Helpers
prettify({Args, Data}) ->
case lists:keyfind(srv_cmd, 1, Args) of
{_, Value} ->
gen_server:cast(ggs_server, {srv_cmd, Value, Args, Data});
_Other ->
case lists:keyfind(game_cmd, 1, Args) of
{_, Value} ->
gen_server:cast(ggs_server, {game_cmd, Value, Args, Data});
_ ->
ok
end end
}, end.
gen_server:cast(ggs_server, Processed).

View file

@ -95,30 +95,30 @@ handle_cast(stop, State) ->
{stop, normal, State}; {stop, normal, State};
% Handle javascript defines % Handle javascript defines
handle_cast({define, Token, Payload}, State) -> handle_cast({srv_cmd, "define", Args, Data}, State) ->
JSVM = getJSVM(Token, State), %JSVM = getJSVM(Token, State),
js_runner:define(JSVM, Payload), %js_runner:define(JSVM, Payload),
send(State#state.lsock, Token, "Okay, defined that for you!"), send(State#state.lsock, "Token", "Okay, defined that for you!"),
{noreply, State}; {noreply, State};
% Handle javascript calls % Handle javascript calls
handle_cast({call, Token, Payload}, State) -> handle_cast({srv_cmd, "call", Args, Data}, State) ->
io:format("Got call request: ~p~n", [Payload]), %io:format("Got call request: ~p~n", [Payload]),
JSVM = getJSVM(Token, State), %JSVM = getJSVM(Token, State),
erlang:display(erlang:port_info(JSVM)), %erlang:display(erlang:port_info(JSVM)),
{ok, Ret} = js_runner:call(JSVM, Payload, []),%Payload, []), %{ok, Ret} = js_runner:call(JSVM, Payload, []),%Payload, []),
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)), %send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
{noreply, State}; {noreply, 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({hello, _, _}, State) -> handle_cast({srv_cmd, "hello", Headers, Data}, State) ->
JSVM = js_runner:boot(), %JSVM = js_runner:boot(),
Client = getRef(), Client = getRef(),
send(State#state.lsock, Client, "This is your refID"), send(State#state.lsock, Client, "This is your refID"),
OldMap = State#state.client_vm_map, %OldMap = State#state.client_vm_map,
NewState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]}, %NewState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]},
gen_server:cast(ggs_backup, {set_backup, NewState}), %gen_server:cast(ggs_backup, {set_backup, NewState}),
{noreply, NewState}. {noreply, State}. %NewState
%%----------------------------------------------------- %%-----------------------------------------------------
%% Helpers %% Helpers
%%----------------------------------------------------- %%-----------------------------------------------------