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
22 #include <utils/linked_list.h>
24 typedef struct private_manager_t private_manager_t
;
27 * private data of manager
29 struct private_manager_t
{
37 * underlying storage database
42 * user id, if we are logged in
53 * Implementation of manager_t.create_gateway_enumerator.
55 static enumerator_t
* create_gateway_enumerator(private_manager_t
*this)
57 return this->store
->create_gateway_enumerator(this->store
, this->user
);
61 * Implementation of manager_t.select_gateway.
63 static gateway_t
* select_gateway(private_manager_t
*this, int select_id
)
67 enumerator_t
*enumerator
;
72 if (this->gateway
) this->gateway
->destroy(this->gateway
);
75 enumerator
= this->store
->create_gateway_enumerator(this->store
, this->user
);
76 while (enumerator
->enumerate(enumerator
, &id
, &name
, &port
, &address
))
82 this->gateway
= gateway_create_unix(name
);
86 host
= host_create_from_string(address
, port
);
89 this->gateway
= gateway_create_tcp(name
, host
);
95 enumerator
->destroy(enumerator
);
101 * Implementation of manager_t.logged_in.
103 static bool logged_in(private_manager_t
*this)
105 return this->user
!= 0;
109 * Implementation of manager_t.login.
111 static bool login(private_manager_t
*this, char *username
, char *password
)
115 this->user
= this->store
->login(this->store
, username
, password
);
117 return this->user
!= 0;
121 * Implementation of manager_t.logout.
123 static void logout(private_manager_t
*this)
127 this->gateway
->destroy(this->gateway
);
128 this->gateway
= NULL
;
134 * Implementation of manager_t.destroy
136 static void destroy(private_manager_t
*this)
138 if (this->gateway
) this->gateway
->destroy(this->gateway
);
145 manager_t
*manager_create(storage_t
*storage
)
147 private_manager_t
*this = malloc_thing(private_manager_t
);
149 this->public.login
= (bool(*)(manager_t
*, char *username
, char *password
))login
;
150 this->public.logged_in
= (bool(*)(manager_t
*))logged_in
;
151 this->public.logout
= (void(*)(manager_t
*))logout
;
152 this->public.create_gateway_enumerator
= (enumerator_t
*(*)(manager_t
*))create_gateway_enumerator
;
153 this->public.select_gateway
= (gateway_t
*(*)(manager_t
*, int id
))select_gateway
;
154 this->public.context
.destroy
= (void(*)(context_t
*))destroy
;
157 this->store
= storage
;
158 this->gateway
= NULL
;
160 return &this->public;