Last active
December 29, 2015 08:49
-
-
Save jasonjohnson/7646605 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(main). | |
-export([run/0, hold/0, example/0]). | |
hold() -> | |
receive | |
% ... | |
after | |
1000 -> | |
io:format("Timed out: ~p~n", [self()]) | |
end. | |
run() -> | |
% First, lets spawn a thing to do something 1 second after | |
% its invocation. | |
HoldPid = spawn(main, hold, []), | |
% Which process are we waiting for? | |
io:format("Holding process: ~p~n", [HoldPid]), | |
% Now, lay down a log entry if we can. | |
case file:open("log", [append]) of | |
{error, Reason} -> | |
io:format("Could not open log file: ~p~n", [Reason]); | |
{ok, Fd} -> | |
io:format("Opened file.~n"), | |
% Got our file handle. Now to write something! | |
file:write(Fd, <<"Some bytes. Represented as a string.">>), | |
file:write(Fd, <<"\n">>), | |
file:close(Fd) | |
end. | |
% 32768 16348 8192 4096 2048 1024 512 256 | |
% 0 0 0 0 0 0 0 0 | |
% 128 64 32 16 8 4 2 1 | |
% 0 0 0 0 0 0 0 0 | |
% | |
% <<A:16, B/binary>> = <<1, 2, 3, 4>>. | |
% | |
% A = 258 | |
% B = <<3, 4>> | |
% | |
% How did we arrive at 258? | |
% | |
% A == 258 because we've placed the bit | |
% signature of the number "1" into the higher | |
% range of our 16-bit sequence. Therefore it | |
% actually represents the value "256". | |
% | |
% This would be a logical left-shift of 8 bits. | |
% | |
% As the number "2" now falls into the lower | |
% range of bits, it is not shifted, simply | |
% applied. | |
% | |
% The resulting integer value is 258. | |
example() -> | |
Bin = <<1,2,3,4>>, | |
% Examine our raw bytes. | |
io:format("Before operations: ~p~n", [Bin]), % <<1,2,3,4>> | |
% Now, take 2 of those bytes (16 bits) and | |
% shift them into an integer. Allow the rest | |
% to fill up our second value. | |
<<A:16, B/binary>> = Bin, | |
% We should now have an integer and our | |
% remainder: | |
io:format("Our integer: ~p~n", [A]), % 258 | |
io:format("Our remainder: ~p~n", [B]). % <<3,4>> | |
% Unused. Some no-ops. | |
connect() -> | |
% Connect to various nodes. Say, that we've scanned | |
% out of a config. | |
net_kernel:connect_node(testing0@jajohnson), | |
net_kernel:connect_node(testing1@jajohnson), | |
% Spawn a process on another node. | |
spawn(testing1@jajohnson, main, example, []), | |
% Our node name and all the nodes we're connected to. | |
node(), | |
nodes(). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment