1 /*----------------------------------------------------------------------------
  2 Name:      XmlBlasterConnectionUnparsedMain.c
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo client for synchronous access.
  6            XmlBlasterConnectionUnparsedMain connects with raw socket to xmlBlaster.
  7            No callbacks are coded, we only have synchronous access and we don't need
  8            multi threading in this case.
  9 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
 10 Compile:
 11   LINUX:   gcc -Wall -g -Wno-long-long -D_REENTRANT -I. -o XmlBlasterConnectionUnparsedMain XmlBlasterConnectionUnparsedMain.c util/helper.c util/msgUtil.c util/Properties.c socket/xmlBlasterSocket.c socket/xmlBlasterZlib.c socket/XmlBlasterConnectionUnparsed.c
 12   WIN:     cl /MT /W3 /Wp64 -D_WINDOWS -I. XmlBlasterConnectionUnparsedMain.c util\msgUtil.c util\Properties.c socket\XmlBlasterConnectionUnparsed.c socket\xmlBlasterSocket.c socket\xmlBlasterZlib.c ws2_32.lib
 13   Solaris: cc -Xc -g -I. -o XmlBlasterConnectionUnparsedMain XmlBlasterConnectionUnparsedMain.c util/msgUtil.c util/Properties.c socket/XmlBlasterConnectionUnparsed.c socket/xmlBlasterSocket.c socket/xmlBlasterZlib.c -lsocket -lnsl 
 14 Invoke:    XmlBlasterConnectionUnparsedMain -dispatch/callback/plugin/socket/hostname develop -dispatch/callback/plugin/socket/port 7607
 15 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/protocol.socket.html
 16 Date:      05/2003
 17 -----------------------------------------------------------------------------*/
 18 #if !defined(__IPhoneOS__)
 19 #include <stdio.h>
 20 #include <stdlib.h>
 21 #include <string.h>
 22 #include <XmlBlasterConnectionUnparsed.h>
 23 
 24 static bool help = false;
 25 
 26 /**
 27  * Test the baby
 28  */
 29 int main(int argc, char** argv)
 30 {
 31    int iarg;
 32    char *response = (char *)0;
 33    XmlBlasterException xmlBlasterException;
 34    XmlBlasterConnectionUnparsed *xb = 0;
 35 
 36    printf("[XmlBlasterConnectionUnparsedMain] Try option '-help' if you need usage informations\n");
 37 
 38    for (iarg=0; iarg < argc; iarg++) {
 39       if (strcmp(argv[iarg], "-help") == 0 || strcmp(argv[iarg], "--help") == 0)
 40          help = true;
 41    }
 42    if (help) {
 43       const char *pp =
 44       "\n\nExample:"
 45       "\n  XmlBlasterConnectionUnparsedMain -logLevel TRACE -dispatch/connection/plugin/socket/hostname server.mars.universe";
 46       printf("Usage:\n%s%s\n", xmlBlasterConnectionUnparsedUsage(), pp);
 47       exit(EXIT_FAILURE);
 48    }
 49 
 50    xb = getXmlBlasterConnectionUnparsed(argc, (const char* const*)argv);
 51 
 52    if (xb->ping(xb, 0, &xmlBlasterException) == (char *)0) {
 53       printf("[XmlBlasterConnectionUnparsedMain] Pinging a not connected server failed -> this is OK\n");
 54    }
 55    else {
 56       printf("[XmlBlasterConnectionUnparsedMain] ERROR: Pinging a not connected server should not be possible\n");
 57    }
 58 
 59    {  /* connect */
 60       char connectQos[2048];
 61       strncpy(connectQos,
 62              "<qos>"
 63              " <securityService type='htpasswd' version='1.0'>"
 64              "  <![CDATA["
 65              "   <user>fritz</user>"
 66              "   <passwd>secret</passwd>"
 67              "  ]]>"
 68              " </securityService>"
 69              "</qos>", 2047);
 70 
 71       response = xb->connect(xb, connectQos, &xmlBlasterException);
 72       if (*xmlBlasterException.errorCode != '\0') {
 73          printf("[XmlBlasterConnectionUnparsedMain] Caught exception during connect errorCode=%s, message=%s", xmlBlasterException.errorCode, xmlBlasterException.message);
 74          freeXmlBlasterConnectionUnparsed(xb);
 75          exit(EXIT_FAILURE);
 76       }
 77       free(response);
 78       printf("[XmlBlasterConnectionUnparsedMain] Connected to xmlBlaster, do some tests ...\n");
 79    }
 80 
 81    {
 82       response = xb->ping(xb, 0, &xmlBlasterException);
 83       if (response == (char *)0) {
 84          printf("[XmlBlasterConnectionUnparsedMain] ERROR: Pinging a connected server failed:  errorCode=%s, message=%s\n",
 85                  xmlBlasterException.errorCode, xmlBlasterException.message);
 86       }
 87       else {
 88          printf("[XmlBlasterConnectionUnparsedMain] Pinging a connected server, response=%s\n", response);
 89          free(response);
 90       }
 91    }
 92 
 93    if (false) { /* subscribe ... -> we have no callback, we ommit subscribe */
 94       const char *key = "<key oid='HelloWorld'/>";
 95       const char *qos = "<qos/>";
 96       printf("[XmlBlasterConnectionUnparsedMain] Subscribe message 'HelloWorld' ...\n");
 97       response = xb->subscribe(xb, key, qos, &xmlBlasterException);
 98       if (*xmlBlasterException.errorCode != '\0') {
 99          printf("[XmlBlasterConnectionUnparsedMain] Caught exception in subscribe errorCode=%s, message=%s", xmlBlasterException.errorCode, xmlBlasterException.message);
100          freeXmlBlasterConnectionUnparsed(xb);
101          exit(EXIT_FAILURE);
102       }
103       printf("[XmlBlasterConnectionUnparsedMain] Subscribe success, returned status is '%s'\n", response);
104       free(response);
105    }
106 
107    {  /* publish ... */
108       MsgUnit msgUnit;
109       printf("[XmlBlasterConnectionUnparsedMain] Publishing message 'HelloWorld' ...\n");
110       msgUnit.key = "<key oid='HelloWorld'/>";
111       msgUnit.content = "Some message payload";
112       msgUnit.contentLen = strlen(msgUnit.content);
113       msgUnit.qos = "<qos><persistent/></qos>";
114       response = xb->publish(xb, &msgUnit, &xmlBlasterException);
115       if (*xmlBlasterException.errorCode != '\0') {
116          printf("[XmlBlasterConnectionUnparsedMain] Caught exception in publish errorCode=%s, message=%s", xmlBlasterException.errorCode, xmlBlasterException.message);
117          freeXmlBlasterConnectionUnparsed(xb);
118          exit(EXIT_FAILURE);
119       }
120       printf("[XmlBlasterConnectionUnparsedMain] Publish success, returned status is '%s'\n", response);
121       free(response);
122    }
123 
124    {  /* unSubscribe ... */
125       QosArr *resp;
126       const char *key = "<key oid='HelloWorld'/>";
127       const char *qos = "<qos/>";
128       printf("[XmlBlasterConnectionUnparsedMain] UnSubscribe message 'HelloWorld' ...\n");
129       resp = xb->unSubscribe(xb, key, qos, &xmlBlasterException);
130       if (resp) {
131          printf("[XmlBlasterConnectionUnparsedMain] Unsbscribe success\n");
132          free(resp);
133       }
134       else {
135          printf("[XmlBlasterConnectionUnparsedMain] Caught exception in unSubscribe errorCode=%s, message=%s\n", xmlBlasterException.errorCode, xmlBlasterException.message);
136          freeXmlBlasterConnectionUnparsed(xb);
137          exit(EXIT_FAILURE);
138       }
139    }
140 
141    {  /* get synchnronous ... */
142       size_t i;
143       const char *key = "<key queryType='XPATH'>//key</key>";
144       const char *qos = "<qos/>";
145       MsgUnitArr *msgUnitArr;
146       printf("[XmlBlasterConnectionUnparsedMain] Get synchronous messages with XPath '//key' ...\n");
147       msgUnitArr = xb->get(xb, key, qos, &xmlBlasterException);
148       if (*xmlBlasterException.errorCode != 0) {
149          printf("[XmlBlasterConnectionUnparsedMain] Caught exception in get errorCode=%s, message=%s", xmlBlasterException.errorCode, xmlBlasterException.message);
150          freeXmlBlasterConnectionUnparsed(xb);
151          exit(EXIT_FAILURE);
152       }
153       if (msgUnitArr != (MsgUnitArr *)0) {
154          for (i=0; i<msgUnitArr->len; i++) {
155             char *contentStr = strFromBlobAlloc(msgUnitArr->msgUnitArr[i].content, msgUnitArr->msgUnitArr[i].contentLen);
156             const char *dots = (msgUnitArr->msgUnitArr[i].contentLen > 96) ? " ..." : "";
157             printf("\n[XmlBlasterConnectionUnparsedMain] Received message#%u/%u:\n"
158                    "-------------------------------------"
159                    "%s\n <content>%.100s%s</content>%s\n"
160                    "-------------------------------------\n",
161                    (unsigned int)(i+1), (unsigned int)msgUnitArr->len,
162                    msgUnitArr->msgUnitArr[i].key,
163                    contentStr, dots,
164                    msgUnitArr->msgUnitArr[i].qos);
165             free(contentStr);
166          }
167          freeMsgUnitArr(msgUnitArr);
168       }
169       else {
170          printf("[XmlBlasterConnectionUnparsedMain] Caught exception in get errorCode=%s, message=%s", xmlBlasterException.errorCode, xmlBlasterException.message);
171          freeXmlBlasterConnectionUnparsed(xb);
172          exit(EXIT_FAILURE);
173       }
174    }
175 
176 
177    {  /* erase ... */
178       QosArr *resp;
179       const char *key = "<key oid='HelloWorld'/>";
180       const char *qos = "<qos/>";
181       printf("[XmlBlasterConnectionUnparsedMain] Erasing message 'HelloWorld' ...\n");
182       resp = xb->erase(xb, key, qos, &xmlBlasterException);
183       if (*xmlBlasterException.errorCode != '\0') {
184          printf("[XmlBlasterConnectionUnparsedMain] Caught exception in erase errorCode=%s, message=%s", xmlBlasterException.errorCode, xmlBlasterException.message);
185          freeXmlBlasterConnectionUnparsed(xb);
186          exit(EXIT_FAILURE);
187       }
188       printf("[XmlBlasterConnectionUnparsedMain] Erase success, returned status is '%s'\n", response);
189       free(resp);
190    }
191 
192    {  /* disconnect ... */
193       if (xb->disconnect(xb, 0, &xmlBlasterException) == false) {
194          printf("[XmlBlasterConnectionUnparsedMain] Caught exception in disconnect, errorCode=%s, message=%s", xmlBlasterException.errorCode, xmlBlasterException.message);
195          freeXmlBlasterConnectionUnparsed(xb);
196          exit(EXIT_FAILURE);
197       }
198    }
199 
200    freeXmlBlasterConnectionUnparsed(xb);
201    printf("[XmlBlasterConnectionUnparsedMain] Good bye.\n");
202    return 0;
203 }
204 #endif


syntax highlighted by Code2HTML, v. 0.9.1