00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
00190 int32_t totalBytesSent;
00191 int32_t totalBytesReceived;
00192
00194 CTPThreadPool threadPool;
00195
00196 private:
00197
00198
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
00219 CTPService();
00220
00221
00222 public:
00223
00224
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
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