New version update
[m6w6/libmemcached] / libmemcached / memcached_behavior.c
1 #include "common.h"
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/tcp.h>
5
6 /*
7 This function is used to modify the behabior of running client.
8
9 We quit all connections so we can reset the sockets.
10 */
11
12 void set_behavior_flag(memcached_st *ptr, memcached_flags temp_flag, uint64_t data)
13 {
14 if (data)
15 ptr->flags|= temp_flag;
16 else
17 ptr->flags&= ~temp_flag;
18 }
19
20 memcached_return memcached_behavior_set(memcached_st *ptr,
21 memcached_behavior flag,
22 uint64_t data)
23 {
24 switch (flag)
25 {
26 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
27 set_behavior_flag(ptr, MEM_SUPPORT_CAS, data);
28 break;
29 case MEMCACHED_BEHAVIOR_NO_BLOCK:
30 set_behavior_flag(ptr, MEM_NO_BLOCK, data);
31 memcached_quit(ptr);
32 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
33 set_behavior_flag(ptr, MEM_BUFFER_REQUESTS, data);
34 memcached_quit(ptr);
35 break;
36 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
37 set_behavior_flag(ptr, MEM_TCP_NODELAY, data);
38 memcached_quit(ptr);
39 break;
40 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
41 ptr->distribution= (memcached_server_distribution)(data);
42 break;
43 case MEMCACHED_BEHAVIOR_HASH:
44 ptr->hash= (memcached_hash)(data);
45 break;
46 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
47 set_behavior_flag(ptr, MEM_USE_CACHE_LOOKUPS, data);
48 memcached_quit(ptr);
49 break;
50 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
51 set_behavior_flag(ptr, MEM_VERIFY_KEY, data);
52 break;
53 case MEMCACHED_BEHAVIOR_KETAMA:
54 set_behavior_flag(ptr, MEM_USE_KETAMA, data);
55 break;
56 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
57 set_behavior_flag(ptr, MEM_USE_SORT_HOSTS, data);
58 break;
59 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
60 ptr->poll_timeout= (int32_t)data;
61 break;
62 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
63 ptr->connect_timeout= (int32_t)data;
64 break;
65 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
66 ptr->retry_timeout= (int32_t)data;
67 break;
68 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
69 ptr->send_size= (int32_t)data;
70 memcached_quit(ptr);
71 break;
72 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
73 ptr->recv_size= (int32_t)data;
74 memcached_quit(ptr);
75 break;
76 case MEMCACHED_BEHAVIOR_USER_DATA:
77 return MEMCACHED_FAILURE;
78 }
79
80 return MEMCACHED_SUCCESS;
81 }
82
83 uint64_t memcached_behavior_get(memcached_st *ptr,
84 memcached_behavior flag)
85 {
86 memcached_flags temp_flag= 0;
87
88 switch (flag)
89 {
90 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
91 temp_flag= MEM_SUPPORT_CAS;
92 break;
93 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
94 temp_flag= MEM_USE_CACHE_LOOKUPS;
95 break;
96 case MEMCACHED_BEHAVIOR_NO_BLOCK:
97 temp_flag= MEM_NO_BLOCK;
98 break;
99 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
100 temp_flag= MEM_BUFFER_REQUESTS;
101 break;
102 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
103 temp_flag= MEM_TCP_NODELAY;
104 break;
105 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
106 temp_flag= MEM_VERIFY_KEY;
107 break;
108 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
109 return ptr->distribution;
110 case MEMCACHED_BEHAVIOR_HASH:
111 return ptr->hash;
112 case MEMCACHED_BEHAVIOR_KETAMA:
113 temp_flag= MEM_USE_KETAMA;
114 break;
115 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
116 temp_flag= MEM_USE_SORT_HOSTS;
117 break;
118 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
119 {
120 return (unsigned long long)ptr->poll_timeout;
121 }
122 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
123 {
124 return (unsigned long long)ptr->connect_timeout;
125 }
126 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
127 {
128 return (unsigned long long)ptr->retry_timeout;
129 }
130 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
131 {
132 int sock_size;
133 socklen_t sock_length= sizeof(int);
134
135 /* REFACTOR */
136 /* We just try the first host, and if it is down we return zero */
137 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
138 return 0;
139
140 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
141 SO_SNDBUF, &sock_size, &sock_length))
142 return 0; /* Zero means error */
143
144 return sock_size;
145 }
146 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
147 {
148 int sock_size;
149 socklen_t sock_length= sizeof(int);
150
151 /* REFACTOR */
152 /* We just try the first host, and if it is down we return zero */
153 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
154 return 0;
155
156 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
157 SO_RCVBUF, &sock_size, &sock_length))
158 return 0; /* Zero means error */
159
160 return sock_size;
161 }
162 case MEMCACHED_BEHAVIOR_USER_DATA:
163 return MEMCACHED_FAILURE;
164 }
165
166 WATCHPOINT_ASSERT(temp_flag); /* Programming mistake if it gets this far */
167 if (ptr->flags & temp_flag)
168 return 1;
169 else
170 return 0;
171
172 return MEMCACHED_SUCCESS;
173 }