| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_DB_DB_HH |
|---|
| 21 | #define GUARD_DB_DB_HH |
|---|
| 22 | |
|---|
| 23 | #include "filing_exception.hh" |
|---|
| 24 | #include "group.hh" |
|---|
| 25 | #include "no_such_article.hh" |
|---|
| 26 | #include "no_such_group.hh" |
|---|
| 27 | #include "threaded.hh" |
|---|
| 28 | |
|---|
| 29 | #include "../server.hh" |
|---|
| 30 | |
|---|
| 31 | #include <boost/asio.hpp> |
|---|
| 32 | #include <boost/nondet_random.hpp> |
|---|
| 33 | #include <boost/shared_ptr.hpp> |
|---|
| 34 | #include <boost/thread/recursive_mutex.hpp> |
|---|
| 35 | #include <fstream> |
|---|
| 36 | |
|---|
| 37 | namespace msg { class entity; } |
|---|
| 38 | |
|---|
| 39 | namespace db |
|---|
| 40 | { |
|---|
| 41 | class msg; |
|---|
| 42 | class user; |
|---|
| 43 | class password_handler; |
|---|
| 44 | class db_reader; |
|---|
| 45 | |
|---|
| 46 | class db : private boost::noncopyable |
|---|
| 47 | , public boost::enable_shared_from_this<db> |
|---|
| 48 | { |
|---|
| 49 | db(); |
|---|
| 50 | void init(); |
|---|
| 51 | |
|---|
| 52 | public: |
|---|
| 53 | typedef std::map<std::string,boost::shared_ptr<group> > |
|---|
| 54 | ::const_iterator group_iterator; |
|---|
| 55 | typedef std::map<std::string,boost::shared_ptr<msg> > |
|---|
| 56 | ::const_iterator msgid_iterator; |
|---|
| 57 | |
|---|
| 58 | static boost::shared_ptr<db> create() { |
|---|
| 59 | boost::shared_ptr<db> rv(new db()); |
|---|
| 60 | rv->init(); |
|---|
| 61 | return rv; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | group_iterator groups_begin() const { return groups.begin(); } |
|---|
| 65 | group_iterator groups_end() const { return groups.end(); } |
|---|
| 66 | |
|---|
| 67 | msgid_iterator msgids_begin() const { return msgid.begin(); } |
|---|
| 68 | msgid_iterator msgids_end() const { return msgid.end(); } |
|---|
| 69 | |
|---|
| 70 | boost::shared_ptr<msg> lookup_msgid(std::string s) { |
|---|
| 71 | boost::lock_guard<boost::recursive_mutex> lg(mt); |
|---|
| 72 | msgid_iterator it = msgid.find(s); |
|---|
| 73 | if (it == msgid.end()) throw no_such_article(); |
|---|
| 74 | return it->second; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | boost::shared_ptr<group> lookup_group(std::string s) { |
|---|
| 78 | boost::lock_guard<boost::recursive_mutex> lg(mt); |
|---|
| 79 | group_iterator it = groups.find(s); |
|---|
| 80 | if (it == groups.end()) throw no_such_group(); |
|---|
| 81 | return it->second; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | boost::shared_ptr<msg> file(boost::shared_ptr< ::msg::entity >, |
|---|
| 85 | boost::asio::ip::address source, |
|---|
| 86 | boost::shared_ptr<user> poster, |
|---|
| 87 | server::conn_cb); |
|---|
| 88 | |
|---|
| 89 | const threaded &get_threaded() { |
|---|
| 90 | boost::lock_guard<boost::recursive_mutex> lg(mt); |
|---|
| 91 | return thr; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | boost::shared_ptr<user> create_user(std::string userid, |
|---|
| 95 | std::string passwd); |
|---|
| 96 | |
|---|
| 97 | boost::shared_ptr<user> lookup_user(std::string userid) { |
|---|
| 98 | boost::lock_guard<boost::recursive_mutex> lg(mt); |
|---|
| 99 | return users[userid]; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | boost::shared_ptr<user> lookup_user_by_email(std::string s) { |
|---|
| 103 | boost::lock_guard<boost::recursive_mutex> lg(mt); |
|---|
| 104 | return users_email[s]; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | boost::shared_ptr<role> create_role(std::string name, |
|---|
| 108 | std::string description); |
|---|
| 109 | |
|---|
| 110 | boost::shared_ptr<role> lookup_role(std::string name) { |
|---|
| 111 | boost::lock_guard<boost::recursive_mutex> lg(mt); |
|---|
| 112 | return roles[name]; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void add_record(std::string record); |
|---|
| 116 | private: |
|---|
| 117 | mutable boost::recursive_mutex mt; |
|---|
| 118 | |
|---|
| 119 | std::map<std::string, boost::shared_ptr<user> > users; |
|---|
| 120 | std::map<std::string, boost::shared_ptr<user> > users_email; |
|---|
| 121 | std::map<std::string, boost::shared_ptr<role> > roles; |
|---|
| 122 | std::map<std::string, boost::shared_ptr<group> > groups; |
|---|
| 123 | std::map<std::string, boost::shared_ptr<msg> > msgid; |
|---|
| 124 | |
|---|
| 125 | std::string dbname; |
|---|
| 126 | |
|---|
| 127 | boost::shared_ptr<password_handler> pwh; |
|---|
| 128 | |
|---|
| 129 | threaded thr; |
|---|
| 130 | group::number global_next; |
|---|
| 131 | |
|---|
| 132 | boost::random_device rand; |
|---|
| 133 | |
|---|
| 134 | void do_newgroup(db_reader &, std::string, |
|---|
| 135 | boost::posix_time::ptime); |
|---|
| 136 | void do_user(db_reader &, std::string); |
|---|
| 137 | void do_article(db_reader &, std::string); |
|---|
| 138 | void do_role(db_reader &, std::string); |
|---|
| 139 | void do_moderation(db_reader &, std::string); |
|---|
| 140 | void read_record(db_reader &dr, boost::posix_time::ptime); |
|---|
| 141 | void register_role(boost::shared_ptr<role>); |
|---|
| 142 | }; |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | #endif |
|---|