Commits

OJ Reeves committed 2bc205f

finalised presentation for BFPG June 2011

  • Participants
  • Parent commits 537a0a0

Comments (0)

Files changed (6)

 syntax: glob
 *.beam
+*.swp
+~*

File BFPG/2011-03/BFPG-2011-March-Erlang.pptx

Binary file modified.

File BFPG/2011-03/echo.erl

 -module(echo).
 -compile(export_all).
 
-echo() ->
+start() ->
+  spawn(fun loop/0).
+
+loop() ->
   receive
     done ->
       io:format("Closing~n", []),
     {Pid, Message} ->
       io:format("Message received: ~s~n", [Message]),
       Pid ! Message,
-      echo();
+      loop();
     _ ->
       io:format("skipping message~n", []),
-      echo()
+      loop()
   end.
 

File BFPG/2011-03/par.erl

 
 map(F, L) ->
   S = self(),
-  [receive {P, R} -> R end || P <- [spawn(fun() -> S ! {self, (catch F(X))} end) || X <- L]].
-
-gather([P|L]) ->
-  receive
-    {P, Result} -> [Result|gather(L)]
-  end;
-gather([]) ->
-  [].
+  [receive {P, R} -> R end || P <- [spawn(fun() -> S ! {self(), (catch F(X))} end) || X <- L]].
 
 data() ->
   lists:map(fun(X) -> lists:duplicate(X, X) end, lists:seq(1, 255)).
 
 munge(X) ->
   io_lib:format("~p", [lists:foldl(fun(E, A) -> E / A end, 1, [lists:sum(X)|X])]).
+
+par_test() ->
+  {T, _} = timer:tc(par, par_load, []),
+  io:format("Average run: ~p~n", [T div 1000]),
+  ok.
+
+par_load() ->
+  lists:map(fun(_) -> map(fun munge/1, data()) end, lists:seq(1,1000)).
+
+seq_test() ->
+  {T, _} = timer:tc(par, seq_load, []),
+  io:format("Average run: ~p~n", [T div 1000]),
+  ok.
+
+seq_load() ->
+  lists:map(fun(_) -> lists:map(fun munge/1, data()) end, lists:seq(1,1000)).

File BFPG/2011-03/ring.erl

+-module(ring).
+-compile(export_all).
+
+start(N) ->
+  spawn(fun() -> create(N) end).
+
+create(0) ->
+  handle_message(fun(_) -> ok end);
+create(N) ->
+  Next = spawn(fun() -> create(N-1) end),
+  handle_message(fun(M) -> Next ! M end).
+
+handle_message(Forward) ->
+  receive
+    quit ->
+      io:format("Quitting~n", []),
+      Forward(quit),
+      ok;
+    Message ->
+      io:format("~p received ~p~n", [self(), Message]),
+      Forward(Message),
+      handle_message(Forward)
+  end.

File BFPG/2011-03/sbw.erl

+-module(sbw).
+-compile(export_all).
+
+start(Port) ->
+  {ok, Listen} = gen_tcp:listen(Port, [binary, {packet, 0}, {reuseaddr, true}, {active, true}]),
+  spawn(fun() -> listen(Listen) end).
+
+listen(Listen) ->
+  {ok, Socket} = gen_tcp:accept(Listen),
+  spawn(fun() -> listen(Listen) end),
+  loop(Socket).
+
+loop(Socket) ->
+  receive
+    {tcp, Socket, Bin} ->
+      io:format("Received binary:~n~p~n", [Bin]),
+      Response = handle_http(Bin),
+      io:format("Responding with: ~p~n", [Response]),
+      gen_tcp:send(Socket, Response);
+    {tcp_closed, Socket} ->
+      io:format("Socket closed")
+  end.
+
+handle_http(Bin) ->
+  Data = binary_to_list(Bin),
+  Request = [Req|_Headers] = re:split(Data, "\r\n", [{return, list}]),
+  io:format("Parts: ~p~n", [Request]),
+  {match, [Uri]} = re:run(Req, "GET (/[^/ ]*)",[{capture,[1],list}]),
+  io:format("Uri: ~p~n", [Uri]),
+  Result = case proplists:get_value(Uri, uris(), undefined) of
+    undefined ->
+      not_found();
+    Handler ->
+      Handler(Request)
+  end,
+  Result.
+
+not_found() ->
+  [header(404, "Not Found"), body("<html><body>Resource doesn't exist</body></html>")].
+
+uris() ->
+  [ {"/echo", fun echo/1}, {"/static", fun static/1}, { "/favicon.ico", fun favicon/1 } ].
+
+echo(Request) ->
+  [header(200, "OK"), body("<html><body><pre>" ++ string:join(Request, "\r\n") ++ "</pre></body></html>")].
+
+static(_) ->
+  [header(200, "OK"), body("<html><body>This is a very boring website.</body></html>")].
+
+
+header(Code, Message) ->
+  ["HTTP/1.1 ", integer_to_list(Code), " ", Message, "\r\nServer: Super Basic Webserver/0.0.0.1 (Windoze)\r\nConnection: close\r\n"].
+
+body(Body) ->
+  ["Content-Type: text/html; charset=UTF-8\r\nContent-Length: ", integer_to_list(length(Body) + 4), "\r\n\r\n", Body, "\r\n\r\n"].
+
+bin_body(Bin) ->
+  ["Content-Type: text/plain\r\nContent-Length: ", integer_to_list(bit_size(Bin) div 8 + 4), "\r\n\r\n", Bin, "\r\n\r\n"].
+
+favicon(_) ->
+  Icon = <<0, 0, 1, 0, 1, 0, 16, 16, 16, 0, 1, 0, 4, 0, 40,
+      1, 0, 0, 22, 0, 0, 0, 40, 0, 0, 0, 16, 0, 0, 0, 32,
+      0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 156,
+      226, 0, 41, 13, 184, 0, 54, 53, 54, 0, 133, 115, 215,
+      0, 86, 86, 87, 0, 250, 249, 251, 0, 64, 35, 192, 0,
+      16, 0, 174, 0, 214, 208, 242, 0, 107, 88, 207, 0, 62,
+      62, 68, 0, 76, 48, 196, 0, 61, 41, 191, 0, 172, 161,
+      224, 0, 211, 211, 211, 0, 156, 156, 155, 0, 94, 94,
+      238, 238, 94, 85, 94, 229, 234, 234, 255, 255, 79,
+      250, 244, 78, 234, 226, 255, 239, 174, 36, 255, 254,
+      95, 239, 254, 85, 245, 238, 143, 245, 137, 53, 85,
+      85, 88, 85, 88, 152, 215, 213, 85, 80, 17, 184, 85,
+      144, 12, 85, 85, 135, 23, 124, 85, 144, 3, 85, 85,
+      183, 17, 17, 188, 125, 0, 85, 85, 199, 113, 113, 119,
+      125, 221, 85, 85, 153, 153, 153, 153, 205, 221, 85,
+      85, 85, 85, 85, 85, 48, 9, 85, 85, 88, 133, 85, 85,
+      144, 214, 85, 85, 145, 17, 133, 85, 189, 215, 213,
+      85, 129, 16, 85, 88, 125, 93, 133, 85, 85, 85, 85,
+      88, 213, 85, 85, 85, 85, 85, 85, 85, 85, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+      0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 10>>,
+  [header(200, "OK"), bin_body(Icon)].
+