1 /*----------------------------------------------------------------------------
2 Name: xmlBlaster/demo/c/socket/TestLeaveServer.c
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Example for all remote method invocations.
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 See: http://www.xmlblaster.org/xmlBlaster/doc/requirements/protocol.socket.html
10 -----------------------------------------------------------------------------*/
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <XmlBlasterAccessUnparsed.h>
15
16 /**
17 * Here we receive the callback messages from xmlBlaster
18 * @param msgUnitArr The received messages
19 * @param userData Is the corresponding XmlBlasterAccessUnparsed * pointer
20 * @param exception An OUT parameter to transport back an exception
21 * @see UpdateFp in XmlBlasterAccessUnparsed.h
22 * @see UpdateFp in CallbackServerUnparsed.h
23 */
24 static bool myUpdate(MsgUnitArr *msgUnitArr, void *userData,
25 XmlBlasterException *exception)
26 {
27 size_t i;
28 bool testException = false;
29 XmlBlasterAccessUnparsed *xa = (XmlBlasterAccessUnparsed *)userData;
30 if (userData != 0) ; /* Supress compiler warning */
31
32 for (i=0; i<msgUnitArr->len; i++) {
33 char *xml = messageUnitToXml(&msgUnitArr->msgUnitArr[i]);
34 printf("[client] CALLBACK update(): Asynchronous message update arrived:%s\n",
35 xml);
36 xmlBlasterFree(xml);
37 msgUnitArr->msgUnitArr[i].responseQos =
38 strcpyAlloc("<qos><state id='OK'/></qos>");
39 /* Return QoS: Everything is OK */
40 }
41 if (testException) {
42 strncpy0(exception->errorCode, "user.clientCode",
43 XMLBLASTEREXCEPTION_ERRORCODE_LEN);
44 strncpy0(exception->message, "I don't want these messages",
45 XMLBLASTEREXCEPTION_MESSAGE_LEN);
46 return false;
47 }
48
49 if (false /*xa->callbackMultiThreaded == true*/) {
50 /* publish from inside the update thread,
51 see -plugin/socket/multiThreaded true */
52 char *response = (char *)0;
53 XmlBlasterException xmlBlasterException;
54 MsgUnit msgUnit;
55 memset(&msgUnit, 0, sizeof(MsgUnit));
56 printf("[client] Publishing message 'HelloWorldCb from update thread' ...\n");
57 msgUnit.key = strcpyAlloc("<key oid='HelloWorldCb'/>");
58 msgUnit.content = strcpyAlloc("Some message payload");
59 msgUnit.contentLen = strlen(msgUnit.content);
60 msgUnit.qos =strcpyAlloc("<qos><persistent/></qos>");
61 response = xa->publish(xa, &msgUnit, &xmlBlasterException);
62 freeMsgUnitData(&msgUnit);
63 if (*xmlBlasterException.errorCode != 0) {
64 printf("[client] Caught exception in publish errorCode=%s, message=%s\n",
65 xmlBlasterException.errorCode, xmlBlasterException.message);
66 xa->disconnect(xa, 0, &xmlBlasterException);
67 freeXmlBlasterAccessUnparsed(xa);
68 exit(EXIT_FAILURE);
69 }
70 printf("[client] Publish success, returned status is '%s'\n", response);
71 xmlBlasterFree(response);
72 }
73
74 return true;
75 }
76
77 /**
78 * Invoke: TestLeaveServer -logLevel TRACE
79 */
80 int main(int argc, char** argv)
81 {
82 int iarg;
83 char *response = (char *)0;
84 /*
85 * callbackSessionId:
86 * Is created by the client and used to validate callback messages in update.
87 * This is sent on connect in ConnectQos.
88 * (Is different from the xmlBlaster secret session ID)
89 */
90 const char *callbackSessionId = "topSecret";
91 XmlBlasterException xmlBlasterException;
92 XmlBlasterAccessUnparsed *xa = 0;
93 int sleepInterval = 0;
94 /*
95 int tmpDbgFlag;
96
97 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
98 _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );
99 tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
100 tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
101 tmpDbgFlag |= _CRTDBG_CHECK_ALWAYS_DF;
102 tmpDbgFlag |= _CRTDBG_ALLOC_MEM_DF;
103 tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
104 _CrtSetDbgFlag(tmpDbgFlag);
105 */
106
107 printf("[client] XmlBlaster %s C SOCKET client, try option '-help' if you need"
108 " usage informations\n", getXmlBlasterVersion());
109
110 for (iarg=0; iarg < argc; iarg++) {
111 if (strcmp(argv[iarg], "-help") == 0 || strcmp(argv[iarg], "--help") == 0) {
112 char usage[XMLBLASTER_MAX_USAGE_LEN];
113 const char *pp =
114 "\n -logLevel ERROR | WARN | INFO | TRACE | DUMP [WARN]"
115 "\n -sleepInterval Milliseconds to wait on callback messages [0]"
116 "\n\nExample:"
117 "\n TestLeaveServer -logLevel TRACE"
118 " -dispatch/connection/plugin/socket/hostname 192.168.2.9"
119 " -sleepInterval 100000";
120 printf("Usage:\nXmlBlaster C SOCKET client %s\n%s%s\n",
121 getXmlBlasterVersion(), xmlBlasterAccessUnparsedUsage(usage), pp);
122 exit(EXIT_FAILURE);
123 }
124 else if (strcmp(argv[iarg], "-sleepInterval") == 0 && iarg < argc-1)
125 sleepInterval = atoi(argv[++iarg]);
126 }
127
128 xa = getXmlBlasterAccessUnparsed(argc, (const char* const*)argv);
129 if (xa->initialize(xa, myUpdate, &xmlBlasterException) == false) {
130 printf("[client] Connection to xmlBlaster failed,"
131 " please start the server or check your configuration\n");
132 freeXmlBlasterAccessUnparsed(xa);
133 exit(EXIT_FAILURE);
134 }
135
136 { /* connect */
137 char connectQos[2048];
138 char callbackQos[1024];
139 sprintf(callbackQos,
140 "<queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>"
141 " <callback type='SOCKET' sessionId='%s'>"
142 " socket://%.120s:%d"
143 " </callback>"
144 "</queue>",
145 callbackSessionId, xa->callbackP->hostCB, xa->callbackP->portCB);
146 sprintf(connectQos,
147 "<qos>"
148 " <securityService type='htpasswd' version='1.0'>"
149 " <![CDATA["
150 " <user>fritz</user>"
151 " <passwd>secret</passwd>"
152 " ]]>"
153 " </securityService>"
154 "%.1024s"
155 "</qos>", callbackQos);
156
157 response = xa->connect(xa, connectQos, myUpdate, &xmlBlasterException);
158 if (*xmlBlasterException.errorCode != 0) {
159 printf("[client] Caught exception during connect errorCode=%s, message=%s\n",
160 xmlBlasterException.errorCode, xmlBlasterException.message);
161 freeXmlBlasterAccessUnparsed(xa);
162 exit(EXIT_FAILURE);
163 }
164 xmlBlasterFree(response);
165 printf("[client] Connected to xmlBlaster, do some tests ...\n");
166 }
167
168 response = xa->ping(xa, 0, &xmlBlasterException);
169 if (response == (char *)0) {
170 printf("[client] ERROR: Pinging a connected server failed: errorCode=%s, message=%s\n",
171 xmlBlasterException.errorCode, xmlBlasterException.message);
172 freeXmlBlasterAccessUnparsed(xa);
173 exit(EXIT_FAILURE);
174 }
175 else {
176 printf("[client] Pinging a connected server, response=%s\n", response);
177 xmlBlasterFree(response);
178 }
179
180 { /* subscribe ... */
181 const char *key = "<key oid='HelloWorld'/>";
182 /*const char *key = "<key queryType='XPATH'>//key</key>";*/
183 const char *qos = "<qos/>";
184 printf("[client] Subscribe message 'HelloWorld' ...\n");
185 response = xa->subscribe(xa, key, qos, &xmlBlasterException);
186 if (*xmlBlasterException.errorCode != 0) {
187 printf("[client] Caught exception in subscribe errorCode=%s, message=%s\n",
188 xmlBlasterException.errorCode, xmlBlasterException.message);
189 xa->disconnect(xa, 0, &xmlBlasterException);
190 freeXmlBlasterAccessUnparsed(xa);
191 exit(EXIT_FAILURE);
192 }
193 printf("[client] Subscribe success, returned status is '%s'\n", response);
194 xmlBlasterFree(response);
195 }
196
197 if (sleepInterval > 0) {
198 printf("[client] Sleeping now and wait on 'HelloWorld' updates (start a publisher somewhere) ...\n");
199 sleepMillis(sleepInterval);
200 }
201
202 { /* publish ... */
203 MsgUnit msgUnit;
204 printf("[client] Publishing message 'HelloWorld' ...\n");
205 msgUnit.key = strcpyAlloc("<key oid='HelloWorld'/>");
206 msgUnit.content = strcpyAlloc("Some message payload");
207 msgUnit.contentLen = strlen(msgUnit.content);
208 msgUnit.qos =strcpyAlloc("<qos><persistent/></qos>");
209 response = xa->publish(xa, &msgUnit, &xmlBlasterException);
210 freeMsgUnitData(&msgUnit);
211 if (*xmlBlasterException.errorCode != 0) {
212 printf("[client] Caught exception in publish errorCode=%s, message=%s\n",
213 xmlBlasterException.errorCode, xmlBlasterException.message);
214 xa->disconnect(xa, 0, &xmlBlasterException);
215 freeXmlBlasterAccessUnparsed(xa);
216 exit(EXIT_FAILURE);
217 }
218 printf("[client] Publish success, returned status is '%s'\n", response);
219 xmlBlasterFree(response);
220 }
221
222 /*
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 freeXmlBlasterAccessUnparsed(xa);
231 printf("[client] Good bye.\n");
232 return 0;
233 }
syntax highlighted by Code2HTML, v. 0.9.1