Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
lazy creation of SS, re-enable script resolver
  • Loading branch information
RJ committed Apr 20, 2009
1 parent b7b9f4d commit f17044a
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 110 deletions.
4 changes: 2 additions & 2 deletions etc/playdar.conf
Expand Up @@ -2,8 +2,8 @@
"name" : "YOURNAMEHERE",
"db" : "collection.db",
"http_port" : 8888,
"load_plugins" : false,
"load_scripts" : false,
"load_plugins" : true,
"load_scripts" : true,

"plugins" : {
"lan" :
Expand Down
2 changes: 0 additions & 2 deletions includes/playdar/playable_item.hpp
Expand Up @@ -178,7 +178,6 @@ class PlayableItem : public ResolvedItem
void set_artist(std::string s) { m_artist = s; }
void set_album(std::string s) { m_album = s; }
void set_track(std::string s) { m_track = s; }
void set_url(std::string s) { m_url = s; }
void set_mimetype(std::string s) { m_mimetype = s; }
void set_duration(int s) { m_duration = s; }
void set_tracknum(int s) { m_tracknum = s; }
Expand All @@ -190,7 +189,6 @@ class PlayableItem : public ResolvedItem
const std::string & album() const { return m_album; }
const std::string & track() const { return m_track; }
const std::string & mimetype() const { return m_mimetype; }
const std::string & url() const { return m_url; }
const int duration() const { return m_duration; }
const int bitrate() const { return m_bitrate; }
const int tracknum() const { return m_tracknum; }
Expand Down
8 changes: 7 additions & 1 deletion includes/playdar/pluginadaptor.h
Expand Up @@ -29,7 +29,8 @@ class PluginAdaptor
template <typename T>
T get(const std::string& key, const T& def) const;

virtual bool report_results(const query_uid& qid, const std::vector< result_pair >&) = 0;
// results are a vector of json result objects
virtual bool report_results(const query_uid& qid, const std::vector< json_spirit::Object >&) = 0;

virtual std::string gen_uuid() const = 0;
virtual void set_rs( ResolverService * rs )
Expand All @@ -43,10 +44,13 @@ class PluginAdaptor
unsigned int targettime() const { return m_targettime; }
unsigned short weight() const { return m_weight; }
const bool script() const { return m_script; }
/// TODO move to norman "get" settings API once done?:
const std::string& scriptpath() const { return m_scriptpath; }

void set_weight(unsigned short w) { m_weight = w; }
void set_targettime(unsigned short t) { m_targettime = t; }
void set_script(bool t) { m_script = t; }
void set_scriptpath(std::string s) { m_scriptpath = s; }


// TEMP!
Expand All @@ -57,7 +61,9 @@ class PluginAdaptor
ResolverService * m_rs; // instance of a plugin
unsigned int m_targettime; // ms before passing to next resolver
unsigned short m_weight; // highest weight runs first.

bool m_script;
std::string m_scriptpath;

};

Expand Down
10 changes: 5 additions & 5 deletions includes/playdar/pluginadaptor_impl.hpp
Expand Up @@ -50,14 +50,14 @@ class PluginAdaptorImpl : public PluginAdaptor
return v;
}

virtual bool report_results(const query_uid& qid, const std::vector< result_pair >& results)
virtual bool report_results(const query_uid& qid, const std::vector< json_spirit::Object >& results)
{
std::vector< std::pair<ri_ptr,ss_ptr> > v;
BOOST_FOREACH( const result_pair & rp, results )
std::vector< ri_ptr > v;
BOOST_FOREACH( const json_spirit::Object & o, results )
{
ri_ptr rip = m_resolver->ri_from_json( rp.first );
ri_ptr rip = m_resolver->ri_from_json( o );
if(!rip) continue;
v.push_back( std::pair<ri_ptr,ss_ptr>(rip, rp.second) );
v.push_back( rip );
}
m_resolver->add_results( qid, v, rs()->name() );
return true;
Expand Down
4 changes: 4 additions & 0 deletions includes/playdar/resolved_item.h
Expand Up @@ -67,6 +67,9 @@ class ResolvedItem
}
void set_source(std::string s) { m_source = s; }

virtual void set_url(std::string s) { m_url = s; }
virtual const std::string & url() const { return m_url; }

//TODO: move this into PlayableItem somehow
virtual void set_streaming_strategy(boost::shared_ptr<class StreamingStrategy> s){}
virtual boost::shared_ptr<class StreamingStrategy> streaming_strategy() const
Expand All @@ -81,6 +84,7 @@ class ResolvedItem
private:
float m_score;
std::string m_source;
std::string m_url;

mutable source_uid m_uuid;

Expand Down
11 changes: 8 additions & 3 deletions includes/playdar/resolver.h
Expand Up @@ -35,14 +35,15 @@ class Resolver
public:
Resolver(MyApplication * app);
~Resolver();
void detect_curl_capabilities();
void load_resolver_plugins();
void load_resolver_scripts();
query_uid dispatch(boost::shared_ptr<ResolverQuery> rq);
query_uid dispatch(boost::shared_ptr<ResolverQuery> rq, rq_callback_t cb);

MyApplication * app(){ return m_app; }
bool add_results(query_uid qid,
const vector< std::pair<ri_ptr,ss_ptr> >& results,
const vector< ri_ptr >& results,
string via);
vector< ri_ptr > get_results(query_uid qid);
int num_results(query_uid qid);
Expand Down Expand Up @@ -111,7 +112,7 @@ class Resolver
MyApplication * m_app;

map< query_uid, rq_ptr > m_queries;
map< source_uid, ss_ptr > m_sid2ss;
map< source_uid, ri_ptr > m_sid2ri;
// timers used to auto-cancel queries that are inactive for long enough:
map< query_uid, boost::asio::deadline_timer* > m_qidtimers;

Expand All @@ -135,9 +136,13 @@ class Resolver
boost::mutex m_mutex;
boost::condition m_cond;


std::vector<std::pair< ri_validator, ri_generator> > m_riList;

// StreamingStrategy factories
std::map< std::string, boost::function<ss_ptr(std::string)> > m_ss_factories;

template <class T>
boost::shared_ptr<T> ss_ptr_generator(string url);
};

}
Expand Down
1 change: 1 addition & 0 deletions includes/playdar/resolver_query.hpp
Expand Up @@ -134,6 +134,7 @@ class ResolverQuery
{
time(&m_atime);
// sort results on score/preference.
// TODO this could be memoized
boost::function
< bool
( const ri_ptr &,
Expand Down
4 changes: 3 additions & 1 deletion includes/playdar/rs_script.h
Expand Up @@ -24,7 +24,7 @@ class rs_script : public ResolverService
public:
rs_script(){}

bool init(pa_ptr pap, std::string script);
bool init(pa_ptr pap);

void start_resolving(rq_ptr rq);
std::string name() const
Expand All @@ -51,6 +51,8 @@ class rs_script : public ResolverService

private:

pa_ptr m_pap;

int m_weight;
int m_targettime;
std::string m_name;
Expand Down
3 changes: 2 additions & 1 deletion includes/playdar/ss_curl.hpp
Expand Up @@ -35,7 +35,7 @@ class CurlStreamingStrategy : public StreamingStrategy
std::cout << "CTOR ss_curl: " << url << std::endl;
reset();
}

/// copy constructor, used by get_instance()
CurlStreamingStrategy(const CurlStreamingStrategy& other)
: m_curl(0)
Expand All @@ -48,6 +48,7 @@ class CurlStreamingStrategy : public StreamingStrategy

~CurlStreamingStrategy()
{
std::cout << "DTOR ss_curl: " << m_url << std::endl;
if(m_thread)
{
m_abort = true; // will cause thread to terminate
Expand Down
13 changes: 3 additions & 10 deletions resolvers/lan/lan.cpp
Expand Up @@ -14,7 +14,6 @@ namespace resolvers {
bool
lan::init(pa_ptr pap)
{
cout << "lan::init" << endl;
m_pap = pap;
broadcast_endpoint_ =
new boost::asio::ip::udp::endpoint
Expand Down Expand Up @@ -246,10 +245,7 @@ lan::handle_receive_from(const boost::system::error_code& error,
//cout << "lan: Got udp response." <<endl;
ri_ptr rip;

// TEMP!
typedef std::pair< json_spirit::Object, ss_ptr > result_pair;
vector< result_pair > final_results;

vector< Object > final_results;
try
{
rip = m_pap->ri_from_json(resobj);
Expand All @@ -265,12 +261,9 @@ lan::handle_receive_from(const boost::system::error_code& error,
string url = rbs.str();
url += "/sid/";
url += rip->id();
boost::shared_ptr<StreamingStrategy>
ss(new CurlStreamingStrategy(url));
pip->set_streaming_strategy(ss); // pointless?

pip->set_url( url );
// TEMP!
final_results.push_back( result_pair(pip->get_json(), ss) );
final_results.push_back( pip->get_json() );
}
}
catch (...)
Expand Down
24 changes: 7 additions & 17 deletions src/playdar/main.cpp
Expand Up @@ -11,13 +11,14 @@

#include "playdar/application.h"
#include <curl/curl.h>

#include "playdar/playdar_request_handler.h"

using namespace std;
using namespace playdar;
namespace po = boost::program_options;

// global !
// global because the sighandler needs it
MyApplication * app = 0;

static void sigfunc(int sig)
Expand Down Expand Up @@ -162,32 +163,20 @@ int main(int ac, char *av[])
cerr << "You must use a config file." << endl;
return 1;
}
string configfile = vm["config"].as<string>();
cout << "Using config file: " << configfile << endl;

Config conf(configfile);
if(conf.get<string>("name", "YOURNAMEHERE")=="YOURNAMEHERE")
{
cerr << "Please edit " << configfile << endl;
cerr << "YOURNAMEHERE is not a valid name." << endl;
cerr << "Autodetecting name: " << conf.name() << endl;
}


try
{

string configfile = vm["config"].as<string>();
cout << "Using config file: " << configfile << endl;

Config conf(configfile);
if(conf.get<string>("name", "YOURNAMEHERE")=="YOURNAMEHERE")
{
cerr << "Please edit " << configfile << endl;
cerr << "YOURNAMEHERE is not a valid name." << endl;
cout << "Autodetecting name: " << conf.name() << endl;
cerr << "Autodetecting name: " << conf.name() << endl;
}

app = new MyApplication(conf);


#ifndef WIN32
/// this might not compile on windows?
struct sigaction setmask;
Expand All @@ -210,6 +199,7 @@ int main(int ac, char *av[])
return 9;
}

app = new MyApplication(conf);
// start http server:
string ip = "0.0.0.0";
boost::thread http_thread(
Expand Down

0 comments on commit f17044a

Please sign in to comment.