.
[sfinx.git] / include / sfinx.H
blobcee51de70f0df5679b8fab259e3e59afc1908a64
2  /*
3   *   Copyright (C) 2007, Rus V. Brushkoff, All rights reserved.
4   */
6 #ifndef _SFINX_H_
7 #define _SFINX_H_
9 #include <F_App.H>
11 #define IBPP_LINUX
12 #include "ibpp.h"
14 #include <string>
15 #include <vector>
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>
22 using namespace F;
23 #ifdef  CCXX_NAMESPACES
24 using namespace ost;
25 using namespace std;
26 #endif
28 #include <stdarg.h>
29 #include <version.H>
30 #include <crypter.H>
32 class app_specific_ui {
33  public:
34   app_specific_ui() { }
35   virtual ~app_specific_ui() { }
36   virtual void alert(bool fatal, const char *fmt, ...) = 0;
39 #include <network.H>
41 class batch_ui: public F_Batch_UI, public app_specific_ui {
42  public:
43    batch_ui() : F_Batch_UI(/* 1 */) { }
44    ~batch_ui() { }
45    void init();
46    void run();
47    void alert(bool fatal, const char *fmt, ...);
50 class sfinx_socket : public TCPSocket {
51  protected:
52    bool onAccept(const InetHostAddress &ia, tpport_t port) {
53      log("sfinx::accept", "Faraon connected from %s:%d",
54        ia.getHostname(), port);
55      return true;
56   }
57  public:
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 {
64   tpport_t peer_port;
65   string peer_name;
67   void run(void);
68   void final() {
69     log("faraon_session", "Disconnected %s:%d", peer_name.c_str(), peer_port);
70    delete this;
71  }
72   bool send_data(u8_t *b, u32_t size) {
73     return !(::send(so, (const char *)b, size, 0) == (s32_t)size);
74  }
75   bool read_data(u8_t *b, u32_t buf_size, u32_t timeout = 0) {
76     u32_t total_read = 0;
77     ssize_t nread;
78     while (total_read != buf_size) {
79       nread = readData(b, buf_size, 0, timeout);
80       if (nread <= 0)
81         return true;
82       total_read += nread;
83    }
84     return false;
85  }
86   bool process_requests();
87   void get_slices(u32_t el_id, u32_t except = 0);
88   void get_objects(u32_t reply_id, u32_t objs_mask);
89   void get_slice(u32_t slice_id, u32_t el_id);
90   void update_slice(sfinx_slice_t *slice);
91   void new_slice(sfinx_slice_t *slice);
92   void relink_slice(sfinx_slice_t *slice);
93   void delete_slice_with_reattach(u32_t slice_id);
94   void delete_slice_assoc_data(u32_t slice_id, bool recursivly);
95   void delete_slice_mark_data_unsorted(u32_t slice_id, bool recursivly);
96   void delete_slice_recursivly(u32_t slice_id);
97   void module_conf_request(u32_t module_id);
98   void update_files_module_conf(sfinx_pair_vector_t *conf);
99   void send_dir(bool sorted, sfinx_file_t *f);
100   void send_file(sfinx_file_t *f);
101   void files_module_classify(sfinx_file_t *f);
102   void edit_file(sfinx_file_t *f);
103   bool gen_desc_file(u32_t obj_type, u32_t obj_id);
104   void search_query(sfinx_search_query_t *);
105   void search_in_files(string &pattern, sfinx_pair_vector_t *r);
106   void mark_files_unsorted(u32_t slice_id, bool recursivly, bool remove = false);
107   void mark_file_unsorted(sfinx_file_t *f, bool remove = false);
108   bool get_file_by_id(sfinx_file_t *f);
110   bool connect();
112  public:
113   faraon_session(TCPSocket &socket, int bufsize) :
114     sfinx_stream(), TCPSession(socket) {
115       getPeer(&peer_port);
116       peer_name = getPeer(&peer_port).getHostname();
118       packet_crypt_types->add(SFINX_PACKET_PLAIN);
119       packet_crypt_types->add(SFINX_PACKET_CRC32);
120       daemon_version->add(SFINX_VERSION_MAJOR);
121       daemon_version->add(SFINX_VERSION_MINOR);
122       daemon_version->add(SFINX_VERSION_PLEVEL);
123       min_auth_levels->add(SFINX_AUTH_USERPASS);
125   ~faraon_session() { }
128 class sfinx_app : public F_App {
129   batch_ui ui1;
130   string dbname_, dbuser_, dbpass_;
131   IBPP::Driver driver_;
132   IBPP::Service svc_;
133   IBPP::Database db_;
134  public:
135   sfinx_pair_vector_t files_module_conf;
136   sfinx_app(int argc, char **argv, const char *name = 0, const char *version = 0,
137     const char *author = 0, const char *license = 0, CommandOption *opts = 0) :
138         F_App(argc, argv, name, version, author, license, opts) {
139                 driver_ = IBPP::DriverFactory();
140                 driver_->Load();
142   ~sfinx_app() {
143    db_->Disconnect();
144    driver_->Unload();
146  app_specific_ui *ui() { return dynamic_cast<app_specific_ui *>(ui_); }
147  // void parse_command_options(void);
148  void dbopen();
149  void listen();
151  void load_module_conf(u32_t mid);
153  u32_t get_object_slice(u32_t obj_type, u32_t obj_id);
154  bool set_sorted_path(sfinx_object_t &obj);
155  bool sorted_path_mkdir(sfinx_object_t &obj);
156  bool sorted_path_mkdir(u32_t type, u32_t id) {
157    sfinx_object_t obj(type, id);
158    return sorted_path_mkdir(obj);
160  u32_t parent_slice_id(u32_t slice_id);
161  bool parent_object(sfinx_object_t &src, sfinx_object_t *dst);
162  string slice_name(u32_t slice_id);
163  string slice_directory(u32_t slice_id);
165  friend class faraon_session;
169 extern sfinx_app *sfinx;
171 #endif