3 * Copyright (C) 2007, Rus V. Brushkoff, All rights reserved.
16 #include <cc++/config.h>
17 #include <cc++/strchar.h>
18 #include <cc++/exception.h>
19 #include <cc++/thread.h>
20 #include <cc++/socket.h>
23 #ifdef CCXX_NAMESPACES
32 class app_specific_ui {
35 virtual ~app_specific_ui() { }
36 virtual void alert(bool fatal, const char *fmt, ...) = 0;
41 class batch_ui: public F_Batch_UI, public app_specific_ui {
43 batch_ui() : F_Batch_UI(/* 1 */) { }
47 void alert(bool fatal, const char *fmt, ...);
50 class sfinx_socket : public TCPSocket {
52 bool onAccept(const InetHostAddress &ia, tpport_t port) {
53 log("sfinx::accept", "Faraon connected from %s:%d",
54 ia.getHostname(), port);
58 sfinx_socket(InetAddress &ia) : TCPSocket(ia, SFINX_PORT,
59 SFINX_SERVER_BACKLOG, SFINX_MAX_PACKET_SIZE) { }
62 class faraon_session : public sfinx_stream, public TCPSession {
69 log("faraon_session", "Disconnected %s:%d", peer_name.c_str(), peer_port);
72 bool send_data(u8_t *b, u32_t size) {
73 return !(::send(so, (const char *)b, size, 0) == (s32_t)size);
75 bool read_data(u8_t *b, u32_t buf_size, u32_t timeout = 0) {
78 while (total_read != buf_size) {
79 nread = readData(b, buf_size, 0, timeout);
86 bool process_requests();
87 void get_objects(sfinx_tid_t reply_id, u32_t obj_mask);
88 void get_slice(sfinx_id_t slice_id, sfinx_tid_t tid);
89 void update_slice(sfinx_slice_t *slice);
90 void new_slice(sfinx_slice_t *slice);
91 void relink_slice(sfinx_slice_t *slice);
92 void delete_slice_with_reattach(sfinx_id_t slice_id);
93 void delete_slice_assoc_data(sfinx_id_t slice_id, bool recursivly);
94 void delete_slice_mark_data_unsorted(sfinx_id_t slice_id, bool recursivly);
95 void delete_slice_recursivly(sfinx_id_t slice_id);
96 void module_conf_request(u32_t module_id);
97 void update_files_module_conf(sfinx_pair_vector_t *conf);
98 void send_unsorted_dir(sfinx_file_t *f);
99 void send_file(sfinx_file_t *f);
100 void send_note(sfinx_note_t *n);
101 void files_module_classify(sfinx_file_t *f);
102 void update_file(sfinx_file_t *f);
103 void delete_file(sfinx_file_t *f);
104 void update_note(sfinx_note_t *n);
105 bool gen_desc_file(sfinx_object_t &o);
106 void search_query(sfinx_search_query_t *);
107 void search_in_files(string &pattern, sfinx_pair_vector_t *r);
108 void search_in_slices(string &pattern, sfinx_pair_vector_t *r);
109 void search_in_notes(string &pattern, sfinx_pair_vector_t *r);
110 void mark_files_unsorted(sfinx_id_t slice_id, bool recursivly, bool remove = false);
111 bool get_file_by_id(sfinx_file_t *f);
112 bool get_object(sfinx_object_t *dst, sfinx_type_t obj_type, sfinx_id_t obj_id);
113 bool get_object(sfinx_object_t *dst) { return get_object(dst, dst->type, dst->id); }
114 void delete_object_recursivly(sfinx_object_t *o, bool remove = false);
115 void delete_from_notes(sfinx_object_t *o, bool remove);
116 void delete_by_mtag_from_notes(sfinx_object_t *o, bool remove);
117 void delete_from_files(sfinx_object_t *o, bool remove);
118 void delete_by_mtag_from_files(sfinx_object_t *o, bool remove);
119 void delete_by_mtag(sfinx_object_t *o, bool remove) {
120 delete_by_mtag_from_notes(o, remove);
121 delete_by_mtag_from_files(o, remove);
124 void notes_module_add(sfinx_note_t *n);
125 void fill_notes(sfinx_slice_t *slice);
126 void fill_files(sfinx_slice_t *slice);
127 void add_slices_from_parent(sfinx_id_t parent_id, IBPP::Transaction tr, sfinx_slice_vector_t *slices,
133 faraon_session(TCPSocket &socket, int bufsize) :
134 sfinx_stream(), TCPSession(socket) {
136 peer_name = getPeer(&peer_port).getHostname();
138 packet_crypt_types->add(SFINX_PACKET_PLAIN);
139 packet_crypt_types->add(SFINX_PACKET_CRC32);
140 daemon_version->add(SFINX_VERSION_MAJOR);
141 daemon_version->add(SFINX_VERSION_MINOR);
142 daemon_version->add(SFINX_VERSION_PLEVEL);
143 min_auth_levels->add(SFINX_AUTH_USERPASS);
145 ~faraon_session() { }
148 class sfinx_app : public F_App {
150 string dbname_, dbuser_, dbpass_;
151 IBPP::Driver driver_;
155 sfinx_pair_vector_t files_module_conf;
156 sfinx_app(int argc, char **argv, const char *name = 0, const char *version = 0,
157 const char *author = 0, const char *license = 0, CommandOption *opts = 0) :
158 F_App(argc, argv, name, version, author, license, opts) {
159 driver_ = IBPP::DriverFactory();
166 app_specific_ui *ui() { return dynamic_cast<app_specific_ui *>(ui_); }
167 // void parse_command_options(void);
171 void load_module_conf(u32_t mid);
173 //u32_t get_object_slice(u32_t obj_type, u32_t obj_id);
174 bool set_sorted_path(sfinx_object_t &obj);
175 bool sorted_path_mkdir(sfinx_object_t &obj);
176 bool sorted_path_mkdir(sfinx_type_t t, sfinx_id_t i, sfinx_id_t m) {
177 sfinx_object_t obj(t, i, m);
178 return sorted_path_mkdir(obj);
180 sfinx_id_t parent_slice_id(sfinx_id_t slice_id);
181 string slice_name(sfinx_id_t slice_id);
182 string slice_directory(sfinx_id_t slice_id);
184 friend class faraon_session;
188 extern sfinx_app *sfinx;