1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 
3 #ifndef MCS_H
4 #define MCS_H
5 
6 #include "src/config.h"
7 
8 /* The mcs API's are a level of indirection from direct libmemcached */
9 /* and libvbucket API usage. */
10 
11 typedef enum {
12     MCS_SUCCESS = 0,
13     MCS_FAILURE,
14     MCS_TIMEOUT,
15     MCS_MAXIMUM_RETURN /* Always add new error code before */
16 } mcs_return;
17 
18 typedef enum {
19     MCS_KIND_UNKNOWN = 0,
20     MCS_KIND_LIBVBUCKET,
21     MCS_KIND_LIBMEMCACHED,
22     MCS_KIND_MAX
23 } mcs_kind;
24 
25 #define MCS_IDENT_SIZE 300
26 #define MCS_HOSTNAME_SIZE 200
27 
28 typedef struct {
29     char hostname[MCS_HOSTNAME_SIZE];
30     int port;
31     SOCKET fd;
32     char *usr;
33     char *pwd;
34     char ident_a[MCS_IDENT_SIZE]; /* A string suitable as a hash key, ascii protocol. */
35     char ident_b[MCS_IDENT_SIZE]; /* A string suitable as a hash key, binary protocol. */
36 } mcs_server_st;
37 
38 typedef struct {
39     mcs_kind       kind;
40     void          *data;     /* Depends on kind. */
41     int            nservers; /* Size of servers array. */
42     mcs_server_st *servers;
43 } mcs_st;
44 
45 mcs_st *mcs_create(mcs_st *ptr, const char *config,
46                    const char *default_usr,
47                    const char *default_pwd,
48                    const char *opts);
49 
50 void mcs_free(mcs_st *ptr);
51 
52 bool mcs_stable_update(mcs_st *curr_version, mcs_st *next_version);
53 
54 uint32_t       mcs_server_count(mcs_st *ptr);
55 mcs_server_st *mcs_server_index(mcs_st *ptr, int i);
56 
57 uint32_t mcs_key_hash(mcs_st *ptr, const char *key, size_t key_length, int *vbucket);
58 
59 void mcs_server_invalid_vbucket(mcs_st *ptr, int server_index, int vbucket);
60 
61 void mcs_server_st_quit(mcs_server_st *ptr, uint8_t io_death);
62 
63 mcs_return mcs_server_st_connect(mcs_server_st *ptr,
64                                  int *errno_out,
65                                  bool blocking);
66 
67 ssize_t mcs_io_write(SOCKET fd, const void *buffer, size_t length);
68 mcs_return mcs_io_read(SOCKET fd, void *dta, size_t size, struct timeval *timeout);
69 void mcs_io_reset(SOCKET fd);
70 
71 const char *mcs_server_st_hostname(mcs_server_st *ptr);
72 int mcs_server_st_port(mcs_server_st *ptr);
73 SOCKET mcs_server_st_fd(mcs_server_st *ptr);
74 const char *mcs_server_st_usr(mcs_server_st *ptr);
75 const char *mcs_server_st_pwd(mcs_server_st *ptr);
76 char *mcs_server_st_ident(mcs_server_st *msst, bool is_ascii);
77 
78 mcs_return mcs_set_sock_opt(SOCKET sock);
79 
80 SOCKET mcs_connect(const char *hostname, int portnum,
81                    int *errno_out, bool blocking);
82 
83 /* ---------------------------------------- */
84 
85 #define MOXI_DEFAULT_LISTEN_PORT      0
86 #define MEMCACHED_DEFAULT_LISTEN_PORT 11210
87 
88 /* ---------------------------------------- */
89 
90 #include <libmemcached/memcached.h>
91 
92 /* ---------------------------------------- */
93 
94 
95 #include <libvbucket/vbucket.h>
96 
97 #undef  MOXI_DEFAULT_LISTEN_PORT
98 #define MOXI_DEFAULT_LISTEN_PORT      11211
99 
100 #undef  MEMCACHED_DEFAULT_LISTEN_PORT
101 #define MEMCACHED_DEFAULT_LISTEN_PORT 0
102 
103 #endif /* MCS_H */
104