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
00057 BEGIN_NAMESPACE
00058
00059
00060
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
00188 int32_t totalBytesSent;
00189 int32_t totalBytesReceived;
00190
00192 CTPThreadPool threadPool;
00193
00194 private:
00195
00196
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
00217 CTPService();
00218
00219
00220 public:
00221
00222
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
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