-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.erl
More file actions
56 lines (51 loc) · 1.39 KB
/
server.erl
File metadata and controls
56 lines (51 loc) · 1.39 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
-module(event).
%-export([start/0]).
-export_all().
start() ->
process_flag(trap_exit, true),
Pid = spawn_link(main({[],[]})),
receive ->
{'EXIT', Pid, normal} -> ok;
{'EXIT', Pid, Reason} -> {'EXIT', Pid, Reason};
{'EXIT',Pid,_} -> start();
Undefined -> Undefined
end.
main({Clients,Events}) when is_list(Clients), is_list(Events)->
receive
{subscribe, Pid} ->
case storeClient(Pid) of
{success, Pid, Ref} -> main({[{Pid,Ref}|Clients],Events});
{error, Reason -> Pid ! Reason
end;
{add, Pid, Event_key, Descr, Time} ->
createEvent(), %%spawn some process
main({[Clients,[createEvent() | Events]});
{cancel, Pid, Event_key} ->
Pid ! stopEvent(Event_key, Events),
main();
{event, Pid, Event_key, Descr} ->
Pid ! {Event_key, Descr},
main();
{terminate, Pid} ->
exit(normal);
{'EXIT', Pid, Reason} ->
{'EXIT', Pid, Reason},
main();
Unknown -> {error, Unknown}
end.
clientMember(Pid,[{Client_pid,Ref} | Clients]) when Pid == Client_pid ->
{Client_pid,Ref};
clientMember(Pid,[{Client_pid,Ref} | Clients]) ->
clientMember(Pid,Clients);
clientMember(Pid,[]) ->
false.
storeClient(Pid,Clients) when is_pid(Pid),is_list(Clients) ->
case clientMember(Pid,Clients) of
false ->
{Pid,Ref} = monitor(process,Pid),
{success,Pid,Ref};
_ ->
{error, "client already subscribed"}
end.
storeClient(Pid,Clients) ->
{error, "unresolved format"}.