diff --git a/src/ggs_network.erl b/src/ggs_network.erl index 1ee46f6..5489069 100644 --- a/src/ggs_network.erl +++ b/src/ggs_network.erl @@ -22,7 +22,8 @@ % export -export([start_link/0,start_link/1]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2]). --export([get_count/1, send/3, send/4]). +-export([get_count/1,crash/0,vms/0,hello/0,echo/0]). +-export([send/3, send/4]). -export([stop/0]). %% gen_server callbacks @@ -61,6 +62,14 @@ init([Port]) -> get_count(get_count) -> gen_server:call(?SERVER, get_count). +crash() -> gen_server:call(?server, _crash). +vms() -> gen_server:call(?SERVER, _vms). +hello() -> gen_server:call(?SERVER, _hello). +echo() -> gen_server:call(?SERVER, {_echo, RefID, _, MSG}). + + + + %%----------------------------------------------------- %% @doc Stops the server. %% @spec stop() -> ok diff --git a/src/ggs_server.erl b/src/ggs_server.erl index 41cfee3..95a53dd 100644 --- a/src/ggs_server.erl +++ b/src/ggs_server.erl @@ -40,16 +40,13 @@ start(Port) -> %% Count = integer() %% @end %%----------------------------------------------------- -<<<<<<< HEAD get_count() -> ggs_network:get_count(get_count). -======= -get_count() -> gen_server:call(?SERVER, get_count). -_crash() -> gen_server:call(?SERVER, _crash). -_vms() -> gen_server:call(?SERVER, _vms). -_hello() -> gen_server:call(?SERVER, _hello). -_echo() -> gen_server:call(?SERVER, {_echo, RefID, _, MSG}). ->>>>>>> 02b72e54457112cb8d576cdcf3e982d2e34407fc + +_crash() -> gen_network:crash(). +_vms() -> gen_network:vms(). +_hello() -> gen_network:hello(). +_echo() -> gen_network:echo(). %%----------------------------------------------------- %% @doc Stops the server. @@ -58,102 +55,3 @@ _echo() -> gen_server:call(?SERVER, {_echo, RefID, _, MSG}). %%----------------------------------------------------- stop() -> ggs_network:stop(). - -%%----------------------------------------------------- -%% gen_server callbacks -%%----------------------------------------------------- - -init([Port]) -> - {ok, LSock} = gen_tcp:listen(Port, [{active, true}, - {reuseaddr, true}]), - {ok, #state{port = Port, lsock = LSock}, 0}. - -handle_call(get_count, _From, State) -> - {reply, {ok, State#state.client_vm_map}, State}; - -handle_call(_crash, _From, State) -> - Zero/10. - {reply, sdas , State}; - -%handle_call(_hello, _From, State) -> -% Client = getRef(); -% send(Socket, Client, "_ok_hello"), -% {Client, JVSM) -%{reply, Client, State}; - -%handle_call(_vms, _From, State) -> -% send(Socket, "RefID", State) -%{reply, , State}; - -%handle_call(_echo, RefID, _, MSG) -> -%{reply, ,State}; - -handle_call(_Message, _From, State) - {reply, error, State}. - -handle_cast(stop, State) -> - {stop, normal, State}. - -handle_info({tcp, Socket, RawData}, State) -> - NewState = do_JSCall(Socket, RawData, State), - OldMap = State#state.client_vm_map, - io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]), - {noreply, State#state{client_vm_map = OldMap ++ [NewState]}}; - -handle_info(timeout, #state{lsock = LSock} = State) -> - {ok, _Sock} = gen_tcp:accept(LSock), - {noreply, State}. - -terminate(_Reason, _State) -> - ok. - -code_change(_OldVsn, State, _Extra) -> - {ok, State}. - -%%----------------------------------------------------- -%% Internal functions -%%----------------------------------------------------- -do_JSCall(Socket, Data, State) -> - JSVM = js_runner:boot(), - js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"), - Parsed = ggs_protocol:parse(Data), - NewState = case Parsed of - {cmd, Command, Parameter} -> - % Set the new state to [] - Ret = js_runner:call(JSVM, "userCommand", - [list_to_binary(Command), - list_to_binary(Parameter)]), - connection:send(Socket, "RefID", "JS says: ", Ret), - []; - % Set the new state to the reference generated, and JSVM associated - {hello} -> - Client = getRef(), - connection:send(Socket, Client, "__ok_hello"), - {Client, JSVM}; - {echo, RefID, _, MSG} -> - connection:send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)), - []; - {crash, Zero} -> - 10/Zero; - {vms} -> - connection:send(Socket, "RefID", State); - % Set the new state to [] - Other -> - ggs_connection:send(Socket, "RefID", "__error"), - [] - end, - % Return the new state - NewState. -%%----------------------------------------------------- -%% Helpers -%%----------------------------------------------------- -getRef() -> - {A1,A2,A3} = now(), - random:seed(A1, A2, A3), - random:uniform(1000). - -getJSVM(RefID, State) -> - VMs = State#state.client_vm_map, - {value, {_,VM}} = lists:keysearch(RefID, 1, VMs), - VM. ->>>>>>> 02b72e54457112cb8d576cdcf3e982d2e34407fc