bad merge.

Conflicts:
	src/ggs_server.erl
This commit is contained in:
Kallfaktorn 2011-02-14 15:20:24 +01:00
commit 25ad4fb37b
19 changed files with 300 additions and 123 deletions

41
src/ggs_backup.erl Normal file
View file

@ -0,0 +1,41 @@
-module(ggs_backup).
-behaviour(gen_server).
%% API
-export([start_link/0 ]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-record(state, {port, lsock, client_vm_map = []}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
init([]) ->
{ok, #state{port = -1, lsock = -1, client_vm_map = -1}, 0}.
handle_call(get_backup, _From, State) ->
BackedUpState = case State of
#state{port = -1, lsock = -1, client_vm_map = -1} ->
not_initialized;
Other ->
Other
end,
{reply, {backup_state, BackedUpState}, State}.
handle_cast({set_backup, NewState}, _State) ->
{noreply, NewState}.
handle_info(_Msg, State) ->
{noreply, State}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
terminate(_Reason, _State) ->
ok.

View file

@ -0,0 +1,68 @@
-module(ggs_mnesia_controller_server).
-behaviour(gen_server).
%% API
-export([start_link/0,
stop/0
]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-record(state, {}).
%%%====================================================
%%% API
%%%====================================================
%%-----------------------------------------------------
%% @doc Starts the server
%% @end
%%-----------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%-----------------------------------------------------
%% @doc Stops the server.
%% @spec stop() -> ok
%% @end
%%-----------------------------------------------------
stop() ->
gen_server:cast(?SERVER, stop).
%%-----------------------------------------------------
%% gen_server callbacks
%%-----------------------------------------------------
init([]) ->
mnesia:create_schema([node()]),
mnesia:start(),
{ok, {}, 0}.
handle_cast(a, State) ->
{noreply, State}.
% Request a value from the Mnesia database
handle_call({getValue, _Key},_From,State) ->
{reply,value_of_key_requested_goes_here, State};
% Set a value in the Mnesia database
handle_call({setValue, _Key, Value},_From,State) ->
{reply,value_set_or_updated, State}.
handle_info(timeout, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%-----------------------------------------------------
%% Internal functions
%%-----------------------------------------------------

View file

@ -1,36 +1,39 @@
%% This is a parser for JSON implemented using mochijson2
%% Don't feed it anything other than valid JSON.
-module(ggs_protocol).
-export([parse/1]).
parse(Data) ->
Message = string:tokens(Data, " "),
io:format(Message),
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.
Message =string:tokens(Data, "\n"),
% Turn "A: B" pairs into "{A, B}" tuples, for searching.
MsgKV = lists:map((fun(Str) ->
list_to_tuple(string:tokens(Str, ": ")) end
), Message),
% Hacky way to build a tuple, filter out not_found later on
Processed = {
case lists:keysearch("Command", 1, MsgKV) of
{value,{_, "define"}} ->
define;
{value,{_, "call"}} ->
call;
{value,{_, "hello"}} ->
hello;
false ->
not_found
end,
case lists:keysearch("Token", 1, MsgKV) of
{value,{_, Value}} ->
Value;
false ->
not_found
end,
case lists:keysearch("Content-Length", 1, MsgKV) of
{value,{_, Value}} ->
{Length, _} = string:to_integer(Value),
[_|Cont] = re:split(Data, "\n\n",[{return,list}]),
Content = string:join(Cont, "\n\n"),
Payload = string:substr(Content,1,Length),
Payload;
false ->
not_found
end
},
gen_server:cast(ggs_server, Processed).

View file

@ -1,54 +0,0 @@
%%%----------------------------------------------------
%%% @author Jonatan Pålsson <Jonatan.p@gmail.com>
%%% @copyright 2010 Jonatan Pålsson
%%% @end
%%%----------------------------------------------------
-module(ggs_server).
% import
-import(ggs_network).
%% API
-export([start_link/0, start_link/1, stop/0 ]).
%-export([crash/0, vms/0, hello/0, echo/0]).
-export([get_count/0]).
%%%====================================================
%%% API
%%%====================================================
%%-----------------------------------------------------
%% @doc Starts the server
%% @end
%%-----------------------------------------------------
start_link() ->
ggs_network:start_link().
start_link(Port) ->
ggs_network:start_link(Port).
%%-----------------------------------------------------
%% @doc Fetches the number of requests made to this server
%% @spec get_count() -> {ok, Count}
%% where
%% Count = integer()
%% @end
%%-----------------------------------------------------
get_count() ->
ggs_network:get_count(get_count).
%crash() -> gss_network:crash().
%vms() -> gss_network:vms().
%hello() -> gss_network:hello().
%echo() -> gss_network:echo().
%%-----------------------------------------------------
%% @doc Stops the server.
%% @spec stop() -> ok
%% @end
%%-----------------------------------------------------
stop() ->
ggs_network:stop().

48
src/ggs_server_sup.erl Normal file
View file

@ -0,0 +1,48 @@
-module(ggs_server_sup).
-behaviour(supervisor).
%% API
-export([start/1, start_link/1]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
start(Port) ->
[FirstArg] = Port,
{IntPort, _} = string:to_integer(FirstArg),
start_link(IntPort).
start_link(Port) ->
supervisor:start_link({local, ?SERVER}, ?MODULE, [Port]).
init([Port]) ->
GGSServer = {ggs_server,
{ggs_server, start_link, [Port]},
permanent,
2000,
worker,
[ggs_server]
},
Backup = {ggs_backup,
{ggs_backup, start_link, []},
permanent,
2000,
worker,
[ggs_backup]
},
MnesiaServer = {ggs_mnesia_controller_server,
{ggs_mnesia_controller_server, start_link, []},
permanent,
2000,
worker,
[ggs_mnesia_controller_server]
},
Children = [MnesiaServer, Backup, GGSServer],
RestartStrategy = { one_for_one, % Restart only crashing child
10, % Allow ten crashes per..
1 % 1 second, then crash supervisor.
},
{ok, {RestartStrategy, Children}}.

View file

@ -17,12 +17,12 @@ start_link(Port) ->
supervisor:start_link({local, ?SERVER}, ?MODULE, [Port]).
init([Port]) ->
Server = {ggs_server,
{ggs_server, start_link, [Port]},
Server = {ggs_server_sup,
{ggs_server_sup, start_link, [Port]},
permanent,
2000,
worker,
[ggs_server]
[ggs_server_sup]
},
Children = [Server],

View file

@ -1,13 +0,0 @@
-module(js_runner).
-export([define/2,call/3, boot/0]).
boot() ->
erlang_js:start(),
{ok, Port} = js_driver:new(),
Port.
define(Port, Data) ->
ok = js:define(Port, list_to_binary(Data)).
call(Port, Func, Params) ->
js:call(Port, list_to_binary(Func), Params).

View file

@ -3,4 +3,5 @@
start() ->
application:start(inets),
application:start(erlang_js),
application:start(ggs).