4
////////////////////////////////////////////////////////////////////////////////
6
// Magic Squares Server
8
////////////////////////////////////////////////////////////////////////////////
10
// Install as a CGI application.
11
// Alternatively, run from command line with arguments IP (which must be the
12
// IP of the current machine you are using) and PORT to run this as a
13
// stand-alone server on a port. For example:
14
// > magicserver.cgi 18081 &
15
// To let 'magic' talk to this service, change the URL in magic.cpp into
16
// "http://localhost:18081"
18
int main(int argc, char **argv)
22
// soap.accept_timeout = 60; // die if no requests are made within 1 minute
26
soap_end(&soap); // clean up
29
{ m = soap_bind(&soap, NULL, atoi(argv[1]), 100);
31
{ soap_print_fault(&soap, stderr);
34
fprintf(stderr, "Socket connection successful %d\n", m);
35
for (int i = 1; ; i++)
36
{ s = soap_accept(&soap);
39
soap_print_fault(&soap, stderr);
41
fprintf(stderr, "Server timed out\n");
44
fprintf(stderr, "%d: accepted %d IP=%d.%d.%d.%d ... ", i, s, (int)(soap.ip>>24)&0xFF, (int)(soap.ip>>16)&0xFF, (int)(soap.ip>>8)&0xFF, (int)soap.ip&0xFF);
45
soap_serve(&soap); // process RPC skeletons
46
fprintf(stderr, "served\n");
48
soap_end(&soap); // clean up
55
////////////////////////////////////////////////////////////////////////////////
57
// Magic Square Algorithm
59
////////////////////////////////////////////////////////////////////////////////
61
int ns1__magic(struct soap *soap, int n, matrix *square)
62
{ int i, j, k, l, key = 2;
64
return soap_sender_fault(soap, "Negative or zero size", "<error xmlns=\"http://tempuri.org/\">The input parameter must be positive</error>");
66
return soap_sender_fault(soap, "size > 100", "<error xmlns=\"http://tempuri.org/\">The input parameter must not be too large</error>");
68
for (i = 0; i < n; i++)
69
for (j = 0; j < n; j++)
89
(*square)[i][j] = key;
95
////////////////////////////////////////////////////////////////////////////////
97
// Class vector Methods
99
////////////////////////////////////////////////////////////////////////////////
106
vector::vector(int n)
107
{ __ptr = (int*)soap_malloc(soap, n*sizeof(int));
112
{ soap_unlink(soap, this); // not required, but just to make sure if someone calls delete on this
115
void vector::resize(int n)
119
p = (int*)soap_malloc(soap, n*sizeof(int));
121
{ for (int i = 0; i < (n <= __size ? n : __size); i++)
123
soap_unlink(soap, __ptr);
130
int& vector::operator[](int i) const
131
{ if (!__ptr || i < 0 || i >= __size)
132
fprintf(stderr, "Array index out of bounds\n");
136
////////////////////////////////////////////////////////////////////////////////
138
// Class matrix Methods
140
////////////////////////////////////////////////////////////////////////////////
147
matrix::matrix(int rows, int cols)
148
{ __ptr = soap_new_vector(soap, rows);
149
for (int i = 0; i < cols; i++)
150
__ptr[i].resize(cols);
155
{ soap_unlink(soap, this); // not required, but just to make sure if someone calls delete on this
158
void matrix::resize(int rows, int cols)
163
{ p = soap_new_vector(soap, rows);
164
for (i = 0; i < (rows <= __size ? rows : __size); i++)
165
{ if (this[i].__size != cols)
166
(*this)[i].resize(cols);
167
(p+i)->__ptr = __ptr[i].__ptr;
168
(p+i)->__size = cols;
170
for (; i < rows; i++)
171
__ptr[i].resize(cols);
174
{ __ptr = soap_new_vector(soap, rows);
175
for (i = 0; i < rows; i++)
176
__ptr[i].resize(cols);
181
for (i = 0; i < __size; i++)
182
__ptr[i].resize(cols);
185
vector& matrix::operator[](int i) const
186
{ if (!__ptr || i < 0 || i >= __size)
187
fprintf(stderr, "Array index out of bounds\n");