|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define _XOPEN_SOURCE 600 |
|
|
#define WIN32_LEAN_AND_MEAN |
|
|
|
|
|
#include <stdlib.h> |
|
|
#include <stdio.h> |
|
|
#include <signal.h> |
|
|
#ifndef _WIN32 |
|
|
#include <sys/socket.h> |
|
|
#include <netinet/in.h> |
|
|
#endif |
|
|
|
|
|
#include <xmlrpc-c/base.h> |
|
|
#include <xmlrpc-c/abyss.h> |
|
|
#include <xmlrpc-c/server.h> |
|
|
#include <xmlrpc-c/server_abyss.h> |
|
|
|
|
|
#include "config.h" |
|
|
|
|
|
|
|
|
static void |
|
|
setupSignalHandlers(void) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32 |
|
|
struct sigaction mysigaction; |
|
|
|
|
|
sigemptyset(&mysigaction.sa_mask); |
|
|
mysigaction.sa_flags = 0; |
|
|
mysigaction.sa_handler = SIG_IGN; |
|
|
sigaction(SIGPIPE, &mysigaction, NULL); |
|
|
#endif |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
printPeerIpAddr(TSession * const abyssSessionP) { |
|
|
|
|
|
#ifdef _WIN32 |
|
|
struct abyss_win_chaninfo * channelInfoP; |
|
|
#else |
|
|
struct abyss_unix_chaninfo * channelInfoP; |
|
|
#endif |
|
|
struct sockaddr_in * sockAddrInP; |
|
|
unsigned char * ipAddr; |
|
|
|
|
|
SessionGetChannelInfo(abyssSessionP, (void*)&channelInfoP); |
|
|
|
|
|
sockAddrInP = (struct sockaddr_in *) &channelInfoP->peerAddr; |
|
|
|
|
|
ipAddr = (unsigned char *)&sockAddrInP->sin_addr.s_addr; |
|
|
|
|
|
printf("RPC is from IP address %u.%u.%u.%u\n", |
|
|
ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static xmlrpc_value * |
|
|
sample_add(xmlrpc_env * const envP, |
|
|
xmlrpc_value * const paramArrayP, |
|
|
void * const serverInfo, |
|
|
void * const channelInfo) { |
|
|
|
|
|
xmlrpc_int x, y, z; |
|
|
|
|
|
printPeerIpAddr(channelInfo); |
|
|
|
|
|
|
|
|
xmlrpc_decompose_value(envP, paramArrayP, "(ii)", &x, &y); |
|
|
if (envP->fault_occurred) |
|
|
return NULL; |
|
|
|
|
|
|
|
|
z = x + y; |
|
|
|
|
|
|
|
|
return xmlrpc_build_value(envP, "i", z); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static xmlrpc_server_shutdown_fn requestShutdown; |
|
|
|
|
|
static void |
|
|
requestShutdown(xmlrpc_env * const faultP, |
|
|
void * const context, |
|
|
const char * const comment, |
|
|
void * const callInfo) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int * const terminationRequestedP = context; |
|
|
TSession * const abyssSessionP = callInfo; |
|
|
|
|
|
xmlrpc_env_init(faultP); |
|
|
|
|
|
fprintf(stderr, "Termination requested: %s\n", comment); |
|
|
|
|
|
printPeerIpAddr(abyssSessionP); |
|
|
|
|
|
*terminationRequestedP = 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
main(int const argc, |
|
|
const char ** const argv) { |
|
|
|
|
|
struct xmlrpc_method_info3 const methodInfo = { |
|
|
.methodName = "sample.add", |
|
|
.methodFunction = &sample_add, |
|
|
.serverInfo = NULL |
|
|
}; |
|
|
TServer abyssServer; |
|
|
xmlrpc_registry * registryP; |
|
|
xmlrpc_env env; |
|
|
int terminationRequested; |
|
|
const char * error; |
|
|
|
|
|
if (argc-1 != 1) { |
|
|
fprintf(stderr, "You must specify 1 argument: The TCP port number " |
|
|
"on which to listen for XML-RPC calls. " |
|
|
"You specified %d.\n", argc-1); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
AbyssInit(&error); |
|
|
|
|
|
xmlrpc_env_init(&env); |
|
|
|
|
|
registryP = xmlrpc_registry_new(&env); |
|
|
|
|
|
xmlrpc_registry_add_method3(&env, registryP, &methodInfo); |
|
|
|
|
|
xmlrpc_registry_set_shutdown(registryP, |
|
|
&requestShutdown, &terminationRequested); |
|
|
|
|
|
ServerCreate(&abyssServer, "XmlRpcServer", atoi(argv[1]), NULL, NULL); |
|
|
|
|
|
xmlrpc_server_abyss_set_handlers2(&abyssServer, "/RPC2", registryP); |
|
|
|
|
|
ServerInit(&abyssServer); |
|
|
|
|
|
setupSignalHandlers(); |
|
|
|
|
|
terminationRequested = 0; |
|
|
|
|
|
while (!terminationRequested) { |
|
|
printf("Waiting for next RPC...\n"); |
|
|
|
|
|
ServerRunOnce(&abyssServer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
ServerFree(&abyssServer); |
|
|
|
|
|
AbyssTerm(); |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|