-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeElement.erl
More file actions
146 lines (120 loc) · 4.46 KB
/
computeElement.erl
File metadata and controls
146 lines (120 loc) · 4.46 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
%%%----------------------------------------------------
%%% Copyright AnalyzERL 2011
%%% All rights reserved. No part of this computer programs(s) may be
%%% used, reproduced,stored in any retrieval system, or transmitted,
%%% in any form or by any means, electronic, mechanical, photocopying,
%%% recording, or otherwise without prior written permission of
%%% the authors
%%%---------------------------------------------------------------------
%%%---------------------------------------------------------------------
%%% Revision History
%%%---------------------------------------------------------------------
%%% Revision 1.0 Author: Souvik Ray (rsouvik@gmail.com)
%%%---------------------------------------------------------------------
-module(computeElement).
-compile(export_all).
-define(OUTPUTFILE, "output.txt").
-define(DEBUGFILE, "debug.txt").
% Computing Element
% handler list will be read from configuration file
start(Name, Node, HandlerList) ->
{ok, Fdd} = file:open(?DEBUGFILE, [append]),
io:fwrite(Fdd, "~p~n", ["Compute Element: Called on node"]),
io:fwrite(Fdd, "~p~n", [Node]),
io:fwrite(Fdd, "~p~n", [HandlerList]),
file:close(Fdd),
%parse HandlerList which is read from config file
global:register_name(erlang:list_to_atom(Name), spawn(erlang:list_to_atom(Node), computeElement, init, [HandlerList])).
init(HandlerList) ->
{ok, Fdd} = file:open(?DEBUGFILE, [append]),
io:fwrite(Fdd, "~p", ["Compute Element: Initialized "]),
file:close(Fdd),
loop(initialize(HandlerList)).
initialize([]) -> [];
initialize([{Handler, InitData}|Rest]) ->
[{Handler, Handler:init(InitData)}|initialize(Rest)].
call(Name, Msg) ->
%Name ! {request, self(), Msg},
{ok, Fd} = file:open(?DEBUGFILE, [append]),
io:fwrite(Fd, "~p", ["Inside call: "]),
file:close(Fd),
global:send(Name, {request, self(), Msg}),
receive
{reply, Reply} ->
{ok, Fdd} = file:open(?DEBUGFILE, [append]),
io:fwrite(Fdd, "~p", ["Reply received from comp element: "]),
io:fwrite(Fdd, "~p~n", [Reply]),
file:close(Fdd),
Reply
end.
reply(To, Msg) ->
To ! {reply, Msg}.
loop(State) ->
receive
{request, From, Msg} ->
{Reply, NewState} = handle_msg(Msg, State),
reply(From, Reply),
handle_output(NewState),
loop(NewState);
{stop, From} ->
reply(From, terminate(State))
end.
%client fun
stop(Name) ->
Name ! {stop, self()},
receive {reply, Reply} -> Reply end.
terminate([]) -> [];
terminate([{Handler, Data}|Rest]) ->
[{Handler, Handler:terminate(Data)}|terminate(Rest)].
%more client funs
add_handler(Name, Handler, InitData) ->
call(Name, {add_handler, Handler, InitData}).
delete_handler(Name, Handler) ->
call(Name, {delete_handler, Handler}).
get_data(Name, Handler) ->
call(Name, {get_data, Handler}).
send_event(Name, Event) ->
call(Name, {send_event, Event}).
send(Name, Event) ->
call(Name, Event).
handle_msg({add_handler, Handler, InitData}, LoopData) ->
{ok, [{Handler, Handler:init(InitData)}|LoopData]};
handle_msg({delete_handler, Handler, InitData}, LoopData) ->
case lists:keysearch(Handler, 1, LoopData) of
false ->
{{error, instance}, LoopData};
{value, {Handler,Data}} ->
Reply = {data, Handler:terminate(Data)},
NewLoopData = lists:keydelete(Handler, 1, LoopData),
{Reply, NewLoopData}
end;
handle_msg({get_data, Handler}, LoopData) ->
case lists:keysearch(Handler, 1, LoopData) of
false -> {{error, instance}, LoopData};
{value, {Handler, Data}} -> {{data, Data}, LoopData}
end;
handle_msg(Event, LoopData) ->
{ok, event(Event, LoopData)}.
event(_Event, []) -> [];
event(Event, [{Handler, Data}|Rest]) ->
[{Handler, Handler:handle_event(Event, Data)}|event(Event, Rest)].
%This can be controlled by timing specs in conf file
handle_output(LoopData) ->
{ok, generate_output(LoopData)}.
%Handler: aggregrator
%Data: {Event-key, Cnt}
generate_output([]) -> [];
generate_output([{Handler, Data}|Rest]) ->
[{Handler, dispatchEvent(Handler:output(Data))}|generate_output(Rest)].
% EventKey is the routing key, EventData is the data generated by Handler output function
dispatchEvent(Data) ->
case Data of
{EventKey, EventType, EventData} ->
Reply = networkManager:route(EventKey, EventType, EventData, self());
%data sink
%print to stdout or store to persistent store
{EventData} ->
{ok, Fd} = file:open(?OUTPUTFILE, [append]),
io:fwrite(Fd, "~p~n", [EventData]),
file:close(Fd)
end.