~clint-fewbar/ubuntu/precise/gearmand/drop-unneeded-patches

« back to all changes in this revision

Viewing changes to libtest/server_container.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2009-09-28 21:43:31 UTC
  • mto: (1.2.3 upstream) (6.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20090928214331-9bku0d3v1b1ypgp4
ImportĀ upstreamĀ versionĀ 0.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2
 
 * 
3
 
 *  libtest
4
 
 *
5
 
 *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
6
 
 *
7
 
 *  This library is free software; you can redistribute it and/or
8
 
 *  modify it under the terms of the GNU Lesser General Public
9
 
 *  License as published by the Free Software Foundation; either
10
 
 *  version 3 of the License, or (at your option) any later version.
11
 
 *
12
 
 *  This library is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 *  Lesser General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU Lesser General Public
18
 
 *  License along with this library; if not, write to the Free Software
19
 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
 
 */
21
 
 
22
 
#include <libtest/common.h>
23
 
 
24
 
#include <cassert>
25
 
#include <cerrno>
26
 
#include <cstdlib>
27
 
#include <iostream>
28
 
 
29
 
#include <algorithm> 
30
 
#include <functional> 
31
 
#include <locale>
32
 
 
33
 
// trim from end 
34
 
static inline std::string &rtrim(std::string &s)
35
 
36
 
  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); 
37
 
  return s; 
38
 
}
39
 
 
40
 
namespace libtest {
41
 
 
42
 
void server_startup_st::push_server(Server *arg)
43
 
{
44
 
  servers.push_back(arg);
45
 
 
46
 
  char port_str[NI_MAXSERV];
47
 
  snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
48
 
 
49
 
  std::string server_config_string;
50
 
  if (arg->has_socket())
51
 
  {
52
 
    server_config_string+= "--socket=";
53
 
    server_config_string+= '"';
54
 
    server_config_string+= arg->socket();
55
 
    server_config_string+= '"';
56
 
    server_config_string+= " ";
57
 
  }
58
 
  else
59
 
  {
60
 
    server_config_string+= "--server=";
61
 
    server_config_string+= arg->hostname();
62
 
    server_config_string+= ":";
63
 
    server_config_string+= port_str;
64
 
    server_config_string+= " ";
65
 
  }
66
 
 
67
 
  server_list+= server_config_string;
68
 
 
69
 
}
70
 
 
71
 
Server* server_startup_st::pop_server()
72
 
{
73
 
  Server *tmp= servers.back();
74
 
  servers.pop_back();
75
 
  return tmp;
76
 
}
77
 
 
78
 
bool server_startup_st::shutdown(uint32_t number_of_host)
79
 
{
80
 
  if (servers.size() > number_of_host)
81
 
  {
82
 
    Server* tmp= servers[number_of_host];
83
 
 
84
 
    if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid()))
85
 
    { }
86
 
    else
87
 
    {
88
 
      return true;
89
 
    }
90
 
  }
91
 
 
92
 
  return false;
93
 
}
94
 
 
95
 
void server_startup_st::shutdown_and_remove()
96
 
{
97
 
  for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
98
 
  {
99
 
    delete *iter;
100
 
  }
101
 
  servers.clear();
102
 
}
103
 
 
104
 
void server_startup_st::shutdown()
105
 
{
106
 
  for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
107
 
  {
108
 
    if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
109
 
    {
110
 
      Error << "Unable to kill:" <<  *(*iter);
111
 
    }
112
 
  }
113
 
}
114
 
 
115
 
void server_startup_st::restart()
116
 
{
117
 
  for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
118
 
  {
119
 
    (*iter)->start();
120
 
  }
121
 
}
122
 
 
123
 
server_startup_st::~server_startup_st()
124
 
{
125
 
  shutdown_and_remove();
126
 
}
127
 
 
128
 
bool server_startup_st::is_debug() const
129
 
{
130
 
  return bool(getenv("LIBTEST_MANUAL_GDB"));
131
 
}
132
 
 
133
 
