CUBRID Engine  latest
connection_less.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * connection_less.c - "connectionless" interface for the client and server
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include "connection_cl.h"
31 #include "connection_less.h"
32 
33 static unsigned short css_make_entry_id (CSS_MAP_ENTRY * anchor);
34 static CSS_MAP_ENTRY *css_get_queued_entry (char *host, CSS_MAP_ENTRY * anchor);
35 
36 /*
37  * css_make_eid() - create an eid which is a combination of the entry id and
38  * the request id
39  * return: enquiry id
40  * entry_id(in): entry id
41  * rid(in): request id
42  */
43 unsigned int
44 css_make_eid (unsigned short entry_id, unsigned short rid)
45 {
46  int top;
47 
48  top = entry_id;
49  return ((top << 16) | rid);
50 }
51 
52 /*
53  * css_return_entry_from_eid() - lookup a queue entry based on the entry id
54  * return: map entry if find, or NULL
55  * eid(in): enquiry id
56  * anchor(in): map entry anchor
57  */
60 {
61  CSS_MAP_ENTRY *map_entry_p;
62  unsigned short entry_id;
63 
64  entry_id = CSS_ENTRYID_FROM_EID (eid);
65  for (map_entry_p = anchor; map_entry_p; map_entry_p = map_entry_p->next)
66  {
67  if (map_entry_p->id == entry_id)
68  {
69  return (map_entry_p);
70  }
71  }
72  return (NULL);
73 }
74 
75 /*
76  * css_make_entry_id() - create an entry structure that will be queued for
77  * reuse
78  * return: entry id
79  * anchor(in): map entry anchor
80  */
81 static unsigned short
83 {
84  CSS_MAP_ENTRY *map_entry_p;
85  static unsigned short entry_id = 0;
86  unsigned short old_value;
87 
88  old_value = entry_id++;
89  if (!entry_id)
90  {
91  entry_id++;
92  }
93 
94  for (map_entry_p = anchor; map_entry_p; map_entry_p = map_entry_p->next)
95  {
96  if (entry_id == old_value)
97  {
99  }
100 
101  if (entry_id == map_entry_p->id)
102  {
103  entry_id++;
104  map_entry_p = anchor;
105  }
106  }
107 
108  return entry_id;
109 }
110 
111 /*
112  * css_queue_connection() - connection onto the connection entry queue
113  * return: man entry if success, or NULL
114  * conn(in): connection
115  * host(in): host name to connec
116  * anchor(out): map entry anchor
117  */
119 css_queue_connection (CSS_CONN_ENTRY * conn, const char *host, CSS_MAP_ENTRY ** anchor)
120 {
121  CSS_MAP_ENTRY *map_entry_p;
122 
123  if (conn == NULL)
124  {
125  return NULL;
126  }
127 
128  map_entry_p = (CSS_MAP_ENTRY *) malloc (sizeof (CSS_MAP_ENTRY));
129  if (map_entry_p != NULL)
130  {
131  if (host)
132  {
133  map_entry_p->key = (char *) malloc (strlen (host) + 1);
134  if (map_entry_p->key != NULL)
135  {
136  strcpy (map_entry_p->key, host);
137  }
138  }
139  else
140  {
141  map_entry_p->key = NULL;
142  }
143  map_entry_p->conn = conn;
144  map_entry_p->next = *anchor;
145  map_entry_p->id = css_make_entry_id (*anchor);
146  *anchor = map_entry_p;
147 
148  return (map_entry_p);
149  }
150 
151  return (NULL);
152 }
153 
154 /*
155  * css_get_queued_entry() - lookup a queue entry that has the same "name" as the
156  * destination
157  * return: map entry if found, or NULL
158  * host(in): host name to find
159  * anchor(in): map entry anchor
160  */
161 static CSS_MAP_ENTRY *
163 {
164  CSS_MAP_ENTRY *map_entry_p;
165 
166  for (map_entry_p = anchor; map_entry_p; map_entry_p = map_entry_p->next)
167  {
168  if (strcmp (host, map_entry_p->key) == 0)
169  {
170  return (map_entry_p);
171  }
172  }
173 
174  return (NULL);
175 }
176 
177 /*
178  * css_remove_queued_connection_by_entry() - remove the entry from our queue
179  * when a connection is "closed"
180  * return: void
181  * entry(in): entry to find
182  * anchor(in/out): map entry anchor
183  */
184 void
186 {
187  CSS_MAP_ENTRY *map_entry_p, *prev_map_entry_p;
188 
189  for (map_entry_p = *anchor, prev_map_entry_p = NULL; map_entry_p;
190  prev_map_entry_p = map_entry_p, map_entry_p = map_entry_p->next)
191  {
192  if (entry == map_entry_p)
193  {
194  if (map_entry_p == *anchor)
195  {
196  *anchor = map_entry_p->next;
197  }
198  else
199  {
200  prev_map_entry_p->next = map_entry_p->next;
201  }
202  break;
203  }
204  }
205 
206  if (map_entry_p)
207  {
208  free_and_init (map_entry_p->key);
209  free_and_init (map_entry_p);
210  }
211 }
212 
213 /*
214  * css_return_open_entry() - make sure that an open entry is returned
215  * return: map entry if open, or NULL
216  * host(in): host name to open
217  * anchor(in/out): map entry anchor
218  *
219  * Note: It does this by looking for a connection currently open to the host.
220  * If one is found, it is tested to be sure it is still open. If it is
221  * not open, or a connection is not found, a new connection is created
222  * and returned.
223  */
226 {
227  CSS_MAP_ENTRY *map_entry_p;
228 
229  map_entry_p = css_get_queued_entry (host, *anchor);
230  if (map_entry_p != NULL)
231  {
232  if (css_test_for_open_conn (map_entry_p->conn))
233  {
234  return (map_entry_p);
235  }
236  }
237 
238  return (NULL);
239 }
240 
241 /*
242  * css_return_entry_from_conn() - check the queue based on a conn_ptr
243  * return: the entry if it exists, or NULL
244  * conn(in): connection
245  * anchor(in): map entry anchor
246  */
249 {
250  CSS_MAP_ENTRY *map_entry_p;
251 
252  for (map_entry_p = anchor; map_entry_p; map_entry_p = map_entry_p->next)
253  {
254  if (map_entry_p->conn == conn)
255  {
256  return (map_entry_p);
257  }
258  }
259 
260  return (NULL);
261 }
262 
263 /*
264  * css_return_eid_from_conn() - return an eid from a conn pointer
265  * return: enquiry id
266  * conn(in): connection
267  * anchor(in/out): map entry anchor
268  * rid(in): request id
269  *
270  * Note: If the conn is not queued, it will be added, and the eid computed.
271  * This is for use by servers ONLY (note lack of host name).
272  */
273 unsigned int
274 css_return_eid_from_conn (CSS_CONN_ENTRY * conn, CSS_MAP_ENTRY ** anchor, unsigned short rid)
275 {
276  CSS_MAP_ENTRY *map_entry_p;
277 
278  map_entry_p = css_return_entry_from_conn (conn, *anchor);
279  if (map_entry_p == NULL)
280  {
281  map_entry_p = css_queue_connection (conn, (char *) "", anchor);
282  }
283 
284  if (map_entry_p == NULL)
285  {
286  return 0;
287  }
288  else
289  {
290  return (css_make_eid (map_entry_p->id, rid));
291  }
292 }
unsigned short id
unsigned int css_make_eid(unsigned short entry_id, unsigned short rid)
char * key
int css_test_for_open_conn(CSS_CONN_ENTRY *conn)
static int eid
Definition: cas_error_log.c:61
CSS_CONN_ENTRY * conn
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
static CSS_MAP_ENTRY * css_get_queued_entry(char *host, CSS_MAP_ENTRY *anchor)
CSS_MAP_ENTRY * css_return_entry_from_eid(unsigned int eid, CSS_MAP_ENTRY *anchor)
#define NULL
Definition: freelistheap.h:34
CSS_MAP_ENTRY * css_return_open_entry(char *host, CSS_MAP_ENTRY **anchor)
CSS_MAP_ENTRY * css_queue_connection(CSS_CONN_ENTRY *conn, const char *host, CSS_MAP_ENTRY **anchor)
CSS_MAP_ENTRY * next
#define ARG_FILE_LINE
Definition: error_manager.h:44
void css_remove_queued_connection_by_entry(CSS_MAP_ENTRY *entry, CSS_MAP_ENTRY **anchor)
#define free_and_init(ptr)
Definition: memory_alloc.h:147
#define strlen(s1)
Definition: intl_support.c:43
unsigned int css_return_eid_from_conn(CSS_CONN_ENTRY *conn, CSS_MAP_ENTRY **anchor, unsigned short rid)
CSS_MAP_ENTRY * css_return_entry_from_conn(CSS_CONN_ENTRY *conn, CSS_MAP_ENTRY *anchor)
#define CSS_ENTRYID_FROM_EID(eid)
#define ERR_CSS_ENTRY_OVERRUN
Definition: error_code.h:415
static char * host
static unsigned short css_make_entry_id(CSS_MAP_ENTRY *anchor)