* Libmemcached library
*
* Copyright (C) 2011 Data Differential, http://datadifferential.com/
- * Copyright (C) 2006-2009 Brian Aker All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
#include <config.h>
#include <iostream>
+#include <cstdlib>
+#include <cassert>
#include <libtest/server.h>
#include <libtest/killpid.h>
{
if (arg.is_socket())
{
- output << arg.hostname;
+ output << arg.hostname();
}
else
{
- output << arg.hostname << ":" << arg.port();
+ output << arg.hostname() << ":" << arg.port();
}
return output; // for multiple << operators
}
+static void global_sleep(void)
+{
+ static struct timespec global_sleep_value= { 0, 50000 };
+
+#ifdef WIN32
+ sleep(1);
+#else
+ nanosleep(&global_sleep_value, NULL);
+#endif
+}
+
+server_st::server_st(in_port_t port_arg, test_server_getpid *get_pid_arg, test_server_ping *ping_arg) :
+ _used(false),
+ _pid(-1),
+ _port(port_arg),
+ __get_pid(get_pid_arg),
+ __ping(ping_arg),
+ _hostname("localhost")
+{
+ pid_file[0]= 0;
+}
+
+server_st::server_st(const std::string &socket_file, test_server_getpid *get_pid_arg, test_server_ping *ping_arg) :
+ _used(false),
+ _pid(-1),
+ _port(0),
+ __get_pid(get_pid_arg),
+ __ping(ping_arg),
+ _hostname(socket_file)
+{
+ pid_file[0]= 0;
+}
+
+
server_st::~server_st()
{
- if (not _used)
+ if (has_pid())
{
kill();
}
}
+bool server_st::start()
+{
+ assert(not _command.empty());
+ assert(not has_pid());
+
+ if (has_pid())
+ return false;
+
+ if (system(_command.c_str()) == -1)
+ return false;
+
+ int count= 30;
+ while (not ping() and --count)
+ {
+ global_sleep();
+ }
+
+ if (count == 0)
+ {
+ return false;
+ }
+
+ _pid= get_pid();
+
+ return has_pid();
+}
+
void server_st::reset_pid()
{
pid_file[0]= 0;
_pid= -1;
}
-bool server_st::kill()
+pid_t server_st::pid()
{
- if (not has_pid() and pid_file[0] == 0)
+ if (not has_pid())
{
- return true;
+ _pid= get_pid();
}
-
- if (has_pid())
+
+ return _pid;
+}
+
+
+bool server_st::kill()
+{
+ if (is_used())
+ return false;
+
+ if ((_pid= get_pid()))
{
- kill_pid(pid());
+ kill_pid(_pid);
if (pid_file[0])
{
unlink(pid_file); // If this happens we may be dealing with a dead server that left its pid file.
return true;
}
+#if 0
else if (pid_file[0])
{
kill_file(pid_file);
return true;
}
+#endif
return false;
}
+
+void server_startup_st::push_server(server_st *arg)
+{
+ servers.push_back(arg);
+}
+
+void server_startup_st::shutdown()
+{
+ for (std::vector<server_st *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
+ {
+ if ((*iter)->is_used())
+ continue;
+
+ (*iter)->kill();
+ }
+}
+
+server_startup_st::~server_startup_st()
+{
+ for (std::vector<server_st *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
+ {
+ delete *iter;
+ }
+ servers.clear();
+}