287f8f0ce0ac344a30958b1f1e8efa1e0e51ea6b
[awesomized/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 {
58 set_behavior_flag(ptr, MEM_USE_SORT_HOSTS, data);
59
60 if (ptr->flags & MEM_USE_SORT_HOSTS)
61 {
62 memcached_quit(ptr);
63 run_distribution(ptr);
64 }
65
66 break;
67 }
68 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
69 ptr->poll_timeout= (int32_t)data;
70 break;
71 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
72 ptr->connect_timeout= (int32_t)data;
73 break;
74 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
75 ptr->retry_timeout= (int32_t)data;
76 break;
77 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
78 ptr->send_size= (int32_t)data;
79 memcached_quit(ptr);
80 break;
81 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
82 ptr->recv_size= (int32_t)data;
83 memcached_quit(ptr);
84 break;
85 case MEMCACHED_BEHAVIOR_USER_DATA:
86 return MEMCACHED_FAILURE;
87 }
88
89 return MEMCACHED_SUCCESS;
90 }
91
92 uint64_t memcached_behavior_get(memcached_st *ptr,
93 memcached_behavior flag)
94 {
95 memcached_flags temp_flag= 0;
96
97 switch (flag)
98 {
99 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
100 temp_flag= MEM_SUPPORT_CAS;
101 break;
102 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
103 temp_flag= MEM_USE_CACHE_LOOKUPS;
104 break;
105 case MEMCACHED_BEHAVIOR_NO_BLOCK:
106 temp_flag= MEM_NO_BLOCK;
107 break;
108 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
109 temp_flag= MEM_BUFFER_REQUESTS;
110 break;
111 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
112 temp_flag= MEM_TCP_NODELAY;
113 break;
114 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
115 temp_flag= MEM_VERIFY_KEY;
116 break;
117 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
118 return ptr->distribution;
119 case MEMCACHED_BEHAVIOR_HASH:
120 return ptr->hash;
121 case MEMCACHED_BEHAVIOR_KETAMA:
122 temp_flag= MEM_USE_KETAMA;
123 break;
124 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
125 temp_flag= MEM_USE_SORT_HOSTS;
126 break;
127 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
128 {
129 return (unsigned long long)ptr->poll_timeout;
130 }
131 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
132 {
133 return (unsigned long long)ptr->connect_timeout;
134 }
135 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
136 {
137 return (unsigned long long)ptr->retry_timeout;
138 }
139 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
140 {
141 int sock_size;
142 socklen_t sock_length= sizeof(int);
143
144 /* REFACTOR */
145 /* We just try the first host, and if it is down we return zero */
146 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
147 return 0;
148
149 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
150 SO_SNDBUF, &sock_size, &sock_length))
151 return 0; /* Zero means error */
152
153 return sock_size;
154 }
155 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
156 {
157 int sock_size;
158 socklen_t sock_length= sizeof(int);
159
160 /* REFACTOR */
161 /* We just try the first host, and if it is down we return zero */
162 if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
163 return 0;
164
165 if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
166 SO_RCVBUF, &sock_size, &sock_length))
167 return 0; /* Zero means error */
168
169 return sock_size;
170 }
171 case MEMCACHED_BEHAVIOR_USER_DATA:
172 return MEMCACHED_FAILURE;
173 }
174
175 WATCHPOINT_ASSERT(temp_flag); /* Programming mistake if it gets this far */
176 if (ptr->flags & temp_flag)
177 return 1;
178 else
179 return 0;
180
181 return MEMCACHED_SUCCESS;
182 }