Permalink
Browse files

* Simplify pipeline mechanics

* Resolver module API update
* Implement 'localonly' option for resolvers (fixes #3)
* Implement 'preference' sorting for results (fixes #7)
  • Loading branch information...
1 parent 7cb4c87 commit 4f0f543e9918f0d86ed4c714965990989a03dc7f @RJ committed Oct 29, 2009
@@ -7,7 +7,7 @@
-behaviour(playdar_resolver).
%% API
--export([start_link/0, resolve/2, weight/1, targettime/1, name/1]).
+-export([start_link/0, resolve/2, weight/1, targettime/1, name/1, localonly/1]).
-export([ stats/1, parse/2, check_index/2 ]).
%% gen_server callbacks
@@ -24,13 +24,14 @@ resolve(Pid, Qry) -> gen_server:cast(Pid, {resolve, Qry}).
weight(_Pid) -> 100.
targettime(_Pid) -> 20.
name(_Pid) -> "Magnatune index".
+localonly(_Pid) -> false.
stats(Pid) -> gen_server:call(Pid, stats).
%% --------------------------------------------------------------------
init([]) ->
{ok, LibPid} = gen_server:start_link(library_dets, ["magnatune"], []),
- resolver:add_resolver(?MODULE, name(self()), weight(self()), targettime(self()), self()),
+ resolver:add_resolver(?MODULE, self()),
?LOG(info, "Magnatune library pid: ~p",[LibPid]),
Mpid = self(),
spawn(fun()->check_index(Mpid, LibPid)end),
View
@@ -1,9 +1,10 @@
-%TODO should probably have a log server that buffers and writes to a log file..
+% include this in all your playdar related code
% logging macros, eg: ?L(info, "something is: ~s", [S]).
--define(LOG(Log_Level, Log_Format, Log_Args), playdar_logger:do_log(?MODULE, Log_Level, Log_Format, Log_Args)).
+-define(LOG(Level, Format, Args), playdar_logger:do_log(?MODULE, Level, Format, Args)).
--define(CONFVAL(ConfVal_K,ConfVal_Def), playdar_config:confval(ConfVal_K,ConfVal_Def)).
+% lookup config value from *.conf files in playdar's etc dir
+-define(CONFVAL(K,Def), playdar_config:confval(K,Def)).
% use by playdar_ctl and ctl commands:
-define(OK, 0).
@@ -14,6 +15,6 @@
% project-wide struct
-record(qry, {qid, % query id
- obj, % json obj
+ obj, % json obj, mochijson2 format
local % true if local user created it, false if via lan etc
}).
@@ -6,7 +6,7 @@
-behaviour(playdar_resolver).
%% API
--export([start_link/0, resolve/2, weight/1, targettime/1, name/1]).
+-export([start_link/0, resolve/2, weight/1, targettime/1, name/1, localonly/1]).
-export([http_req/2]).
%% gen_server callbacks
@@ -20,6 +20,7 @@ resolve(_Pid, _Qry) -> ok.
weight(_Pid) -> 0.
targettime(_Pid) -> 0.
name(_Pid) -> "audioscrobbler".
+localonly(_Pid) -> true.
%%%%
@@ -4,7 +4,7 @@
-behaviour(playdar_resolver).
%% API
--export([start_link/0, resolve/2, weight/1, targettime/1, name/1]).
+-export([start_link/0, resolve/2, weight/1, targettime/1, name/1, localonly/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -18,10 +18,11 @@ resolve(Pid, Qry) -> gen_server:cast(Pid, {resolve, Qry}).
weight(_Pid) -> 50.
targettime(_Pid) -> 25.
name(_Pid) -> "Fake Mokele Resolver".
+localonly(_Pid) -> true.
%% gen_server callbacks
init([]) ->
- resolver:add_resolver(?MODULE, name(self()), weight(self()), targettime(self()), self()),
+ resolver:add_resolver(?MODULE, self()),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
@@ -1,14 +1,14 @@
%% Uses UDP multicast to resolve against other instances of this code
%% running elsewhere on the LAN. Streams via HTTP.
-
-module(lan_resolver).
-
-include("playdar.hrl").
-behaviour(gen_server).
-behaviour(playdar_resolver).
%% API
--export([start_link/0, resolve/2, weight/1, targettime/1, name/1, send_response/5]).
+-export([start_link/0, resolve/2, weight/1, targettime/1, name/1, localonly/1]).
+
+-export([send_response/5]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -25,6 +25,7 @@ resolve(Pid, Qry) -> gen_server:cast(Pid, {resolve, Qry}).
weight(_Pid) -> 95.
targettime(_Pid) -> 50.
name(_Pid) -> "Lan".
+localonly(_Pid) -> false.
%% Used as a callback when qry results arrive:
send_response(Pid, A, Qid, Ip, Port) ->
@@ -45,7 +46,7 @@ init([]) ->
% Normal socket, which direct replies are sent to:
{ok, SockP}= gen_udp:open(Port, [binary,
{reuseaddr, true}, {ip, {0,0,0,0}}]),
- resolver:add_resolver(?MODULE, name(self()), weight(self()), targettime(self()), self()),
+ resolver:add_resolver(?MODULE, self()),
{ok, #state{sock=Sock,
sockp=SockP,
seenqids=SQ,
@@ -5,7 +5,8 @@
-behaviour(playdar_resolver).
%% API
--export([start_link/0, resolve/2, weight/1, targettime/1, name/1, dump_library/1]).
+-export([start_link/0, resolve/2, weight/1, targettime/1, name/1, localonly/1]).
+-export([dump_library/1]).
-export([scan/2, stats/1, add_file/5, sync/1 ]).
%% gen_server callbacks
@@ -26,6 +27,7 @@ resolve(Pid, Qry) -> gen_server:cast(Pid, {resolve, Qry}).
weight(_Pid) -> 100.
targettime(_Pid) -> 20.
name(_Pid) -> "Local Library using DETS".
+localonly(_Pid) -> false.
stats(Pid) -> gen_server:call(Pid, stats).
@@ -50,7 +52,7 @@ init([]) ->
[proplists:get_value(size, dets:info(Fdb), -1)]),
% start the scanner (kind of a hack, but deadlock if we do it in init here):
self() ! start_scanner,
- resolver:add_resolver(?MODULE, name(self()), weight(self()), targettime(self()), self()),
+ resolver:add_resolver(?MODULE, self()),
{ok, #state{scanner=undefined, ndb=Ndb, fdb=Fdb, customname=""}}.
handle_cast(sync, State) ->
@@ -7,8 +7,9 @@
%% playdar_reader exports:
-export([reader_protocols/0]).
--export([start_link/0, resolve/2, weight/1, targettime/1, name/1,
- playdartcp_reader/3, reader_start_link/3]).
+-export([start_link/0, resolve/2, weight/1, targettime/1, name/1, localonly/1]).
+
+-export([playdartcp_reader/3, reader_start_link/3]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -22,6 +23,7 @@ resolve(_Pid, Qry) -> gen_server:cast(playdartcp_router, {resolve, Qry}).
weight(_Pid) -> 60.
targettime(_Pid) -> 1000.
name(_Pid) -> "playdartcp".
+localonly(_Pid) -> false.
reader_start_link(A, Pid, Ref) -> spawn_link( ?MODULE, playdartcp_reader, [A, Pid, Ref]).
@@ -36,7 +38,7 @@ init([]) ->
% Connect to any peers listed in the config file:
lists:foreach(fun({Ip,Port})->playdartcp_router:connect(Ip,Port)end, ?CONFVAL({playdartcp,peers},[])),
% Register us as a playdar resolver:
- resolver:add_resolver(?MODULE, name(self()), weight(self()), targettime(self()), self()),
+ resolver:add_resolver(?MODULE, self()),
% Register our web request handler (for our localhost web gui)
http_registry:register_handler("playdartcp", fun playdartcp_web:http_req/2, "playdartcp Connection Status Page", "/playdartcp"),
% register our ctl cmd:
View
@@ -6,13 +6,15 @@
<td>Module</td>
<td>Weight</td>
<td>Target Time</td>
+<td>Localonly</td>
</tr>
{% for r in resolvers %}
<tr style="background-color: {% cycle white,lightyellow %}">
<td>{{ r.name }}</td>
<td>{{ r.mod }} </td>
<td>{{ r.weight }}</td>
<td>{{ r.targettime }}</td>
+<td>{{ r.localonly }}</td>
</tr>
{% endfor %}
</table>
View
@@ -49,7 +49,15 @@ loop1(Req, DocRoot) ->
Resolvers = [ [{"mod", atom_to_list(proplists:get_value(mod, Pl))}|proplists:delete(mod,Pl)]
|| Pl <- resolver:resolvers() ],
HttpMenus = http_registry:get_all(),
- Vars = [ {resolvers, Resolvers},
+ % change bools to strings, for rendering (yuk, TODO hack erlydtl)
+ Resolvers1 = [ begin
+ LOS = case proplists:get_value(localonly, Resolver, false) of
+ true -> "yes";
+ false -> "no"
+ end,
+ [ {localonly, LOS} | proplists:delete(localonly, Resolver) ]
+ end || Resolver <- Resolvers ],
+ Vars = [ {resolvers, Resolvers1},
{protocols, playdar_reader_registry:get_all()},
{http_paths, HttpMenus}
],
Oops, something went wrong.

0 comments on commit 4f0f543

Please sign in to comment.