Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(fsm_telephone).
- -export([start/1, call/2, idle/1, ringing/2, dial/2, connected/1, start_ringing/1, start_tone/1, stop_ringing/1]).
- start(Name) ->
- Bill = [],
- Pid = spawn(fsm_telephone, idle, [Bill]),
- register(Name, Pid), ok.
- call(Name, Msg) ->
- Name ! {request, self(), Msg},
- receive {reply, Reply} -> Reply end.
- reply(To, Msg) ->
- To ! {reply, Msg}.
- idle(Bill) ->
- receive
- {request, From, {Number, incoming}} ->
- start_ringing(From),
- ringing(Number, Bill);
- {request, From, {Number, call}} ->
- start_tone(From),
- dial(Number, Bill);
- {request, From, bill} ->
- reply(From, 'Billing'),
- io:format("~w~n", [Bill]),
- idle(Bill)
- end.
- ringing(Number, Bill) ->
- receive
- {request, From, other_on_hook} ->
- stop_ringing(From),
- idle(Bill);
- {request, From, off_hook} ->
- stop_ringing(From),
- {{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime(),
- Bill1 = Bill++[{'Incoming call', Number, {{Year,Month,Day},{Hour,Min,Sec}}}],
- connected(Bill1)
- end.
- dial(Number, Bill) ->
- receive
- {request, From, on_hook} ->
- reply(From, 'Cancel calling'),
- idle(Bill);
- {request, From, connect} ->
- {{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime(),
- Bill1 = Bill++[{'Outcoming call', Number, {{Year,Month,Day},{Hour,Min,Sec}}}],
- reply(From, connected),
- connected(Bill1)
- end.
- connected(Bill) ->
- receive
- {request, From, on_hook} ->
- reply(From, 'Calling finished!!!'),
- {{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime(),
- Bill1 = Bill++[{'End Call', {{Year,Month,Day},{Hour,Min,Sec}}}],
- idle(Bill1)
- end.
- start_ringing(To) -> reply(To, 'Ring ring!!!').
- start_tone(To) -> reply(To, 'Signal tone!!!').
- stop_ringing(To) -> reply(To, 'Stop ringing').
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement