zogzog

Untitled

Nov 8th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.27 KB | None | 0 0
  1. %%% Name            :Marcus Näslund
  2. %%% Personal number :9312105118
  3. %%% E-mail          :
  4.  
  5. -module(dist_erlang).
  6. -record(object, {pid, dbkey, value}).
  7. %% DO NOT CHANGE THE EXPORT STATEMENT!
  8. -export([start/0, init/0, stop/0, store/2, fetch/1, flush/0,
  9.          task/1, dist_task/1, pmap/2, faulty_task/1
  10.         ]).
  11. %% End of no-change area
  12.  
  13.  
  14. start() ->
  15.   case whereis(sts) of
  16.     undefined ->
  17.       Pid = spawn(dist_erlang,init,[]),
  18.       register(sts,Pid),
  19.       {ok,Pid};
  20.     Pid ->
  21.       {ok,Pid}
  22.   end.
  23.  
  24.  
  25. stop() ->
  26.   case whereis(sts) of
  27.     undefined ->
  28.       already_stopped;
  29.     Pid ->
  30.       true = exit(Pid,stop),
  31.       stopped
  32.   end.
  33.  
  34.  
  35. store(Key,Value) ->
  36.   case whereis(sts) of
  37.     undefined ->
  38.       no_server_running;
  39.     Pid ->
  40.       Pid ! {self(),store, Key, Value},
  41.       receive
  42.         Result ->
  43.           Result
  44.       after 1500 ->
  45.         request_timeout
  46.       end
  47.   end.
  48.  
  49.  
  50. fetch(Key) ->
  51.   case whereis(sts) of
  52.     undefined ->
  53.       no_server_running;
  54.     Pid ->
  55.       Pid ! {self(),fetch, Key},
  56.       receive
  57.         Result ->
  58.           Result
  59.       after 1500 ->
  60.         reqest_timeout
  61.       end
  62.   end.
  63.  
  64. flush() ->
  65.   case whereis(sts) of
  66.     undefined ->
  67.       no_server_running;
  68.     Pid ->
  69.       Pid ! {self(),flush},
  70.       receive
  71.         Result ->
  72.           Result
  73.       after 1500 ->
  74.         requet_timeout
  75.       end
  76.   end.
  77.  
  78. init() ->
  79.   loop([]).
  80.  
  81. %% Server loop
  82. loop(Db) ->
  83.   receive
  84.     {Pid,fetch,Key} ->
  85.       Pid ! fetch(Pid,Key,Db),
  86.       loop(Db);
  87.  
  88.     {Pid,store,Key,Value} ->
  89.       case fetch(Pid,Key,Db) of
  90.         {ok,_} ->
  91.           ok, Pid ! {ok,Value};
  92.         {error,not_found} ->
  93.           no_value, Pid ! {ok,no_value}
  94.       end,
  95.       Db2 = [#object{pid=Pid,dbkey=Key,value=Value}|Db],
  96.       loop(Db2);
  97.  
  98.     {Pid,flush} ->
  99.       NewDb = flush(Pid,Db,[]),
  100.       Pid ! {ok,flushed},
  101.       loop(NewDb)
  102.   end,
  103.   loop(Db).
  104.  
  105.  
  106.  
  107. fetch(Pid,Key,[H|T]) ->
  108.   case H of
  109.     #object{pid=Pid,dbkey=Key} ->
  110.       {ok,H#object.value};
  111.     H ->
  112.       fetch(Pid,Key,T)
  113.   end;
  114.  
  115. fetch(_,_,[]) ->
  116.   {error,not_found}.
  117.  
  118.  
  119. flush(Pid,[H|T],Db) ->
  120.   case H of
  121.     #object{pid=Pid} ->
  122.       flush(Pid,T,Db);
  123.     H ->
  124.       flush(Pid,T,[H|Db])
  125.   end;
  126.  
  127. flush(_,_,Db) ->
  128.   Db.
Advertisement
Add Comment
Please, Sign In to add comment