diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/eco_pool.erl | 42 | ||||
-rw-r--r-- | src/eco_proc.erl | 26 | ||||
-rw-r--r-- | src/eco_tb.erl | 12 | ||||
-rw-r--r-- | src/eco_time.erl | 30 | ||||
-rw-r--r-- | src/eco_watcher.erl | 16 |
5 files changed, 126 insertions, 0 deletions
diff --git a/src/eco_pool.erl b/src/eco_pool.erl new file mode 100644 index 0000000..63fbc3d --- /dev/null +++ b/src/eco_pool.erl @@ -0,0 +1,42 @@ +-module(eco_pool). +-export([start_pool/1, start_random_pool/2]). + +start_pool(Data) -> + pool_loop(Data). + +start_random_pool(Size, Max) -> + random:seed(now()), + Data = [random:uniform(Max) || _ <- lists:seq(1, Size)], + pool_loop(Data). + +pool_loop(PoolData) -> + receive + {From, stop} -> + From ! {ok, stopped}, + {ok, stopped}; + {From, drain} -> + From ! {ok, drained}, + pool_loop([]); + {From, set, Data} -> + From ! {ok, set, Data}, + pool_loop(Data); + {From, put, Data} -> + From ! {ok, put, Data}, + pool_loop(PoolData ++ Data); + {From, get, Data} -> + New = PoolData -- Data, + Test = PoolData -- New, + if + Test =:= Data -> + From ! {ok, get, Data}, + pool_loop(New); + true -> + From ! {error, get, Data}, + pool_loop(PoolData) + end; + {From, look} -> + From ! {ok, look, PoolData}, + pool_loop(PoolData); + Msg -> + {error, invalid_message, Msg} + end. diff --git a/src/eco_proc.erl b/src/eco_proc.erl new file mode 100644 index 0000000..c179a26 --- /dev/null +++ b/src/eco_proc.erl @@ -0,0 +1,26 @@ +-module(eco_proc). +-export([proc/6]). + +proc(Watcher, _, _, _, 0, _) -> + Watcher ! {died, self()}; +proc(Watcher, Gets, Puts, Pool, Life, MaxLife) -> + receive + {From, stop} -> + From ! {ok, stopped, self()}, + Watcher ! {stopped, self()}, + {ok, stopped}; + {From, tick} -> + From ! {self(), tock}, + Watcher ! {tick, self()}, + Pool ! {self(), get, Gets}, + receive + {ok, get, Gets} -> + Pool ! {self(), put, Puts}, + receive + {ok, put, Puts} -> + proc(Watcher, Gets, Puts, Pool, MaxLife, MaxLife) + end; + {error, get, Gets} -> + proc(Watcher, Gets, Puts, Pool, Life - 1, MaxLife) + end + end. diff --git a/src/eco_tb.erl b/src/eco_tb.erl new file mode 100644 index 0000000..82119f6 --- /dev/null +++ b/src/eco_tb.erl @@ -0,0 +1,12 @@ +-module(eco_tb). +-export([start/0]). + +start() -> + Watcher = spawn(eco_watcher, watcher, [self()]), + Pool = spawn(eco_pool, start_pool, [[1,1,1]]), + Proc1 = spawn(eco_proc, proc, [Watcher, [1], [], Pool, 3, 3]), + _Ticker = spawn(eco_time, ticker, [Watcher, 1000, [Proc1], 3000]), + receive + {error, notock, _, _} -> + init:stop() + end. diff --git a/src/eco_time.erl b/src/eco_time.erl new file mode 100644 index 0000000..587cca5 --- /dev/null +++ b/src/eco_time.erl @@ -0,0 +1,30 @@ +-module(eco_time). +-export([ticker/4]). + +ticker(Watcher, Interval, ProcList, Timeout) -> + receive + {From, stop} -> + From ! {ok, stopped}, + {ok, stopped} + after Interval -> + handle_tick(self(), ProcList, Timeout), + receive + {ok, endtick} -> + Watcher ! {endtick, self()}, + ticker(Watcher, Interval, ProcList, Timeout); + {error, notock, Proc} -> + Watcher ! {error, notock, Proc, self()}, + {error, notock, Proc} + end + end. + +handle_tick(Ticker, [], _) -> + Ticker ! {ok, endtick}; +handle_tick(Ticker, [This | Next], Timeout) -> + This ! {self(), tick}, + receive + {This, tock} -> + handle_tick(Ticker, Next, Timeout) + after Timeout -> + Ticker ! {error, notock, This} + end. diff --git a/src/eco_watcher.erl b/src/eco_watcher.erl new file mode 100644 index 0000000..ce14ab4 --- /dev/null +++ b/src/eco_watcher.erl @@ -0,0 +1,16 @@ +-module(eco_watcher). +-export([watcher/1]). + +watcher(Super) -> + receive + {From, stop} -> + From ! {ok, stopped}, + {ok, stopped}; + {error, notock, A, B} -> + Super ! {error, notock, A, B}, + io:format("~p~n", [{error, notock, A, B}]), + watcher(Super); + Msg -> + io:format("~p~n", [Msg]), + watcher(Super) + end. |