src/include/common/spds/SyncManager.h

00001 /*
00002  * Funambol is a mobile platform developed by Funambol, Inc. 
00003  * Copyright (C) 2003 - 2007 Funambol, Inc.
00004  * 
00005  * This program is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Affero General Public License version 3 as published by
00007  * the Free Software Foundation with the addition of the following permission 
00008  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
00009  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
00010  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015  * details.
00016  * 
00017  * You should have received a copy of the GNU Affero General Public License 
00018  * along with this program; if not, see http://www.gnu.org/licenses or write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00020  * MA 02110-1301 USA.
00021  * 
00022  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
00023  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
00024  * 
00025  * The interactive user interfaces in modified source and object code versions
00026  * of this program must display Appropriate Legal Notices, as required under
00027  * Section 5 of the GNU Affero General Public License version 3.
00028  * 
00029  * In accordance with Section 7(b) of the GNU Affero General Public License
00030  * version 3, these Appropriate Legal Notices must retain the display of the
00031  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
00032  * feasible for technical reasons, the Appropriate Legal Notices must display
00033  * the words "Powered by Funambol".
00034  */
00035 
00036 #ifndef INCL_SYNC_MANAGER
00037 #define INCL_SYNC_MANAGER
00038 
00042 #include "base/util/ArrayList.h"
00043 #include "http/TransportAgent.h"
00044 #include "spds/constants.h"
00045 #include "spds/SyncManagerConfig.h"
00046 #include "spds/SyncSource.h"
00047 #include "spds/SyncMLBuilder.h"
00048 #include "spds/SyncMLProcessor.h"
00049 #include "spds/CredentialHandler.h"
00050 #include "spds/CredentialHandler.h"
00051 #include "spds/SyncReport.h"
00052 
00053 
00054 typedef enum {
00055                 STATE_START        = 0,
00056                 STATE_PKG1_SENDING = 1,
00057                 STATE_PKG1_SENT    = 2,
00058                 STATE_PKG3_SENDING = 3,
00059                 STATE_PKG3_SENT    = 4,
00060                 STATE_PKG5_SENDING = 5,
00061                 STATE_PKG5_SENT    = 6
00062              } SyncManagerState ;
00063 
00064 
00065 // Tolerance to data size for incoming items (106%) -> will be allocated some more space.
00066 #define DATA_SIZE_TOLERANCE      1.06
00067 
00068 
00069 static void fillContentTypeInfoList(ArrayList &l, const char*  types);
00070 
00071 
00079 class SyncManager {
00080 
00081     public:
00089         SyncManager(SyncManagerConfig& config, SyncReport& report);
00090         ~SyncManager();
00091 
00092         int prepareSync(SyncSource** sources);
00093 
00094         int sync();
00095 
00096         int endSync();
00097 
00108         virtual DevInf *createDeviceInfo();
00109 
00110     private:
00111 
00112         // SyncManager makes local key safe to use in SyncML by
00113         // encoding it as b64 if it contains special characters. The
00114         // SyncML standard says that the key should be a "URI" or
00115         // "URN"; we interpret that less strictly as "do not
00116         // include characters which actually break XML".
00117         //
00118         // Encoded keys are sent as "funambol-b64-<encoded original
00119         // key>". When receiving a key from the server it is only decoded
00120         // if it contains this magic tag, therefore an updated client
00121         // remains compatible with a server that already contains keys.
00122         static const char encodedKeyPrefix[];
00123 
00124         void encodeItemKey(SyncItem *syncItem);
00125         void decodeItemKey(SyncItem *syncItem);
00126 
00127         // Struct used to pass command info to the method processSyncItem
00128         struct CommandInfo {
00129             const char* commandName;
00130             const char* cmdRef;
00131             const char* format;
00132             const char* dataType;
00133             long size;
00134         };
00135 
00136         DevInf* devInf;
00137         SyncManagerConfig& config;
00138         SyncReport& syncReport;
00139 
00140         CredentialHandler credentialHandler;
00141         SyncMLBuilder syncMLBuilder;
00142         SyncMLProcessor syncMLProcessor;
00143         TransportAgent* transportAgent;
00144 
00145         SyncManagerState currentState;
00146         SyncSource** sources;
00147         ArrayList* commands;
00148         ArrayList** mappings;
00149 
00150         // Now using sources[i].checkState() method
00151         //int* check;
00152 
00153         int  sourcesNumber;
00154         int  count;
00155 
00156         /* A list of syncsource names from server. The server sends sources
00157          * modifications sorted as alerts in this list. This array is retrieved from
00158          * SyncMLProcessor::getSortedSourcesFromServer.
00159          */
00160         char** sortedSourcesFromServer;
00161 
00162                 ArrayList** allItemsList;
00163 
00164         StringBuffer syncURL;
00165         StringBuffer deviceId;
00166         int responseTimeout;  // the response timeout for a rensponse from server (default = 5min) [in seconds]
00167         int maxMsgSize;       // the max message size. Default = 512k. Setting it implies LargeObject support.
00168         int maxObjSize;       // The maximum object size. The server gets this in the Meta init message and should obey it.
00169         BOOL loSupport;             // enable support for large objects - without it large outgoing items are not split
00170         unsigned int readBufferSize; // the size of the buffer to store chunk of incoming stream.
00171         char  credentialInfo[1024]; // used to store info for the des;b64 encription
00172 
00173         // Handling of incomplete incoming objects by processSyncItem().
00174         // Always active, even if Large Object support is off,
00175         // just in case the server happens to rely on it.
00176         //
00177         class IncomingSyncItem : public SyncItem {
00178           public:
00179             IncomingSyncItem(const WCHAR* key,
00180                              const CommandInfo &cmdInfo,
00181                              int currentSource) :
00182                 SyncItem(key),
00183                 offset(0),
00184                 cmdName(cmdInfo.commandName),
00185                 cmdRef(cmdInfo.cmdRef),
00186                 sourceIndex(currentSource) {
00187             }
00188 
00189             long offset;                // number of bytes already received, append at this point
00190             const StringBuffer cmdName; // name of the command which started the incomplete item
00191             const StringBuffer cmdRef;  // reference of the command which started the incomplete item
00192             const int sourceIndex;      // the index of the source to which the incomplete item belongs
00193         } *incomingItem;       // sync item which is not complete yet, more data expected
00194 
00195         void initialize();
00196         BOOL readSyncSourceDefinition(SyncSource& source);
00197         BOOL commitChanges(SyncSource& source);
00198         int assignSources(SyncSource** sources);
00199 
00200         Status *processSyncItem(Item* item, const CommandInfo &cmdInfo, SyncMLBuilder &syncMLBuilder);
00201         BOOL checkForServerChanges(SyncML* syncml, ArrayList &statusList);
00202 
00203         const char*  getUserAgent(SyncManagerConfig& config);
00204         bool isToExit();
00205         void setSourceStateAndError(unsigned int index, SourceState  state,
00206                                     unsigned int code,  const char*  msg);
00207 
00208 
00209         // Used to reserve some more space (DATA_SIZE_TOLERANCE) for incoming items.
00210         long getToleranceDataSize(long size);
00211         bool testIfDataSizeMismatch(long allocatedSize, long receivedSize);
00212 
00221         SyncItem* getItem(SyncSource& source, SyncItem* (SyncSource::* getItem)());
00222 
00227         void addMapCommand(int sourceIndex);
00228 };
00229 
00232 #endif
00233 

Generated on Thu Mar 6 14:25:05 2008 for Funambol C++ Client Library by  doxygen 1.5.2