syncnotification.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 234 行
CPP
234 行
/*
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*/
#include "base/fscapi.h"
#include "base/Log.h"
#include "base/util/utils.h"
#include "syncml/core/SyncNotification.h"
#include "base/globalsdef.h"
USE_NAMESPACE
/*
* This class represent a Server Alerted Notification for DS message (Pkg0).
* After a call to method parse(), the object contains the message data in
* a usable form.
*/
////////////////////// Support datatypes
#pragma pack(1)
// Notification header
union notification_hdr {
struct {
unsigned version:10;
unsigned ui_mode:2;
unsigned initiator:1;
unsigned reserved1:19;
unsigned reserved2:8;
unsigned session_id:16;
unsigned server_id_len:8;
} s;
struct {
unsigned long w1;
unsigned long w2;
} r;
};
struct sync_hdr {
unsigned num_sync: 4;
unsigned reserved: 4;
};
union sync_body {
struct {
unsigned reserved: 4;
unsigned sync_type: 4;
unsigned content_type: 24;
} s;
unsigned long r;
};
/*
* Constructor
*/
SyncNotification::SyncNotification()
{
reset(false);
}
/*
* Destructor
*/
SyncNotification::~SyncNotification() {
reset(true);
}
// Reset attributes, freing memory if needed
void SyncNotification::reset(bool free) {
if(free){
if (syncAlerts)
delete [] syncAlerts;
if (serverId)
delete [] serverId;
}
version=0;
uiMode=UI_NotSpecified;
initiator=ServerInitiated;
sessionId=0;
serverId=0;
numSyncs=0;
syncAlerts=0;
}
/**
* @brief Processes the PKG0 message, populating the
* object's attributes.
*
* @param msg the PKG0 message from the server
*
* @returns 0 on success, an error code defined by
* NotifProcErrors otherwise.
*/
SN_Errors SyncNotification::parse (const char *msg, int len)
{
// This macro increments local pointer p or return error if out of bounds.
#define IncP(Num) if((p+(Num))<=end) p+=(Num); else return SNErr_Incomplete
const char *digest;
notification_hdr *hdr;
int server_id_len=10;
const char *p=msg, *end=msg+len; // message cursor and boundary
//LOG.setLevel(LOG_LEVEL_DEBUG);
//LOG.debug("SyncNotification::parse START....");
if(!msg){
return SNErr_NullValue;
}
reset(true);
// TODO: check MD5!
digest=msg; IncP(16);
// Cast message to header
hdr = (notification_hdr *)p;
// copy header values
version = hdr->s.version;
uiMode = (SN_UIMode) hdr->s.ui_mode;
initiator = (SN_Initiator) hdr->s.initiator;
sessionId = hdr->s.session_id;
server_id_len = hdr->s.server_id_len;
IncP(8);
//LOG.debug("After header.");
// get server identifier
serverId = stringdup(p, server_id_len);
IncP(server_id_len);
//char dbg[256];
//sprintf(dbg, "serverId: %s", serverId);
//LOG.debug(dbg);
// get num sync
numSyncs = (*p)>>4;
IncP(1);
//sprintf(dbg, "numSyncs: %d", numSyncs);
//LOG.debug(dbg);
// allocate syncalert objects
syncAlerts = new SyncAlert[numSyncs];
// Init all sync items
for(int i=0; i<numSyncs; i++){
char *tmps;
sync_body *sync;
unsigned char server_URI_len;
int ret;
sync = (sync_body *)p;
IncP(sizeof(sync_body));
// set syncAlert attributes
server_URI_len = *p;
IncP(1);
tmps = stringdup(p, server_URI_len);
ret = syncAlerts[i].set(sync->s.sync_type, sync->s.content_type, tmps);
delete [] tmps;
// Check errors
if (ret){
return SNErr_InvSync;
}
IncP(server_URI_len);
}
return SNErr_Ok;
}
// Accessor methods
int SyncNotification::getVersion() {
return version;
}
SN_UIMode SyncNotification::getUIMode() {
return uiMode;
}
SN_Initiator SyncNotification::getInitiator() {
return initiator;
}
int SyncNotification::getSessionId() {
return sessionId;
}
const char *SyncNotification::getServerId() {
return serverId;
}
int SyncNotification::getNumSyncs() {
return numSyncs;
}
SyncAlert* SyncNotification::getSyncAlert ( int idx ) {
if (idx < 0 || idx > numSyncs)
return 0;
return &syncAlerts[idx];
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?