From: Kidong L. <bri...@us...> - 2004-04-11 15:31:01
|
Update of /cvsroot/syncml-ctoolkit/toolkit/src/examples In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31132/examples Added Files: Makefile README builder_wrapper.c builder_wrapper.h callback_handler.c callback_handler.h dynamicTransports receiver.c sender.c Log Message: Added examples, sender and receiver --- NEW FILE: receiver.c --- /** * @file Receiver exmple */ #include <stdio.h> /* Include header files required in any SyncML session */ #include <sml.h> #include <smldef.h> #include <smldtd.h> #include <xpt.h> #include "builder_wrapper.h" /** * Receive the SyncML document from the transport layer * * @param id instance ID * @param serviceID transport service ID * @see xptReceiveData */ void myReceiveData(InstanceID_t id, XptServiceID_t serviceID) { XptCommunicationID_t conn = 0; MemPtr_t writeBuffer = NULL; MemSize_t bytesWritten = 0; size_t bytesReceived = 0; VoidPtr_t userData = NULL; XptCommunicationInfo_t docInfo; /* Lock the workspace buffer to write the received document into (from the transport layer) */ smlLockWriteBuffer(id, &writeBuffer, &bytesWritten); C(xptOpenCommunication(serviceID, XPT_REQUEST_RECEIVER, &conn)); C(xptBeginExchange(conn)); C(xptGetDocumentInfo(conn, &docInfo)); /* Receive bytes from the transport layer */ C(xptReceiveData(conn, writeBuffer, bytesWritten, &bytesReceived)); printf("Received Data = '%s'\n", writeBuffer); /* Turn the workspace buffer over to SyncML for document processing */ smlUnlockWriteBuffer(id, bytesReceived); /* Prepare the callback parameter userData here! userData is a void pointer handed over to every callback function as one of the function arguments. The Toolkit doesn't touch the content of this structure. For instance, this mechanism can be used by the application to pass data to the callback routines. */ C(smlSetUserData(id, &userData)); /* --- Parse commands & invoke callback routines of the application -- */ C(smlProcessData(id, SML_ALL_COMMANDS)); docInfo.cbSize = sizeof(docInfo); docInfo.cbLength = 0; docInfo.auth = NULL; strcpy(docInfo.mimeType, "application/vnd.syncml+xml"); strcpy(docInfo.docName, "sync"); C(xptSetDocumentInfo(conn, &docInfo)); /* Send zero-byte response to the transport layer */ /* TODO: Should send real response */ { char sendBuffer[100 + 1] = ""; size_t bytesSent = 0; C(xptSendData(conn, sendBuffer, strlen(sendBuffer), &bytesSent)); } C(xptSendComplete(conn)); C(xptEndExchange(conn)); C(xptCloseCommunication(conn)); } int main(void) { /* Declare vars for instance id and for options structures */ /* (both per-Toolkit-session and per-instance) */ InstanceID_t id; XptServiceID_t serviceID; const struct XptProtocolInfo *xptProtocolInfo; /* --- 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 0; } --- NEW FILE: callback_handler.h --- #ifndef _CALLBACK_HANDLER_H #define _CALLBACK_HANDLER_H #include <smlerr.h> #include <smldtd.h> #include <smldef.h> Ret_t myHandleStartMessage(InstanceID_t id, VoidPtr_t userData, SmlSyncHdrPtr_t pSyncHdr); Ret_t myHandleStartSync(InstanceID_t id, VoidPtr_t userData, SmlSyncPtr_t pSync); Ret_t myHandleAdd(InstanceID_t id, VoidPtr_t userData, SmlAddPtr_t pAdd); Ret_t myHandleEndSync(InstanceID_t id, VoidPtr_t userData); Ret_t myHandleEndMessage(InstanceID_t id, VoidPtr_t userData, Boolean_t final); #endif /* _CALLBACK_HANDLER_H */ --- NEW FILE: dynamicTransports --- xpthttp --- NEW FILE: sender.c --- /** * @file Sender exmple */ #include <stdio.h> /* Include header files required in any SyncML session */ #include <sml.h> #include <smldef.h> #include <smldtd.h> #include <xpt.h> #include "builder_wrapper.h" /** * Send the SyncML document to the transport layer * * @param id instance ID * @return return value of xptSendData * @see xptSendData */ Ret_t mySendData(InstanceID_t id, XptServiceID_t serviceID) { XptCommunicationID_t conn = 0; MemPtr_t readBuffer = NULL; MemSize_t bytesRead = 0; size_t bytesSent = 0; XptCommunicationInfo_t docInfo; /* Lock the workspace for reading the assembled SyncML document */ smlLockReadBuffer(id, &readBuffer, &bytesRead); C(xptOpenCommunication(serviceID, XPT_REQUEST_SENDER, &conn)); C(xptBeginExchange(conn)); docInfo.cbSize = sizeof(docInfo); docInfo.cbLength = bytesRead; docInfo.auth = NULL; docInfo.hmacInfo = NULL; strcpy(docInfo.mimeType, "application/vnd.syncml+xml"); strcpy(docInfo.docName, "sync"); C(xptSetDocumentInfo(conn, &docInfo)); /* Pass data (the SyncML document in the buffer) to transport layer */ C(xptSendData(conn, readBuffer, bytesRead, &bytesSent)); C(xptSendComplete(conn)); printf("Sent Data = '%s'\n", readBuffer); /* Unlock the workspace so the Toolkit controls it again */ C(smlUnlockReadBuffer(id, bytesSent)); C(xptGetDocumentInfo(conn, &docInfo)); /* Receive zero-byte response from transport binding */ { size_t bytesReceived = 0; char receiveBuf[100 + 1] = ""; /* NOTE: Should receive at least more than one data */ C(xptReceiveData(conn, receiveBuf, 1, &bytesReceived)); } C(xptEndExchange(conn)); C(xptCloseCommunication(conn)); return SML_ERR_OK; } int main(void) { InstanceID_t id; XptServiceID_t serviceID; const struct XptProtocolInfo *xptProtocolInfo; /* --- Initialize SyncML reference toolkit --- */ /* Initialize SyncML Reference Toolkit */ C(myInit()); /* Initialize the SyncML instance */ C(myInitInstance(&id)); /* --- Start building SyncML document --- */ /* Start a new message using the syncHdr proto element */ C(myStartMessage(id)); /* Continue adding SyncML commands to the workspace. The proto element structures holding the parameters associated with each command need to be allocated and set to appropriate values before usage. */ /* Start sync cmd */ C(myStartSync(id)); /* Start add cmd */ C(myAddCmd(id)); /* End the sync block */ C(myEndSync(id)); /* --- End message --- */ C(myEndMessage(id)); /* --- Send the SyncML document --- */ C(xptGetProtocol("HTTP", &xptProtocolInfo)); C(xptSelectProtocol(xptProtocolInfo->id, "HOST=127.0.0.1:8080", XPT_CLIENT, &serviceID)); C(mySendData(id, serviceID)); C(xptDeselectProtocol(serviceID)); /* --- Close SyncML instance and Toolkit session --- */ C(myTerminateInstance(id)); C(myTerminate()); return 0; } --- NEW FILE: README --- This examples shows usage about SyncML C Toolkit API. It's based on sample scenarios in SyncML Reference Toolkit Manual. Sender sends a message adding a vCard to receiver. And receiver shows the received message from sender. How to run sender and receiver 1. After building examples, you can get sender and receiver binaries. 2. Run the receiver > receiver (on DOS prompt) $ ./receiver (on Unix shell) 3. Run the sender on another DOS prompt or Unix shell > sender (on DOS prompt) $ ./sender (on Unix shell) --- NEW FILE: callback_handler.c --- /** * @file Callback implementations */ #include "callback_handler.h" #include <stdio.h> #include <smlmetinfdtd.h> #include <smldevinfdtd.h> #include <mgrutil.h> /** * Callback handling <SyncHdr> tag * * @param id instance ID * @param pSync pointer containing infomation about <SyncHdr> block * @return SML_ERR_OK(=0) * @see smlInitInstance */ Ret_t myHandleStartMessage(InstanceID_t id, VoidPtr_t userData, SmlSyncHdrPtr_t pSyncHdr) { printf("Calling %s() at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); printf("SyncHdr.VerDTD = %s\n", (char *) smlPcdata2String(pSyncHdr->version)); printf("SyncHdr.VerProto = %s\n", smlPcdata2String(pSyncHdr->proto)); printf("SyncHdr.SessionID = %s\n", smlPcdata2String(pSyncHdr->sessionID)); printf("SyncHdr.MsgID = %s\n", smlPcdata2String(pSyncHdr->msgID)); printf("SyncHdr.Flags = %d\n", pSyncHdr->flags); printf("SyncHdr.Target.locURI = %s\n", smlPcdata2String(pSyncHdr->target->locURI)); printf("SyncHdr.Source.locURI = %s\n", smlPcdata2String(pSyncHdr->source->locURI)); printf("SyncHdr.RespURI = %s\n", smlPcdata2String(pSyncHdr->respURI)); /* Skipped SyncHdr.Cred */ printf("SyncHdr.Meta = %s\n", smlPcdata2String(pSyncHdr->meta)); return SML_ERR_OK; } /** * Callback handling <Sync> command * * @param id instance ID * @param pSync pointer containing infomation about <Sync> block * @return SML_ERR_OK(=0) * @see smlInitInstance */ Ret_t myHandleStartSync(InstanceID_t id, VoidPtr_t userData, SmlSyncPtr_t pSync) { printf("Calling %s() at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); printf("Sync.CmdID = %s\n", smlPcdata2String(pSync->cmdID)); printf("Sync.Target.LocURI = %s\n", smlPcdata2String(pSync->target->locURI)); printf("Sync.Source.LocURI = %s\n", smlPcdata2String(pSync->source->locURI)); return SML_ERR_OK; } /** * Callback handling <Add> command * * @param id instance ID * @param pSync pointer containing infomation about <Add> command * @return SML_ERR_OK(=0) * @see smlInitInstance */ Ret_t myHandleAdd(InstanceID_t id, VoidPtr_t userData, SmlAddPtr_t pAdd) { SmlItemListPtr_t ele; printf("Calling %s() at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); printf("Add.CmdID = %s\n", smlPcdata2String(pAdd->cmdID)); printf("Add.Flags = %d\n", pAdd->flags); printf("Add.Meta = %s\n", smlPcdata2String(pAdd->meta)); ele = pAdd->itemList; while (ele != NULL) { printf("Add.ItemList.Item.Source.LocURI = %s\n", smlPcdata2String(ele->item->source->locURI)); printf("Add.ItemList.Item.Data = %s\n", smlPcdata2String(ele->item->data)); /* putInDB(ele->item); */ ele = ele->next; } return SML_ERR_OK; } Ret_t myHandleEndSync(InstanceID_t id, VoidPtr_t userData) { printf("Calling %s() at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); return SML_ERR_OK; } Ret_t myHandleEndMessage(InstanceID_t id, VoidPtr_t userData, Boolean_t final) { printf("Calling %s() at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); return SML_ERR_OK; } --- NEW FILE: Makefile --- INC = -I../sml/inc \ -I../sml/inc/win \ -I../sml/mgr/inc \ -I../xpt/manager/inc \ -I../xpt/manager/linux LDFLAGS = -L../../bin/linux -lsml -lxpt #-lefence CFLAGS = $(INC) -Wall -g -O2 -pedantic OBJS_COMMON = callback_handler.o builder_wrapper.o OBJS_SENDER = sender.o $(OBJS_COMMON) OBJS_RECEIVER = receiver.o $(OBJS_COMMON) TARGET_SENDER = sender TARGET_RECEIVER = receiver TARGET = $(TARGET_SENDER) $(TARGET_RECEIVER) all : $(TARGET) $(TARGET_SENDER) : $(OBJS_SENDER) $(TARGET_RECEIVER) : $(OBJS_RECEIVER) clean : rm -rf *.o $(TARGET) core *~ new : $(MAKE) clean $(MAKE) --- NEW FILE: builder_wrapper.h --- #ifndef _BUILDER_WRAPPER_H #define _BUILDER_WRAPPER_H #include <smldef.h> #define C(X) { \ int rc = SML_ERR_OK; \ rc = X; \ if (rc != SML_ERR_OK) \ { \ printf("%s:%d:rc = %d, hex(rc) = %x\n", __FILE__, __LINE__, rc, rc); \ } \ } Ret_t myInit(void); Ret_t myInitInstance(InstanceID_t * pID); Ret_t myStartMessage(InstanceID_t id); Ret_t myAlertCmd(InstanceID_t id); Ret_t myStartSync(InstanceID_t id); Ret_t myAddCmd(InstanceID_t id); Ret_t myEndSync(InstanceID_t id); Ret_t myEndMessage(InstanceID_t id); Ret_t myTerminateInstance(InstanceID_t id); Ret_t myTerminate(void); #endif /* _BUILDER_WRAPPER_H */ --- NEW FILE: builder_wrapper.c --- /** * @file Command builder wrapper */ #include "builder_wrapper.h" #include <stdio.h> #include <sml.h> #include <smldef.h> #include <smldtd.h> #include <smlmetinfdtd.h> #include <smldevinfdtd.h> #include <mgrutil.h> #include <xpt.h> #include "callback_handler.h" /** * Initialize SyncML Reference Toolkit * * @return return value of smlInit * @see smlInit */ Ret_t myInit(void) { SmlOptions_t options; /* Set Toolkit options structure */ options.defaultPrintFunc = NULL; options.maxWorkspaceAvailMem = 100000; /* Initialize SyncML Toolkit */ return smlInit(&options); } /** * Initialize the SyncML instance * * @param pID pointer to instance ID * @return return value of smlInitInstance * @see smlInitInstance */ Ret_t myInitInstance(InstanceID_t * pID) { SmlCallbacks_t callbacks; SmlInstanceOptions_t options; /* Set instance */ options.encoding = SML_XML; options.workspaceName = "MyWorkSpace"; options.workspaceSize = 10000; /* Allocate for callbacks structure */ callbacks.startMessageFunc = myHandleStartMessage; callbacks.startSyncFunc = myHandleStartSync; callbacks.addCmdFunc = myHandleAdd; callbacks.endSyncFunc = myHandleEndSync; callbacks.endMessageFunc = myHandleEndMessage; /* Initialize the SyncML instance; set up callbacks and options */ /* and get an instance Id "id" */ return smlInitInstance(&callbacks, &options, NULL, pID); } /** * Start building SyncML document * * @param id instance ID * @return return value of smlStartMessage * @see smlStartMessage * @see smlStartMessageExt */ Ret_t myStartMessage(InstanceID_t id) { SmlSyncHdr_t hdr; SmlSource_t source; SmlTarget_t target; /* Create SyncML proto element for message header */ hdr.elementType = SML_PE_HEADER; hdr.version = smlString2Pcdata("1.1"); hdr.proto = smlString2Pcdata("SyncML/1.1"); hdr.sessionID = smlString2Pcdata("1"); hdr.msgID = smlString2Pcdata("1000"); hdr.flags = SmlNoResp_f; target.locURI = smlString2Pcdata("http://data.sync.server.url"); target.locName = smlString2Pcdata("Data Sync Server"); hdr.target = ⌖ source.locURI = smlString2Pcdata("data_sync_client"); source.locName = smlString2Pcdata("Data Sync Client"); hdr.source = &source; hdr.cred = NULL; hdr.meta = NULL; hdr.respURI = NULL; /* Start a new message using the SyncHdr proto element */ return smlStartMessageExt(id, &hdr, SML_VERS_1_1); } /** * Start building Sync command * * @param id instance ID * @return return value of smlStartSync * @see smlStartSync */ Ret_t myStartSync(InstanceID_t id) { SmlSync_t sync; SmlSource_t source; SmlTarget_t target; /* Start sync cmd */ sync.elementType = SML_PE_SYNC_START; sync.cmdID = smlString2Pcdata("1"); target.locURI = smlString2Pcdata("./contacts_server"); target.locName = smlString2Pcdata("Sync Server Database"); sync.target = ⌖ source.locURI = smlString2Pcdata("./contacts_client"); source.locName = smlString2Pcdata("Sync Client Database"); sync.source = &source; sync.cred = NULL; return smlStartSync(id, &sync); } /** * Start building Add command * * @param id instance ID * @return return value of smlAddCmd * @see smlAddCmd */ Ret_t myAddCmd(InstanceID_t id) { SmlAdd_t add; SmlItem_t item; SmlItemList_t itemList; SmlSource_t source; SmlTarget_t target; /* Add cmd */ add.elementType = SML_PE_ADD; add.cmdID = smlString2Pcdata("2"); add.cred = NULL; add.meta = smlString2Pcdata("<Type xmlns='syncml:metinf'>text/x-vcard</Type>"); add.flags = SmlNoResp_f; target.locURI = smlString2Pcdata("1"); target.locName = smlString2Pcdata("Element ID at Target"); item.target = ⌖ source.locURI = smlString2Pcdata("2"); source.locName = smlString2Pcdata("Element ID at Source"); item.source = &source; item.data = smlString2Pcdata("BEGIN:VCARD\n" "VERSION:2.1\n" "N:Puppy;Dusty\n" "ORG:Columbia Internet\n" "EMAIL:dus...@us...\n" "URL:http://www.userfriendly.org\n" "TEL;WORK:+123 456 78 9012\n" "END:VCARD\n"); item.meta = NULL; itemList.item = &item; itemList.next = NULL; add.itemList = &itemList; return smlAddCmd(id, &add); } /** * End the sync block * * @param id instance ID * @return return value of smlEndSync * @see smlEndSync */ Ret_t myEndSync(InstanceID_t id) { return smlEndSync(id); } /** * End the sync message * * @param id instance ID * @return return value of smlEndMessage * @see smlEndMessage */ Ret_t myEndMessage(InstanceID_t id) { /* This ends the SyncML document ... it has been assembled */ /* SmlFinal_f says this is the last message in the SyncML package */ /* (since it's the only one) */ return smlEndMessage(id, SmlFinal_f); } /** * Close SyncML instance * * @param id instance ID * @return return value of smlTerminateInstance * @see smlTerminateInstance */ Ret_t myTerminateInstance(InstanceID_t id) { return smlTerminateInstance(id); } /** * Close SyncML Toolkit session * * @return return value of smlTerminate * @see smlTerminate */ Ret_t myTerminate(void) { return smlTerminate(); } |