1 /*----------------------------------------------------------------------------
2 Name: xmlBlaster/demo/c/socket/HelloWorldUdp.c
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Example for one calls with UDP.
6 Author: "Marcel Ruff" <xmlBlaster@marcelruff.info>
7 Compile: cd xmlBlaster; build c
8 (Win: copy xmlBlaster\src\c\socket\pthreadVC2.dll to your PATH)
9 Invoke: HelloWorldUdp -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 * Here we receive the callback messages from xmlBlaster
19 * @param msgUnitArr The received messages
20 * @param userData Is the corresponding XmlBlasterAccessUnparsed * pointer
21 * @param exception An OUT parameter to transport back an exception
22 * @see UpdateFp in XmlBlasterAccessUnparsed.h
23 * @see UpdateFp in CallbackServerUnparsed.h
24 */
25 static bool myUpdate(MsgUnitArr *msgUnitArr, void *userData,
26 XmlBlasterException *exception)
27 {
28 size_t i;
29 /*XmlBlasterAccessUnparsed *xa = (XmlBlasterAccessUnparsed *)userData;*/
30 if (userData != 0) ; /* Supress compiler warning */
31 if (exception != 0) ; /* Supress compiler warning */
32
33 for (i=0; i<msgUnitArr->len; i++) {
34 char *xml = messageUnitToXml(&msgUnitArr->msgUnitArr[i]);
35 printf("[client] CALLBACK update(): Asynchronous message update arrived:%s\n",
36 xml);
37 xmlBlasterFree(xml);
38 }
39
40 return true;
41 }
42
43 /**
44 * Invoke: HelloWorldUdp -logLevel TRACE
45 */
46 int main(int argc, char** argv)
47 {
48 int iarg;
49 char *response = (char *)0;
50 /*
51 * callbackSessionId:
52 * Is created by the client and used to validate callback messages in update.
53 * This is sent on connect in ConnectQos.
54 * (Is different from the xmlBlaster secret session ID)
55 */
56 const char *callbackSessionId = "topSecret";
57 XmlBlasterException xmlBlasterException;
58 XmlBlasterAccessUnparsed *xa = 0;
59 int sleepInterval = 1000;
60 bool updateOneway = true;
61
62 printf("[client] XmlBlaster %s C SOCKET client, try option '-help' if you need"
63 " usage informations\n", getXmlBlasterVersion());
64
65 for (iarg=0; iarg < argc; iarg++) {
66 if (strcmp(argv[iarg], "-help") == 0 || strcmp(argv[iarg], "--help") == 0) {
67 char usage[XMLBLASTER_MAX_USAGE_LEN];
68 const char *pp =
69 "\n -logLevel ERROR | WARN | INFO | TRACE | DUMP [WARN]"
70 "\n -sleepInterval Milliseconds to wait on callback messages [1000]"
71 "\n\nExample:"
72 "\n HelloWorldUdp -logLevel TRACE"
73 " -dispatch/connection/plugin/socket/enableUdp true"
74 " -dispatch/connection/plugin/socket/hostname 192.168.2.9"
75 " -updateOneway true";
76 printf("Usage:\nXmlBlaster C SOCKET client %s\n%s%s\n",
77 getXmlBlasterVersion(), xmlBlasterAccessUnparsedUsage(usage), pp);
78 exit(EXIT_FAILURE);
79 }
80 else if (strcmp(argv[iarg], "-sleepInterval") == 0 && iarg < argc-1)
81 sleepInterval = atoi(argv[++iarg]);
82 else if (strcmp(argv[iarg], "-updateOneway") == 0 && iarg < argc-1)
83 updateOneway = (!strcmp(argv[++iarg], "true")) ? true : false;
84 }
85
86 xa = getXmlBlasterAccessUnparsed(argc, (const char* const*)argv);
87 if (xa->initialize(xa, myUpdate, &xmlBlasterException) == false) {
88 printf("[client] Connection to xmlBlaster failed,"
89 " please start the server or check your configuration\n");
90 freeXmlBlasterAccessUnparsed(xa);
91 exit(EXIT_FAILURE);
92 }
93
94 { /* connect */
95 char connectQos[2048];
96 char callbackQos[1024];
97 /* oneway='true' as a general callback attribute is not used here */
98 sprintf(callbackQos,
99 "<queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>"
100 " <callback type='SOCKET' sessionId='%s'>"
101 " socket://%.120s:%d"
102 " </callback>"
103 "</queue>",
104 callbackSessionId, xa->callbackP->hostCB, xa->callbackP->portCB);
105 sprintf(connectQos,
106 "<qos>"
107 " <securityService type='htpasswd' version='1.0'>"
108 " <![CDATA["
109 " <user>fritz</user>"
110 " <passwd>secret</passwd>"
111 " ]]>"
112 " </securityService>"
113 "%.1024s"
114 "</qos>", callbackQos);
115
116 response = xa->connect(xa, connectQos, myUpdate, &xmlBlasterException);
117 if (*xmlBlasterException.errorCode != 0) {
118 printf("[client] Caught exception during connect errorCode=%s, message=%s\n",
119 xmlBlasterException.errorCode, xmlBlasterException.message);
120 freeXmlBlasterAccessUnparsed(xa);
121 exit(EXIT_FAILURE);
122 }
123 xmlBlasterFree(response);
124 printf("[client] Connected to xmlBlaster, do some tests ...\n");
125 }
126
127 { /* subscribe ... */
128 const char *key = "<key oid='HelloWorld'/>";
129 char qos[1024];
130 if (updateOneway)
131 strcpy(qos, "<qos><updateOneway/><notify>false</notify></qos>");
132 else
133 strcpy(qos, "<qos><notify>false</notify></qos>");
134 printf("[client] Subscribe message 'HelloWorld' with updateOneway=%s callback ...\n", updateOneway?"true":"false");
135 response = xa->subscribe(xa, key, qos, &xmlBlasterException);
136 if (*xmlBlasterException.errorCode != 0) {
137 printf("[client] Caught exception in subscribe errorCode=%s, message=%s\n",
138 xmlBlasterException.errorCode, xmlBlasterException.message);
139 xa->disconnect(xa, 0, &xmlBlasterException);
140 freeXmlBlasterAccessUnparsed(xa);
141 exit(EXIT_FAILURE);
142 }
143 printf("[client] Subscribe success, returned status is '%s'\n", response);
144 xmlBlasterFree(response);
145 }
146
147 { /* publishOneway */
148 MsgUnitArr holder;
149 memset(&holder, 0, sizeof(MsgUnitArr));
150 printf("[client] Publishing oneway messages 'HelloWorld' ...\n");
151 holder.len = 2;
152 holder.msgUnitArr = (MsgUnit *)calloc(holder.len, sizeof(MsgUnit));
153 holder.msgUnitArr[0].key = strcpyAlloc("<key oid='HelloWorld'/>");
154 holder.msgUnitArr[0].content = strcpyAlloc("Some message payload");
155 holder.msgUnitArr[0].contentLen = strlen(holder.msgUnitArr[0].content);
156 holder.msgUnitArr[0].qos =strcpyAlloc("<qos><expiration lifeTime='1000'/><forceUpdate>false</forceUpdate></qos>");
157
158 holder.msgUnitArr[1].key = strcpyAlloc("<key oid='HelloWorld'/>");
159 holder.msgUnitArr[1].content = strcpyAlloc("Some other message payload");
160 holder.msgUnitArr[1].contentLen = strlen(holder.msgUnitArr[1].content);
161 holder.msgUnitArr[1].qos =strcpyAlloc("<qos><expiration lifeTime='1000'/><forceUpdate>false</forceUpdate></qos>");
162
163 xa->publishOneway(xa, &holder, &xmlBlasterException);
164
165 freeMsgUnitArrInternal(&holder);
166 if (*xmlBlasterException.errorCode != 0) {
167 printf("[client] Caught exception in publishOneway errorCode=%s, message=%s\n",
168 xmlBlasterException.errorCode, xmlBlasterException.message);
169 xa->disconnect(xa, 0, &xmlBlasterException);
170 freeXmlBlasterAccessUnparsed(xa);
171 exit(EXIT_FAILURE);
172 }
173 }
174
175 printf("[client] Waiting one second on update messages ...\n");
176 sleepMillis(sleepInterval);
177
178 { /* erase ... */
179 QosArr* resp;
180 const char *key = "<key oid='HelloWorld'/>";
181 /*const char *key = "<key oid='' queryType='XPATH'>//key</key>";*/
182 const char *qos = "<qos/>";
183 printf("[client] Erasing message 'HelloWorld' ...\n");
184 resp = xa->erase(xa, key, qos, &xmlBlasterException);
185 if (*xmlBlasterException.errorCode != 0) {
186 printf("[client] Caught exception in erase errorCode=%s, message=%s\n",
187 xmlBlasterException.errorCode, xmlBlasterException.message);
188 xa->disconnect(xa, 0, &xmlBlasterException);
189 freeXmlBlasterAccessUnparsed(xa);
190 exit(EXIT_FAILURE);
191 }
192 if (resp != 0) {
193 size_t i;
194 for (i=0; i<resp->len; i++) {
195 printf("[client] Erase success, returned status is '%s'\n", resp->qosArr[i]);
196 }
197 freeQosArr(resp);
198 }
199 }
200
201 if (xa->disconnect(xa, 0, &xmlBlasterException) == false) {
202 printf("[client] Caught exception in disconnect, errorCode=%s, message=%s\n",
203 xmlBlasterException.errorCode, xmlBlasterException.message);
204 freeXmlBlasterAccessUnparsed(xa);
205 exit(EXIT_FAILURE);
206 }
207
208 freeXmlBlasterAccessUnparsed(xa);
209 printf("[client] Good bye.\n");
210 return 0;
211 }
syntax highlighted by Code2HTML, v. 0.9.1