2 * Copyright (C) 2010 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "tnccs_manager.h"
19 #include <utils/linked_list.h>
20 #include <threading/rwlock.h>
22 typedef struct private_tnccs_manager_t private_tnccs_manager_t
;
23 typedef struct tnccs_entry_t tnccs_entry_t
;
24 typedef struct tnccs_connection_entry_t tnccs_connection_entry_t
;
27 * TNCCS constructor entry
29 struct tnccs_entry_t
{
37 * constructor function to create instance
39 tnccs_constructor_t constructor
;
43 * TNCCS connection entry
45 struct tnccs_connection_entry_t
{
59 * private data of tnccs_manager
61 struct private_tnccs_manager_t
{
66 tnccs_manager_t
public;
69 * list of TNCCS protocol entries
71 linked_list_t
*protocols
;
74 * connection ID counter
76 TNC_ConnectionID connection_id
;
79 * list of TNCCS connection entries
81 linked_list_t
*connections
;
84 * rwlock to lock TNCCS protocol and connection entries
90 METHOD(tnccs_manager_t
, add_method
, void,
91 private_tnccs_manager_t
*this, tnccs_type_t type
,
92 tnccs_constructor_t constructor
)
94 tnccs_entry_t
*entry
= malloc_thing(tnccs_entry_t
);
97 entry
->constructor
= constructor
;
99 this->lock
->write_lock(this->lock
);
100 this->protocols
->insert_last(this->protocols
, entry
);
101 this->lock
->unlock(this->lock
);
104 METHOD(tnccs_manager_t
, remove_method
, void,
105 private_tnccs_manager_t
*this, tnccs_constructor_t constructor
)
107 enumerator_t
*enumerator
;
108 tnccs_entry_t
*entry
;
110 this->lock
->write_lock(this->lock
);
111 enumerator
= this->protocols
->create_enumerator(this->protocols
);
112 while (enumerator
->enumerate(enumerator
, &entry
))
114 if (constructor
== entry
->constructor
)
116 this->protocols
->remove_at(this->protocols
, enumerator
);
120 enumerator
->destroy(enumerator
);
121 this->lock
->unlock(this->lock
);
124 METHOD(tnccs_manager_t
, create_instance
, tnccs_t
*,
125 private_tnccs_manager_t
*this, tnccs_type_t type
, bool is_server
)
127 enumerator_t
*enumerator
;
128 tnccs_entry_t
*entry
;
129 tnccs_t
*protocol
= NULL
;
131 this->lock
->read_lock(this->lock
);
132 enumerator
= this->protocols
->create_enumerator(this->protocols
);
133 while (enumerator
->enumerate(enumerator
, &entry
))
135 if (type
== entry
->type
)
137 protocol
= entry
->constructor(is_server
);
144 enumerator
->destroy(enumerator
);
145 this->lock
->unlock(this->lock
);
149 METHOD(tnccs_manager_t
, create_connection
, TNC_ConnectionID
,
150 private_tnccs_manager_t
*this, tnccs_t
*tnccs
)
152 tnccs_connection_entry_t
*entry
= malloc_thing(tnccs_connection_entry_t
);
154 entry
->id
= ++this->connection_id
;
155 entry
->tnccs
= tnccs
;
157 this->lock
->write_lock(this->lock
);
158 this->connections
->insert_last(this->connections
, entry
);
159 this->lock
->unlock(this->lock
);
161 DBG1(DBG_TNC
, "assigned TNCCS Connection ID %u", entry
->id
);
165 METHOD(tnccs_manager_t
, remove_connection
, void,
166 private_tnccs_manager_t
*this, TNC_ConnectionID id
)
168 enumerator_t
*enumerator
;
169 tnccs_connection_entry_t
*entry
;
171 this->lock
->write_lock(this->lock
);
172 enumerator
= this->connections
->create_enumerator(this->connections
);
173 while (enumerator
->enumerate(enumerator
, &entry
))
177 this->connections
->remove_at(this->connections
, enumerator
);
179 DBG1(DBG_TNC
, "removed TNCCS Connection ID %u", id
);
182 enumerator
->destroy(enumerator
);
183 this->lock
->unlock(this->lock
);
186 METHOD(tnccs_manager_t
, destroy
, void,
187 private_tnccs_manager_t
*this)
189 this->protocols
->destroy_function(this->protocols
, free
);
190 this->connections
->destroy_function(this->connections
, free
);
191 this->lock
->destroy(this->lock
);
198 tnccs_manager_t
*tnccs_manager_create()
200 private_tnccs_manager_t
*this;
204 .add_method
= _add_method
,
205 .remove_method
= _remove_method
,
206 .create_instance
= _create_instance
,
207 .create_connection
= _create_connection
,
208 .remove_connection
= _remove_connection
,
211 .protocols
= linked_list_create(),
212 .connections
= linked_list_create(),
213 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
216 return &this->public;