1 /*------------------------------------------------------------------------------
  2 Name:      FileWriterCallback.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 #ifndef _CONTRIB_FILEWRITERCALLBACK_H
  7 #define _CONTRIB_FILEWRITERCALLBACK_H
  8 
  9 #include <util/xmlBlasterDef.h>
 10 #include <util/Global.h>
 11 #include <util/qos/ConnectQos.h>
 12 #include <client/I_ConnectionProblems.h>
 13 #include <client/I_Callback.h>
 14 #include <util/thread/ThreadImpl.h>
 15 #include <util/dispatch/I_PostSendListener.h>
 16 #include <util/ReferenceCounterBase.h>
 17 #include <util/ReferenceHolder.h>
 18 #include <string>
 19 #include <vector>
 20 #include <map>
 21 
 22 /* The following comment is used by doxygen for the main html page: */
 23 /*! \mainpage Hints about the C++ client library usage.
 24  *
 25  * \section intro_sec The C++ client library
 26  *
 27  * The xmlBlaster C++ client library supports access to xmlBlaster with asynchronous callbacks,
 28  * client side queuing and fail safe reconnect using the CORBA or SOCKET protocol plugin.
 29  * Details about compilation and its usage can be found in the 
 30  * http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.cpp.html requirement.
 31  *
 32  * As a C++ developer your entry point to use is the class org::xmlBlaster::client::FileWriterCallback and
 33  * a complete overview demo code is HelloWorld2.cpp
 34  *
 35  * \section c_sec The C client library
 36  * The C client library offers some basic functionality like the SOCKET protocol with
 37  * the struct #FileWriterCallbackUnparsed or persistent queues with struct #I_Queue.
 38  * These features are heavily used by the C++ library.
 39  * If you need a tiny xmlBlaster access you can choose to use the C client library directly
 40  * without any C++ code.
 41  *
 42  * For details read the
 43  * http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.c.socket.html requirement and the
 44  * http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.c.queue.html requirement.
 45  * and look at the API documentation at http://www.xmlblaster.org/xmlBlaster/doc/doxygen/c/html/index.html
 46  */
 47 
 48 // Note: I_ConnectionProblems.h includes I_ConnectionsHandler.h includes I_XmlBlasterConnection.h
 49 //       which includes all EraseQos, SubscribeKey etc.
 50 //       -> We could try to avoid this by forward declaration, but all cpp files must
 51 //          then include them thereselves.
 52 
 53 // Declare classes without the need to include them in this header file
 54 namespace org { namespace xmlBlaster { namespace util {
 55    class MessageUnit;
 56 }}}
 57 namespace org { namespace xmlBlaster { namespace util { namespace dispatch {
 58    class DispatchManager;
 59    class ConnectionsHandler;
 60 }}}}
 61 namespace org { namespace xmlBlaster { namespace client { namespace protocol {
 62    class I_CallbackServer;
 63 }}}}
 64 
 65 namespace org { namespace xmlBlaster { namespace contrib {
 66 
 67 /*
 68  * The interface org::xmlBlaster::client::I_CallbackRaw/I_Callback/I_CallbackExtended are enforced by AbstractCallbackExtended
 69  * is for the protocol drivers.
 70  */
 71 typedef std::map<std::string, org::xmlBlaster::client::I_Callback*> CallbackMapType;
 72 typedef std::map<std::string, std::string> StringMap;
 73 
 74 /**
 75  * This is the main entry point for programmers to the C++ client library. 
 76  * 
 77  * Exactly one Global instance and one instance of this are a pair which can't be
 78  * mixed with other instances. 
 79  */
 80 class Dll_Export FileWriterCallback : public org::xmlBlaster::client::I_Callback
 81 {
 82 private:
 83    std::string ME;
 84    org::xmlBlaster::util::Global& global_;
 85    int BUF_SIZE;
 86    std::string dirName_;
 87    std::string lockExtention_;
 88    bool overwrite_;
 89    std::string tmpDirName_;
 90    bool keepDumpFiles_;
 91    std::string directory_;
 92    std::string tmpDirectory_;
 93    org::xmlBlaster::util::I_Log& log_;
 94    char *buf_;
 95    int bufSize_;
 96       
 97 // TODO REMOVE NEXT LINE AFTER TESTING 
 98 public:
 99    void storeChunk(std::string &tmpDir, std::string &fileName, long chunkNumber, std::string &sep, bool overwrite, const char *content, long length);
100 
101    /**
102     * Returns the number used as the postfix of this file. If there is no such number, -1 is
103     * returned. If the content is null or empty, an exception is thrown. If there is a postfix
104     * but it is not a number, a -1 is returned.
105     *
106     * @param filename the filename to check.
107     * @param prefix the prefix of the filename (the part before the number)
108     * @return a long specifying the postfix number of the file.
109     * @throws Exception if the content is null or empty.
110     */
111    long extractNumberPostfixFromFile(std::string &filename, std::string &prefix);
112 
113    /**
114     * Fills the vector with a list of files found.
115     *
116     * @param dir
117     * @param prefix if not empty, only files starting with the specified prefix will be returned.
118     * @param files
119     */
120    void getdir(std::string &dir, std::string &prefix, std::vector<std::string> &files);
121    
122    /**
123     * The filename prefix for which to retrieve all chunks.
124     * @param fileName
125     * @param sep
126     * @return
127     * @throws Exception
128     */
129    void getChunkFilenames(std::string &fileName, std::string &sep, std::vector<std::string> &filenames);
130 
131    /**
132     * Puts all chunks stored in separate files together in one single one.
133     *
134     * @param fileName The name of the complete (destination) file.
135     * @param expectedChunks The number of expected chunks. If the number of chunks found is
136     * bigger, the exceeding ones are ignored and a warning is written to the logs. If the number
137     * is too low it checks if the file already exists. If it exists a warning is written and
138     * the method returns, otherwise an exception is thrown. Keeps also track of locking the file.
139     * @param lastContent the content of the last chunk of the message (can be null).
140     * @param isCompleteMsg is true if the content of the message is contained in one single
141     * chunk (i.e. if the message is not chunked).
142     * @throws Exception If an error occurs when writing / reading the files. This method tries
143     * to clean up the destination file in case of an exception when writing.
144     */
145    void putAllChunksTogether(std::string &fileName, std::string &subDir, long expectedChunks, const char *buf, long bufSize, bool isCompleteMsg);
146 
147    /**
148     * Deletes the specified file from the file system.
149     * Never throws Exceptions
150     * @param file the file object to be deleted.
151     * @return if it can not delete the file it returns false, true otherwise.
152     */
153    bool deleteFile(std::string &file);
154 
155 public:
156 
157    /**
158     * Creates a callback
159     * @param dirName The name of the directory where to store the files.
160     * @param tmpDirName the name of the directory where to store the temporary files.
161     * @param lockExtention The extention to use to lock the reading of the file. This is used if the
162     * entries have to be retrieved by the filepoller. Until such a file exists, the entry is not
163     * processed by the file poller.
164     * @param overwrite if set to true it will overwrite existing files.
165     * @throws Exception
166     */
167    FileWriterCallback(org::xmlBlaster::util::Global &global, std::string &dirName, std::string &tmpDirName, std::string &lockExtention, bool overwrite, bool keepDumpFiles);
168 
169    std::string update(const std::string &sessionId,
170                        org::xmlBlaster::client::key::UpdateKey &updateKey,
171                        const unsigned char *content, long contentSize,
172                        org::xmlBlaster::client::qos::UpdateQos &updateQos);
173 
174    virtual ~FileWriterCallback();
175 
176 };
177 
178 typedef org::xmlBlaster::util::ReferenceHolder<org::xmlBlaster::contrib::FileWriterCallback> FileWriterCallbackRef;
179 
180 }}} // namespaces
181 
182 #endif


syntax highlighted by Code2HTML, v. 0.9.1