Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first really simple implementation of handling udp protocol

  • Loading branch information...
commit caa051527e2acf45b6818ec2645deac5c62bf720 1 parent a69f841
@hukl authored
View
4 apps/bitturret/src/bitturret.app.src
@@ -9,5 +9,7 @@
sasl
]},
{mod, {bitturret_app, []}},
- {env, []}
+ {env, [
+ {port, 8080}
+ ]}
]}.
View
62 apps/bitturret/src/bitturret_handler.erl
@@ -0,0 +1,62 @@
+-module(bitturret_handler).
+-behavior(gen_server).
+
+% Managment API
+-export([start/0, start_link/0, stop/0]).
+
+% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
+ code_change/3]).
+
+% Public API
+-export([]).
+
+-record(state, { socket }).
+
+%% ===================================================================
+%% Management API
+%% ===================================================================
+
+start() ->
+ gen_server:start( {local, ?MODULE}, ?MODULE, [], []).
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+stop() -> gen_server:cast(?MODULE, stop).
+
+%% ===================================================================
+%% gen_server callbacks
+%% ===================================================================
+
+init([]) ->
+ {ok, Port} = application:get_env(port),
+ {ok, Socket} = gen_udp:open(Port, [binary, {active, once}]),
+ { ok, #state{ socket = Socket } }.
+
+handle_call( _Msg, _From, State ) ->
+ { reply, ok, State }.
+
+handle_cast( stop, State ) ->
+ { stop, normal, State };
+
+handle_cast( _Msg, State ) ->
+ { stop, normal, State }.
+
+code_change(_OldVsn, State, _Extra) ->
+ { ok, State }.
+
+handle_info( {udp, Socket, IP, Port, Msg}, State ) ->
+ bitturret_worker:handle({Socket, IP, Port}, Msg),
+ inet:setopts(State#state.socket, [{active, once}]),
+ { noreply, State }.
+
+terminate( _Reason, _State ) ->
+ whatever.
+
+%% ===================================================================
+%% Public API
+%% ===================================================================
+
+
+
View
2  apps/bitturret/src/bitturret_sup.erl
@@ -23,7 +23,7 @@ start_link() ->
%% ===================================================================
init([]) ->
- Children = [],
+ Children = [?CHILD(bitturret_handler, worker)],
RestartStrategy = {one_for_one, 0, 1},
View
40 apps/bitturret/src/bitturret_worker.erl
@@ -0,0 +1,40 @@
+-module(bitturret_worker).
+
+-export([handle/2]).
+
+
+handle(From, <<ConnectionID:64/big, 0:32/big, TransactionID:32/big>>) ->
+ error_logger:info_msg("Connection Request"),
+ error_logger:info_msg("C: ~p A: ~p T: ~p~n", [ConnectionID, 0, TransactionID]),
+ Response = <<0:32/big, TransactionID:32/big, ConnectionID:64/big>>,
+ send_response( From, Response );
+
+handle(From,
+ <<
+ ConnectionID:64/big,
+ Action:32/big,
+ TransactionID:32/big,
+ InfoHash:160/bitstring,
+ PeerID:160/bitstring,
+ Downloaded:64/big,
+ Left:64/big,
+ Uploaded:64/big,
+ Event:32/big,
+ IPaddress:32/big,
+ Key:32/big,
+ NumWait:32/big,
+ Port:16/big
+ >>) ->
+
+ error_logger:info_msg("Announce Request"),
+ error_logger:info_msg("Info Hash: ~p~n", [InfoHash]),
+
+ Peers = << <<IP:32/big,Port:16/big>> || {IP,Port} <- [{3261057657, 51413}] >>,
+ Response = <<1:32/big, TransactionID:32/big, 60:32/big, 10:32/big, 5:32/big, Peers/binary>>,
+ send_response(From, Response);
+
+handle(From, Msg) ->
+ error_logger:info_msg("Something else: ~p~n", [Msg]).
+
+send_response({Socket, IP, Port}, Response) ->
+ gen_udp:send(Socket, IP, Port, Response).
Please sign in to comment.
Something went wrong with that request. Please try again.