~ubuntu-branches/ubuntu/raring/gsoap/raring-proposed

« back to all changes in this revision

Viewing changes to gsoap/samples/xml-rpc-json/xml-rpc-currentTimeServer.cpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2011-11-01 05:14:38 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20111101051438-3bm51yiv71ng1uc6
Tags: 2.8.4-1
* New upstream release
* Drop gsoap-ipv6.patch implemented upstream
* Link gsoap SSL shared libraries with libssl (Closes: #646228)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
        xml-rpc-currentTimeServer.cpp
3
3
 
4
4
        XML-RPC currenTime server (C++ version)
 
5
        CGI or stand-alone multi-threaded server
5
6
 
6
7
        Returns XML-RPC message with current time to client.
7
8
 
8
9
        Compile:
9
10
        soapcpp2 xml-rpc.h
10
 
        cc xml-rpc-currentTimeServer.cpp xml-rpc.cpp xml-rpc-io.cpp stdsoap2.cpp soapC.cpp
 
11
        cc -o xml-rpc-currentTimeServer xml-rpc-currentTimeServer.cpp xml-rpc.cpp xml-rpc-io.cpp stdsoap2.cpp soapC.cpp
 
12
        Install as CGI on Web server
 
13
        Or run as stand-alone server (e.g. on port 18000):
 
14
        ./xml-rpc-currentTimeServer 18000
11
15
 
12
16
--------------------------------------------------------------------------------
13
17
gSOAP XML Web services tools
40
44
#include "soapH.h"
41
45
#include "xml-rpc-io.h"
42
46
 
 
47
#include <unistd.h>
 
48
#ifdef _POSIX_THREADS
 
49
#include <pthread.h>    // use Pthreads
 
50
#endif
 
51
 
 
52
#define BACKLOG (100)   // Max. request backlog
 
53
#define MAX_THR (8)     // Max. threads to serve requests
 
54
 
43
55
using namespace std;
44
56
 
45
57
int serve_request(soap*);
46
58
 
47
59
int main(int argc, char **argv)
48
60
{
49
 
  soap *ctx = soap_new();
 
61
  soap *ctx = soap_new1(SOAP_IO_KEEPALIVE | SOAP_XML_INDENT);
 
62
  ctx->send_timeout = 5; // 5 sec
 
63
  ctx->recv_timeout = 5; // 5 sec
50
64
 
51
65
  if (argc < 2)
52
66
    return serve_request(ctx);
53
67
 
54
68
  int port = atoi(argv[1]);
55
69
 
56
 
  if (!soap_valid_socket(soap_bind(ctx, NULL, port, 100)))
 
70
#ifdef _POSIX_THREADS
 
71
  pthread_t tid;
 
72
#endif
 
73
 
 
74
  if (!soap_valid_socket(soap_bind(ctx, NULL, port, BACKLOG)))
57
75
  {
58
76
    soap_print_fault(ctx, stderr);
59
77
    exit(1);
67
85
    }
68
86
    else
69
87
    {
 
88
#ifdef _POSIX_THREADS
 
89
      pthread_create(&tid, NULL, (void*(*)(void*))serve_request, (void*)soap_copy(ctx));
 
90
#else
70
91
      serve_request(ctx);
 
92
#endif
71
93
    }
72
94
  }
73
95
  soap_free(ctx);
77
99
 
78
100
int serve_request(soap* ctx)
79
101
{
 
102
  fprintf(stderr, "Started... ");
 
103
 
80
104
  methodCall m(ctx);
81
105
 
82
 
  if (m.recv() != SOAP_OK)
83
 
    soap_print_fault(ctx, stderr);
84
 
  else
 
106
#ifdef _POSIX_THREADS
 
107
  pthread_detach(pthread_self());
 
108
#endif
 
109
 
 
110
  // HTTP keep-alive max number of iterations
 
111
  unsigned int k = ctx->max_keep_alive;
 
112
 
 
113
  do
85
114
  {
86
 
    methodResponse r(ctx);
 
115
    if (ctx->max_keep_alive > 0 && !--k)
 
116
      ctx->keep_alive = 0;
87
117
 
 
118
    if (m.recv() != SOAP_OK)
 
119
      soap_print_fault(ctx, stderr);
 
120
    else
 
121
    {
 
122
      methodResponse r(ctx);
 
123
  
88
124
    if (!strcmp(m.name(), "currentTime.getCurrentTime"))
89
 
      // method name matches: first parameter of response is time
90
 
      r[0] = time(0);
91
 
    else
92
 
      // otherwise, set fault
93
 
      r.set_fault("Wrong method");
94
 
 
95
 
    if (r.send() != SOAP_OK)
96
 
      soap_print_fault(ctx, stderr);
97
 
  }
98
 
 
99
 
  // close (but keep-alive keeps socket open)
100
 
  soap_closesock(ctx);
 
125
        // method name matches: first parameter of response is time
 
126
        r[0] = time(0);
 
127
      else
 
128
        // otherwise, set fault
 
129
        r.set_fault("Wrong method");
 
130
 
 
131
      if (r.send() != SOAP_OK)
 
132
        soap_print_fault(ctx, stderr);
 
133
    }
 
134
    // close (keep-alive may keep socket open when client supports it)
 
135
    soap_closesock(ctx);
 
136
 
 
137
  } while (ctx->keep_alive);
 
138
 
 
139
  int err = ctx->error;
101
140
 
102
141
  // clean up
103
142
  soap_destroy(ctx);
104
143
  soap_end(ctx);
105
144
 
106
 
  return ctx->error;
 
145
#ifdef _POSIX_THREADS
 
146
  // free the ctx copy for this thread
 
147
  soap_free(ctx);
 
148
#endif
 
149
 
 
150
  fprintf(stderr, "done\n");
 
151
 
 
152
  return err;
107
153
}
108
154
 
109
155
/* Don't need a namespace table. We put an empty one here to avoid link errors */