Permalink
Browse files

config object now holds config rather than app()

  • Loading branch information...
1 parent 8d96302 commit 7e37ba407095493888987573175a1c3a77f93466 @RJ committed Mar 9, 2009
@@ -12,9 +12,8 @@
},
"darknet" :
{
- "enabled" : "yes",
+ "enabled" : "no",
"port" : 9999
- },
- "blah" : "foo"
+ }
}
}
@@ -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
-
@@ -0,0 +1 @@
+"make install" from a plugin dir will put a .resolver file in here
@@ -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,46 +26,14 @@ 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()
{
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) {
@@ -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,23 +26,29 @@ 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();
boost::asio::ip::address_v4 private_ip();
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;
-
+
};
@@ -51,31 +51,46 @@ 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());
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)
{
@@ -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")
Oops, something went wrong.

0 comments on commit 7e37ba4

Please sign in to comment.