bool server_startup_st::is_valgrind() const
134
 
{
135
 
  return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
136
 
}
137
 
 
138
 
bool server_startup_st::is_helgrind() const
139
 
{
140
 
  return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
141
 
}
142
 
 
143
 
 
144
 
bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
145
 
{
146
 
  Outn();
147
 
  (void)try_port;
148
 
 
149
 
  set_max_port(try_port);
150
 
 
151
 
  // Look to see if we are being provided ports to use
152
 
  {
153
 
    char variable_buffer[1024];
154
 
    snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
155
 
 
156
 
    char *var;
157
 
    if ((var= getenv(variable_buffer)))
158
 
    {
159
 
      in_port_t tmp= in_port_t(atoi(var));
160
 
 
161
 
      if (tmp > 0)
162
 
        try_port= tmp;
163
 
    }
164
 
  }
165
 
 
166
 
  libtest::Server *server= NULL;
167
 
  if (0)
168
 
  { }
169
 
  else if (server_type.compare("gearmand") == 0)
170
 
  {
171
 
    if (GEARMAND_BINARY)
172
 
    {
173
 
      if (HAVE_LIBGEARMAN)
174
 
      {
175
 
        server= build_gearmand("127.0.0.1", try_port);
176
 
      }
177
 
      else
178
 
      {
179
 
        Error << "Libgearman was not found";
180
 
      }
181
 
    } 
182
 
    else
183
 
    {
184
 
      Error << "No gearmand binary is available";
185
 
    }
186
 
  }
187
 
  else if (server_type.compare("blobslap_worker") == 0)
188
 
  {
189
 
    if (GEARMAND_BINARY)
190
 
    {
191
 
      if (HAVE_LIBGEARMAN)
192
 
      {
193
 
        server= build_blobslap_worker(try_port);
194
 
      }
195
 
      else
196
 
      {
197
 
        Error << "Libgearman was not found";
198
 
      }
199
 
    }
200
 
    else
201
 
    {
202
 
      Error << "No gearmand binary is available";
203
 
    }
204
 
  }
205
 
  else if (server_type.compare("memcached-sasl") == 0)
206
 
  {
207
 
    if (MEMCACHED_SASL_BINARY)
208
 
    {
209
 
      if (HAVE_LIBMEMCACHED)
210
 
      {
211
 
        server= build_memcached_sasl("127.0.0.1", try_port, construct.username(), construct.password());
212
 
      }
213
 
      else
214
 
      {
215
 
        Error << "Libmemcached was not found";
216
 
      }
217
 
    }
218
 
    else
219
 
    {
220
 
      Error << "No memcached binary that was compiled with sasl is available";
221
 
    }
222
 
  }
223
 
  else if (server_type.compare("memcached") == 0)
224
 
  {
225
 
    if (MEMCACHED_BINARY)
226
 
    {
227
 
      if (HAVE_LIBMEMCACHED)
228
 
      {
229
 
        server= build_memcached("127.0.0.1", try_port);
230
 
      }
231
 
      else
232
 
      {
233
 
        Error << "Libmemcached was not found";
234
 
      }
235
 
    }
236
 
    else
237
 
    {
238
 
      Error << "No memcached binary is available";
239
 
    }
240
 
  }
241
 
  else
242
 
  {
243
 
    Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
244
 
  }
245
 
 
246
 
  if (server == NULL)
247
 
  {
248
 
    Error << "Failure occured while creating server: " <<  server_type;
249
 
    return false;
250
 
  }
251
 
 
252
 
  /*
253
 
    We will now cycle the server we have created.
254
 
  */
255
 
  if (not server->cycle())
256
 
  {
257
 
    Error << "Could not start up server " << *server;
258
 
    delete server;
259
 
    return false;
260
 
  }
261
 
 
262
 
  server->build(argc, argv);
263
 
 
264
 
  if (construct.is_debug())
265
 
  {
266
 
    Out << "Pausing for startup, hit return when ready.";
267
 
    std::string gdb_command= server->base_command();
268
 
    std::string options;
269
 
    Out << "run " << server->args(options);
270
 
    getchar();
271
 
  }
272
 
  else if (server->start() == false)
273
 
  {
274
 
    Error << "Failed to start " << *server;
275
 
    delete server;
276
 
    return false;
277
 
  }
278
 
  else
279
 
  {
280
 
    Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
281
 
  }
282
 
 
283
 
  construct.push_server(server);
284
 
 
285
 
  if (default_port() == 0)
286
 
  {
287
 
    assert(server->has_port());
288
 
    set_default_port(server->port());
289
 
  }
290
 
 
291
 
  Outn();
292
 
 
293
 
  return true;
294
 
}
295
 
 
296
 
bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
297
 
{
298
 
  (void)try_port;
299
 
  Outn();
300
 
 
301
 
  Server *server= NULL;
302
 
  if (0)
303
 
  { }
304
 
  else if (server_type.compare("gearmand") == 0)
305
 
  {
306
 
    Error << "Socket files are not supported for gearmand yet";
307
 
  }
308
 
  else if (server_type.compare("memcached-sasl") == 0)
309
 
  {
310
 
    if (MEMCACHED_SASL_BINARY)
311
 
    {
312
 
      if (HAVE_LIBMEMCACHED)
313
 
      {
314
 
        server= build_memcached_sasl_socket("127.0.0.1", try_port, username(), password());
315
 
      }
316
 
      else
317
 
      {
318
 
        Error << "Libmemcached was not found";
319
 
      }
320
 
    }
321
 
    else
322
 
    {
323
 
      Error << "No memcached binary is available";
324
 
    }
325
 
  }
326
 
  else if (server_type.compare("memcached") == 0)
327
 
  {
328
 
    if (MEMCACHED_BINARY)
329
 
    {
330
 
      if (HAVE_LIBMEMCACHED)
331
 
      {
332
 
        server= build_memcached_socket("127.0.0.1", try_port);
333
 
      }
334
 
      else
335
 
      {
336
 
        Error << "Libmemcached was not found";
337
 
      }
338
 
    }
339
 
    else
340
 
    {
341
 
      Error << "No memcached binary is available";
342
 
    }
343
 
  }
344
 
  else
345
 
  {
346
 
    Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
347
 
  }
348
 
 
349
 
  if (server == NULL)
350
 
  {
351
 
    Error << "Failure occured while creating server: " <<  server_type;
352
 
    return false;
353
 
  }
354
 
 
355
 
  /*
356
 
    We will now cycle the server we have created.
357
 
  */
358
 
  if (not server->cycle())
359
 
  {
360
 
    Error << "Could not start up server " << *server;
361
 
    delete server;
362
 
    return false;
363
 
  }
364
 
 
365
 
  server->build(argc, argv);
366
 
 
367
 
  if (is_debug())
368
 
  {
369
 
    Out << "Pausing for startup, hit return when ready.";
370
 
    std::string gdb_command= server->base_command();
371
 
    std::string options;
372
 
    Out << "run " << server->args(options);
373
 
    getchar();
374
 
  }
375
 
  else if (not server->start())
376
 
  {
377
 
    Error << "Failed to start " << *server;
378
 
    delete server;
379
 
    return false;
380
 
  }
381
 
  else
382
 
  {
383
 
    Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
384
 
  }
385
 
 
386
 
  push_server(server);
387
 
 
388
 
  set_default_socket(server->socket().c_str());
389
 
 
390
 
  Outn();
391
 
 
392
 
  return true;
393
 
}
394
 
 
395
 
std::string server_startup_st::option_string() const
396
 
{
397
 
  std::string temp= server_list;
398
 
  rtrim(temp);
399
 
  return temp;
400
 
}
401
 
 
402
 
 
403
 
} // namespace libtest