00001 /*---------------------------------------------------------------------------- 00002 Name: xmlBlaster/demo/c/socket/Get.c 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Demo to get xmlBlaster messages from command line 00006 Author: "Marcel Ruff" <xmlBlaster@marcelruff.info> 00007 Compile: cd xmlBlaster; build.sh c 00008 (Win: copy xmlBlaster\src\c\socket\pthreadVC2.dll to your PATH) 00009 Invoke: Get -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 00035 int main(int argc, char** argv) 00036 { 00037 int iarg; 00038 XmlBlasterException xmlBlasterException; 00039 XmlBlasterAccessUnparsed *xa = 0; 00040 bool disconnect = true; 00041 bool verbose = true; 00042 00043 for (iarg=0; iarg < argc; iarg++) { 00044 if (strcmp(argv[iarg], "-help") == 0 || strcmp(argv[iarg], "--help") == 0) { 00045 char usage[XMLBLASTER_MAX_USAGE_LEN]; 00046 const char *pp = 00047 "\n\nExample:" 00048 "\n Get -logLevel TRACE" 00049 " -dispatch/connection/plugin/socket/hostname 192.168.2.9"; 00050 printf("Usage:\nXmlBlaster C SOCKET client %s\n%s%s\n", 00051 getXmlBlasterVersion(), xmlBlasterAccessUnparsedUsage(usage), pp); 00052 exit(EXIT_FAILURE); 00053 } 00054 } 00055 00056 xa = getXmlBlasterAccessUnparsed(argc, (const char* const* )argv); 00057 if (xa->initialize(xa, 0, &xmlBlasterException) == false) { 00058 printf("[client] Connection to xmlBlaster failed," 00059 " please start the server or check your configuration\n"); 00060 freeXmlBlasterAccessUnparsed(xa); 00061 exit(EXIT_FAILURE); 00062 } 00063 00064 verbose = xa->props->getBool(xa->props, "verbose", verbose); 00065 if (verbose) 00066 printf("[client] XmlBlaster %s C SOCKET client, try option '-help' if you need" 00067 " usage informations\n", getXmlBlasterVersion()); 00068 00069 disconnect = xa->props->getBool(xa->props, "disconnect", disconnect); 00070 00071 { /* connect */ 00072 char *response = (char *)0; 00073 const char * const sessionName = xa->props->getString(xa->props, "session.name", "Get"); 00074 int maxSessions = xa->props->getInt(xa->props, "session.maxSessions", 10); 00075 const bool persistent = xa->props->getBool(xa->props, "persistentConnection", false); 00076 char connectQos[4096]; 00077 sprintf(connectQos, 00078 "<qos>" 00079 " <securityService type='htpasswd' version='1.0'>" 00080 " <![CDATA[" 00081 " <user>%.80s</user>" 00082 " <passwd>secret</passwd>" 00083 " ]]>" 00084 " </securityService>" 00085 " <session name='%.80s' timeout='3600000' maxSessions='%d' clearSessions='false' reconnectSameClientOnly='false'/>" 00086 " %.20s" 00087 "</qos>", sessionName, sessionName, maxSessions, persistent?"<persistent/>":""); 00088 00089 response = xa->connect(xa, connectQos, 0, &xmlBlasterException); 00090 if (*xmlBlasterException.errorCode != 0) { 00091 printf("[client] Caught exception during connect errorCode=%s, message=%s\n", 00092 xmlBlasterException.errorCode, xmlBlasterException.message); 00093 freeXmlBlasterAccessUnparsed(xa); 00094 exit(EXIT_FAILURE); 00095 } 00096 xmlBlasterFree(response); 00097 if (verbose) 00098 printf("[client] Connected to xmlBlaster, do some tests ...\n"); 00099 } 00100 00101 { /* get synchronous ... */ 00102 size_t i; 00103 int iGet; 00104 const char *getToken = 0; 00105 00106 char key[4098]; 00107 const char *oid = xa->props->getString(xa->props, "oid", "Hello"); 00108 const char *domain = xa->props->getString(xa->props, "domain", 0); 00109 const char *xpath = xa->props->getString(xa->props, "xpath", 0); 00110 00111 char filterQos[2048]; 00112 char qos[4098]; 00113 long sleep = xa->props->getLong(xa->props, "sleep", -1L); /* -1 == interactive */ 00114 int numGet = xa->props->getInt(xa->props, "numGet", 1); 00115 bool dumpRawContent = xa->props->getBool(xa->props, "dumpRawContent", false); 00116 bool persistent = xa->props->getBool(xa->props, "get/qos/persistent", true); 00117 int historyNumUpdates = xa->props->getInt(xa->props, "get/qos/historyNumUpdates", 1); 00118 bool historyNewestFirst = xa->props->getBool(xa->props, "get/qos/historyNewestFirst", true); 00119 bool wantContent = xa->props->getBool(xa->props, "get/qos/wantContent", true); 00120 const char *filterType = xa->props->getString(xa->props, "get/qos/filter.type", "GnuRegexFilter"); 00121 const char *filterVersion = xa->props->getString(xa->props, "get/qos/filter.version", "1.0"); 00122 const char *filterQuery = xa->props->getString(xa->props, "get/qos/filter.query", 0); /* "^H.*$" */ 00123 /*Map clientPropertyMap = xa->props->getInt(xa->props, "clientProperty", (Map)0); */ 00124 00125 if (domain) { 00126 sprintf(key, "<key domain='%.512s'/>", domain); 00127 getToken = domain; 00128 } 00129 else if (xpath) { 00130 sprintf(key, "<key queryType='XPATH'>%.512s</key>", xpath); 00131 getToken = xpath; 00132 } 00133 else { 00134 sprintf(key, "<key oid='%.512s' queryType='EXACT'/>", oid); 00135 getToken = oid; 00136 } 00137 00138 if (filterQuery) { 00139 sprintf(filterQos, " <filter type='%.100s' version='%.50s'>%.1800s</filter>", 00140 filterType, filterVersion, filterQuery); 00141 } 00142 else 00143 *filterQos = 0; 00144 00145 sprintf(qos, "<qos>" 00146 " <content>%.20s</content>" 00147 " <persistent>%.20s</persistent>" 00148 "%.2048s" 00149 " <history numEntries='%d' newestFirst='%.20s'/>" 00150 "</qos>", 00151 wantContent?"true":"false", 00152 persistent?"true":"false", 00153 filterQos, 00154 historyNumUpdates, 00155 historyNewestFirst?"true":"false" 00156 ); 00157 00158 if (verbose) { 00159 printf("[client] Get key: %s\n", key); 00160 printf("[client] Get qos: %s\n", qos); 00161 } 00162 00163 for (iGet=0; iGet<numGet; iGet++) { 00164 MsgUnitArr *msgUnitArr; 00165 00166 if (sleep < 0) { /* -1 */ 00167 char msg[20]; 00168 printf("[client] Hit a key to get '%s' #%d/%d ('b' to break) >> ", getToken, iGet, numGet); 00169 fgets(msg, 19, stdin); 00170 if (*msg == 'b') 00171 break; 00172 } 00173 else if (sleep == 0) { 00174 if (verbose) printf("[client] Get '%s' #%d/%d", oid, iGet, numGet); 00175 } 00176 else { 00177 sleepMillis(sleep); 00178 if (verbose) printf("[client] Get '%s' #%d/%d", oid, iGet, numGet); 00179 } 00180 00181 msgUnitArr = xa->get(xa, key, qos, &xmlBlasterException); 00182 if (*xmlBlasterException.errorCode != 0) { 00183 printf("[client] Caught exception in get errorCode=%s, message=%s\n", 00184 xmlBlasterException.errorCode, xmlBlasterException.message); 00185 xa->disconnect(xa, 0, &xmlBlasterException); 00186 freeXmlBlasterAccessUnparsed(xa); 00187 exit(EXIT_FAILURE); 00188 } 00189 if (msgUnitArr != (MsgUnitArr *)0) { 00190 for (i=0; i<msgUnitArr->len; i++) { 00191 if (dumpRawContent) { 00192 MsgUnit *msg = &msgUnitArr->msgUnitArr[i]; 00193 char *contentStr = strFromBlobAlloc(msg->content, msg->contentLen); 00194 printf("%s", contentStr); 00195 xmlBlasterFree(contentStr); 00196 } 00197 else { 00198 char *m = messageUnitToXmlLimited(&msgUnitArr->msgUnitArr[i], 100); 00199 printf("\n[client] Get synchronous returned message#%lu/%lu:\n" 00200 "-------------------------------------" 00201 "%s\n" 00202 "-------------------------------------\n", 00203 (unsigned long)(i+1), (unsigned long)msgUnitArr->len, m); 00204 xmlBlasterFree(m); 00205 } 00206 } 00207 if (msgUnitArr->len == 0) { 00208 printf("\n[client] Get '%s' synchronous: No message found\n", getToken); 00209 } 00210 freeMsgUnitArr(msgUnitArr); 00211 } 00212 else { 00213 printf("[client] Caught exception in get errorCode=%s, message=%s\n", 00214 xmlBlasterException.errorCode, xmlBlasterException.message); 00215 xa->disconnect(xa, 0, &xmlBlasterException); 00216 freeXmlBlasterAccessUnparsed(xa); 00217 exit(EXIT_FAILURE); 00218 } 00219 } 00220 } 00221 00222 if (disconnect) { 00223 if (xa->disconnect(xa, 0, &xmlBlasterException) == false) { 00224 printf("[client] Caught exception in disconnect, errorCode=%s, message=%s\n", 00225 xmlBlasterException.errorCode, xmlBlasterException.message); 00226 freeXmlBlasterAccessUnparsed(xa); 00227 exit(EXIT_FAILURE); 00228 } 00229 } 00230 00231 freeXmlBlasterAccessUnparsed(xa); 00232 if (verbose) printf("[client] Good bye.\n"); 00233 return 0; 00234 } 00235