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 mark_files_unsorted(sfinx_id_t slice_id, bool recursivly, bool remove = false);
109 bool get_file_by_id(sfinx_file_t *f);
110 bool get_object(sfinx_object_t *dst, sfinx_type_t obj_type, sfinx_id_t obj_id);
111 bool get_object(sfinx_object_t *dst) { return get_object(dst, dst->type, dst->id); }
112 void delete_object_recursivly(sfinx_object_t *o, bool remove = false);
113 void delete_from_notes(sfinx_object_t *o, bool remove);
114 void delete_by_mtag_from_notes(sfinx_object_t *o, bool remove);
115 void delete_from_files(sfinx_object_t *o, bool remove);
116 void delete_by_mtag_from_files(sfinx_object_t *o, bool remove);
117 void delete_by_mtag(sfinx_object_t *o, bool remove) {
118 delete_by_mtag_from_notes(o, remove);
119 delete_by_mtag_from_files(o, remove);
122 void notes_module_add(sfinx_note_t *n);
123 void fill_notes(sfinx_slice_t *slice);
124 void fill_files(sfinx_slice_t *slice);
125 void add_slices_from_parent(sfinx_id_t parent_id, IBPP::Transaction tr, sfinx_slice_vector_t *slices,
131 faraon_session(TCPSocket &socket, int bufsize) :
132 sfinx_stream(), TCPSession(socket) {
134 peer_name = getPeer(&peer_port).getHostname();
136 packet_crypt_types->add(SFINX_PACKET_PLAIN);
137 packet_crypt_types->add(SFINX_PACKET_CRC32);
138 daemon_version->add(SFINX_VERSION_MAJOR);
139 daemon_version->add(SFINX_VERSION_MINOR);
140 daemon_version->add(SFINX_VERSION_PLEVEL);
141 min_auth_levels->add(SFINX_AUTH_USERPASS);
143 ~faraon_session() { }
146 class sfinx_app : public F_App {
148 string dbname_, dbuser_, dbpass_;
149 IBPP::Driver driver_;
153 sfinx_pair_vector_t files_module_conf;
154 sfinx_app(int argc, char **argv, const char *name = 0, const char *version = 0,
155 const char *author = 0, const char *license = 0, CommandOption *opts = 0) :
156 F_App(argc, argv, name, version, author, license, opts) {
157 driver_ = IBPP::DriverFactory();
164 app_specific_ui *ui() { return dynamic_cast<app_specific_ui *>(ui_); }
165 // void parse_command_options(void);
169 void load_module_conf(u32_t mid);
171 //u32_t get_object_slice(u32_t obj_type, u32_t obj_id);
172 bool set_sorted_path(sfinx_object_t &obj);
173 bool sorted_path_mkdir(sfinx_object_t &obj);
174 bool sorted_path_mkdir(sfinx_type_t t, sfinx_id_t i, sfinx_id_t m) {
175 sfinx_object_t obj(t, i, m);
176 return sorted_path_mkdir(obj);
178 sfinx_id_t parent_slice_id(sfinx_id_t slice_id);
179 string slice_name(sfinx_id_t slice_id);
180 string slice_directory(sfinx_id_t slice_id);
182 friend class faraon_session;
186 extern sfinx_app *sfinx;