summaryrefslogtreecommitdiff
path: root/src/manager/manager.c
blob: fb89c6b722e00f7ec80f811e668ec826419015aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "manager.h"

#include "gateway.h"

#include <utils/linked_list.h>

typedef struct private_manager_t private_manager_t;

/**
 * private data of manager
 */
struct private_manager_t {

	/**
	 * public functions
	 */
	manager_t public;

	/**
	 * underlying storage database
	 */
	storage_t *store;

	/**
	 * user id, if we are logged in
	 */
	int user;

	/**
	 * selected gateway
	 */
	gateway_t *gateway;
};

/**
 * Implementation of manager_t.create_gateway_enumerator.
 */
static enumerator_t* create_gateway_enumerator(private_manager_t *this)
{
	return this->store->create_gateway_enumerator(this->store, this->user);
}

/**
 * Implementation of manager_t.select_gateway.
 */
static gateway_t* select_gateway(private_manager_t *this, int select_id)
{
	if (select_id != 0)
	{
		enumerator_t *enumerator;
		int id, port;
		char *name, *address;
		host_t *host;

		if (this->gateway) this->gateway->destroy(this->gateway);
		this->gateway = NULL;

		enumerator = this->store->create_gateway_enumerator(this->store, this->user);
		while (enumerator->enumerate(enumerator, &id, &name, &port, &address))
		{
			if (select_id == id)
			{
				if (port == 0)
				{
					this->gateway = gateway_create_unix(name);
				}
				else
				{
					host = host_create_from_string(address, port);
					if (host)
					{
						this->gateway = gateway_create_tcp(name, host);
					}
				}
				break;
			}
		}
		enumerator->destroy(enumerator);
	}
	return this->gateway;
}

/**
 * Implementation of manager_t.logged_in.
 */
static bool logged_in(private_manager_t *this)
{
	return this->user != 0;
}

/**
 * Implementation of manager_t.login.
 */
static bool login(private_manager_t *this, char *username, char *password)
{
	if (!this->user)
	{
		this->user = this->store->login(this->store, username, password);
	}
	return this->user != 0;
}

/**
 * Implementation of manager_t.logout.
 */
static void logout(private_manager_t *this)
{
	if (this->gateway)
	{
		this->gateway->destroy(this->gateway);
		this->gateway = NULL;
	}
	this->user = 0;
}

/**
 * Implementation of manager_t.destroy
 */
static void destroy(private_manager_t *this)
{
	if (this->gateway) this->gateway->destroy(this->gateway);
	free(this);
}

/*
 * see header file
 */
manager_t *manager_create(storage_t *storage)
{
	private_manager_t *this = malloc_thing(private_manager_t);

	this->public.login = (bool(*)(manager_t*, char *username, char *password))login;
	this->public.logged_in = (bool(*)(manager_t*))logged_in;
	this->public.logout = (void(*)(manager_t*))logout;
	this->public.create_gateway_enumerator = (enumerator_t*(*)(manager_t*))create_gateway_enumerator;
	this->public.select_gateway = (gateway_t*(*)(manager_t*, int id))select_gateway;
	this->public.context.destroy = (void(*)(context_t*))destroy;

	this->user = 0;
	this->store = storage;
	this->gateway = NULL;

	return &this->public;
}