ia64/xen-unstable

view tools/xcs/connection.c @ 3966:0b9ff7354a95

bitkeeper revision 1.1236.1.47 (4225ff68LvKUt73k78dKjnXxsnQt7A)

Fix tools build. Clean up barrier defs.
Signed-off-by: Jerone Young <jyoung5@us.ibm.com>
Signed-off-by: Keir Fraser <keir.fraser@cl.cam.ac.uk>
author kaf24@scramble.cl.cam.ac.uk
date Wed Mar 02 18:01:12 2005 +0000 (2005-03-02)
parents 0a4b76b6b5a0
children
line source
1 /*
2 * connection.c
3 *
4 * State associated with a client connection to xcs.
5 *
6 * Copyright (c) 2004, Andrew Warfield
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "xcs.h"
14 connection_t *connection_list = NULL;
16 #define CONNECTED(_c) (((_c)->ctrl_fd != -1) || ((_c)->data_fd != -1))
18 connection_t *get_con_by_session(unsigned long session_id)
19 {
20 connection_t **c, *ent = NULL;
22 c = &connection_list;
24 DPRINTF("looking for id: %lu : %lu\n", session_id, (*c)->id);
26 while (*c != NULL)
27 {
28 if ((*c)->id == session_id)
29 return (*c);
30 c = &(*c)->next;
31 }
33 return ent;
34 }
36 connection_t *connection_new()
37 {
38 connection_t *con;
40 con = (connection_t *)malloc(sizeof(connection_t));
41 if (con == NULL)
42 {
43 DPRINTF("couldn't allocate a new connection\n");
44 return NULL;
45 }
47 con->bindings = NULL;
48 con->data_fd = -1;
49 con->ctrl_fd = -1;
51 /* connections need a unique session id.
52 * - this approach probably gets fixed later, but for the moment
53 * is unique, and clearly identifies a connection.
54 */
55 con->id = (unsigned long)con;
57 /* add it to the connection list */
58 con->next = connection_list;
59 connection_list = con;
61 return (con);
62 }
64 void connection_free(connection_t *con)
65 {
66 /* first free all subscribed bindings: */
68 while (con->bindings != NULL)
69 xcs_unbind(con, con->bindings->key.port, con->bindings->key.type);
71 /* now free the connection. */
72 free(con);
73 }
75 int connection_add_binding(connection_t *con, binding_key_t *key)
76 {
77 binding_key_ent_t *key_ent;
79 key_ent = (binding_key_ent_t *)malloc(sizeof(binding_key_ent_t));
80 if (key_ent == NULL)
81 {
82 DPRINTF("couldn't alloc key in connection_add_binding\n");
83 return -1;
84 }
86 key_ent->key = *key;
87 key_ent->next = con->bindings;
88 con->bindings = key_ent;
90 return 0;
91 }
93 int connection_remove_binding(connection_t *con, binding_key_t *key)
94 {
95 binding_key_ent_t *key_ent;
96 binding_key_ent_t **curs = &con->bindings;
98 while ((*curs != NULL) && (!BINDING_KEYS_EQUAL(&(*curs)->key, key)))
99 curs = &(*curs)->next;
101 if (*curs != NULL) {
102 key_ent = *curs;
103 *curs = (*curs)->next;
104 free(key_ent);
105 }
107 return 0;
108 }
111 int connection_has_binding(connection_t *con, binding_key_t *key)
112 {
113 binding_key_ent_t *ent;
114 int ret = 0;
116 ent = con->bindings;
118 while (ent != NULL)
119 {
120 if (BINDING_KEYS_EQUAL(key, &ent->key))
121 {
122 ret = 1;
123 break;
124 }
125 ent = ent->next;
126 }
128 return ret;
129 }
132 void gc_connection_list(void)
133 {
134 connection_t **c, *ent = NULL;
135 struct timeval now, delta;
137 c = &connection_list;
138 gettimeofday(&now, NULL);
140 while ( *c != NULL )
141 {
142 if ( !CONNECTED(*c) )
143 {
144 timersub(&now, &(*c)->disconnect_time, &delta);
145 if ( delta.tv_sec >= XCS_SESSION_TIMEOUT )
146 {
147 DPRINTF(" : Freeing connection %lu after %lds\n",
148 (*c)->id, delta.tv_sec);
149 ent = *c;
150 *c = (*c)->next;
151 connection_free(ent);
152 continue;
153 }
154 }
155 c = &(*c)->next;
156 }
157 }