-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_serialization_server.erl
35 lines (28 loc) · 1.18 KB
/
event_serialization_server.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-module(communication_server).
-include("command_descriptor.hrl").
-behaviour(gen_server).
-export([start/0, stop/0, register/1, call/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
start() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() -> gen_server:call(?MODULE, stop).
register(CncHost) -> gen_server:cast(?MODULE, {register, CncHost}).
call(Packet) -> gen_server:cast(?MODULE, {call, Packet}).
init([]) ->
io:format("*** init on CNC *** ~p~n", [?MODULE]),
{ok, 1}.
handle_call(stop, _From, N) ->
io:format("*** stopping *** ~p~n", [?MODULE]),
{stop, normal, stopped, N + 1}.
handle_cast({register, CncHost}, SeqNum) ->
io:format("*** registering with CNC *** ~p~n", [CncHost]),
Msg = #command_descriptor{originating_host=bar@fawkes,
destination_host=CncHost,
id = SeqNum},
rpc:call(CncHost, command_server, call, [{register, Msg}]),
{noreply, SeqNum + 1};
handle_cast({call, Packet}, N) ->
io:format("*** call from CNC *** ~p~n", [Packet]),
{noreply, N + 1}.
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, Extra) -> {ok, State, Extra}.