📄 pimserver.cpp
字号:
smlLibMemset(&docInfo,0,(MemSize_t)sizeof(XptCommunicationInfo_t));
docInfo.cbSize = sizeof(docInfo);
docInfo.cbLength = bytesRead;
docInfo.auth = NULL;
strcpy(docInfo.mimeType, "application/vnd.syncml+wbxml");
strcpy(docInfo.docName, "sync");
if(xptSetDocumentInfo(conn, &docInfo)==SML_ERR_OK)
{
if(xptSendData(conn, readBuffer, bytesRead, &bytesSent)==SML_ERR_OK)
{
}
xptSendComplete(conn);
}
if(pSess->m_pHead!=NULL){
pSess->m_pEnd = NULL;
pSess->m_pHead = NULL;
}
}
void tmlReceiveData(InstanceID_t id, XptServiceID_t serviceID,XptCommunicationID_t conn,XptCommunicationInfo_t* pdocInfo)
{
MemPtr_t tmlwriteBuffer = NULL;
MemSize_t tmlbytesWritten = 0;
size_t tmlbytesReceived = 0;
size_t tmlpkgbytesReceived = 0;
VoidPtr_t tmluserData = NULL;
MemPtr_t tmlreadBuffer = NULL;
MemSize_t tmlbytesRead = 0;
size_t tmlbytesSent = 0;
tmlLockWriteBuffer(id, &tmlwriteBuffer, &tmlbytesWritten);
XPTDEBUG(("tmlBuffer (%d,%d)\n",tmlwriteBuffer,tmlbytesWritten));
while(true)
{
C(xptReceiveData(conn, tmlwriteBuffer+tmlpkgbytesReceived, tmlbytesWritten, &tmlbytesReceived));
tmlpkgbytesReceived = tmlpkgbytesReceived + tmlbytesReceived;
XPTDEBUG(("T(text/plain)(time=%s) =%d \n",_get_nowtime(),pdocInfo->cbLength));
if(tmlpkgbytesReceived>=(int)pdocInfo->cbLength){
break;
}
if((int)pdocInfo->cbLength<=0){
break;
}
if(tmlbytesReceived<=0){
break;
}
}
tmlwriteBuffer[tmlpkgbytesReceived] = 0;
if(tmlpkgbytesReceived<15000)
XPTDEBUG(("Received Data(text/plain) %d= \n '%s'\n",tmlpkgbytesReceived, tmlwriteBuffer));
Ret_t t = tmlUnlockWriteBuffer(id, tmlpkgbytesReceived);
C(tmlProcessData(id, TML_ALL_COMMANDS));
tmlUnlockReadBuffer(id, tmlpkgbytesReceived);
tmlLockReadBuffer(id, &tmlreadBuffer, &tmlbytesRead);
tmlUnlockReadBuffer(id, tmlbytesRead);
CTmlSession*pSess = tmgr.GetActiveSession();
if(pSess!=NULL)
{
pSess->m_nCmdID = 1;
if(pSess->m_pHead!=NULL)
{
TmlSessionCmdItem_t*ptmp = pSess->m_pHead ;
TmlCmdTHdr_s* pTHdr = NULL;
while(ptmp!=NULL)
{
if(ptmp->ItemType==TML_PE_THDR)
{
pTHdr = (TmlCmdTHdr_s*) ptmp->CmdItem;
pSess->HandleStartMessage(id,pTHdr);
}
else if(ptmp->ItemType==TML_PE_RESOURCE)
{
TmlCmdResource_s* pRes = (TmlCmdResource_s*) ptmp->CmdItem;
XPTDEBUG(("pSess->m_nIsSend =%d\n",pSess->m_nIsSend));
if(pSess->m_nIsSend == 1)
pSess->HandleResource( id,pRes);
else
pSess->HandleSendResource( id,pRes);
//TmlCmdResource_s* pRes = (TmlCmdResource_s*) ptmp->CmdItem;
//pSess->HandleResource( id,pRes);
}
else if(ptmp->ItemType==TML_PE_CONTACT)
{
TmlCmdContact_s* pRes = (TmlCmdContact_s*) ptmp->CmdItem;
XPTDEBUG(("pSess->m_nIsSend =%d\n",pSess->m_nIsSend));
pSess->HandleContact( id,pRes);
}
else if(ptmp->ItemType==TML_PE_RESOURCESDIRECTORY)
{
TmlCmdResourcesDirectory_s* pResDir = (TmlCmdResourcesDirectory_s*) ptmp->CmdItem;
pSess->HandleResourcesDirectory(id,pResDir);
}
else if(ptmp->ItemType==TML_PE_MAGICMMSS_START)
{
TmlCmdMagicmmss_s* pMagicmmss = (TmlCmdMagicmmss_s*) ptmp->CmdItem;
pSess->HandleMagicmmss(id,pMagicmmss);
}
else if(ptmp->ItemType==TML_PE_RESOURCES_START)
{
//TmlCmdMagicmmss_s* pMagicmmss = (TmlCmdMagicmmss_s*) ptmp->CmdItem;
//pSess->HandleMagicmmss(id,pMagicmmss);
}
else if(ptmp->ItemType==TML_PE_CONTACTS_START)
{
TmlCmdContacts_s* pContacts = (TmlCmdContacts_s*) ptmp->CmdItem;
pSess->HandleContacts(id,pContacts);
}
else if(ptmp->ItemType==TML_PE_MAGICMMS)
{
TmlCmdMagicmms_s* pMagicmms = (TmlCmdMagicmms_s*) ptmp->CmdItem;
pSess->HandleMagicmms(id,pMagicmms);
}
else if(ptmp->ItemType==TML_PE_MSTATUS)
{
TmlCmdMStatus_s* pMStatus = (TmlCmdMStatus_s*) ptmp->CmdItem;
pSess->HandleMStatus(id,pMStatus);
}
else if(ptmp->ItemType==TML_PE_RSTATUS)
{
TmlCmdRStatus_s* pRStatus = (TmlCmdRStatus_s*) ptmp->CmdItem;
pSess->HandleRStatus(id,pRStatus);
}
else if(ptmp->ItemType==TML_PE_CSTATUS)
{
TmlCmdCStatus_s* pCStatus = (TmlCmdCStatus_s*) ptmp->CmdItem;
pSess->HandleCStatus(id,pCStatus);
}
else if(ptmp->ItemType==TML_PE_CSTATUSS_START)
{
TmlCmdCStatuss_s* pCStatuss = (TmlCmdCStatuss_s*) ptmp->CmdItem;
pSess->HandleCStatuss(id,pCStatuss);
}
else if(ptmp->ItemType==TML_PE_CONTACTS_END)
{
//TmlCmdCStatuss_s* pCStatuss = (TmlCmdCStatuss_s*) ptmp->CmdItem;
pSess->HandleEndContacts(id);
}
else if(ptmp->ItemType==TML_PE_CSTATUSS_END)
{
//TmlCmdCStatuss_s* pCStatuss = (TmlCmdCStatuss_s*) ptmp->CmdItem;
//pSess->HandleCStatuss(id,pCStatuss);
}
else if(ptmp->ItemType==TML_PE_FINAL)
{
XPTDEBUG(("TML_PE_FINAL\n"));
TmlCmdFinal_s * pFinal = (TmlCmdFinal_s*) ptmp->CmdItem;
pSess->HandleFinal(id,pFinal);
}
else
{
XPTDEBUG(("ptmp->ItemType=%d\n",ptmp->ItemType));
}
ptmp = ptmp->next;
}
if(pSess->m_pHead!=NULL)
{
pSess->m_pEnd = NULL;
pSess->m_pHead = NULL;
}
}
tmlFinal(id,tmlString2Pcdata("1"));
tmlEndMessage(id, TmlFinal_f);
tmlUnlockReadBuffer(id, tmlpkgbytesReceived);
tmlLockReadBuffer(id, &tmlreadBuffer, &tmlbytesRead);
tmlreadBuffer[tmlbytesRead]=0;
//if(tmlbytesRead<15000)
XPTDEBUG(("Sended Data = '%s'\n", tmlreadBuffer));
printf((char*)tmlreadBuffer);
}
tmlUnlockReadBuffer(id, tmlbytesRead);
//发送数据
XptCommunicationInfo_t docInfo;
tmlLibMemset(&docInfo,0,(MemSize_t)sizeof(XptCommunicationInfo_t));
docInfo.cbSize = sizeof(docInfo);
docInfo.cbLength = tmlbytesRead;
docInfo.auth = NULL;
strcpy(docInfo.mimeType, "text/plain");
strcpy(docInfo.docName, "sync");
if(xptSetDocumentInfo(conn, &docInfo)==SML_ERR_OK)
{
if(xptSendData(conn, tmlreadBuffer, tmlbytesRead, &tmlbytesSent)==SML_ERR_OK)
{
}
xptSendComplete(conn);
}
if(pSess->m_nStoreFlag==0)
tmgr.DeleteSession(pSess);
}
void myReceiveData(InstanceID_t id, XptServiceID_t serviceID)
{
XptCommunicationID_t conn = 0;
XptCommunicationInfo_t docInfo;
C(xptOpenCommunication(serviceID, XPT_REQUEST_RECEIVER, &conn));
C(xptBeginExchange(conn));
int rc=0;
rc = xptGetDocumentInfo(conn, &docInfo);
if(rc !=SML_ERR_OK)
{
printf("%s:%d:rc = %d, hex(rc) = %x\n", __FILE__, __LINE__, rc, rc);
const XptErrorInformation_t *errInfo = xptGetLastError();
printf("ErrInfo =%s\n",errInfo->errorMessage);
}
if(strcmp(docInfo.mimeType,"text/plain")==0)
{
tmlReceiveData(id, serviceID,conn,&docInfo);
}
else
{
smlReceiveData(id, serviceID,conn,&docInfo);
}
C(xptEndExchange(conn));
C(xptCloseCommunication(conn));
}
//返回0表示处理完成,非0表示剩余处理个数
int myGetReplace(InstanceID_t id,CSyncSession*pSess)
{
char _sql[1024];
char _vert[100];
int _count = 0;
CMySet _set;
int _remainbytes = _get_remainbytes(id,pSess);
//如果慢同步,则把所有的flag=0的更新为3
sprintf(_sql,"select count(*) from pim_contact where userid='%s' and flag=3",pSess->m_sUser);
if(workdb.PrepareSql(_sql, _set)==FALSE){
XPTDEBUG(("SQL='%s' error='%s'\n",_sql,workdb.GetSqlError()));
}
else{
if(workdb.FetchData()){
_count = atoi(_set.GetFieldValue(0));
XPTDEBUG(("SQL='%s' count=%d\n",_sql,_count));
if(_count==0){
return 0;
}
else{
XPTDEBUG(("SQL='%s' error='%s'\n",_sql,workdb.GetSqlError()));
return 0;
}
}
}
sprintf(_sql,"select contact_cell,contact_name,locuri from pim_contact where userid='%s' and flag=3",pSess->m_sUser);
if(workdb.PrepareSql(_sql, _set)==FALSE){
XPTDEBUG(("SQL='%s' error='%s'\n",_sql,workdb.GetSqlError()));
}
else{
char _cell[1024];
char _name[1024];
char _locuri[1024];
while(workdb.FetchData()&&_remainbytes){
strcpy(_cell , _set.GetFieldValue(0));
strcpy(_name , _set.GetFieldValue(1));
strcpy(_locuri, _set.GetFieldValue(2));
XPTDEBUG(("cell=%s\n",_cell));
XPTDEBUG(("name=%s\n",_name));
XPTDEBUG(("locuri=%s\n",_locuri));
CVcard _card;
char _target[1024];
_card.Encode(_name,_cell,_target);
SmlReplace_t _replace;
smlLibMemset(&_replace,0,(MemSize_t)sizeof(SmlReplace_t));
_replace.elementType = SML_PE_REPLACE;
sprintf(_vert , "%d",pSess->m_nCmdID++);
_replace.cmdID = smlString2Pcdata(_vert);
_replace.meta = (SmlPcdataPtr_t)smlLibMalloc(sizeof(SmlPcdata_t));
smlLibMemset(_replace.meta, 0, sizeof(SmlPcdata_t));
_replace.meta->contentType=SML_PCDATA_EXTENSION;
_replace.meta->extension=SML_EXT_METINF;
_replace.meta->content = (SmlMetInfMetInfPtr_t)smlLibMalloc(sizeof(SmlMetInfMetInf_t));
smlLibMemset(_replace.meta->content, 0, sizeof(SmlMetInfMetInf_t));
SmlMetInfMetInfPtr_t _pMeta = (SmlMetInfMetInfPtr_t)(_replace.meta->content);
_pMeta->type = smlString2Pcdata("text/x-vcard") ;
_replace.itemList = (SmlItemListPtr_t)smlLibMalloc(sizeof(SmlItemList_t));
smlLibMemset(_replace.itemList, 0, sizeof(SmlItemList_t));
SmlItemPtr_t _pItem = (SmlItemPtr_t)smlLibMalloc(sizeof(SmlItem_t));
smlLibMemset(_pItem, 0, sizeof(SmlItem_t));
_replace.itemList->item = _pItem;
_pItem->target = (SmlSourcePtr_t)smlLibMalloc(sizeof(SmlSource_t));
_pItem->target->locURI = smlString2Pcdata(_locuri);
_pItem->target->locName = NULL;
_pItem->data =smlString2Pcdata(_target) ;
smlReplaceCmd( id, &_replace);
_remainbytes = _get_remainbytes(id,pSess);
}
}
sprintf(_sql,"select count(*) from pim_contact where userid='%s' and flag=3",pSess->m_sUser);
if(workdb.PrepareSql(_sql,_set)==FALSE){
XPTDEBUG(("SQL='%s' error='%s'\n",_sql,workdb.GetSqlError()));
}
else{
if(workdb.FetchData()){
_count = atoi(_set.GetFieldValue(0));
XPTDEBUG(("SQL='%s' count=%d\n",_sql,_count));
if(_count==0){
return 0;
}
}
else{
XPTDEBUG(("sql=%s error=%s\n",_sql,workdb.GetSqlError()));
return 0;
}
}
return _count;
}
int myGetAdd(InstanceID_t id,CSyncSession*pSess)
{
char _sql[1024];
char _vert[100];
CMySet _set;
int _count = 0;
int _remainbytes = _get_remainbytes(id,pSess);
sprintf(_sql,"select count(*) from pim_contact where userid='%s' and flag=1",pSess->m_sUser);
if(workdb.PrepareSql(_sql,_set)==FALSE){
XPTDEBUG(("SQL='%s' error='%s'\n",_sql,workdb.GetSqlError()));
return 0;
}
else{
if(workdb.FetchData()){
_count = atoi(_set.GetFieldValue(0));
XPTDEBUG(("sql=%s Count=%d\n",_sql,_count));
if(_count==0){
XPTDEBUG(("set Sync_ServerSyncOK\n"));
pSess->m_eServerState = Sync_ServerSyncOK;
return 0;
}
}
}
int _guid ;
sprintf(_sql,"select contact_cell,contact_name,id from pim_contact where userid='%s' and flag=1 order by id ",pSess->m_sUser);
if(workdb.PrepareSql(_sql, _set)==FALSE){
XPTDEBUG(("sql=%s error=%s\n",_sql,workdb.GetSqlError()));
return 0;
}
else{
char _cell[1024];
char _name[1024];
char _locuri[1024];
while(workdb.FetchData()){
if(_remainbytes==0){
break;
}
strcpy(_cell , _set.GetFieldValue(0));
strcpy(_name , _set.GetFieldValue(1));
_guid = atoi(_set.GetFieldValue(2));
XPTDEBUG(("cell=%s\n",_cell));
XPTDEBUG(("name=%s\n",_name));
CVcard _card;
char _target[1024];
_card.Encode(_name,_cell,_target);
SmlAdd_t _add;
smlLibMemset(&_add,0,(MemSize_t)sizeof(SmlAdd_t));
_add.elementType = SML_PE_ADD;
sprintf(_vert , "%d",pSess->m_nCmdID++);
_add.cmdID = smlString2Pcdata(_vert);
_add.meta = (SmlPcdataPtr_t)smlLibMalloc(sizeof(SmlPcdata_t));
smlLibMemset(_add.meta, 0, sizeof(SmlPcdata_t));
_add.meta->contentType=SML_PCDATA_EXTENSION;
_add.meta->extension=SML_EXT_METINF;
_add.meta->content = (SmlMetInfMetInfPtr_t)smlLibMalloc(sizeof(SmlMetInfMetInf_t));
smlLibMemset(_add.meta->content, 0, sizeof(SmlMetInfMetInf_t));
SmlMetInfMetInfPtr_t _pMeta = (SmlMetInfMetInfPtr_t)(_add.meta->content);
_pMeta->type = smlString2Pcdata("text/x-vcard") ;
_add.itemList = (SmlItemListPtr_t)smlLibMalloc(sizeof(SmlItemList_t));
smlLibMemset(_add.itemList, 0, sizeof(SmlItemList_t));
SmlItemPtr_t _pItem = (SmlItemPtr_t)smlLibMalloc(sizeof(SmlItem_t));
smlLibMemset(_pItem, 0, sizeof(SmlItem_t));
_add.itemList->item = _pItem;
_pItem->source = (SmlSourcePtr_t)smlLibMalloc(sizeof(SmlSource_t));
sprintf(_vert , "%d",_guid);
_pItem->source->locURI = smlString2Pcdata(_vert);
_pItem->source->locName = NULL;
_pItem->data =smlString2Pcdata(_target) ;
smlAddCmd( id, &_add);
_remainbytes = _get_remainbytes(id,pSess);
}
sprintf(_sql,"select count(*) from pim_contact where userid='%s' and flag=1",pSess->m_sUser);
_count = 0;
if(workdb.PrepareSql(_sql, _set)==FALSE){
XPTDEBUG(("sql=%s error=%s\n",_sql,workdb.GetSqlError()));
return 0;
}
else{
if(workdb.FetchData()){
_count = atoi(_set.GetFieldValue(0));
XPTDEBUG(("SQL='%s' count=%d\n",_sql,_count));
if(_count==0){
XPTDEBUG(("set Sync_ServerSyncOK\n"));
pSess->m_eServerState = Sync_ServerSyncOK;
return 0;
}
}
}
}
return _count;
}
void _log_output(String_t outputString)
{
fseek(stream ,0,SEEK_END);
String_t tm = _get_nowtime();
char tmp[512];
sprintf(tmp,"%s ",tm);
fwrite(tmp, strlen(tmp),1,stream);
fwrite(outputString, strlen(outputString),1,stream);
fflush(stream);
printf(outputString);
}
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
CString s;
}
InstanceID_t id;
XptServiceID_t serviceID;
const struct XptProtocolInfo *xptProtocolInfo;
if((stream = fopen("ds-server.txt", "w")) == NULL)
exit(-1);
if(workdb.ConnectDB("pimserver","sa","trasin"))
{
printf("workdb connect success!\n");
}
else
{
printf("workdb connect failed!\n");
// return 1 ;
}
/* --- Initialize SyncML Reference Toolkit --- */
C(myInit());
/* Initialize the SyncML instance; set up callbacks and options */
/* and get an instance Id "id" */
C(myInitInstance(&id));
C(xptGetProtocol("HTTP", &xptProtocolInfo));
C(xptSelectProtocol(xptProtocolInfo->id,
"SERVERPORT=8080", XPT_SERVER, &serviceID));
while (1)
{
/* --- Receive the SyncML document --- */
myReceiveData(id, serviceID);
}
C(xptDeselectProtocol(serviceID));
/* --- Close SyncML instance and Toolkit session -- */
C(myTerminateInstance(id));
C(myTerminate());
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -