src/include/common/spds/SyncManager.h

00001 /*
00002  * Copyright (C) 2003-2007 Funambol, Inc
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 2 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00016  * 02111-1307  USA
00017  */
00018 
00019 #ifndef INCL_SYNC_MANAGER
00020 #define INCL_SYNC_MANAGER
00021 
00025 #include "base/util/ArrayList.h"
00026 #include "http/TransportAgent.h"
00027 #include "spds/constants.h"
00028 #include "spds/SyncManagerConfig.h"
00029 #include "spds/SyncSource.h"
00030 #include "spds/SyncMLBuilder.h"
00031 #include "spds/SyncMLProcessor.h"
00032 #include "spds/CredentialHandler.h"
00033 #include "spds/CredentialHandler.h"
00034 #include "spds/SyncReport.h"
00035 
00036 
00037 typedef enum {
00038                 STATE_START        = 0,
00039                 STATE_PKG1_SENDING = 1,
00040                 STATE_PKG1_SENT    = 2,
00041                 STATE_PKG3_SENDING = 3,
00042                 STATE_PKG3_SENT    = 4,
00043                 STATE_PKG5_SENDING = 5,
00044                 STATE_PKG5_SENT    = 6
00045              } SyncManagerState ;
00046 
00047 
00048 // Tolerance to data size for incoming items (106%) -> will be allocated some more space.
00049 #define DATA_SIZE_TOLERANCE      1.06
00050 
00051 
00052 static void fillContentTypeInfoList(ArrayList &l, const char*  types);
00053 
00054 
00062 class SyncManager {
00063 
00064     public:
00072         SyncManager(SyncManagerConfig& config, SyncReport& report) EXTRA_SECTION_01;
00073         ~SyncManager() EXTRA_SECTION_01;
00074 
00075         int prepareSync(SyncSource** sources) EXTRA_SECTION_01;
00076 
00077         int sync() EXTRA_SECTION_01;
00078 
00079         int endSync() EXTRA_SECTION_01;
00080 
00091         virtual DevInf *createDeviceInfo() EXTRA_SECTION_01;
00092 
00093     private:
00094 
00095         // SyncManager makes local key safe to use in SyncML by
00096         // encoding it as b64 if it contains special characters. The
00097         // SyncML standard says that the key should be a "URI" or
00098         // "URN"; we interpret that less strictly as "do not
00099         // include characters which actually break XML".
00100         //
00101         // Encoded keys are sent as "funambol-b64-<encoded original
00102         // key>". When receiving a key from the server it is only decoded
00103         // if it contains this magic tag, therefore an updated client
00104         // remains compatible with a server that already contains keys.
00105         static const char encodedKeyPrefix[];
00106 
00107         void encodeItemKey(SyncItem *syncItem);
00108         void decodeItemKey(SyncItem *syncItem);
00109 
00110         // Struct used to pass command info to the method processSyncItem
00111         struct CommandInfo {
00112             const char* commandName;
00113             const char* cmdRef;
00114             const char* format;
00115             const char* dataType;
00116             long size;
00117         };
00118 
00119         DevInf* devInf;
00120         SyncManagerConfig& config;
00121         SyncReport& syncReport;
00122 
00123         CredentialHandler credentialHandler;
00124         SyncMLBuilder syncMLBuilder;
00125         SyncMLProcessor syncMLProcessor;
00126         TransportAgent* transportAgent;
00127 
00128         SyncManagerState currentState;
00129         SyncSource** sources;
00130         ArrayList* commands;
00131         ArrayList** mappings;
00132 
00133         // Now using sources[i].checkState() method
00134         //int* check;
00135 
00136         int  sourcesNumber;
00137         int  count;
00138 
00139         /* A list of syncsource names from server. The server sends sources
00140          * modifications sorted as alerts in this list. This array is retrieved from
00141          * SyncMLProcessor::getSortedSourcesFromServer.
00142          */
00143         char** sortedSourcesFromServer;
00144 
00145                 ArrayList** allItemsList;
00146 
00147         StringBuffer syncURL;
00148         StringBuffer deviceId;
00149         int responseTimeout;  // the response timeout for a rensponse from server (default = 5min) [in seconds]
00150         int maxMsgSize;       // the max message size. Default = 512k. Setting it implies LargeObject support.
00151         int maxObjSize;       // The maximum object size. The server gets this in the Meta init message and should obey it.
00152         BOOL loSupport;             // enable support for large objects - without it large outgoing items are not split
00153         unsigned int readBufferSize; // the size of the buffer to store chunk of incoming stream.
00154         char  credentialInfo[1024]; // used to store info for the des;b64 encription
00155 
00156         // Handling of incomplete incoming objects by processSyncItem().
00157         // Always active, even if Large Object support is off,
00158         // just in case the server happens to rely on it.
00159         //
00160         class IncomingSyncItem : public SyncItem {
00161           public:
00162             IncomingSyncItem(const WCHAR* key,
00163                              const CommandInfo &cmdInfo,
00164                              int currentSource) :
00165                 SyncItem(key),
00166                 offset(0),
00167                 cmdName(cmdInfo.commandName),
00168                 cmdRef(cmdInfo.cmdRef),
00169                 sourceIndex(currentSource) {
00170             }
00171 
00172             long offset;                // number of bytes already received, append at this point
00173             const StringBuffer cmdName; // name of the command which started the incomplete item
00174             const StringBuffer cmdRef;  // reference of the command which started the incomplete item
00175             const int sourceIndex;      // the index of the source to which the incomplete item belongs
00176         } *incomingItem;       // sync item which is not complete yet, more data expected
00177 
00178         void initialize() EXTRA_SECTION_01;
00179         BOOL readSyncSourceDefinition(SyncSource& source) EXTRA_SECTION_01;
00180         BOOL commitChanges(SyncSource& source) EXTRA_SECTION_01;
00181         int assignSources(SyncSource** sources) EXTRA_SECTION_01;
00182 
00183         Status *processSyncItem(Item* item, const CommandInfo &cmdInfo, SyncMLBuilder &syncMLBuilder) EXTRA_SECTION_01;
00184         BOOL checkForServerChanges(SyncML* syncml, ArrayList &statusList) EXTRA_SECTION_01;
00185 
00186         const char*  getUserAgent(SyncManagerConfig& config) EXTRA_SECTION_01;
00187         bool isToExit();
00188         void setSourceStateAndError(unsigned int index, SourceState  state,
00189                                     unsigned int code,  const char*  msg);
00190 
00191 
00192         // Used to reserve some more space (DATA_SIZE_TOLERANCE) for incoming items.
00193         long getToleranceDataSize(long size);
00194         bool testIfDataSizeMismatch(long allocatedSize, long receivedSize);
00195 
00204         SyncItem* getItem(SyncSource& source, SyncItem* (SyncSource::* getItem)());
00205 };
00206 
00209 #endif
00210 

Generated on Fri Jun 15 11:38:30 2007 for Funambol C++ Client Library by  doxygen 1.5.2