Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

config object now holds config rather than app()

  • Loading branch information...
commit 7e37ba407095493888987573175a1c3a77f93466 1 parent 8d96302
@RJ authored
Showing with 207 additions and 419 deletions.
  1. +2 −3 playdar-daemon/etc/playdar.conf
  2. +0 −46 playdar-daemon/etc/playdar.ini
  3. +1 −0  playdar-daemon/plugins/README.txt
  4. +3 −109 playdar-daemon/src/application/application.cpp
  5. +14 −8 playdar-daemon/src/application/application.h
  6. +20 −5 playdar-daemon/src/application/config.hpp
  7. +3 −0  playdar-daemon/src/application/settings_test.cpp
  8. +29 −113 playdar-daemon/src/playdar/main.cpp
  9. +4 −2 playdar-daemon/src/playdar/playdar_request_handler.cpp
  10. +1 −1  playdar-daemon/src/playdar/playdar_request_handler.h
  11. +21 −16 playdar-daemon/src/resolvers/darknet/darknet.cpp
  12. +1 −1  playdar-daemon/src/resolvers/darknet/darknet.h
  13. +16 −5 playdar-daemon/src/resolvers/gateway_script/gateway_script.cpp
  14. +1 −1  playdar-daemon/src/resolvers/gateway_script/gateway_script.h
  15. +16 −14 playdar-daemon/src/resolvers/lan/lan_udp.cpp
  16. +1 −1  playdar-daemon/src/resolvers/lan/lan_udp.h
  17. +30 −11 playdar-daemon/src/resolvers/playable_item.cpp
  18. +31 −77 playdar-daemon/src/resolvers/resolver.cpp
  19. +7 −1 playdar-daemon/src/resolvers/resolver_service.h
  20. +4 −3 playdar-daemon/src/resolvers/rs_local_library.cpp
  21. +2 −2 playdar-daemon/src/resolvers/rs_local_library.h
