Updating test framework for startup/shutdown of memcached.
[awesomized/libmemcached] / libtest / server.h
index e849f29a924b05587732ee67e1fb0dcc1d712e5d..c209881e3de25c3cf2b23ab97b87b44012de5235 100644 (file)
 
 #pragma once
 
-/*
-  Server startup and shutdown functions.
-*/
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include <cstring>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <string>
+#include <unistd.h>
+
+#define SERVERS_TO_CREATE 5
+
+struct server_st {
+private:
+  bool _used;
+  pid_t _pid;
+  in_port_t _port;
+  char pid_file[FILENAME_MAX]; // Did we start it, or was it just sitting there?
+public:
+
+  char hostname[NI_MAXHOST];
+
+  server_st() :
+    _used(false),
+    _pid(-1),
+    _port(0)
+  {
+    pid_file[0]= 0;
+    strncpy(hostname, "localhost", sizeof(hostname));
+  }
+
+  void set_port(in_port_t arg)
+  {
+    _port= arg;
+  }
+
+  in_port_t port() const
+  {
+    return _port;
+  }
+
+  bool has_port() const
+  {
+    return not _port == 0;
+  }
+
+  void set_used()
+  {
+    _used= true;
+  }
+
+  void set_pid(pid_t arg)
+  {
+    _pid= arg;
+  }
+
+  pid_t pid() const
+  {
+    return _pid;
+  }
 
-#include <libmemcached/memcached.h>
+  bool is_used() const
+  {
+    return _used;
+  }
 
-typedef struct server_startup_st server_startup_st;
+  ~server_st();
+
+  bool has_pid()
+  {
+    return _pid > 0;
+  }
+
+  bool is_socket() const
+  {
+    return hostname[0] == '/';
+  }
+
+  void set_hostname(const char *arg)
+  {
+    strncpy(hostname, arg, sizeof(hostname));
+  }
+
+  bool kill();
+
+private:
+  void reset_pid();
+};
+
+std::ostream& operator<<(std::ostream& output, const server_st &arg);
 
 struct server_startup_st
 {
   uint8_t count;
   uint8_t udp;
-  memcached_server_st *servers;
-  char *server_list;
+  std::string server_list;
+  server_st server[SERVERS_TO_CREATE];
+
+  server_startup_st() :
+    count(SERVERS_TO_CREATE),
+    udp(0)
+  { }
+
+  ~server_startup_st()
+  { }
 };
 
-void server_startup(server_startup_st *construct);
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+bool server_startup(server_startup_st *construct);
 void server_shutdown(server_startup_st *construct);
 
 #ifdef __cplusplus