~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to tools/gsoap/gsoap-linux-2.7/samples/magic/.svn/text-base/magicserver.cpp.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "soapH.h"
2
 
#include "magic.nsmap"
3
 
 
4
 
////////////////////////////////////////////////////////////////////////////////
5
 
//
6
 
//      Magic Squares Server
7
 
//
8
 
////////////////////////////////////////////////////////////////////////////////
9
 
 
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"
17
 
 
18
 
int main(int argc, char **argv)
19
 
{ struct soap soap;
20
 
  int m, s;
21
 
  soap_init(&soap);
22
 
  // soap.accept_timeout = 60; // die if no requests are made within 1 minute
23
 
  if (argc < 2)
24
 
  { soap_serve(&soap);
25
 
    soap_destroy(&soap);
26
 
    soap_end(&soap);    // clean up 
27
 
  }
28
 
  else
29
 
  { m = soap_bind(&soap, NULL, atoi(argv[1]), 100);
30
 
    if (m < 0)
31
 
    { soap_print_fault(&soap, stderr);
32
 
      exit(1);
33
 
    }
34
 
    fprintf(stderr, "Socket connection successful %d\n", m);
35
 
    for (int i = 1; ; i++)
36
 
    { s = soap_accept(&soap);
37
 
      if (s < 0)
38
 
      { if (soap.errnum)
39
 
          soap_print_fault(&soap, stderr);
40
 
        else
41
 
          fprintf(stderr, "Server timed out\n");
42
 
        break;
43
 
      }
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");
47
 
      soap_destroy(&soap);
48
 
      soap_end(&soap);  // clean up 
49
 
    }
50
 
  }
51
 
  soap_done(&soap);
52
 
  return 0;
53
 
}
54
 
 
55
 
////////////////////////////////////////////////////////////////////////////////
56
 
//
57
 
//      Magic Square Algorithm
58
 
//
59
 
////////////////////////////////////////////////////////////////////////////////
60
 
 
61
 
int ns1__magic(struct soap *soap, int n, matrix *square)
62
 
{ int i, j, k, l, key = 2;
63
 
  if (n < 1)
64
 
    return soap_sender_fault(soap, "Negative or zero size", "<error xmlns=\"http://tempuri.org/\">The input parameter must be positive</error>");
65
 
  if (n > 100)
66
 
    return soap_sender_fault(soap, "size > 100", "<error xmlns=\"http://tempuri.org/\">The input parameter must not be too large</error>");
67
 
  square->resize(n, n);
68
 
  for (i = 0; i < n; i++)
69
 
    for (j = 0; j < n; j++)
70
 
      (*square)[i][j] = 0;
71
 
  i = 0;
72
 
  j = (n-1)/2;
73
 
  (*square)[i][j] = 1;
74
 
  while (key <= n*n)
75
 
  { if (i-1 < 0)
76
 
      k = n-1;
77
 
    else
78
 
      k = i-1;
79
 
    if (j-1 < 0)
80
 
      l = n-1;
81
 
    else
82
 
      l = j-1;
83
 
    if ((*square)[k][l])
84
 
      i = (i+1) % n;
85
 
    else
86
 
    { i = k;
87
 
      j = l;
88
 
    }
89
 
    (*square)[i][j] = key;
90
 
    key++;
91
 
  }
92
 
  return SOAP_OK;
93
 
}
94
 
 
95
 
////////////////////////////////////////////////////////////////////////////////
96
 
//
97
 
//      Class vector Methods
98
 
//
99
 
////////////////////////////////////////////////////////////////////////////////
100
 
 
101
 
vector::vector()
102
 
{ __ptr = 0;
103
 
  __size = 0;
104
 
}
105
 
 
106
 
vector::vector(int n)
107
 
{ __ptr = (int*)soap_malloc(soap, n*sizeof(int));
108
 
  __size = n;
109
 
}
110
 
 
111
 
vector::~vector()
112
 
{ soap_unlink(soap, this); // not required, but just to make sure if someone calls delete on this
113
 
}
114
 
 
115
 
void vector::resize(int n)
116
 
{ int *p;
117
 
  if (__size == n)
118
 
    return;
119
 
  p = (int*)soap_malloc(soap, n*sizeof(int));
120
 
  if (__ptr)
121
 
  { for (int i = 0; i < (n <= __size ? n : __size); i++)
122
 
      p[i] = __ptr[i];
123
 
    soap_unlink(soap, __ptr);
124
 
    free(__ptr);
125
 
  }
126
 
  __size = n;
127
 
  __ptr = p;
128
 
}
129
 
 
130
 
int& vector::operator[](int i) const
131
 
{ if (!__ptr || i < 0 || i >= __size)
132
 
    fprintf(stderr, "Array index out of bounds\n");
133
 
  return (__ptr)[i];
134
 
}
135
 
 
136
 
////////////////////////////////////////////////////////////////////////////////
137
 
//
138
 
//      Class matrix Methods
139
 
//
140
 
////////////////////////////////////////////////////////////////////////////////
141
 
 
142
 
matrix::matrix()
143
 
{ __ptr = 0;
144
 
  __size = 0;
145
 
}
146
 
 
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);
151
 
  __size = rows;
152
 
}
153
 
 
154
 
matrix::~matrix()
155
 
{ soap_unlink(soap, this); // not required, but just to make sure if someone calls delete on this
156
 
}
157
 
 
158
 
void matrix::resize(int rows, int cols)
159
 
{ int i;
160
 
  vector *p;
161
 
  if (__size != rows)
162
 
  { if (__ptr)
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;
169
 
      }
170
 
      for (; i < rows; i++)
171
 
        __ptr[i].resize(cols);
172
 
    }
173
 
    else
174
 
    { __ptr = soap_new_vector(soap, rows);
175
 
      for (i = 0; i < rows; i++)
176
 
        __ptr[i].resize(cols);
177
 
      __size = rows;
178
 
    }
179
 
  }
180
 
  else
181
 
    for (i = 0; i < __size; i++)
182
 
      __ptr[i].resize(cols);
183
 
}
184
 
 
185
 
vector& matrix::operator[](int i) const
186
 
{ if (!__ptr || i < 0 || i >= __size)
187
 
    fprintf(stderr, "Array index out of bounds\n");
188
 
  return __ptr[i];
189
 
}