demo/c/socket/HelloWorldUdp.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002 Name:      xmlBlaster/demo/c/socket/HelloWorldUdp.c
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Example for one calls with UDP.
00006 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
00007 Compile:   cd xmlBlaster; build c
00008            (Win: copy xmlBlaster\src\c\socket\pthreadVC2.dll to your PATH)
00009 Invoke:    HelloWorldUdp -help
00010 See:    http://www.xmlblaster.org/xmlBlaster/doc/requirements/protocol.socket.html
00011 -----------------------------------------------------------------------------*/
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <XmlBlasterAccessUnparsed.h>
00016 
00025 static bool myUpdate(MsgUnitArr *msgUnitArr, void *userData,
00026                      XmlBlasterException *exception)
00027 {
00028    size_t i;
00029    /*XmlBlasterAccessUnparsed *xa = (XmlBlasterAccessUnparsed *)userData;*/
00030    if (userData != 0) ;   /* Supress compiler warning */
00031    if (exception != 0) ;  /* Supress compiler warning */
00032 
00033    for (i=0; i<msgUnitArr->len; i++) {
00034       char *xml = messageUnitToXml(&msgUnitArr->msgUnitArr[i]);
00035       printf("[client] CALLBACK update(): Asynchronous message update arrived:%s\n",
00036              xml);
00037       xmlBlasterFree(xml);
00038    }
00039 
00040    return true;
00041 }
00042 
00046 int main(int argc, char** argv)
00047 {
00048    int iarg;
00049    char *response = (char *)0;
00050    /*
00051       * callbackSessionId:
00052       * Is created by the client and used to validate callback messages in update. 
00053       * This is sent on connect in ConnectQos.
00054       * (Is different from the xmlBlaster secret session ID)
00055       */
00056    const char *callbackSessionId = "topSecret";
00057    XmlBlasterException xmlBlasterException;
00058    XmlBlasterAccessUnparsed *xa = 0;
00059    int sleepInterval = 1000;
00060    bool updateOneway = true;
00061 
00062    printf("[client] XmlBlaster %s C SOCKET client, try option '-help' if you need"
00063           " usage informations\n", getXmlBlasterVersion());
00064 
00065    for (iarg=0; iarg < argc; iarg++) {
00066       if (strcmp(argv[iarg], "-help") == 0 || strcmp(argv[iarg], "--help") == 0) {
00067          char usage[XMLBLASTER_MAX_USAGE_LEN];
00068          const char *pp =
00069          "\n   -logLevel            ERROR | WARN | INFO | TRACE | DUMP [WARN]"
00070          "\n   -sleepInterval       Milliseconds to wait on callback messages [1000]"
00071          "\n\nExample:"
00072          "\n  HelloWorldUdp -logLevel TRACE"
00073          " -dispatch/connection/plugin/socket/enableUdp true"
00074          " -dispatch/connection/plugin/socket/hostname 192.168.2.9"
00075          " -updateOneway true";
00076          printf("Usage:\nXmlBlaster C SOCKET client %s\n%s%s\n",
00077                   getXmlBlasterVersion(), xmlBlasterAccessUnparsedUsage(usage), pp);
00078          exit(EXIT_FAILURE);
00079       }
00080       else if (strcmp(argv[iarg], "-sleepInterval") == 0 && iarg < argc-1)
00081          sleepInterval = atoi(argv[++iarg]);
00082       else if (strcmp(argv[iarg], "-updateOneway") == 0 && iarg < argc-1)
00083          updateOneway = (!strcmp(argv[++iarg], "true")) ? true : false;
00084    }
00085 
00086    xa = getXmlBlasterAccessUnparsed(argc, (const char* const*)argv);
00087    if (xa->initialize(xa, myUpdate, &xmlBlasterException) == false) {
00088       printf("[client] Connection to xmlBlaster failed,"
00089              " please start the server or check your configuration\n");
00090       freeXmlBlasterAccessUnparsed(xa);
00091       exit(EXIT_FAILURE);
00092    }
00093 
00094    {  /* connect */
00095       char connectQos[2048];
00096       char callbackQos[1024];
00097       /*  oneway='true' as a general callback attribute is not used here */
00098       sprintf(callbackQos,
00099                "<queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>"
00100                "  <callback type='SOCKET' sessionId='%s'>"
00101                "    socket://%.120s:%d"
00102                "  </callback>"
00103                "</queue>",
00104                callbackSessionId, xa->callbackP->hostCB, xa->callbackP->portCB);
00105       sprintf(connectQos,
00106                "<qos>"
00107                " <securityService type='htpasswd' version='1.0'>"
00108                "  <![CDATA["
00109                "   <user>fritz</user>"
00110                "   <passwd>secret</passwd>"
00111                "  ]]>"
00112                " </securityService>"
00113                "%.1024s"
00114                "</qos>", callbackQos);
00115 
00116       response = xa->connect(xa, connectQos, myUpdate, &xmlBlasterException);
00117       if (*xmlBlasterException.errorCode != 0) {
00118          printf("[client] Caught exception during connect errorCode=%s, message=%s\n",
00119                   xmlBlasterException.errorCode, xmlBlasterException.message);
00120          freeXmlBlasterAccessUnparsed(xa);
00121          exit(EXIT_FAILURE);
00122       }
00123       xmlBlasterFree(response);
00124       printf("[client] Connected to xmlBlaster, do some tests ...\n");
00125    }
00126 
00127    { /* subscribe ... */
00128       const char *key = "<key oid='HelloWorld'/>";
00129       char qos[1024];
00130       if (updateOneway)
00131          strcpy(qos, "<qos><updateOneway/><notify>false</notify></qos>");
00132       else
00133          strcpy(qos, "<qos><notify>false</notify></qos>");
00134       printf("[client] Subscribe message 'HelloWorld' with updateOneway=%s callback ...\n", updateOneway?"true":"false");
00135       response = xa->subscribe(xa, key, qos, &xmlBlasterException);
00136       if (*xmlBlasterException.errorCode != 0) {
00137          printf("[client] Caught exception in subscribe errorCode=%s, message=%s\n",
00138                   xmlBlasterException.errorCode, xmlBlasterException.message);
00139          xa->disconnect(xa, 0, &xmlBlasterException);
00140          freeXmlBlasterAccessUnparsed(xa);
00141          exit(EXIT_FAILURE);
00142       }
00143       printf("[client] Subscribe success, returned status is '%s'\n", response);
00144       xmlBlasterFree(response);
00145    }
00146    
00147    {  /* publishOneway */
00148       MsgUnitArr holder;
00149       memset(&holder, 0, sizeof(MsgUnitArr));
00150       printf("[client] Publishing oneway messages 'HelloWorld' ...\n");
00151       holder.len = 2;
00152       holder.msgUnitArr = (MsgUnit *)calloc(holder.len, sizeof(MsgUnit));
00153       holder.msgUnitArr[0].key = strcpyAlloc("<key oid='HelloWorld'/>");
00154       holder.msgUnitArr[0].content = strcpyAlloc("Some message payload");
00155       holder.msgUnitArr[0].contentLen = strlen(holder.msgUnitArr[0].content);
00156       holder.msgUnitArr[0].qos =strcpyAlloc("<qos><expiration lifeTime='1000'/><forceUpdate>false</forceUpdate></qos>");
00157 
00158       holder.msgUnitArr[1].key = strcpyAlloc("<key oid='HelloWorld'/>");
00159       holder.msgUnitArr[1].content = strcpyAlloc("Some other message payload");
00160       holder.msgUnitArr[1].contentLen = strlen(holder.msgUnitArr[1].content);
00161       holder.msgUnitArr[1].qos =strcpyAlloc("<qos><expiration lifeTime='1000'/><forceUpdate>false</forceUpdate></qos>");
00162 
00163       xa->publishOneway(xa, &holder, &xmlBlasterException);
00164       
00165       freeMsgUnitArrInternal(&holder);
00166       if (*xmlBlasterException.errorCode != 0) {
00167          printf("[client] Caught exception in publishOneway errorCode=%s, message=%s\n",
00168                   xmlBlasterException.errorCode, xmlBlasterException.message);
00169          xa->disconnect(xa, 0, &xmlBlasterException);
00170          freeXmlBlasterAccessUnparsed(xa);
00171          exit(EXIT_FAILURE);
00172       }
00173    }
00174 
00175    printf("[client] Waiting one second on update messages ...\n");
00176    sleepMillis(sleepInterval);
00177  
00178    {  /* erase ... */
00179       QosArr* resp;
00180       const char *key = "<key oid='HelloWorld'/>";
00181       /*const char *key = "<key oid='' queryType='XPATH'>//key</key>";*/
00182       const char *qos = "<qos/>";
00183       printf("[client] Erasing message 'HelloWorld' ...\n");
00184       resp = xa->erase(xa, key, qos, &xmlBlasterException);
00185       if (*xmlBlasterException.errorCode != 0) {
00186          printf("[client] Caught exception in erase errorCode=%s, message=%s\n",
00187                   xmlBlasterException.errorCode, xmlBlasterException.message);
00188          xa->disconnect(xa, 0, &xmlBlasterException);
00189          freeXmlBlasterAccessUnparsed(xa);
00190          exit(EXIT_FAILURE);
00191       }
00192       if (resp != 0) {
00193          size_t i;
00194          for (i=0; i<resp->len; i++) {
00195             printf("[client] Erase success, returned status is '%s'\n", resp->qosArr[i]);
00196          }
00197          freeQosArr(resp);
00198       }
00199    }
00200 
00201    if (xa->disconnect(xa, 0, &xmlBlasterException) == false) {
00202       printf("[client] Caught exception in disconnect, errorCode=%s, message=%s\n",
00203                xmlBlasterException.errorCode, xmlBlasterException.message);
00204       freeXmlBlasterAccessUnparsed(xa);
00205       exit(EXIT_FAILURE);
00206    }
00207 
00208    freeXmlBlasterAccessUnparsed(xa);
00209    printf("[client] Good bye.\n");
00210    return 0;
00211 }
00212