1 /*----------------------------------------------------------------------------
  2 Name:      xmlBlaster/demo/c/socket/Get.c
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo to get xmlBlaster messages from command line
  6 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7 Compile:   cd xmlBlaster; build.sh c
  8            (Win: copy xmlBlaster\src\c\socket\pthreadVC2.dll to your PATH)
  9 Invoke:    Get -help
 10 See:    http://www.xmlblaster.org/xmlBlaster/doc/requirements/protocol.socket.html
 11 -----------------------------------------------------------------------------*/
 12 #include <stdio.h>
 13 #include <stdlib.h>
 14 #include <string.h>
 15 #include <XmlBlasterAccessUnparsed.h>
 16 
 17 /**
 18  * Demo client to synchronous get messages. 
 19  * Not all GetQos functionality is implemented.
 20  *
 21  * Access all messages: 
 22  *   Get -xpath //key
 23  *
 24  * Access all user names:
 25  *   Get -oid __sys__UserList
 26  *
 27  * Access free memory:
 28  *   Get -oid __cmd:?freeMem
 29  *
 30  * Access silently, useful for scripting:
 31  *   Get -oid __cmd:?freeMem -verbose false -sleep 0  -dumpRawContent true -logLevel WARN
 32  *   Get -oid __cmd:?topicList -verbose false -sleep 0  -dumpRawContent true -logLevel WARN
 33  *   Get -oid __cmd:?clientList -verbose false -sleep 0  -dumpRawContent true -logLevel WARN
 34  */
 35 int main(int argc, char** argv)
 36 {
 37    int iarg;
 38    XmlBlasterException xmlBlasterException;
 39    XmlBlasterAccessUnparsed *xa = 0;
 40    bool disconnect = true;
 41    bool verbose = true;
 42 
 43    for (iarg=0; iarg < argc; iarg++) {
 44       if (strcmp(argv[iarg], "-help") == 0 || strcmp(argv[iarg], "--help") == 0) {
 45          char usage[XMLBLASTER_MAX_USAGE_LEN];
 46          const char *pp =
 47          "\n\nExample:"
 48          "\n  Get -logLevel TRACE"
 49          " -dispatch/connection/plugin/socket/hostname 192.168.2.9";
 50          printf("Usage:\nXmlBlaster C SOCKET client %s\n%s%s\n",
 51                   getXmlBlasterVersion(), xmlBlasterAccessUnparsedUsage(usage), pp);
 52          exit(EXIT_FAILURE);
 53       }
 54    }
 55 
 56    xa = getXmlBlasterAccessUnparsed(argc, (const char* const* )argv);
 57    if (xa->initialize(xa, 0, &xmlBlasterException) == false) {
 58       printf("[client] Connection to xmlBlaster failed,"
 59              " please start the server or check your configuration\n");
 60       freeXmlBlasterAccessUnparsed(xa);
 61       exit(EXIT_FAILURE);
 62    }
 63 
 64    verbose = xa->props->getBool(xa->props, "verbose", verbose);
 65    if (verbose)
 66       printf("[client] XmlBlaster %s C SOCKET client, try option '-help' if you need"
 67              " usage informations\n", getXmlBlasterVersion());
 68 
 69    disconnect = xa->props->getBool(xa->props, "disconnect", disconnect);
 70 
 71    {  /* connect */
 72       char *response = (char *)0;
 73       const char * const sessionName = xa->props->getString(xa->props, "session.name", "Get");
 74       int maxSessions = xa->props->getInt(xa->props, "session.maxSessions", 10);
 75       const bool persistent = xa->props->getBool(xa->props, "persistentConnection", false);
 76       char connectQos[4096];
 77       sprintf(connectQos,
 78                "<qos>"
 79                " <securityService type='htpasswd' version='1.0'>"
 80                "  <![CDATA["
 81                "   <user>%.80s</user>"
 82                "   <passwd>secret</passwd>"
 83                "  ]]>"
 84                " </securityService>"
 85                " <session name='%.80s' timeout='3600000' maxSessions='%d' clearSessions='false' reconnectSameClientOnly='false'/>"
 86                " %.20s"
 87                "</qos>", sessionName, sessionName, maxSessions, persistent?"<persistent/>":"");
 88 
 89       response = xa->connect(xa, connectQos, 0, &xmlBlasterException);
 90       if (*xmlBlasterException.errorCode != 0) {
 91          printf("[client] Caught exception during connect errorCode=%s, message=%s\n",
 92                   xmlBlasterException.errorCode, xmlBlasterException.message);
 93          freeXmlBlasterAccessUnparsed(xa);
 94          exit(EXIT_FAILURE);
 95       }
 96       xmlBlasterFree(response);
 97       if (verbose)
 98          printf("[client] Connected to xmlBlaster, do some tests ...\n");
 99    }
100 
101    {  /* get synchronous ... */
102       size_t i;
103       int iGet;
104       const char *getToken = 0;
105 
106       char key[4098];
107       const char *oid = xa->props->getString(xa->props, "oid", "Hello");
108       const char *domain = xa->props->getString(xa->props, "domain", 0);
109       const char *xpath = xa->props->getString(xa->props, "xpath", 0);
110 
111       char filterQos[2048];
112       char qos[4098];
113       long sleep = xa->props->getLong(xa->props, "sleep", -1L); /* -1 == interactive */
114       int numGet = xa->props->getInt(xa->props, "numGet", 1);
115       bool dumpRawContent = xa->props->getBool(xa->props, "dumpRawContent", false);
116       bool persistent = xa->props->getBool(xa->props, "get/qos/persistent", true);
117       int historyNumUpdates = xa->props->getInt(xa->props, "get/qos/historyNumUpdates", 1);
118       bool historyNewestFirst = xa->props->getBool(xa->props, "get/qos/historyNewestFirst", true);
119       bool wantContent = xa->props->getBool(xa->props, "get/qos/wantContent", true);
120       const char *filterType = xa->props->getString(xa->props, "get/qos/filter.type", "GnuRegexFilter");
121       const char *filterVersion = xa->props->getString(xa->props, "get/qos/filter.version", "1.0");
122       const char *filterQuery = xa->props->getString(xa->props, "get/qos/filter.query", 0);  /* "^H.*$" */
123       /*Map clientPropertyMap = xa->props->getInt(xa->props, "clientProperty", (Map)0); */
124 
125       if (domain) {
126          sprintf(key, "<key domain='%.512s'/>", domain);
127          getToken = domain;
128       }
129       else if (xpath) {
130          sprintf(key, "<key queryType='XPATH'>%.512s</key>", xpath);
131          getToken = xpath;
132       }
133       else {
134          sprintf(key, "<key oid='%.512s' queryType='EXACT'/>", oid);
135          getToken = oid;
136       }
137 
138       if (filterQuery) {
139          sprintf(filterQos, " <filter type='%.100s' version='%.50s'>%.1800s</filter>",
140                  filterType, filterVersion, filterQuery);
141       }
142       else
143          *filterQos = 0;
144 
145       sprintf(qos, "<qos>"
146                    " <content>%.20s</content>"
147                    " <persistent>%.20s</persistent>"
148                    "%.2048s"
149                    " <history numEntries='%d' newestFirst='%.20s'/>"
150                    "</qos>",
151                    wantContent?"true":"false",
152                    persistent?"true":"false",
153                    filterQos,
154                    historyNumUpdates,
155                    historyNewestFirst?"true":"false"
156                    );
157 
158       if (verbose) {
159          printf("[client] Get key: %s\n", key);
160          printf("[client] Get qos: %s\n", qos);
161       }
162 
163       for (iGet=0; iGet<numGet; iGet++) {
164          MsgUnitArr *msgUnitArr;
165 
166          if (sleep < 0) { /* -1 */
167             char msg[20];
168             printf("[client] Hit a key to get '%s' #%d/%d ('b' to break) >> ", getToken, iGet, numGet);
169             fgets(msg, 19, stdin);
170             if (*msg == 'b') 
171                break;
172          }
173          else if (sleep == 0) {
174             if (verbose) printf("[client] Get '%s' #%d/%d", oid, iGet, numGet);
175          }
176          else {
177             sleepMillis(sleep);
178             if (verbose) printf("[client] Get '%s' #%d/%d", oid, iGet, numGet);
179          }
180 
181          msgUnitArr = xa->get(xa, key, qos, &xmlBlasterException);
182          if (*xmlBlasterException.errorCode != 0) {
183             printf("[client] Caught exception in get errorCode=%s, message=%s\n",
184                      xmlBlasterException.errorCode, xmlBlasterException.message);
185             xa->disconnect(xa, 0, &xmlBlasterException);
186             freeXmlBlasterAccessUnparsed(xa);
187             exit(EXIT_FAILURE);
188          }
189          if (msgUnitArr != (MsgUnitArr *)0) {
190             for (i=0; i<msgUnitArr->len; i++) {
191                if (dumpRawContent) {
192                   MsgUnit *msg = &msgUnitArr->msgUnitArr[i];
193                   char *contentStr = strFromBlobAlloc(msg->content, msg->contentLen);
194                   printf("%s", contentStr);
195                   xmlBlasterFree(contentStr);
196                }
197                else {
198                   char *m = messageUnitToXmlLimited(&msgUnitArr->msgUnitArr[i], 100);
199                   printf("\n[client] Get synchronous returned message#%lu/%lu:\n"
200                            "-------------------------------------"
201                            "%s\n"
202                            "-------------------------------------\n",
203                            (unsigned long)(i+1), (unsigned long)msgUnitArr->len, m);
204                   xmlBlasterFree(m);
205                }
206             }
207             if (msgUnitArr->len == 0) {
208                printf("\n[client] Get '%s' synchronous: No message found\n", getToken);
209             }
210             freeMsgUnitArr(msgUnitArr);
211          }
212          else {
213             printf("[client] Caught exception in get errorCode=%s, message=%s\n",
214                      xmlBlasterException.errorCode, xmlBlasterException.message);
215             xa->disconnect(xa, 0, &xmlBlasterException);
216             freeXmlBlasterAccessUnparsed(xa);
217             exit(EXIT_FAILURE);
218          }
219       }
220    }
221 
222    if (disconnect) {
223       if (xa->disconnect(xa, 0, &xmlBlasterException) == false) {
224          printf("[client] Caught exception in disconnect, errorCode=%s, message=%s\n",
225                   xmlBlasterException.errorCode, xmlBlasterException.message);
226          freeXmlBlasterAccessUnparsed(xa);
227          exit(EXIT_FAILURE);
228       }
229    }
230 
231    freeXmlBlasterAccessUnparsed(xa);
232    if (verbose) printf("[client] Good bye.\n");
233    return 0;
234 }


syntax highlighted by Code2HTML, v. 0.9.1