-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
HpyHacking
committed
Apr 27, 2012
1 parent
83a0ce9
commit def1504
Showing
22 changed files
with
1,514 additions
and
1,308 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-module(tcp_server). | ||
-export([start/0, send/2]). | ||
|
||
-define(log(Message), | ||
fun() -> | ||
io:foramt("<~p> LOGGER: ~p~n", [self(), Message]) | ||
end()). | ||
|
||
start() -> | ||
{ok, ListenSocket} = gen_tcp:listen(5000, [binary, {active, false}]), | ||
wait_connect(ListenSocket, 0). | ||
|
||
wait_connect(ListenSocket, Count) -> | ||
?log("Begin Wait Connection ..."). | ||
{ok, Socket} = gen_tcp:accept(ListenSocket), | ||
?log("Receive a TCP request ..."). |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-module(udp_server). | ||
-export([start_link/0, stop/0]). | ||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]). | ||
-export([send/1]). | ||
|
||
-behaviour(gen_server). | ||
|
||
-define(SERVER_PORT, 18000). | ||
-define(CLIENT_PORT, 18001). | ||
|
||
start_link() -> | ||
gen_server:start_link({local, ?MODULE}, ?MODULE, null, []). | ||
|
||
stop() -> | ||
gen_server:cast(?MODULE, stop). | ||
|
||
init(_Params) -> | ||
{ok, Sock} = gen_udp:open(?SERVER_PORT), | ||
io:format("Init Sock ~p~n", [Sock]), | ||
{ok, {server, Sock}}. | ||
|
||
terminate(Reason, {server, Sock}) -> | ||
gen_udp:close(Sock). | ||
|
||
handle_cast(stop, {server, Sock}) -> | ||
{stop, normal, {server, Sock}}. | ||
|
||
handle_info({udp, Client, _Ip, _Port, Msg}, LoopData) -> | ||
io:format("receive udp data ~p from ~p~n", [Msg, Client]), | ||
{noreply, LoopData}; | ||
|
||
handle_info(Msg, LoopData) -> | ||
io:format("receive info ~p~n", [Msg]), | ||
{noreply, LoopData}. | ||
|
||
send(Msg) -> | ||
gen_server:call(?MODULE, {message, Msg}). | ||
|
||
handle_call({message, Msg}, _From, {server, Sock}) -> | ||
{ok, Client} = gen_udp:open(?CLIENT_PORT), | ||
io:format("Sock ~p Client ~p~n", [Sock, Client]), | ||
gen_udp:send(Client, {127,0,0,1}, ?SERVER_PORT, Msg), | ||
gen_udp:close(Client), | ||
{reply, ok, {server, Sock}}. |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-module(ping). | ||
-export([start/0, send/1, loop/0]). | ||
|
||
start() -> spawn_link(ping, loop, []). | ||
|
||
send(Pid) -> | ||
Pid ! {self(), ping}, | ||
receive pong -> pong end. | ||
|
||
loop() -> | ||
receive | ||
{Pid, ping} -> | ||
spawn(crash, do_not_exist, []), | ||
Pid ! pong, | ||
loop() | ||
end. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-module(code_lock). | ||
-behaviour(gen_fsm). | ||
|
||
-export([start_link/1]). | ||
-export([button/1]). | ||
-export([init/1, locked/2, open/2]). | ||
|
||
% client function | ||
% | ||
start_link(Code) -> | ||
gen_fsm:start({local, ?MODULE}, code_lock, Code, []). | ||
|
||
button(Digit) -> | ||
gen_fsm:send_event(?MODULE, {button, Digit}). | ||
|
||
% callback function | ||
% | ||
init(Code) -> | ||
io:format("new code lock ~p ~n", [Code]), | ||
{ok, locked, {[], Code}}. | ||
|
||
locked({button, Digit}, {SoFar, Code}) -> | ||
case [Digit|SoFar] of | ||
Code -> | ||
% do_unlock(), | ||
io:format("do unlock"), | ||
{next_state, open, {[], Code}, 3000}; | ||
Incomplete when length(Incomplete)<length(Code) -> | ||
{next_state, locked, {Incomplete, Code}}; | ||
_Wrong -> | ||
{next_state, locked, {[], Code}} | ||
end. | ||
|
||
open(timeout, State) -> | ||
io:format("do lock"), | ||
% do_lock(), | ||
{next_state, locked, State}. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-module(door). | ||
-export([start/1, init/1]). | ||
|
||
start(Codes) -> | ||
spawn(fun() -> door:init(Codes) end). | ||
|
||
init(Codes) -> | ||
init_lock(Codes), | ||
loop(). | ||
|
||
init_lock([]) -> | ||
ok; | ||
|
||
init_lock([Code|Codes]) -> | ||
Res = code_lock:start_link(Code), | ||
io:format("Result is ~p~n", [Res]), | ||
init_lock(Codes). | ||
|
||
loop() -> | ||
receive | ||
ok -> | ||
{ok}, | ||
loop() | ||
end. |
This file contains 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
Oops, something went wrong.