4 * @brief Implementation of manager_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include <utils/linked_list.h>
29 typedef struct private_manager_t private_manager_t
;
32 * private data of manager
34 struct private_manager_t
{
47 * user id, if we are logged in
58 * Implementation of manager_t.create_gateway_enumerator.
60 static enumerator_t
* create_gateway_enumerator(private_manager_t
*this)
62 return this->db
->create_gateway_enumerator(this->db
, this->user
);
66 * Implementation of manager_t.select_gateway.
68 static gateway_t
* select_gateway(private_manager_t
*this, int select_id
)
72 enumerator_t
*enumerator
;
77 if (this->gateway
) this->gateway
->destroy(this->gateway
);
80 enumerator
= this->db
->create_gateway_enumerator(this->db
, this->user
);
81 while (enumerator
->enumerate(enumerator
, &id
, &name
, &port
, &address
))
87 this->gateway
= gateway_create_unix(name
);
91 host
= host_create_from_string(address
, port
);
94 this->gateway
= gateway_create_tcp(name
, host
);
100 enumerator
->destroy(enumerator
);
102 return this->gateway
;
106 * Implementation of manager_t.logged_in.
108 static bool logged_in(private_manager_t
*this)
110 return this->user
!= 0;
114 * Implementation of manager_t.login.
116 static bool login(private_manager_t
*this, char *username
, char *password
)
120 this->user
= this->db
->login(this->db
, username
, password
);
122 return this->user
!= 0;
126 * Implementation of manager_t.logout.
128 static void logout(private_manager_t
*this)
132 this->gateway
->destroy(this->gateway
);
133 this->gateway
= NULL
;
139 * Implementation of manager_t.destroy
141 static void destroy(private_manager_t
*this)
143 if (this->gateway
) this->gateway
->destroy(this->gateway
);
150 manager_t
*manager_create(database_t
*database
)
152 private_manager_t
*this = malloc_thing(private_manager_t
);
154 this->public.login
= (bool(*)(manager_t
*, char *username
, char *password
))login
;
155 this->public.logged_in
= (bool(*)(manager_t
*))logged_in
;
156 this->public.logout
= (void(*)(manager_t
*))logout
;
157 this->public.create_gateway_enumerator
= (enumerator_t
*(*)(manager_t
*))create_gateway_enumerator
;
158 this->public.select_gateway
= (gateway_t
*(*)(manager_t
*, int id
))select_gateway
;
159 this->public.context
.destroy
= (void(*)(context_t
*))destroy
;
163 this->gateway
= NULL
;
165 return &this->public;