Added rudimentary parser, and connected to JS
Added rudimentary parser, and connected to JS squash! Added rudimentary parser, and connected to JS
This commit is contained in:
parent
1f3f7daa9a
commit
f8cf749e5f
10 changed files with 15501 additions and 23 deletions
|
@ -4,20 +4,23 @@
|
|||
-module(ggs_protocol).
|
||||
-export([parse/1]).
|
||||
|
||||
parse(JSONData) ->
|
||||
{struct, Struct} = js_mochijson2:decode(JSONData),
|
||||
io:format("~p~n", [Struct]),
|
||||
[{RequestType, Rest}] = Struct,%proplists:get_value(<<"request">>, Struct),
|
||||
case RequestType of
|
||||
<<"define">> ->
|
||||
%Name = proplists:get_value(<<"name">>, Rest),
|
||||
%{struct, Name} = proplists:get_value(<<"name">>, Rest),
|
||||
{struct, Rest2} = Rest,
|
||||
io:format("~p", [Rest2]),
|
||||
ok_you_said_define;
|
||||
<<"call">> ->
|
||||
ok_you_said_call;
|
||||
parse(Data) ->
|
||||
Message = string:tokens(Data, " "),
|
||||
case Message of
|
||||
[RefID, "__error", Message ] ->
|
||||
{ok, you_said_error};
|
||||
[_, "__boot" ] ->
|
||||
{ok, you_said_boot};
|
||||
[RefID, "__stop"] ->
|
||||
{ok, you_said_stop};
|
||||
[RefID, "__start"] ->
|
||||
{ok, you_said_start};
|
||||
[_, "__hello" , _ ] ->
|
||||
{ok, you_said_hello};
|
||||
[RefID, "__define", JavaScript ] ->
|
||||
{ok, you_said_define};
|
||||
[RefID, Command, Parameter ] ->
|
||||
{cmd, Command, Parameter};
|
||||
Other ->
|
||||
io:format("~p", [RequestType]),
|
||||
ok_i_dont_understand
|
||||
{out_of_bounds, Other}
|
||||
end.
|
||||
|
|
|
@ -72,10 +72,10 @@ handle_cast(stop, State) ->
|
|||
{stop, normal, State}.
|
||||
|
||||
handle_info({tcp, Socket, RawData}, State) ->
|
||||
%do_JSCall(Socket, RawData),
|
||||
Parsed = ggs_protocol:parse(RawData),
|
||||
do_JSDefine(Socket, RawData),
|
||||
%Parsed = ggs_protocol:parse(RawData),
|
||||
RequestCount = State#state.request_count,
|
||||
gen_tcp:send(Socket, io_lib:fwrite("~p~n", [Parsed])),
|
||||
%gen_tcp:send(Socket, io_lib:fwrite("~p~n", [Parsed])),
|
||||
{noreply, State#state{request_count = RequestCount + 1}};
|
||||
|
||||
handle_info(timeout, #state{lsock = LSock} = State) ->
|
||||
|
@ -94,8 +94,17 @@ code_change(_OldVsn, State, _Extra) ->
|
|||
|
||||
do_JSDefine(Socket, Data) ->
|
||||
JSVM = js_runner:boot(),
|
||||
Ret = js_runner:define(JSVM, Data),
|
||||
gen_tcp:send(Socket, io_lib:fwrite("~p~n", [Ret])).
|
||||
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
||||
Parsed = ggs_protocol:parse(Data),
|
||||
case Parsed of
|
||||
{cmd, Command, Parameter} ->
|
||||
Ret = js_runner:call(JSVM, "userCommand",
|
||||
[list_to_binary(Command),
|
||||
list_to_binary(Parameter)]),
|
||||
gen_tcp:send(Socket, io_lib:fwrite("JS says: ~p~n", [Ret]));
|
||||
Other ->
|
||||
ok
|
||||
end.
|
||||
|
||||
do_JSCall(Socket, Function, Parameters) ->
|
||||
ok.
|
||||
|
|
|
@ -10,4 +10,4 @@ define(Port, Data) ->
|
|||
ok = js:define(Port, list_to_binary(Data)).
|
||||
|
||||
call(Port, Func, Params) ->
|
||||
js:call(Port, list_to_binary(Func), [Params]).
|
||||
js:call(Port, list_to_binary(Func), Params).
|
||||
|
|
Reference in a new issue