Merge branch 'jonte_hackage'
This commit is contained in:
commit
81ada85440
2 changed files with 29 additions and 92 deletions
|
@ -1,6 +1,3 @@
|
||||||
%% This is a parser for JSON implemented using mochijson2
|
|
||||||
%% Don't feed it anything other than valid JSON.
|
|
||||||
|
|
||||||
-module(ggs_protocol).
|
-module(ggs_protocol).
|
||||||
-export([parse/1]).
|
-export([parse/1]).
|
||||||
|
|
||||||
|
@ -39,31 +36,4 @@ parse(Data) ->
|
||||||
not_found
|
not_found
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
Processed.
|
gen_server:cast(ggs_server, Processed).
|
||||||
% case Message of
|
|
||||||
% ["__get_vms"] ->
|
|
||||||
% {vms};
|
|
||||||
% [RefID, "__error", Size, Message ] ->
|
|
||||||
% {ok, you_said_error};
|
|
||||||
% [_, "__boot", _ ] ->
|
|
||||||
% {ok, you_said_boot};
|
|
||||||
% [RefID, "__stop", _] ->
|
|
||||||
% {ok, you_said_stop};
|
|
||||||
% [RefID, "__start", _] ->
|
|
||||||
% {ok, you_said_start};
|
|
||||||
% ["__hello", _] ->
|
|
||||||
% {hello};
|
|
||||||
% [RefID, "__define",_, JavaScript ] ->
|
|
||||||
% {ok, you_said_define};
|
|
||||||
% [RefID, "__echo", Length, Msg ] ->
|
|
||||||
% {Ref, _} = string:to_integer(RefID),
|
|
||||||
% {echo, Ref, Length, Msg};
|
|
||||||
% [RefID, Command, _, Parameter ] ->
|
|
||||||
% {cmd, Command, Parameter};
|
|
||||||
% %% Debugging tools, not for production use
|
|
||||||
% ["__crash"] ->
|
|
||||||
% {crash, 0};
|
|
||||||
% %% End debugging tools
|
|
||||||
% Other ->
|
|
||||||
% {out_of_bounds, Other}
|
|
||||||
% end.
|
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/1,
|
-export([start_link/1,
|
||||||
start_link/0,
|
start_link/0,
|
||||||
get_count/0,
|
|
||||||
stop/0
|
stop/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -39,16 +38,6 @@ start_link(Port) ->
|
||||||
start_link() ->
|
start_link() ->
|
||||||
start_link(?DEFAULT_PORT).
|
start_link(?DEFAULT_PORT).
|
||||||
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
%% @doc Fetches the number of requests made to this server
|
|
||||||
%% @spec get_count() -> {ok, Count}
|
|
||||||
%% where
|
|
||||||
%% Count = integer()
|
|
||||||
%% @end
|
|
||||||
%%-----------------------------------------------------
|
|
||||||
get_count() ->
|
|
||||||
gen_server:call(?SERVER, get_count).
|
|
||||||
|
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% @doc Stops the server.
|
%% @doc Stops the server.
|
||||||
%% @spec stop() -> ok
|
%% @spec stop() -> ok
|
||||||
|
@ -73,21 +62,14 @@ init([Port]) ->
|
||||||
{ok, State#state{lsock = LSock}, 0}
|
{ok, State#state{lsock = LSock}, 0}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_call(get_count, _From, State) ->
|
|
||||||
{reply, {ok, State#state.client_vm_map}, State};
|
|
||||||
|
|
||||||
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}.
|
||||||
|
|
||||||
handle_cast(stop, State) ->
|
|
||||||
{stop, normal, State}.
|
|
||||||
|
|
||||||
handle_info({tcp, Socket, RawData}, State) ->
|
handle_info({tcp, Socket, RawData}, State) ->
|
||||||
NewState = do_JSCall(Socket, RawData, State),
|
ggs_protocol:parse(RawData),
|
||||||
OldMap = State#state.client_vm_map,
|
{noreply, State#state{lsock = Socket}};
|
||||||
io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]),
|
|
||||||
{noreply, State#state{client_vm_map = OldMap ++ [NewState]}};
|
|
||||||
|
|
||||||
handle_info({tcp_closed, Socket}, State) ->
|
handle_info({tcp_closed, Socket}, State) ->
|
||||||
gen_tcp:close(Socket),
|
gen_tcp:close(Socket),
|
||||||
|
@ -109,49 +91,34 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
|
handle_cast(stop, State) ->
|
||||||
|
{stop, normal, State};
|
||||||
|
|
||||||
do_JSCall(Socket, Data, State) ->
|
% Handle javascript defines
|
||||||
Parsed = ggs_protocol:parse(Data),
|
handle_cast({define, Token, Payload}, State) ->
|
||||||
NewState = case Parsed of
|
JSVM = getJSVM(Token, State),
|
||||||
{define, Token, Payload} ->
|
js_runner:define(JSVM, Payload),
|
||||||
JSVM = getJSVM(Token, State),
|
send(State#state.lsock, Token, "Okay, defined that for you!"),
|
||||||
js_runner:define(JSVM, Payload),
|
{noreply, State};
|
||||||
send(Socket, Token, "Okay, defined that for you!"),
|
|
||||||
[];
|
% Handle javascript calls
|
||||||
{call, Token, Payload} ->
|
handle_cast({call, Token, Payload}, 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(Socket, Token, "JS says:", binary_to_list(Ret));
|
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
% Set the new state to the reference generated, and JSVM associated
|
% Set the new state to the reference generated, and JSVM associated
|
||||||
{hello, _, _} ->
|
handle_cast({hello, _, _}, State) ->
|
||||||
JSVM = js_runner:boot(),
|
JSVM = js_runner:boot(),
|
||||||
Client = getRef(),
|
Client = getRef(),
|
||||||
send(Socket, Client, "This is your refID"),
|
send(State#state.lsock, Client, "This is your refID"),
|
||||||
|
OldMap = State#state.client_vm_map,
|
||||||
%% This shouldnt be here...
|
NewState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]},
|
||||||
OldMap = State#state.client_vm_map,
|
gen_server:cast(ggs_backup, {set_backup, NewState}),
|
||||||
BackupState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]},
|
{noreply, NewState}.
|
||||||
gen_server:cast(ggs_backup, {set_backup, BackupState}),
|
|
||||||
%%
|
|
||||||
|
|
||||||
{Client, JSVM};
|
|
||||||
{echo, RefID, _, MSG} ->
|
|
||||||
send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)),
|
|
||||||
[];
|
|
||||||
{crash, Zero} ->
|
|
||||||
10/Zero;
|
|
||||||
{vms} ->
|
|
||||||
send(Socket, "RefID", State);
|
|
||||||
% Set the new state to []
|
|
||||||
Other ->
|
|
||||||
send(Socket, "RefID", "__error"),
|
|
||||||
[]
|
|
||||||
end,
|
|
||||||
% Return the new state
|
|
||||||
NewState.
|
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% Helpers
|
%% Helpers
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
|
|
Reference in a new issue