View
5 playdar-daemon/etc/playdar.conf
@@ -12,9 +12,8 @@
},
"darknet" :
{
- "enabled" : "yes",
+ "enabled" : "no",
"port" : 9999
- },
- "blah" : "foo"
+ }
}
}
View
46 playdar-daemon/etc/playdar.ini
@@ -1,46 +0,0 @@
-#
-# Playdar Config
-#
-# $ ./playdar -c /path/to/playdar.ini
-#
-# You can override options on the command line using "--section.option value"
-#
-# $ ./playdar -c /path/to/playdar.ini --app.db ./test.db
-#
-###############################################################################
-# Main application settings, replace with your own values: #
-###############################################################################
-[app]
-# name used for your collection on the network:
-name="YOURNAMEHERE"
-
-# provide an absolute path here:
-db="collection.db"
-
-# remember to allow this through your firewall,
-# if you have one for your LAN interface:
-http_port=8888
-
-
-###############################################################################
-# Resolver-specific settings #
-###############################################################################
-
-# this one uses UDP multicast on the LAN
-# hint: this multicast address will probably work fine for you
-# so don't change it unless you know what you're doing
-[resolver.lan_udp]
-enabled="yes"
-multicast="239.255.0.1"
-port=8888
-
-# this one uses a remote playdar's HTTP API directly
-# hint: ssh tunnels are useful here, playdar is immature
-# and it's not advisable to expose it to the internet directly
-# eg: ssh -L 8889:localhost:8888 my-remote-machine
-# will make your localhost:8889 tunnel to playdar on your remote box
-[resolver.remote_http]
-enabled="no"
-ip="127.0.0.1"
-port=8889
-
View
1  playdar-daemon/plugins/README.txt
@@ -0,0 +1 @@
+"make install" from a plugin dir will put a .resolver file in here
View
112 playdar-daemon/src/application/application.cpp
@@ -14,11 +14,10 @@
using namespace std;
-MyApplication::MyApplication(boost::program_options::variables_map opt)
+MyApplication::MyApplication(playdar::Config c)
+ : m_config(c)
{
-
- m_po = opt;
- string db_path = option<string>("app.db");
+ string db_path = conf()->get<string>("db");
m_library = new Library( db_path, this );
m_resolver = new Resolver(this);
@@ -27,7 +26,6 @@ MyApplication::MyApplication(boost::program_options::variables_map opt)
m_work = boost::shared_ptr<boost::asio::io_service::work>
(new boost::asio::io_service::work(*m_ios));
- do_auto_config();
}
MyApplication::~MyApplication()
@@ -35,38 +33,7 @@ MyApplication::~MyApplication()
delete(m_library);
}
-void
-MyApplication::do_auto_config()
-{
- cout << "Autodetecting stuff..." << endl;
- string hostname = boost::asio::ip::host_name();
- cout << "Hostname: " << hostname << endl;
- boost::asio::ip::tcp::resolver resolver(*m_ios);
- boost::asio::ip::tcp::resolver::query
- query(hostname, "");
- boost::asio::ip::tcp::resolver::iterator iter =
- resolver.resolve(query);
- boost::asio::ip::tcp::resolver::iterator end_marker;
- boost::asio::ip::tcp::endpoint ep;
- boost::asio::ip::address_v4 ipaddr;
- while (iter != end_marker)
- {
- ep = *iter++;
- cout << "Found address: " << ep.address().to_string() << endl;
- }
- /*
- ep.port(m_port);
- }
- m_socket = boost::shared_ptr<boost::asio::ip::tcp::socket>(new tcp::socket(m_io_service));
-
- boost::system::error_code error = boost::asio::error::host_not_found;
- m_socket->connect(ep, error);
- if (error) throw boost::system::system_error(error);
-*/
-
-
-}
string
MyApplication::gen_uuid()
@@ -101,78 +68,6 @@ MyApplication::gen_uuid()
delete(uuid_str);
return retval;
}
-
-
-
-template <typename T> T
-MyApplication::option(string o, T def)
-{
- if(m_po.count(o)==0)
- {
- cerr << "Option '"<< o <<"' not set, using default: " << def << endl;
- return def;
- }
- return m_po[o].as<T>();
-}
-
-template <typename T> T
-MyApplication::option(string o)
-{
- if(m_po.count(o)==0)
- {
- cerr << "WARNING: Option '"<< o <<"' not set! creating empty value" << endl;
- T def;
- return def;
- }
- return m_po[o].as<T>();
-}
-
-
-std::string
-MyApplication::name()
-{
- return option<string>("app.name");
-}
-
-unsigned short
-MyApplication::http_port()
-{
- return (unsigned short)(option<int>("app.http_port"));
-}
-
-unsigned short
-MyApplication::multicast_port()
-{
- return (unsigned short)(option<int>("resolver.lan_udp.port"));
-}
-
-boost::asio::ip::address_v4
-MyApplication::private_ip()
-{
- //return boost::asio::ip::address_v4::from_string( option<string>("app.private_ip") );
- return boost::asio::ip::address_v4::from_string("127.0.0.1");
-}
-
-boost::asio::ip::address_v4
-MyApplication::public_ip()
-{
- return private_ip();
-}
-
-boost::asio::ip::address_v4
-MyApplication::multicast_ip()
-{
- return boost::asio::ip::address_v4::from_string( option<string>("resolver.lan_udp.multicast") );
-}
-
-// get base http url on private network, no trailing slash
-string
-MyApplication::httpbase()
-{
- ostringstream s;
- s << "http://" << private_ip().to_string() << ":" << http_port();
- return s.str();
-}
Library *
MyApplication::library()
@@ -186,7 +81,6 @@ MyApplication::resolver()
return m_resolver;
}
-
int
MyApplication::levenshtein(const std::string & source, const std::string & target) {
View
22 playdar-daemon/src/application/application.h
@@ -6,6 +6,7 @@
#include <boost/asio.hpp>
#include "application/types.h"
+#include "application/config.hpp"
#include <boost/thread.hpp>
#include <boost/program_options.hpp>
@@ -25,8 +26,9 @@ class Resolver;
class MyApplication
{
public:
- MyApplication(boost::program_options::variables_map opt);
+ MyApplication(playdar::Config c);
~MyApplication();
+ /*
std::string name();
unsigned short http_port();
unsigned short multicast_port();
@@ -34,14 +36,19 @@ class MyApplication
boost::asio::ip::address_v4 public_ip();
boost::asio::ip::address_v4 multicast_ip();
string httpbase();
+ */
Library * library();
Resolver * resolver();
- boost::program_options::variables_map popt(){ return m_po ; }
+ //boost::program_options::variables_map popt(){ return m_po ; }
static string gen_uuid();
- template <typename T> T option(string o, T def);
- template <typename T> T option(string o);
-
+ //template <typename T> T option(string o, T def);
+ //template <typename T> T option(string o);
+
+ playdar::Config * conf()
+ {
+ return &m_config;
+ }
// RANDOM UTILITY FUNCTIONS TOSSED IN HERE FOR NOW:
@@ -58,15 +65,14 @@ class MyApplication
private:
- void do_auto_config();
boost::shared_ptr<boost::asio::io_service::work> m_work;
boost::shared_ptr<boost::asio::io_service> m_ios;
+ playdar::Config m_config;
Library * m_library;
Resolver * m_resolver;
- boost::program_options::variables_map m_po;
-
+
};
View
25 playdar-daemon/src/application/config.hpp
@@ -51,24 +51,31 @@ class Config
// get a value from json object
// or value from nested *objects* by using a key of first.second.third
template <typename T>
- T get(string k, T def)
+ T get(string k) const
+ {
+ T def;
+ return get<T>(k,def);
+ }
+
+ template <typename T>
+ T get(string k, T def) const
{
std::vector<std::string> toks;
boost::split(toks, k, boost::is_any_of("."));
Value val = m_mainval;
map<string,Value> mp;
- int i = 0;
- cout << "getting: " << k << " size: " << toks.size() << endl;
+ unsigned int i = 0;
+ //cout << "getting: " << k << " size: " << toks.size() << endl;
do
{
if(val.type() != obj_type) return def;
obj_to_map(val.get_obj(), mp);
if( mp.find(toks[i]) == mp.end() )
{
- cerr << "1 Can't find " << toks[i] << endl;
+ //cerr << "1 Can't find " << toks[i] << endl;
return def;
}
- cout << "Got " << toks[i] << endl;
+ //cout << "Got " << toks[i] << endl;
val = mp[toks[i]];
}
while(++i < toks.size());
@@ -76,6 +83,14 @@ class Config
return val.get_value<T>();
}
+ string httpbase()
+ {
+ ostringstream s;
+ s << "http://127.0.0.1" << ":" << get<int>("http_port", 8888);
+ return s.str();
+ }
+
+ // NOT WORKING YET
template <typename T>
bool set(string k, T def)
{
View
3  playdar-daemon/src/application/settings_test.cpp
@@ -2,10 +2,13 @@
#include <stdio.h>
#include <iostream>
#include "config.hpp"
+#include <boost/filesystem.hpp>
using namespace std;
int main (int argc, char** argv)
{
+ cout << boost::filesystem::initial_path().string() << endl;
+
playdar::Config c(argv[1]);
cout << c.str() <<endl;
cout << c.get<string>("name","DEF")
View
142 playdar-daemon/src/playdar/main.cpp
@@ -28,8 +28,6 @@ ostream& operator<<(ostream& os, const vector<T>& v)
void start_http_server(string ip, int port, int conc, MyApplication *app)
{
cout << "HTTP server starting on: http://" << ip << ":" << port << "/" << endl;
- // won't work with >1 worker atm, because library db code isn't
- // yet threadsafe, amongst other things.
moost::http::server<playdar_request_handler> s(ip, port, 3);
s.request_handler().init(app);
s.run();
@@ -40,9 +38,6 @@ int main(int ac, char *av[])
{
try {
int opt;
-
- // Declare a group of options that will be
- // allowed only on command line
po::options_description generic("Generic options");
generic.add_options()
("config,c", po::value<string>(),
@@ -50,143 +45,64 @@ int main(int ac, char *av[])
("version,v", "print version string")
("help,h", "print this message")
;
- // Declare a group of options that will be
- // allowed both on command line and in
- // config file
- po::options_description config("App config");
- config.add_options()
- ("app.name", po::value<string>(),
+/*
+ ("name", po::value<string>(),
"Name used for your collection on the network")
- ("app.db", po::value<string>(),
+ ("db", po::value<string>(),
"Path to your database file")
- ("app.http_port", po::value<int>(&opt)->default_value(8888),
+ ("http_port", po::value<int>(&opt)->default_value(8888),
"Port used for local webserver")
;
-
- /*
- ("app.private_ip", po::value<string>()->default_value("127.0.0.1"),
- "For LAN resolving - set this to your 192.168.x or 10.x address")
- ;
- */
- // options to configure various resolvers
- po::options_description resolver("Resolver Options");
- resolver.add_options()
- ("resolver.remote_http.enabled", po::value<string>()->default_value("no"),
- "Enable this resolver yes/no")
- ("resolver.remote_http.ip", po::value<string>(),
- "IP to a remote Playdar install")
- ("resolver.remote_http.port", po::value<int>(&opt),
- "Port to a remote Playdar install (HTTP API)")
-
- ("resolver.lan_udp.enabled", po::value<string>()->default_value("no"),
- "Enable this resolver yes/no")
- ("resolver.lan_udp.multicast", po::value<string>()->default_value("239.255.0.1"),
- "Multicast IP")
- ("resolver.lan_udp.port", po::value<int>(&opt)->default_value(8888),
- "UDP port to bind to")
-
- ("resolver.darknet.enabled", po::value<string>()->default_value("no"),
- "Enable this resolver yes/no")
- ("resolver.darknet.port", po::value<int>(&opt)->default_value(9999),
- "TCP port for darknet resolver")
- ("resolver.darknet.remote_ip", po::value<string>()->default_value(""),
- "Remote peer IP address")
- ("resolver.darknet.remote_port", po::value<int>(&opt)->default_value(9999),
- "Remote peer port number")
- ;
-
+*/
po::options_description cmdline_options;
- cmdline_options.add(generic).add(config).add(resolver);
-
- po::options_description config_file_options;
- config_file_options.add(config).add(resolver);
+ cmdline_options.add(generic);
po::options_description visible("Playdar configuration");
- visible.add(generic).add(config);
-/*
- po::positional_options_description p;
- p.add("input-file", -1);
-*/
+ visible.add(generic);
+
po::variables_map vm;
po::parsed_options parsedopts_cmd =
po::command_line_parser(ac, av).
- options(cmdline_options).allow_unregistered().run();
+ options(cmdline_options).run();
store(parsedopts_cmd, vm);
notify(vm);
-
-
- if(!vm.count("config"))
- {
- cerr << "You must use a config file." << endl;
- return 1;
- }
- string configfile = vm["config"].as<string>();
- cout << "Using config file: " << configfile << endl;
- ifstream ifs(configfile.c_str());
- if(!ifs.is_open())
- {
- cerr << "Could not open: " << configfile << endl;
- return 1;
- }
- po::parsed_options parsedopts_ini = po::parse_config_file(ifs, config_file_options, true/*allow unreg*/);
- store(parsedopts_ini , vm);
- // FIXME does allow unrecognied even work with config files? who knows.
- /*
- vector<string> unrec = collect_unrecognized(vm, po::exclude_positional);
- BOOST_FOREACH(string & s, unrec)
- {
- cout << "Unrec: " << s << endl;
- }
- */
- notify(vm);
-
-
- cout << "Parsed config options."<<endl;
-
+
if (vm.count("help")) {
cout << visible << "\n";
return 0;
}
if (vm.count("version")) {
- cout << "0.1\n";
+ cout << "TODO\n";
return 0;
}
- if( !vm.count("app.name") )
+
+ if(!vm.count("config"))
{
- cout << visible << endl;
+ cerr << "You must use a config file." << endl;
return 1;
}
- /*
- if( !vm.count("foo") )
+ string configfile = vm["config"].as<string>();
+ cout << "Using config file: " << configfile << endl;
+
+ playdar::Config conf(configfile);
+ if(conf.get<string>("name")=="YOURNAMEHERE")
{
- cout << "foo detected" << endl;
-
- vector<string> to_pass_further = collect_unrecognized(parsedopts_ini.options, po::exclude_positional);
-
- cout << "collected unrec: " << to_pass_further.size() << endl;
- BOOST_FOREACH(string & k, to_pass_further)
- {
- cout <<"opt: "<< k << endl;
- }
- //cout << "Foo: " << vm["foo"].as<string>() << endl;
- return 1;
+ cerr << "Please edit " << configfile << endl;
+ cerr << "YOURNAMEHERE is not a valid name." << endl;
+ return 42;
}
-
-*/
- MyApplication * app = new MyApplication(vm);
+ MyApplication * app = new MyApplication(conf);
// start http server:
- string ip = "0.0.0.0"; //app->private_ip().to_string();
-
- boost::thread http_thread(&start_http_server, ip, app->http_port(), 1, app);
-
- cout << endl
- << "Now check http://"<< app->private_ip().to_string() << ":"
- << app->http_port() <<"/" << endl
- << "Or see a live demo on http://www.playdar.org/" << endl << endl;
+ string ip = "0.0.0.0";
+ boost::thread http_thread(&start_http_server, ip,
+ app->conf()->get<int>("http_port"),
+ 1,
+ app);
+
// Lame interactive mode for debugging:
/*
RS_darknet * dnet = static_cast<RS_darknet *>( app->resolver()->get_darknet());
View
6 playdar-daemon/src/playdar/playdar_request_handler.cpp
@@ -68,7 +68,8 @@ playdar_request_handler::handle_request(const moost::http::request& req, moost::
if(parts[1] == "") // Req: /
{
- serve_body("<h1>Playdar: " + app()->name() + "</h1><a href='/stats'>Stats</a><hr/>For quick and dirty resolving, you can try constructing an URL like: <code>"+app()->httpbase()+"/quickplay/ARTIST/ALBUM/TRACK</code><br/><br/>For the real demo that uses the JSON API, check <a href=\"http://www.playdar.org/\">Playdar.org</a>.", req, rep);
+ serve_body("<h1>Playdar: " + app()->conf()->get<string>("name")
+ + "</h1><a href='/stats'>Stats</a><hr/>For quick and dirty resolving, you can try constructing an URL like: <code>"+app()->conf()->httpbase()+"/quickplay/ARTIST/ALBUM/TRACK</code><br/><br/>For the real demo that uses the JSON API, check <a href=\"http://www.playdar.org/\">Playdar.org</a>.", req, rep);
}
else if(parts[1]=="static")
{
@@ -307,7 +308,8 @@ void
playdar_request_handler::serve_stats(const moost::http::request& req, moost::http::reply& rep)
{
std::ostringstream reply;
- reply << "<h1>Stats: " << app()->name() << "</h1>"
+ reply << "<h1>Stats: " << app()->conf()->get<string>("name")
+ << "</h1>"
<< "<h2>Local Library</h2>"
<< "<table>"
<< "<tr><td>Num Files</td><td>" << app()->library()->num_files() << "</td></tr>\n"
View
2  playdar-daemon/src/playdar/playdar_request_handler.h
@@ -39,7 +39,7 @@ class playdar_request_handler : public moost::http::request_handler_base<playdar
string sid_to_url(source_uid sid)
{
- string u = app()->httpbase();
+ string u = app()->conf()->httpbase();
u += "/sid/" + sid;
return u;
}
View
37 playdar-daemon/src/resolvers/darknet/darknet.cpp
@@ -20,30 +20,35 @@
using namespace playdar::resolvers;
void
-darknet::init(MyApplication * a)
+darknet::init(playdar::Config * c, MyApplication * a)
{
- m_app = a;
- unsigned short port = app()->popt()["resolver.darknet.port"].as<int>();
- m_io_service = boost::shared_ptr<boost::asio::io_service>(new boost::asio::io_service);
- m_work = boost::shared_ptr<boost::asio::io_service::work>(new boost::asio::io_service::work(*m_io_service));
- m_servent = boost::shared_ptr< Servent >(new Servent(*m_io_service, port, this));
+ m_app = a;
+ m_conf = c;
+ unsigned short port = conf()->get<int>("plugins.darknet.port",9999);
+ m_io_service = boost::shared_ptr<boost::asio::io_service>
+ (new boost::asio::io_service);
+ m_work = boost::shared_ptr<boost::asio::io_service::work>
+ (new boost::asio::io_service::work(*m_io_service));
+ m_servent = boost::shared_ptr< Servent >
+ (new Servent(*m_io_service, port, this));
// start io_services:
cout << "Darknet servent coming online on port " << port <<endl;
- boost::thread_group threads;
- for (std::size_t i = 0; i < 10; ++i)
+ boost::thread_group threads; // TODO configurable threads?
+ for (std::size_t i = 0; i < 5; ++i)
{
threads.create_thread(boost::bind(
&boost::asio::io_service::run, m_io_service.get()));
}
- //boost::thread thr(boost::bind(&darknet::start_io, this, m_io_service));
- // get peers:
- if(app()->popt()["resolver.darknet.remote_ip"].as<string>().length())
+ // get peers: TODO support multiple/list from config
+ string remote_ip = conf()->get<string>("plugins.darknet.peerip","");
+ if(remote_ip!="")
{
- string remote_ip = app()->popt()["resolver.darknet.remote_ip"].as<string>();
- unsigned short remote_port = app()->popt()["resolver.darknet.remote_port"].as<int>();
- cout << "Attempting peer connect: " << remote_ip << ":" << remote_port << endl;
+ unsigned short remote_port = conf()->get<int>
+ ("plugins.darknet.peerport",9999);
+ cout << "Attempting peer connect: "
+ << remote_ip << ":" << remote_port << endl;
boost::asio::ip::address_v4 ipaddr = boost::asio::ip::address_v4::from_string(remote_ip);
boost::asio::ip::tcp::endpoint ep(ipaddr, remote_port);
m_servent->connect_to_remote(ep);
@@ -67,7 +72,7 @@ bool
darknet::new_incoming_connection( connection_ptr conn )
{
// Send welcome message, containing our identity
- msg_ptr lm(new LameMsg(app()->name(), WELCOME));
+ msg_ptr lm(new LameMsg(conf()->get<string>("name"), WELCOME));
send_msg(conn, lm);
return true;
}
@@ -82,7 +87,7 @@ darknet::new_outgoing_connection( connection_ptr conn, boost::asio::ip::tcp::end
void
darknet::send_identify(connection_ptr conn )
{
- msg_ptr lm(new LameMsg(app()->name(), IDENTIFY));
+ msg_ptr lm(new LameMsg(conf()->get<string>("name"), IDENTIFY));
send_msg(conn, lm);
}
View
2  playdar-daemon/src/resolvers/darknet/darknet.h
@@ -27,7 +27,7 @@ class darknet : public ResolverService
public:
darknet(){};
- void init(MyApplication * a);
+ void init(playdar::Config * c, MyApplication * a);
void start_resolving(boost::shared_ptr<ResolverQuery> rq);
std::string name() { return "Darknet"; }
View
21 playdar-daemon/src/resolvers/gateway_script/gateway_script.cpp
@@ -7,13 +7,24 @@ namespace playdar {
namespace resolvers {
void
-gateway_script::init(MyApplication * a)
+gateway_script::init(playdar::Config * c, MyApplication * a)
{
- m_app = a;
+ m_app = a;
+ m_conf = c;
m_dead = false;
- m_scriptpath = "./etc/demo-resolver.php";
- cout << "HTTP Gateway script starting: "<<m_scriptpath << endl;
- init_worker();
+ m_scriptpath = conf()->get<string>
+ ("plugins.gateway_script.path","");
+ if(m_scriptpath=="")
+ {
+ cout << "No script path specified. gateway plugin failed."
+ << endl;
+ m_dead = true;
+ }
+ else
+ {
+ cout << "HTTP Gateway script starting: "<<m_scriptpath << endl;
+ init_worker();
+ }
}
void
View
2  playdar-daemon/src/resolvers/gateway_script/gateway_script.h
@@ -21,7 +21,7 @@ class gateway_script : public ResolverService
public:
gateway_script(){}
- void init(MyApplication * a);
+ void init(playdar::Config * c, MyApplication * a);
void start_resolving(boost::shared_ptr<ResolverQuery> rq);
std::string name() { return string("Gateway script: ")+m_scriptpath; }
View
30 playdar-daemon/src/resolvers/lan/lan_udp.cpp
@@ -11,11 +11,15 @@ namespace playdar {
namespace resolvers {
void
-lan_udp::init(MyApplication * a)
+lan_udp::init(playdar::Config * c, MyApplication * a)
{
- m_app = a;
- broadcast_endpoint_ = new boost::asio::ip::udp::endpoint
- (app()->multicast_ip(), app()->multicast_port());
+ m_app = a;
+ m_conf = c;
+ broadcast_endpoint_ =
+ new boost::asio::ip::udp::endpoint
+ ( boost::asio::ip::address::from_string
+ (conf()->get<string> ("plugins.lan_udp.multicast")),
+ conf()->get<int>("plugins.lan_udp.port"));
boost::thread m_responder_thread(&lan_udp::run, this);
}
@@ -30,7 +34,7 @@ lan_udp::start_resolving(boost::shared_ptr<ResolverQuery> rq)
{
using namespace json_spirit;
Object jq;
- jq.push_back( Pair("from_name", app()->name()) );
+ jq.push_back( Pair("from_name", conf()->get<string>("name")) );
jq.push_back( Pair("query", rq->get_json()) );
ostringstream querystr;
write_formatted( jq, querystr );
@@ -43,8 +47,9 @@ lan_udp::run()
boost::asio::io_service io_service;
start_listening(io_service,
boost::asio::ip::address::from_string("0.0.0.0"),
- app()->multicast_ip(),
- app()->multicast_port());
+ boost::asio::ip::address::from_string
+ (conf()->get<string>("plugins.lan_udp.multicast")),
+ conf()->get<int>("plugins.lan_udp.port"));
cout << "DL UDP Resolver is online udp://"
<< socket_->local_endpoint().address() << ":"
@@ -52,7 +57,7 @@ lan_udp::run()
<< endl;
// announce our presence to the LAN:
string hello = "OHAI ";
- hello += app()->name();
+ hello += conf()->get<string>("name");
async_send(broadcast_endpoint_, hello);
io_service.run();
@@ -134,13 +139,12 @@ lan_udp::handle_receive_from(const boost::system::error_code& error,
do
{
- if( sender_address.to_string() == "127.0.0.1" ||
- sender_address == app()->private_ip() ||
- sender_address == app()->public_ip() )
+ if( sender_address.to_string() == "127.0.0.1" )
{
// TODO detect our actual LAN IP and bail out here
// if it came from our IP.
- // Will bail anyway once parsed and dupe QID noticed.
+ // Will bail anyway once parsed and dupe QID noticed,
+ // but more efficient to do it here.
// cout << "* Ignoring udp msg from self" << endl;
break;
}
@@ -195,8 +199,6 @@ lan_udp::handle_receive_from(const boost::system::error_code& error,
if(pis.size()>0){
BOOST_FOREACH(boost::shared_ptr<PlayableItem> & pip, pis)
{
- //string url = app()->httpbase();
- //url += "/sid/" + pip->id();
Object response;
response.push_back( Pair("qid", qid) );
Object result = pip->get_json();
View
2  playdar-daemon/src/resolvers/lan/lan_udp.h
@@ -27,7 +27,7 @@ class lan_udp : public ResolverService
public:
lan_udp(){}
- void init(MyApplication * a);
+ void init(playdar::Config * c, MyApplication * a);
void run();
void start_resolving(boost::shared_ptr<ResolverQuery> rq);
std::string name() { return "LAN/UDP"; }
View
41 playdar-daemon/src/resolvers/playable_item.cpp
@@ -34,22 +34,41 @@ PlayableItem::from_json(json_spirit::Object resobj)
map<string,Value> resobj_map;
obj_to_map(resobj, resobj_map);
- if(resobj_map.find("artist")!=resobj_map.end()) artist = resobj_map["artist"].get_str();
- if(resobj_map.find("album")!=resobj_map.end()) album = resobj_map["album"].get_str();
- if(resobj_map.find("track")!=resobj_map.end()) track = resobj_map["track"].get_str();
- if(resobj_map.find("sid")!=resobj_map.end()) sid = resobj_map["sid"].get_str();
- if(resobj_map.find("source")!=resobj_map.end()) source = resobj_map["source"].get_str();
- if(resobj_map.find("mimetype")!=resobj_map.end()) mimetype= resobj_map["mimetype"].get_str();
+ if(resobj_map.find("artist")!=resobj_map.end())
+ artist = resobj_map["artist"].get_str();
+
+ if(resobj_map.find("album")!=resobj_map.end())
+ album = resobj_map["album"].get_str();
+
+ if(resobj_map.find("track")!=resobj_map.end())
+ track = resobj_map["track"].get_str();
+
+ if(resobj_map.find("sid")!=resobj_map.end())
+ sid = resobj_map["sid"].get_str();
+
+ if(resobj_map.find("source")!=resobj_map.end())
+ source = resobj_map["source"].get_str();
+
+ if(resobj_map.find("mimetype")!=resobj_map.end())
+ mimetype= resobj_map["mimetype"].get_str();
+
- if(resobj_map.find("size")!=resobj_map.end()) size = resobj_map["size"].get_int();
- if(resobj_map.find("bitrate")!=resobj_map.end()) bitrate = resobj_map["bitrate"].get_int();
- if(resobj_map.find("duration")!=resobj_map.end()) duration= resobj_map["duration"].get_int();
+ if(resobj_map.find("size")!=resobj_map.end())
+ size = resobj_map["size"].get_int();
+
+ if(resobj_map.find("bitrate")!=resobj_map.end())
+ bitrate = resobj_map["bitrate"].get_int();
+
+ if(resobj_map.find("duration")!=resobj_map.end())
+ duration= resobj_map["duration"].get_int();
- if(resobj_map.find("score")!=resobj_map.end()) score = (float)resobj_map["score"].get_real();
+ if(resobj_map.find("score")!=resobj_map.end())
+ score = (float)resobj_map["score"].get_real();
if(!artist.length() && !track.length()) throw;
- boost::shared_ptr<PlayableItem> pip( new PlayableItem(artist, album, track) );
+ boost::shared_ptr<PlayableItem>
+ pip( new PlayableItem(artist, album, track) );
if(sid.length()) pip->set_id(sid);
if(source.length()) pip->set_source(source);
View
108 playdar-daemon/src/resolvers/resolver.cpp
@@ -22,50 +22,16 @@
#include <boost/filesystem.hpp>
Resolver::Resolver(MyApplication * app)
+ :m_app(app)
{
- m_app = app;
m_id_counter = 0;
- cout << "Resolver started." << endl;
-
+ cout << "Resolver starting..." << endl;
+ // Initialize built-in local library resolver:
m_rs_local = 0;
-
-
m_rs_local = new RS_local_library();
- m_rs_local->init(app);
-
+ m_rs_local->init(m_app->conf(), app);
+ // Load all non built-in resolvers:
load_resolvers();
-
-/*
- //TODO dynamic loading at runtime using PDL or Boost.extension
- if(app->option<string>("resolver.remote_http.enabled")=="yes")
- {
- try
- {
- string rip = app->option<string>("resolver.remote_http.ip");
- unsigned short rport = (unsigned short) (app->option<int>("resolver.remote_http.port"));
- boost::asio::ip::address_v4 bip = boost::asio::ip::address_v4::from_string(rip);
- m_rs_http_playdar = new RS_http_playdar(app, bip, rport);
- }
- catch(exception e)
- {
- cerr << "Failed to load remote_http resolver: " << e.what() << endl;
- }
- }
-
- if(app->option<string>("resolver.lan_udp.enabled")=="yes")
- {
- m_rs_lan = new RS_lan_udp(app);
- }
- if(app->option<string>("resolver.darknet.enabled")=="yes")
- {
- m_rs_darknet = new RS_darknet(app);
- }
- */
- /*if(app->option<string>("resolver.gateway_http.enabled")=="yes")
- {
- m_rs_http_gateway_script = new RS_http_gateway_script(app);
- }*/
-
}
// dynamically load resolver plugins:
@@ -73,22 +39,41 @@ void
Resolver::load_resolvers()
{
namespace bfs = boost::filesystem;
- cout << "Loading resolver plugins from: ./plugins" << endl;
bfs::directory_iterator end_itr;
- bfs::path p("./plugins/");
+ //bfs::path appdir = bfs::initial_path();
+ //bfs::path p(appdir.string() +"/plugins/");
+ bfs::path p("plugins");
+ cout << "Loading resolver plugins from: "
+ << p.string() << endl;
for(bfs::directory_iterator itr( p ); itr != end_itr; ++itr)
{
if ( bfs::is_directory(itr->status()) ) continue;
string pluginfile = itr->string();
- if(bfs::extension(pluginfile)!=".resolver") continue;
+ if(bfs::extension(pluginfile)!=".resolver")
+ {
+ cerr << "Skipping '" << pluginfile
+ << "' from plugins directory" << endl;
+ continue;
+ }
string classname = bfs::basename(pluginfile);
+ string confopt = "resolvers.";
+ confopt += classname;
+ confopt += ".enabled";
+ if(app()->conf()->get<string>(confopt, "yes") == "no")
+ {
+ cout << "Skipping '" << classname
+ <<"' - disabled in config file." << endl;
+ continue;
+ }
try
{
- PDL::DynamicLoader & dynamicLoader = PDL::DynamicLoader::Instance();
+ PDL::DynamicLoader & dynamicLoader =
+ PDL::DynamicLoader::Instance();
cout << "-> Trying: " << pluginfile << endl;
- ResolverService * instance = dynamicLoader.GetClassInstance< ResolverService >
- ( pluginfile.c_str(), classname.c_str() );
- instance->init(app());
+ ResolverService * instance =
+ dynamicLoader.GetClassInstance< ResolverService >
+ ( pluginfile.c_str(), classname.c_str() );
+ instance->init(app()->conf(), app());
cout << "-> Loaded: " << instance->name() << endl;
m_resolvers.push_back(instance);
}
@@ -98,35 +83,6 @@ Resolver::load_resolvers()
}
}
cout << "Num Resolvers Loaded: " << m_resolvers.size() << endl;
-
-
-/*
- using namespace boost::extensions;
- string library_path = "./plugin.so";
- shared_library lib(library_path);
- // Attempt to open the shared library.
- if (!lib.open()) {
- std::cerr << "Library failed to open: " << library_path << std::endl;
- return;
- }
- type_map types;
- if (!lib.call(types)) {
- std::cerr << "Function not found!" << std::endl;
- return;
- }
- std::map<std::string, factory<ResolverService, MyApplication *> >& factories(types.get());
- if (factories.empty()) {
- std::cerr << "No resolvers found found!" << std::endl;
- return;
- }
- for (std::map<std::string, factory<ResolverService, MyApplication *> >::iterator it
- = factories.begin();
- it != factories.end(); ++it)
- {
- std::cout << "Loading: " << it->first << std::endl;
- boost::scoped_ptr<ResolverService> current_resolver(it->second.create(m_app));
- std::cout << "Created: " << current_resolver->name() << endl;
- }*/
}
// start resolving! (non-blocking)
@@ -224,8 +180,6 @@ Resolver::num_results(query_uid qid)
bool
Resolver::query_exists(const query_uid & qid)
{
- //boost::mutex::scoped_lock lock(m_mut);
- //map< query_uid, boost::shared_ptr<ResolverQuery> >::const_iterator it = m_queries.find(qid);
return (m_queries.find(qid) != m_queries.end());
}
View
8 playdar-daemon/src/resolvers/resolver_service.h
@@ -15,11 +15,16 @@ class ResolverService : public PDL::DynamicClass, std::exception
ResolverService(){}
- virtual void init(MyApplication * a)
+ virtual void init(playdar::Config * c, MyApplication * a)
{
m_app = a;
+ m_conf = c;
}
+ virtual const playdar::Config * conf() const
+ {
+ return m_conf;
+ }
virtual std::string name() = 0;
@@ -34,6 +39,7 @@ class ResolverService : public PDL::DynamicClass, std::exception
protected:
virtual ~ResolverService() throw() { }
+ playdar::Config * m_conf;
MyApplication * m_app;
};
#endif
View
7 playdar-daemon/src/resolvers/rs_local_library.cpp
@@ -11,9 +11,10 @@
void
-RS_local_library::init(MyApplication * a)
+RS_local_library::init(playdar::Config * c, MyApplication * a)
{
- m_app = a;
+ m_app = a;
+ m_conf = c;
cout << "Local library resolver: " << app()->library()->num_files() << " files indexed." << endl;
if(app()->library()->num_files() == 0)
{
@@ -177,7 +178,7 @@ RS_local_library::start_resolving(boost::shared_ptr<ResolverQuery> rq)
{
boost::shared_ptr<PlayableItem> pip = app()->library()->playable_item_from_fid(fid);
pip->set_score(finalscore);
- pip->set_source(app()->name());
+ pip->set_source(conf()->get<string>("name"));
final_results.push_back( pip );
}
}
View
4 playdar-daemon/src/resolvers/rs_local_library.h
@@ -7,13 +7,13 @@ class RS_local_library : public ResolverService
{
public:
RS_local_library(){}
- void init(MyApplication * a);
+ void init(playdar::Config * c, MyApplication * a);
// : ResolverService(a)
//{
// }
void start_resolving(boost::shared_ptr<ResolverQuery> rq);
- std::string name() { return string("Local Library on ")+app()->name(); }
+ std::string name() { return string("Local Library on ")+conf()->get<string>("name"); }
protected:
~RS_local_library() throw() {};
private:
Please sign in to comment.
Something went wrong with that request. Please try again.