2 * Copyright (C) 2007 Martin Willi
3 * 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
20 #include <utils/linked_list.h>
22 typedef struct private_manager_t private_manager_t
;
25 * private data of manager
27 struct private_manager_t
{
35 * underlying storage database
40 * user id, if we are logged in
51 * Implementation of manager_t.create_gateway_enumerator.
53 static enumerator_t
* create_gateway_enumerator(private_manager_t
*this)
55 return this->store
->create_gateway_enumerator(this->store
, this->user
);
59 * Implementation of manager_t.select_gateway.
61 static gateway_t
* select_gateway(private_manager_t
*this, int select_id
)
65 enumerator_t
*enumerator
;
70 if (this->gateway
) this->gateway
->destroy(this->gateway
);
73 enumerator
= this->store
->create_gateway_enumerator(this->store
, this->user
);
74 while (enumerator
->enumerate(enumerator
, &id
, &name
, &port
, &address
))
80 this->gateway
= gateway_create_unix(name
);
84 host
= host_create_from_string(address
, port
);
87 this->gateway
= gateway_create_tcp(name
, host
);
93 enumerator
->destroy(enumerator
);
99 * Implementation of manager_t.logged_in.
101 static bool logged_in(private_manager_t
*this)
103 return this->user
!= 0;
107 * Implementation of manager_t.login.
109 static bool login(private_manager_t
*this, char *username
, char *password
)
113 this->user
= this->store
->login(this->store
, username
, password
);
115 return this->user
!= 0;
119 * Implementation of manager_t.logout.
121 static void logout(private_manager_t
*this)
125 this->gateway
->destroy(this->gateway
);
126 this->gateway
= NULL
;
132 * Implementation of manager_t.destroy
134 static void destroy(private_manager_t
*this)
136 if (this->gateway
) this->gateway
->destroy(this->gateway
);
143 manager_t
*manager_create(storage_t
*storage
)
145 private_manager_t
*this = malloc_thing(private_manager_t
);
147 this->public.login
= (bool(*)(manager_t
*, char *username
, char *password
))login
;
148 this->public.logged_in
= (bool(*)(manager_t
*))logged_in
;
149 this->public.logout
= (void(*)(manager_t
*))logout
;
150 this->public.create_gateway_enumerator
= (enumerator_t
*(*)(manager_t
*))create_gateway_enumerator
;
151 this->public.select_gateway
= (gateway_t
*(*)(manager_t
*, int id
))select_gateway
;
152 this->public.context
.destroy
= (void(*)(context_t
*))destroy
;
155 this->store
= storage
;
156 this->gateway
= NULL
;
158 return &this->public;