src/include/common/spds/SyncManager.h

00001 /*
00002  * Copyright (C) 2003-2007 Funambol
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 as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  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 maxModPerMsg;  // the max modification per message
00154         unsigned int readBufferSize; // the size of the buffer to store chunk of incoming stream.
00155         char  credentialInfo[1024]; // used to store info for the des;b64 encription
00156 
00157         // Handling of incomplete incoming objects by processSyncItem().
00158         // Always active, even if Large Object support is off,
00159         // just in case the server happens to rely on it.
00160         // 
00161         class IncomingSyncItem : public SyncItem {
00162           public:
00163             IncomingSyncItem(const WCHAR* key,
00164                              const CommandInfo &cmdInfo,
00165                              int currentSource) :
00166                 SyncItem(key),
00167                 offset(0),
00168                 cmdName(cmdInfo.commandName),
00169                 cmdRef(cmdInfo.cmdRef),
00170                 sourceIndex(currentSource) {
00171             }
00172             
00173             long offset;                // number of bytes already received, append at this point
00174             const StringBuffer cmdName; // name of the command which started the incomplete item
00175             const StringBuffer cmdRef;  // reference of the command which started the incomplete item
00176             const int sourceIndex;      // the index of the source to which the incomplete item belongs
00177         } *incomingItem;       // sync item which is not complete yet, more data expected
00178     
00179         void initialize() EXTRA_SECTION_01;
00180         BOOL readSyncSourceDefinition(SyncSource& source) EXTRA_SECTION_01;
00181         BOOL commitChanges(SyncSource& source) EXTRA_SECTION_01;
00182         int assignSources(SyncSource** sources) EXTRA_SECTION_01;
00183         
00184         Status *processSyncItem(Item* item, const CommandInfo &cmdInfo, SyncMLBuilder &syncMLBuilder) EXTRA_SECTION_01;
00185         BOOL checkForServerChanges(SyncML* syncml, ArrayList &statusList) EXTRA_SECTION_01;
00186 
00187         const char*  getUserAgent(SyncManagerConfig& config) EXTRA_SECTION_01;
00188         bool isToExit();
00189         void setSourceStateAndError(unsigned int index, SourceState  state,
00190                                     unsigned int code,  const char*  msg);
00191 
00192 
00193         // Used to reserve some more space (DATA_SIZE_TOLERANCE) for incoming items.
00194         long getToleranceDataSize(long size);
00195         bool testIfDataSizeMismatch(long allocatedSize, long receivedSize);
00196           
00205         SyncItem* getItem(SyncSource& source, SyncItem* (SyncSource::* getItem)());
00206 };
00207 
00210 #endif
00211 

Generated on Fri Apr 27 12:29:18 2007 for Funambol C++ Client Library by  doxygen 1.5.2