2 * @file gateway_controller.c
4 * @brief Implementation of gateway_controller_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
23 #include "gateway_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
32 typedef struct private_gateway_controller_t private_gateway_controller_t
;
35 * private data of the gateway_controller
37 struct private_gateway_controller_t
{
42 gateway_controller_t
public;
51 static void list(private_gateway_controller_t
*this,
52 request_t
*request
, response_t
*response
)
55 enumerator_t
*enumerator
;
59 t
= template_create("templates/gateway/list.cs");
60 enumerator
= this->manager
->create_gateway_enumerator(this->manager
);
61 while (enumerator
->enumerate(enumerator
, &id
, &name
, &port
, &address
))
63 t
->setf(t
, "gateways.%d.name=%s", id
, name
);
66 t
->setf(t
, "gateways.%d.address=tcp://%s:%d", id
, address
, port
);
70 t
->setf(t
, "gateways.%d.address=unix://%s", id
, address
);
73 enumerator
->destroy(enumerator
);
74 t
->set(t
, "action", "select");
75 t
->render(t
, response
);
79 static void _select(private_gateway_controller_t
*this,
80 request_t
*request
, response_t
*response
)
84 id
= request
->get_post_data(request
, "gateway");
87 if (this->manager
->select_gateway(this->manager
, atoi(id
)))
89 response
->redirect(response
, "status/ikesalist");
93 response
->printf(response
, "selecting dings failed: %s", id
);
97 * redirect to authentication login
99 static void login(private_gateway_controller_t
*this,
100 request_t
*request
, response_t
*response
)
102 response
->redirect(response
, "auth/login");
106 * Implementation of controller_t.get_name
108 static char* get_name(private_gateway_controller_t
*this)
114 * Implementation of controller_t.get_handler
116 static controller_handler_t
get_handler(private_gateway_controller_t
*this, char *name
)
118 if (!this->manager
->logged_in(this->manager
)) return (controller_handler_t
)login
;
119 if (streq(name
, "list")) return (controller_handler_t
)list
;
120 if (streq(name
, "select")) return (controller_handler_t
)_select
;
125 * Implementation of controller_t.destroy
127 static void destroy(private_gateway_controller_t
*this)
135 controller_t
*gateway_controller_create(context_t
*context
, void *param
)
137 private_gateway_controller_t
*this = malloc_thing(private_gateway_controller_t
);
139 this->public.controller
.get_name
= (char*(*)(controller_t
*))get_name
;
140 this->public.controller
.get_handler
= (controller_handler_t(*)(controller_t
*,char*))get_handler
;
141 this->public.controller
.destroy
= (void(*)(controller_t
*))destroy
;
143 this->manager
= (manager_t
*)context
;
145 return &this->public.controller
;