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
00204 protected:
00205
00206
00207 CTPService();
00208
00209
00210 public:
00211
00212
00213 static CTPService* getInstance();
00214
00215
00216 static void dispose();
00217
00218 ~CTPService();
00219
00220 FThread* startCTP();
00221 int32_t stopCTP();
00222 int32_t openConnection();
00223 int32_t closeConnection();
00224 int32_t receive();
00225
00226
00227 int32_t sendReadyMsg();
00228 int32_t sendAuthMsg();
00229 int32_t sendByeMsg();
00230
00231 CTPMessage* receiveStatusMsg();
00232
00236 CTPConfig* getConfig() { return &config; }
00237
00239 CtpState getCtpState() { return ctpState; }
00240
00242 void setCtpState(CtpState v) { ctpState = v; }
00243
00245 bool isLeaving() { return leaving; }
00246
00249 void setLeaving(bool value) { leaving = value; }
00250
00251
00263 void registerPushListener(PushListener& listener) { pushListener = &listener; }
00264
00273 void syncNotificationReceived(SyncNotification* sn);
00274
00282 void notifyError(const int errorCode, const int additionalInfo = 0);
00283
00284
00286 void stopHeartbeatThread();
00287
00289 void stopCmdTimeoutThread();
00290
00292 void stopReceiverThread();
00293
00295 void stopCtpThread();
00296
00297
00307 ArrayList getUriListFromSAN(SyncNotification* sn);
00308
00309
00310 private:
00311 void hexDump(char *buf, int len);
00312 int extractMsgLength(const char* package, int packageLen);
00313 bool saveNonceParam(CTPMessage* authStatusMsg);
00314
00320 bool stopThread(FThread* thread);
00321 };
00322
00323
00324
00325 END_NAMESPACE
00326
00328 #endif
00329