Skip to content

Instantly share code, notes, and snippets.

@7-fl
7-fl / freq_gen_add_freqs.md
Last active May 7, 2017 03:29
Added the add_freqs() function

gs.erl:

-module(gf).
-behavior(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start/0, allocate/0, deallocate/1, stop/0, client/2, test1/0, test2/0]).
-export([cast_report/0, call_report/0, add_frequencies/1]).

%%====== Server Internal Function Replacements ==========
@7-fl
7-fl / freq_gen_report.md
Last active May 6, 2017 17:24
Added call_report/0 and cast_report/0

gf.erl:

-module(gf).
-behavior(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start/0, allocate/0, deallocate/1, stop/0, client/2, test1/0, test2/0]).
-export([cast_report/0, call_report/0]).
@7-fl
7-fl / freq_gen_cast.md
Last active May 6, 2017 17:22
Convert frequency server to gen_server using handle_cast()

gf.erl:

-module(gf).
-behavior(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start/0, allocate/0, deallocate/1, stop/0, client/2, test1/0, test2/0]).

%%====== Server Internal function Replacements ==========

init([]) ->
@7-fl
7-fl / freq_gen_call.md
Last active May 6, 2017 17:26
Convert frequency server to use gen_server using handle_call()

A shell script for running my erlang program:

run.sh:

#!/usr/bin/env sh

erlc -W gf.erl
erl -s gf test2

Start instructions for four distributed servers:

  • First shell (note all shells need to be started in node mode):
$ erl -sname s1
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.2  (abort with ^G)
(s1@MyMBP)1> 
@7-fl
7-fl / most_frequencies_router.md
Last active May 2, 2017 22:19
most frequencies router -- multiple severs

Instead of sending server responses back to the router, which could potentially create a bottleneck at the router, I had the router keep count of the available frequencies. When an allocate request passes through the router, the router decreases the server's available count; and when a deallocate request passes through the router, the router increases the server's available count (I didn't address the case where a client tries to deallocate a frequency it doesn't own). I also let the clients, which are running infinite loops, fail after I stop the router/servers.

In my code, a server is a triple {ServerPid, Freqs, AvailableCount}. For allocate requests, the router sorts the servers using lists:keysort() on the third term to get the server with the highest AvailableCount. For deallocate requests, the router finds the server corresponding to the frequency: if no servers correspond to the frequency, then the router sends the client an error message; otherwise the router sends a deallcoate request

@7-fl
7-fl / alternating_router.md
Created May 2, 2017 18:41
Alternating router -- multiple servers

fs.erl:

%% Based on code from 
%%   Erlang Programming
%%   Francecso Cesarini and Simon Thompson
%%   O'Reilly, 2008
%%   http://oreilly.com/catalog/9780596518189/
%%   http://www.erlangprogramming.org/
%%   (c) Francesco Cesarini and Simon Thompson
@7-fl
7-fl / a5.md
Last active May 1, 2017 05:20
Hot code loading with frequency server

original f4.erl:

I changed the original f4.erl server code to contain an additonal clause in loop():

-export([loop/1]).

loop(Frequencies) ->
    receive
        {request, _Pid, switch_code} ->
@7-fl
7-fl / review.md
Last active April 28, 2017 09:31
Review of Kevin Marth's Supervisor-Frequency Server

Correctness

If I kill the supervisor using the observer app, your code will print out erroneous information. When the supervisor is killed, your code executes:

        {'EXIT',Client,_} -> 
            io:format("server: #~p client exit ~p~n",[Number,Client]), 
            server(Number,exited(Frequencies,Client)) 
@7-fl
7-fl / f3.erl
Created April 24, 2017 01:10
Creating clients for the frequency server/week2
%% Based on code from
%% Erlang Programming
%% Francecso Cesarini and Simon Thompson
%% O'Reilly, 2008
%% http://oreilly.com/catalog/9780596518189/
%% http://www.erlangprogramming.org/
%% (c) Francesco Cesarini and Simon Thompson
-module(f3).
-export([start/0,allocate/0,deallocate/1,stop/0]).