src/include/common/push/CTPService.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_CTP_SERVICE
00037 #define INCL_CTP_SERVICE
00038 
00041 #include "base/globalsdef.h"
00042 #include "base/fscapi.h"
00043 
00044 #include "push/FThread.h"
00045 #include "push/FSocket.h"
00046 #include "push/PushListener.h"
00047 #include "push/CTPMessage.h"
00048 #include "push/CTPConfig.h"
00049 #include "push/CTPThreadPool.h"
00050 
00052 #define CTP_PROTOCOL_VERSION            0x10
00053 
00054 #define CTP_RETRY_INCREASE_FACTOR       2
00055 
00056 
00057 BEGIN_NAMESPACE
00058 
00059 
00060 // Private Threads
00061 class CTPThread : public FThread {
00062 
00063 public:
00064     CTPThread();
00065     ~CTPThread();
00066     void run();
00067     int32_t getErrorCode() { return errorCode; }
00068 
00069 private:
00070     int32_t errorCode;
00071     bool saveNonceParam(CTPMessage* authStatusMsg);
00072 };
00073 
00074 class ReceiverThread : public FThread {
00075 public:
00076     ReceiverThread();
00077     ~ReceiverThread();
00078     void run();
00079     int32_t getErrorCode() { return errorCode; }
00080 
00081 private:
00082     int32_t errorCode;
00083 };
00084 
00085 class HeartbeatThread : public FThread {
00086 public:
00087     HeartbeatThread();
00088     ~HeartbeatThread();
00089     void run();
00090     int32_t getErrorCode() { return errorCode; }
00091     void softTerminate();
00092 
00093 private:
00094     int32_t errorCode;
00095 
00096 };
00097 
00098 class CmdTimeoutThread : public FThread {
00099 
00100 public:
00101     CmdTimeoutThread();
00102     ~CmdTimeoutThread();
00103     void run();
00104     void softTerminate();
00105 };
00106 
00107 
00111 class CTPService {
00112 
00113 public:
00120     typedef enum {
00121         CTP_STATE_DISCONNECTED          = 0, 
00122         CTP_STATE_SLEEPING              = 1,
00123         CTP_STATE_CONNECTING            = 2, 
00124         CTP_STATE_CONNECTED             = 3,
00125         CTP_STATE_AUTHENTICATING        = 4, 
00126         CTP_STATE_READY                 = 5, 
00127         CTP_STATE_WAITING_RESPONSE      = 6, 
00128         CTP_STATE_CLOSING               = 7
00129     } CtpState;
00130     
00134     typedef enum {
00135         CTP_ERROR_NOT_AUTHENTICATED         = 1,
00136         CTP_ERROR_UNAUTHORIZED              = 2, 
00137         CTP_ERROR_AUTH_FORBIDDEN            = 3, 
00138         CTP_ERROR_RECEIVED_UNKNOWN_COMMAND  = 4,
00139         CTP_ERROR_RECEIVED_STATUS_ERROR     = 5, 
00140         CTP_ERROR_RECEIVED_WRONG_COMMAND    = 6, 
00141         CTP_ERROR_ANOTHER_INSTANCE          = 7,
00142         CTP_ERROR_SENDING_READY             = 8,
00143         CTP_ERROR_RECEIVING_STATUS          = 9,
00144         CTP_ERROR_RECEIVE_TIMOUT            = 10,
00145         CTP_ERROR_CONNECTION_FAILED         = 11
00146     } CtpError;
00147 
00148 private:
00149 
00151     static CTPService* pinstance;
00152 
00153 
00155     CTPConfig config;
00156 
00158     CtpState ctpState;
00159 
00161     bool leaving;
00162 
00164     FSocket* ctpSocket;
00165     
00172     PushListener* pushListener;
00173     
00174 
00176     CTPThread* ctpThread;                   
00178     ReceiverThread* receiverThread;
00180     HeartbeatThread* heartbeatThread;
00182     CmdTimeoutThread* cmdTimeoutThread;
00183 
00185     CTPMessage* receivedMsg;
00186 
00187     // For debugging
00188     int32_t totalBytesSent;
00189     int32_t totalBytesReceived;
00190 
00192     CTPThreadPool threadPool;
00193 
00194 private:
00195 
00196     // Private methods:
00197     int32_t sendMsg(CTPMessage* message);
00198     StringBuffer createMD5Credentials();
00199     StringBuffer createErrorMsg(uint32_t errorCode = 0);
00200     
00201 
00211     ArrayList getUriListFromSAN(SyncNotification* sn);
00212 
00213     
00214 protected:
00215 
00216     // Constructor
00217     CTPService();
00218 
00219 
00220 public:
00221 
00222     // Method to get the sole instance of CTPService
00223     static CTPService* getInstance();
00224 
00225     ~CTPService();
00226 
00227     FThread* startCTP();
00228     int32_t stopCTP();
00229     int32_t openConnection();
00230     int32_t closeConnection();
00231     int32_t receive();
00232 
00233     // Create and send messages through the socket.
00234     int32_t sendReadyMsg();
00235     int32_t sendAuthMsg();
00236     int32_t sendByeMsg();
00237 
00238     CTPMessage* receiveStatusMsg();
00239 
00243     CTPConfig* getConfig() { return &config; }
00244 
00246     CtpState getCtpState()    { return ctpState; }
00247 
00249     void setCtpState(CtpState v) { ctpState = v; }
00250 
00252     bool isLeaving() { return leaving; }
00253 
00256     void setLeaving(bool value) { leaving = value; }
00257     
00258     
00270     void registerPushListener(PushListener& listener) { pushListener = &listener; }
00271     
00280     void syncNotificationReceived(SyncNotification* sn);
00281     
00289     void notifyError(const int errorCode, const int additionalInfo = 0);
00290     
00291     
00293     void stopHeartbeatThread();
00294     
00296     void stopCmdTimeoutThread();
00297     
00299     void stopReceiverThread();
00300     
00302     void stopCtpThread();
00303 
00304     
00305 private:
00306     void hexDump(char *buf, int len);
00307     int extractMsgLength(const char* package, int packageLen);
00308     bool saveNonceParam(CTPMessage* authStatusMsg);
00309     
00315     bool stopThread(FThread* thread);
00316 };
00317 
00318 
00319 
00320 END_NAMESPACE
00321 
00323 #endif
00324 

Generated on Fri Jul 25 15:04:15 2008 for Funambol C++ Client Library by  doxygen 1.5.1