Back to index

im-sdk  12.3.91
IMUtil.hh
Go to the documentation of this file.
00001 #ifndef IMUTIL_HH_
00002 #define IMUTIL_HH_
00003 #include <vector>
00004 #include <string>
00005 #include <sys/types.h>
00006 #include "basictype.hh"
00007 
00008 struct DirectoryEntry {
00009     string filename;
00010     string dirname;
00011     string fullname;
00012     bool readable_p;
00013     bool writable_p;
00014     bool directory_p;
00015 };
00016 typedef vector<DirectoryEntry> DirectoryInfoVec;
00017 
00018 class DirectoryInfo
00019 {
00020     string path;
00021     bool subdirp;
00022     bool unreadablep;
00023     DirectoryInfoVec dirvec;
00024 
00025     bool addfile(const char *filename);
00026 
00027   public:
00028     DirectoryInfoVec* get_directory_info() { return &dirvec; }
00029     bool refresh(
00030        const char *path = NULL
00031     );
00032     DirectoryInfo(
00033        bool include_subdir_p = false, 
00034        bool include_unreable_p = false
00035     );
00036 };
00037 
00038 class LEDirectoryInfo : DirectoryInfo {
00039     string path;
00040     DirectoryInfoVec dirvec;
00041 
00042     bool addfile(const char *filename);
00043 
00044   public:
00045     DirectoryInfoVec* get_directory_info() { return &dirvec; }
00046     bool refresh(
00047        const char *path = NULL
00048     );
00049     LEDirectoryInfo() : DirectoryInfo(true, false) {
00050     };
00051 };
00052 
00053 
00054 class IMSocketAddress;
00055 class IMSocketTrans;
00056 class IMSocket;
00057 typedef vector<IMSocketAddress> IMSocketAddressVec;
00058 typedef vector<IMSocket*> IMSocketVec;
00059 
00060 class IMSocketAddress
00061 {
00062   public:
00063     enum ADDRESS_TYPE {
00064            INET,
00065            UNIX_DOMAIN,
00066            UNIX_DOMAIN_PER_USER
00067     };
00068     enum TRANSPORT_TYPE {
00069             NORMAL,
00070             TLS
00071     };
00072 
00073   private:
00074     ADDRESS_TYPE type;
00075     TRANSPORT_TYPE trans_type;
00076     string address;
00077     string service;
00078     void *addrinfo;
00079     bool is_dir_created;
00080 
00081     void* get_addrinfo();
00082     void configure_socket(
00083        int fd
00084     );
00085     void bind_fail(
00086        int fd
00087     );
00088     int setup_inet(
00089        void *paddressinfo
00090     );
00091     int setup_unix();
00092 
00093   public:
00094     int create_sockets(
00095        IMSocketVec &sockvec
00096     );
00097 
00098     IMSocketAddress(
00099        ADDRESS_TYPE type,
00100        const string &address,
00101        const string &service
00102     );
00103     IMSocketAddress(
00104         ADDRESS_TYPE type,
00105         TRANSPORT_TYPE trans_type,
00106         const string &address,
00107         const string &service
00108     );
00109     ~IMSocketAddress();
00110 };
00111 
00112 class IMSocket
00113 {
00114   public:
00115     enum LISTEN_STATUS {
00116            CLOSED,
00117            OPEN,
00118            ERROR
00119     };
00120   private:
00121     int fd;
00122     int trans_type;
00123     enum LISTEN_STATUS status;
00124 
00125     bool do_listen();
00126 
00127     // inhibit duplication
00128     IMSocket(
00129        const IMSocket& a
00130     );
00131     IMSocket& operator=(
00132        const IMSocket& a
00133     );
00134 
00135   public:
00136     bool listen();
00137     IMSocketTrans* accept();
00138     int get_fd();
00139 
00140     enum LISTEN_STATUS get_status() const
00141     { return status; }
00142 
00143     IMSocket(
00144        int fd
00145     );
00146     IMSocket(
00147         int fd,
00148         int trans_type
00149     );
00150     ~IMSocket();
00151 };
00152 
00153 class IMSocketTrans
00154 {
00155     int fd;
00156     // inhibit duplication.
00157     IMSocketTrans(
00158        const IMSocketTrans& a
00159     );
00160     IMSocketTrans& operator=(
00161        const IMSocketTrans& a
00162     );
00163   public:
00164     int get_fd() const
00165     { return fd; }
00166     virtual int send(
00167        const void *,
00168        size_t n
00169     );
00170     virtual int recv(
00171        void*,
00172        size_t n
00173     );
00174     IMSocketTrans(int fd);
00175     virtual ~IMSocketTrans();
00176 
00177   // friend class IMSocket;
00178 };
00179 
00180 class IMSocketListen
00181 {
00182     enum IMSocket::LISTEN_STATUS status;
00183     IMSocketVec sockvec;
00184 
00185     // inhibit duplication.
00186     IMSocketListen(
00187        const IMSocketListen& a
00188     );
00189     IMSocketListen& operator=(
00190        const IMSocketListen& a
00191     );
00192 
00193   public:
00194     bool errorp()
00195     { return (status == IMSocket::ERROR); }
00196     bool listen(
00197        IMSocketAddressVec &endvec
00198     );
00199     IMSocketTrans* accept();
00200     IMSocketListen();
00201     ~IMSocketListen();
00202 };
00203 
00204 // singleton class
00205 class IMDaemon
00206 {
00207     bool already_finished;
00208     static IMDaemon *pimdaemon;
00209     void closefd();
00210     bool discard_parent();
00211     bool daemonize();
00212     
00213     IMDaemon();
00214   public:
00215     static IMDaemon* get_instance();
00216     static void cleanup();
00217     void setup(
00218        const char *todir
00219     );
00220 };
00221 
00222 /* file manipulation fuctions */
00223 
00224 // return NULL if error occurs.
00225 extern CARD8BIT* IM_read_file(
00226     const char* filename,
00227     size_t size
00228 );
00229 // return -1 if error occurs.
00230 extern int IM_file_size(
00231     const char* filename
00232 );
00233 // return NULL if error occurs.
00234 extern void IM_unix_domain_socket_file_dir(
00235     string    user_name,
00236     string &  dir
00237 );
00238 
00239 #endif /* not IMUTIL_HH_ */
00240 
00241 /* Local Variables: */
00242 /* c-file-style: "iiim-project" */
00243 /* End: */