00001 #ifndef _DHT_KADC_CLIENT_H_
00002 #define _DHT_KADC_CLIENT_H_
00003
00004 #include <utility>
00005 #include <algorithm>
00006 #include <string>
00007 #include <list>
00008 #include <map>
00009
00010 #include "../client.h"
00011 #include "shared_queue.h"
00012 #include "message.h"
00013 #include "observer_info.h"
00014
00015
00016
00017 extern "C" {
00018 #include <KadC/int128.h>
00019 #include <KadC/rbt.h>
00020 #include <KadC/MD4.h>
00021 #include <KadC/KadCalloc.h>
00022 #include <KadC/KadClog.h>
00023 #include <KadC/KadCapi.h>
00024 }
00025
00026 namespace dht {
00027 namespace kadc {
00028 using namespace std;
00029
00064 class client : public dht::client {
00065 public:
00067 typedef shared_queue<message *> message_queue_type;
00068 friend class state;
00069 friend class reactor_event_handler;
00071 private:
00072
00073
00074 mutable KadCcontext _kcc;
00075 bool _kstarted;
00076 string _init_file;
00077 addr_inet_type _ext_addr;
00078 reactor_type *_reactor;
00079
00080 size_t _find_threads,
00081 _find_duration,
00082 _store_threads,
00083 _store_duration;
00084 size_t _find_max_hits;
00085
00086 typedef map<task *, task *> running_tasks_type;
00087 typedef list<observer_info> message_obsvs_type;
00088
00089 running_tasks_type _running_tasks;
00090 message_queue_type _msg_queue;
00091 message_obsvs_type _msg_observers;
00092
00093 class reactor_event_handler *_rehandler;
00094
00095 class state *_state;
00096 int _state_out;
00097
00098
00099 inline void _kad_context(KadCcontext kcc) { _kcc = kcc; }
00100 inline KadCcontext *_kad_context() { return &_kcc; }
00101
00102
00103 inline void _external_addr(const addr_inet_type &a) { _ext_addr = a; }
00104
00105 inline message_queue_type *_message_queue() { return &_msg_queue; }
00106
00107 void _change_state(class state *s);
00108
00109 void _change_state_out(int t);
00110 void _task_add(task *t);
00111 void _quit_all_tasks();
00112 void _wait_running_tasks();
00113 void _quit_task(task *t);
00114 inline int _running_tasks_size() { return _running_tasks.size(); }
00115
00116 void _attach_observer_messages(const observer_info &oi);
00117 bool _detach_observer_messages(observer_info *oi);
00118
00119 inline bool _kadc_started() const { return _kstarted; }
00120 inline void _kadc_started(bool t) { _kstarted = t; }
00121
00122 void _process_queue();
00123 void _process_msg(message *tm);
00124 public:
00126 const static int msg_connect = 1;
00127 const static int msg_disconnect = 2;
00128 const static int msg_store = 3;
00129 const static int msg_search_result = 4;
00130 const static int msg_search_done = 5;
00131 const static int msg_task_exit = 6;
00133
00134 client();
00135 virtual ~client();
00136
00140 inline const char *init_file() { return _init_file.c_str(); }
00141
00148 virtual void init(const name_value_map &opts);
00149
00166 virtual void deinit();
00167
00168 virtual void connect(dht::notify_handler *handler = NULL);
00169 virtual void disconnect(dht::notify_handler *handler = NULL);
00170
00171 virtual void find(const dht::key &fkey,
00172 dht::search_handler *handler);
00173
00174 virtual void store(const dht::key &skey,
00175 const dht::value &svalue,
00176 dht::notify_handler *handler = NULL);
00177
00178 virtual const addr_inet_type &external_addr();
00179
00180 virtual int process(time_value_type &max_wait);
00181 virtual int process(time_value_type *max_wait = NULL);
00182 virtual reactor_type *reactor();
00183 virtual void reactor(reactor_type *reactor);
00184 virtual int handler_cancel(notify_handler *handler);
00185
00194 inline size_t find_threads(size_t t) {
00195 return _find_threads = std::min<size_t>(t, 20);
00196 }
00202 inline size_t find_threads() const { return _find_threads; }
00203
00208 inline size_t find_duration(size_t s) {
00209 return _find_duration = std::min<size_t>(s, 200);
00210 }
00216 inline size_t find_duration() const { return _find_duration; }
00217
00219 inline size_t find_max_hits(size_t t) {
00220 return _find_max_hits = std::min<size_t>(t, 20);
00221 }
00222
00223 inline size_t find_max_hits() const { return _find_max_hits; }
00225
00234 inline size_t store_threads(size_t t) {
00235 return _store_threads = std::min<size_t>(t, 20);
00236 }
00242 inline size_t store_threads() const { return _store_threads; }
00243
00248 inline size_t store_duration(size_t s) {
00249 return _store_duration = std::min<size_t>(s, 200);
00250 }
00256 inline size_t store_duration() const { return _store_duration; }
00257
00275 int write_inifile(const char *target_file = NULL);
00276
00280 size_t contacted_nodes() const;
00284 size_t contacts() const;
00285
00299 static void logfile(const std::string &path);
00300
00301 };
00302
00303 }
00304 }
00305
00306 #endif //_DHT_KADC_CLIENT_H_