1 /*------------------------------------------------------------------------------
2 Name: callbackServer.c
3
4 Project: xmlBlaster.org
5
6 Comment: Example how to receive asynchronous callback messages from xmlBlaster
7 with C and XmlRpc (see README)
8 See http://xmlrpc-c.sourceforge.net/
9
10 Author: xmlBlaster@marcelruff.info
11
12 Compile: Read xmlrpc-c/doc/overview.txt
13 SERVER_CFLAGS=`xmlrpc-c-config abyss-server --cflags`
14 SERVER_LIBS=`xmlrpc-c-config abyss-server --libs`
15 gcc $SERVER_CFLAGS -o callbackServer callbackServer.c $SERVER_LIBS -Wall
16
17 Invoke: callbackServer <pathToXmlrpcConf>abyss.conf
18 Please edit abyss.conf before starting,
19 e.g. set Port 8081
20 ------------------------------------------------------------------------------*/
21 #include <stdio.h>
22 #include <string.h>
23 #include <xmlrpc.h>
24 #include <xmlrpc_abyss.h>
25
26 xmlrpc_value *update (xmlrpc_env *env, xmlrpc_value *param_array, void *user_data);
27 xmlrpc_value *ping (xmlrpc_env *env, xmlrpc_value *param_array, void *user_data);
28
29
30 /**
31 * Starts XmlRpc Webserver as configured in abyss.conf
32 */
33 int main (int argc, char **argv)
34 {
35 if (argc != 2) {
36 fprintf(stderr, "Usage: servertest abyss.conf\n");
37 exit(1);
38 }
39
40 xmlrpc_server_abyss_init(XMLRPC_SERVER_ABYSS_NO_FLAGS, argv[1]);
41 xmlrpc_server_abyss_add_method("update", &update, NULL);
42 xmlrpc_server_abyss_add_method("ping", &ping, NULL);
43
44 printf("server: switching to background.\n");
45 xmlrpc_server_abyss_run();
46
47 /* We never reach this point. */
48 return 0;
49 }
50
51
52 /**
53 * Update message arrives here.
54 * This is the callback invoked from xmlBlaster
55 */
56 xmlrpc_value *update (xmlrpc_env *env, xmlrpc_value *param_array, void *user_data)
57 {
58 char *cbSessionId=NULL; // A unique ID, as specified by the subscriber (we can check it to trust xmlBlaster)
59 char *key=NULL; // The message meta information
60 char *qos=NULL; // Some Quality of Service information
61 unsigned char *content=NULL; // binary data, the message content
62 size_t len;
63 xmlrpc_value *retVal = NULL;
64 char *retData = NULL;
65
66 { // print info ...
67 printf("\n\n-------------------------------------------\n");
68 if (env->fault_occurred)
69 printf("callbackServer: Entering update(), ERROR message arrives ...\n");
70 else
71 printf("callbackServer: Entering update(), message arrives ...\n\n");
72 }
73
74 // Parse the received message ...
75 xmlrpc_parse_value(env, param_array, "(ss6s*)", &cbSessionId, &key, &content, &len, &qos);
76 if (env->fault_occurred) {
77 fprintf(stderr, "callbackServer: Error when parsing message ... %d, %d, %s\n",
78 env->fault_occurred, env->fault_code, env->fault_string);
79 xmlrpc_env_clean(env);
80 return xmlrpc_build_value(env, "s", "<qos><state>ERROR</state></qos>");
81 }
82
83 { // dump what we have got ...
84 unsigned char buf[len+2];
85 strncpy(buf, content, len);
86 *(buf + len) = 0;
87 printf("cbSessionId=%s\nkey=%s\ncontent=%s\nqos=%s\n",
88 cbSessionId, key, buf, qos);
89 printf("\n-------------------------------------------\n");
90 }
91
92 // Return our result
93 retData = "<qos><state>OK</state></qos>";
94 retVal = xmlrpc_build_value(env, "s#", retData, strlen(retData));
95 return retVal;
96 }
97
98
99 /**
100 * Update message arrives here.
101 * This is the callback invoked from xmlBlaster
102 */
103 xmlrpc_value *ping (xmlrpc_env *env, xmlrpc_value *param_array, void *user_data)
104 {
105 xmlrpc_value *retVal = NULL;
106 char *retData = NULL;
107
108 { // print info ...
109 printf("\n\n-------------------------------------------\n");
110 if (env->fault_occurred)
111 printf("callbackServer: Entering ping(), ERROR message arrives ...\n");
112 else
113 printf("callbackServer: Entering ping(), message arrives ...\n\n");
114 }
115
116 // Return our result
117 retData = "<qos><state>OK</state></qos>";
118 retVal = xmlrpc_build_value(env, "s#", retData, strlen(retData));
119 return retVal;
120 }
syntax highlighted by Code2HTML, v. 0.9.1