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>
26 #ifdef CCXX_NAMESPACES
35 class app_specific_ui {
38 virtual ~app_specific_ui() { }
39 virtual void alert(bool fatal, const char *fmt, ...) = 0;
44 class batch_ui: public F_Batch_UI, public app_specific_ui {
46 batch_ui() : F_Batch_UI(/* 1 */) { }
50 void alert(bool fatal, const char *fmt, ...);
53 class sfinx_socket : public TCPSocket {
55 bool onAccept(const InetHostAddress &ia, tpport_t port) {
56 log("sfinx::accept", "Faraon connected from %s:%d",
57 ia.getHostname(), port);
61 sfinx_socket(InetAddress &ia) : TCPSocket(ia, SFINX_PORT,
62 SFINX_SERVER_BACKLOG, SFINX_MAX_PACKET_SIZE) { }
65 class faraon_session : public sfinx_stream, public TCPSession {
72 log("faraon_session", "Disconnected %s:%d", peer_name.c_str(), peer_port);
75 bool send_data(u8_t *b, u32_t size) {
76 return !(::send(so, (const char *)b, size, 0) == (s32_t)size);
78 bool read_data(u8_t *b, u32_t buf_size, u32_t timeout = 0) {
81 while (total_read != buf_size) {
82 nread = readData(b, buf_size, 0, timeout);
89 bool process_requests();
90 void get_objects(sfinx_tid_t reply_id, u32_t obj_mask);
91 void get_slice(sfinx_id_t slice_id, sfinx_tid_t tid);
92 void update_slice(sfinx_slice_t *slice);
93 void new_slice(sfinx_slice_t *slice);
94 void relink_slice(sfinx_slice_t *slice);
95 void delete_slice_with_reattach(sfinx_id_t slice_id);
96 void delete_slice_assoc_data(sfinx_id_t slice_id, bool recursivly);
97 void delete_slice_mark_data_unsorted(sfinx_id_t slice_id, bool recursivly);
98 void delete_slice_recursivly(sfinx_id_t slice_id);
99 void module_conf_request(u32_t module_id);
100 void update_files_module_conf(sfinx_pair_vector_t *conf);
101 void send_unsorted_dir(sfinx_file_t *f);
102 void send_file(sfinx_file_t *f);
103 void send_note(sfinx_note_t *n);
104 void files_module_classify(sfinx_file_t *f);
105 void update_file(sfinx_file_t *f);
106 void delete_file(sfinx_file_t *f);
107 void update_note(sfinx_note_t *n);
108 bool gen_desc_file(sfinx_object_t &o);
109 void search_query(sfinx_search_query_t *);
110 void search_in_files(string &pattern, sfinx_pair_vector_t *r, string &where);
111 void search_in_slices(string &pattern, sfinx_pair_vector_t *r, string &where);
112 void search_in_notes(string &pattern, sfinx_pair_vector_t *r, string &where);
113 void mark_files_unsorted(sfinx_id_t slice_id, bool recursivly, bool remove = false);
114 bool get_file_by_id(sfinx_file_t *f);
115 bool get_object(sfinx_object_t *dst, sfinx_type_t obj_type, sfinx_id_t obj_id);
116 bool get_object(sfinx_object_t *dst) { return get_object(dst, dst->type, dst->id); }
117 void delete_object_recursivly(sfinx_object_t *o, bool remove = false);
118 void delete_from_notes(sfinx_object_t *o, bool remove);
119 void delete_by_mtag_from_notes(sfinx_object_t *o, bool remove);
120 void delete_from_files(sfinx_object_t *o, bool remove);
121 void delete_by_mtag_from_files(sfinx_object_t *o, bool remove);
122 void delete_by_mtag(sfinx_object_t *o, bool remove) {
123 delete_by_mtag_from_notes(o, remove);
124 delete_by_mtag_from_files(o, remove);
127 void notes_module_add(sfinx_note_t *n);
128 void fill_notes(sfinx_slice_t *slice);
129 void fill_files(sfinx_slice_t *slice);
130 void add_slices_from_parent(sfinx_id_t parent_id, IBPP::Transaction tr, sfinx_slice_vector_t *slices,
136 faraon_session(TCPSocket &socket, int bufsize) :
137 sfinx_stream(), TCPSession(socket) {
139 peer_name = getPeer(&peer_port).getHostname();
141 packet_crypt_types->add(SFINX_PACKET_PLAIN);
142 packet_crypt_types->add(SFINX_PACKET_CRC32);
143 daemon_version->add(SFINX_VERSION_MAJOR);
144 daemon_version->add(SFINX_VERSION_MINOR);
145 daemon_version->add(SFINX_VERSION_PLEVEL);
146 min_auth_levels->add(SFINX_AUTH_USERPASS);
148 ~faraon_session() { }
151 class sfinx_app : public F_App {
153 string dbname_, dbuser_, dbpass_, db_dir;
154 IBPP::Driver driver_;
157 iconv_t to_utf8_cd, from_utf8_cd;
159 sfinx_pair_vector_t files_module_conf;
160 sfinx_app(int argc, char **argv, const char *name = 0, const char *version = 0,
161 const char *author = 0, const char *license = 0, CommandOption *opts = 0) :
162 F_App(argc, argv, name, version, author, license, opts) {
163 parse_command_options();
164 to_utf8_cd = iconv_open ("UTF8", nl_langinfo(CODESET));
165 if (to_utf8_cd == (iconv_t) -1) {
167 F::log("iconv", "conversion from \'%s\' to UTF8 not available",
168 nl_langinfo(CODESET));
170 F::log("iconv", "iconv_open: %s", strerror(errno));
172 from_utf8_cd = iconv_open (nl_langinfo(CODESET), "UTF8");
173 if (from_utf8_cd == (iconv_t) -1) {
175 F::log("iconv", "conversion from UTF8 to \'%s\' not available",
176 nl_langinfo(CODESET));
178 F::log("iconv", "iconv_open: %s", strerror(errno));
180 driver_ = IBPP::DriverFactory();
186 iconv_close(to_utf8_cd);
187 iconv_close(from_utf8_cd);
189 app_specific_ui *ui() { return dynamic_cast<app_specific_ui *>(ui_); }
190 void parse_command_options(void);
193 string fs2utf8(string &s);
194 string utf82fs(string &s);
196 void load_module_conf(u32_t mid);
198 //u32_t get_object_slice(u32_t obj_type, u32_t obj_id);
199 bool set_sorted_path(sfinx_object_t &obj);
200 bool sorted_path_mkdir(sfinx_object_t &obj);
201 bool sorted_path_mkdir(sfinx_type_t t, sfinx_id_t i, sfinx_id_t m) {
202 sfinx_object_t obj(t, i, m);
203 return sorted_path_mkdir(obj);
205 sfinx_id_t parent_slice_id(sfinx_id_t slice_id);
206 string slice_name(sfinx_id_t slice_id);
207 string slice_directory(sfinx_id_t slice_id);
209 friend class faraon_session;
213 extern sfinx_app *sfinx;