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 #define CTP_SEND_BYE_MSG                0   
00059 BEGIN_NAMESPACE
00060 
00061 
00062 // Private Threads
00063 class CTPThread : public FThread {
00064 
00065 public:
00066     CTPThread();
00067     ~CTPThread();
00068     void run();
00069     int32_t getErrorCode() { return errorCode; }
00070 
00071 private:
00072     int32_t errorCode;
00073     bool saveNonceParam(CTPMessage* authStatusMsg);
00074 };
00075 
00076 class ReceiverThread : public FThread {
00077 public:
00078     ReceiverThread();
00079     ~ReceiverThread();
00080     void run();
00081     int32_t getErrorCode() { return errorCode; }
00082 
00083 private:
00084     int32_t errorCode;
00085 };
00086 
00087 class HeartbeatThread : public FThread {
00088 public:
00089     HeartbeatThread();
00090     ~HeartbeatThread();
00091     void run();
00092     int32_t getErrorCode() { return errorCode; }
00093     void softTerminate();
00094 
00095 private:
00096     int32_t errorCode;
00097 
00098 };
00099 
00100 class CmdTimeoutThread : public FThread {
00101 
00102 public:
00103     CmdTimeoutThread();
00104     ~CmdTimeoutThread();
00105     void run();
00106     void softTerminate();
00107 };
00108 
00109 
00113 class CTPService {
00114 
00115 public:
00122     typedef enum {
00123         CTP_STATE_DISCONNECTED          = 0, 
00124         CTP_STATE_SLEEPING              = 1,
00125         CTP_STATE_CONNECTING            = 2, 
00126         CTP_STATE_CONNECTED             = 3,
00127         CTP_STATE_AUTHENTICATING        = 4, 
00128         CTP_STATE_READY                 = 5, 
00129         CTP_STATE_WAITING_RESPONSE      = 6, 
00130         CTP_STATE_CLOSING               = 7
00131     } CtpState;
00132     
00136     typedef enum {
00137         CTP_ERROR_NOT_AUTHENTICATED         = 1,
00138         CTP_ERROR_UNAUTHORIZED              = 2, 
00139         CTP_ERROR_AUTH_FORBIDDEN            = 3, 
00140         CTP_ERROR_RECEIVED_UNKNOWN_COMMAND  = 4,
00141         CTP_ERROR_RECEIVED_STATUS_ERROR     = 5, 
00142         CTP_ERROR_RECEIVED_WRONG_COMMAND    = 6, 
00143         CTP_ERROR_ANOTHER_INSTANCE          = 7,
00144         CTP_ERROR_SENDING_READY             = 8,
00145         CTP_ERROR_RECEIVING_STATUS          = 9,
00146         CTP_ERROR_RECEIVE_TIMOUT            = 10,
00147         CTP_ERROR_CONNECTION_FAILED         = 11
00148     } CtpError;
00149 
00150 private:
00151 
00153     static CTPService* pinstance;
00154 
00155 
00157     CTPConfig config;
00158 
00160     CtpState ctpState;
00161 
00163     bool leaving;
00164 
00166     FSocket* ctpSocket;
00167     
00174     PushListener* pushListener;
00175     
00176 
00178     CTPThread* ctpThread;                   
00180     ReceiverThread* receiverThread;
00182     HeartbeatThread* heartbeatThread;
00184     CmdTimeoutThread* cmdTimeoutThread;
00185 
00187     CTPMessage* receivedMsg;
00188 
00189     // For debugging
00190     int32_t totalBytesSent;
00191     int32_t totalBytesReceived;
00192 
00194     CTPThreadPool threadPool;
00195 
00196 private:
00197 
00198     // Private methods:
00199     int32_t sendMsg(CTPMessage* message);
00200     StringBuffer createMD5Credentials();
00201     StringBuffer createErrorMsg(uint32_t errorCode = 0);
00202     
00203 
00213     ArrayList getUriListFromSAN(SyncNotification* sn);
00214 
00215     
00216 protected:
00217 
00218     // Constructor
00219     CTPService();
00220 
00221 
00222 public:
00223 
00224     // Method to get the sole instance of CTPService
00225     static CTPService* getInstance();
00226 
00227     ~CTPService();
00228 
00229     FThread* startCTP();
00230     int32_t stopCTP();
00231     int32_t openConnection();
00232     int32_t closeConnection();
00233     int32_t receive();
00234 
00235     // Create and send messages through the socket.
00236     int32_t sendReadyMsg();
00237     int32_t sendAuthMsg();
00238     int32_t sendByeMsg();
00239 
00240     CTPMessage* receiveStatusMsg();
00241 
00245     CTPConfig* getConfig() { return &config; }
00246 
00248     CtpState getCtpState()    { return ctpState; }
00249 
00251     void setCtpState(CtpState v) { ctpState = v; }
00252 
00254     bool isLeaving() { return leaving; }
00255 
00258     void setLeaving(bool value) { leaving = value; }
00259     
00260     
00272     void registerPushListener(PushListener& listener) { pushListener = &listener; }
00273     
00282     void syncNotificationReceived(SyncNotification* sn);
00283     
00291     void notifyError(const int errorCode, const int additionalInfo = 0);
00292     
00293     
00295     void stopHeartbeatThread();
00296     
00298     void stopCmdTimeoutThread();
00299     
00301     void stopReceiverThread();
00302     
00304     void stopCtpThread();
00305 
00306     
00307 private:
00308     void hexDump(char *buf, int len);
00309     int extractMsgLength(const char* package, int packageLen);
00310     bool saveNonceParam(CTPMessage* authStatusMsg);
00311     
00317     bool stopThread(FThread* thread);
00318 };
00319 
00320 
00321 
00322 END_NAMESPACE
00323 
00325 #endif
00326 

Generated on Wed Jan 14 17:15:36 2009 for Funambol C++ Client Library by  doxygen 1.5.2