Switched over to new protocol module
This commit is contained in:
parent
a2854ef469
commit
df197dab40
2 changed files with 29 additions and 27 deletions
|
@ -1,10 +1,20 @@
|
||||||
-module(ggs_protocol).
|
-module(ggs_protocol).
|
||||||
-export([parse/1]).
|
-export([parse/1, getToken/1]).
|
||||||
|
|
||||||
|
%% API Functions
|
||||||
parse(Data) ->
|
parse(Data) ->
|
||||||
Parsed = do_parse(Data, []),
|
Parsed = do_parse(Data, []),
|
||||||
prettify(Parsed).
|
prettify(Parsed).
|
||||||
|
|
||||||
|
getToken(Parsed) ->
|
||||||
|
case lists:keyfind(token, 1, Parsed) of
|
||||||
|
{_, Value} ->
|
||||||
|
Value;
|
||||||
|
false ->
|
||||||
|
false
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% Internal helpers
|
||||||
do_parse(Data, ParsedMessage) ->
|
do_parse(Data, ParsedMessage) ->
|
||||||
NewLinePos = string:chr(Data, $\n),
|
NewLinePos = string:chr(Data, $\n),
|
||||||
Line = string:substr(Data, 1, NewLinePos-1),
|
Line = string:substr(Data, 1, NewLinePos-1),
|
||||||
|
@ -15,7 +25,8 @@ do_parse(Data, ParsedMessage) ->
|
||||||
{separator, data_next} ->
|
{separator, data_next} ->
|
||||||
{_, Value} = lists:keyfind(content_len, 1, ParsedMessage),
|
{_, Value} = lists:keyfind(content_len, 1, ParsedMessage),
|
||||||
{ContentLength, []} = string:to_integer(Value),
|
{ContentLength, []} = string:to_integer(Value),
|
||||||
{ParsedMessage, handle_data(string:substr(Data, NewLinePos+1), ContentLength)}
|
{data, ArgumentData} = handle_data(string:substr(Data, NewLinePos+1), ContentLength),
|
||||||
|
{ParsedMessage, ArgumentData}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle([[]]) ->
|
handle([[]]) ->
|
||||||
|
@ -33,7 +44,6 @@ handle_data(Data, Length) ->
|
||||||
{data, string:substr(Data,1,Length)}.
|
{data, string:substr(Data,1,Length)}.
|
||||||
|
|
||||||
|
|
||||||
%% Helpers
|
|
||||||
prettify({Args, Data}) ->
|
prettify({Args, Data}) ->
|
||||||
case lists:keyfind(srv_cmd, 1, Args) of
|
case lists:keyfind(srv_cmd, 1, Args) of
|
||||||
{_, Value} ->
|
{_, Value} ->
|
||||||
|
|
|
@ -1,10 +1,3 @@
|
||||||
%%%----------------------------------------------------
|
|
||||||
%%% @author Jonatan Pålsson <Jonatan.p@gmail.com>
|
|
||||||
%%% @copyright 2010 Jonatan Pålsson
|
|
||||||
%%% @doc RPC over TCP server
|
|
||||||
%%% @end
|
|
||||||
%%%----------------------------------------------------
|
|
||||||
|
|
||||||
-module(ggs_server).
|
-module(ggs_server).
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
|
@ -95,37 +88,36 @@ handle_cast(stop, State) ->
|
||||||
{stop, normal, State};
|
{stop, normal, State};
|
||||||
|
|
||||||
% Handle javascript defines
|
% Handle javascript defines
|
||||||
handle_cast({srv_cmd, "define", Args, Data}, State) ->
|
handle_cast({srv_cmd, "define", Headers, Data}, State) ->
|
||||||
%JSVM = getJSVM(Token, State),
|
Token = ggs_protocol:getToken(Headers),
|
||||||
%js_runner:define(JSVM, Payload),
|
JSVM = getJSVM(Token, State),
|
||||||
|
js_runner:define(JSVM, Data),
|
||||||
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({srv_cmd, "call", Args, Data}, State) ->
|
handle_cast({srv_cmd, "call", Headers, Data}, State) ->
|
||||||
%io:format("Got call request: ~p~n", [Payload]),
|
Token = ggs_protocol:getToken(Headers),
|
||||||
%JSVM = getJSVM(Token, State),
|
io:format("Got call request: ~p~n", [Data]),
|
||||||
%erlang:display(erlang:port_info(JSVM)),
|
JSVM = getJSVM(Token, State),
|
||||||
%{ok, Ret} = js_runner:call(JSVM, Payload, []),%Payload, []),
|
erlang:display(erlang:port_info(JSVM)),
|
||||||
%send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
|
{ok, Ret} = js_runner:call(JSVM, Data, []),%Payload, []),
|
||||||
|
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({srv_cmd, "hello", Headers, Data}, 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, State}. %NewState
|
{noreply, NewState}.
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% Helpers
|
%% Helpers
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
getRef() ->
|
getRef() ->
|
||||||
%{A1,A2,A3} = now(),
|
|
||||||
%#random:seed(A1, A2, A3),
|
|
||||||
%random:uniform(1000).
|
|
||||||
string:strip(os:cmd("uuidgen"), right, $\n ).
|
string:strip(os:cmd("uuidgen"), right, $\n ).
|
||||||
|
|
||||||
getJSVM(RefID, State) ->
|
getJSVM(RefID, State) ->
|
||||||
|
|
Reference in a new issue