Merge branch 'stroke-timeout'
[strongswan.git] / src / manager / manager.c
index dc27f86..207800b 100644 (file)
@@ -1,10 +1,3 @@
-/**
- * @file manager.c
- *
- * @brief Implementation of manager_t.
- *
- */
-
 /*
  * Copyright (C) 2007 Martin Willi
  * Hochschule fuer Technik Rapperswil
@@ -24,7 +17,7 @@
 
 #include "gateway.h"
 
-#include <utils/linked_list.h>
+#include <collections/linked_list.h>
 
 typedef struct private_manager_t private_manager_t;
 
@@ -37,35 +30,31 @@ struct private_manager_t {
         * public functions
         */
        manager_t public;
-       
+
        /**
-        * underlying database
+        * underlying storage database
         */
-       database_t *db;
-       
+       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)
+};
+
+METHOD(manager_t, create_gateway_enumerator, enumerator_t*,
+       private_manager_t *this)
 {
-       return this->db->create_gateway_enumerator(this->db, this->user);
+       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)
+METHOD(manager_t, select_gateway, gateway_t*,
+       private_manager_t *this, int select_id)
 {
        if (select_id != 0)
        {
@@ -73,21 +62,25 @@ static gateway_t* select_gateway(private_manager_t *this, int select_id)
                int id, port;
                char *name, *address;
                host_t *host;
-               
+
                if (this->gateway) this->gateway->destroy(this->gateway);
                this->gateway = NULL;
-               
-               enumerator = this->db->create_gateway_enumerator(this->db, this->user);
+
+               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)
+                               if (port == 0)
+                               {
+                                       this->gateway = gateway_create_unix(name);
+                               }
+                               else
                                {
                                        host = host_create_from_string(address, port);
                                        if (host)
                                        {
-                                               this->gateway = gateway_create(name, host);
+                                               this->gateway = gateway_create_tcp(name, host);
                                        }
                                }
                                break;
@@ -98,40 +91,35 @@ static gateway_t* select_gateway(private_manager_t *this, int select_id)
        return this->gateway;
 }
 
-/**
- * Implementation of manager_t.logged_in.
- */
-static bool logged_in(private_manager_t *this)
+METHOD(manager_t, logged_in, bool,
+       private_manager_t *this)
 {
        return this->user != 0;
 }
 
-/**
- * Implementation of manager_t.login.
- */
-static bool login(private_manager_t *this, char *username, char *password)
+METHOD(manager_t, login, bool,
+       private_manager_t *this, char *username, char *password)
 {
        if (!this->user)
        {
-               this->user = this->db->login(this->db, username, password);
+               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)
+METHOD(manager_t, logout, void,
+       private_manager_t *this)
 {
+       if (this->gateway)
+       {
+               this->gateway->destroy(this->gateway);
+               this->gateway = NULL;
+       }
        this->user = 0;
-       this->gateway->destroy(this->gateway);
-       this->gateway = NULL;
 }
 
-/**
- * Implementation of manager_t.destroy
- */
-static void destroy(private_manager_t *this)
+METHOD(context_t, destroy, void,
+       private_manager_t *this)
 {
        if (this->gateway) this->gateway->destroy(this->gateway);
        free(this);
@@ -140,21 +128,24 @@ static void destroy(private_manager_t *this)
 /*
  * see header file
  */
-manager_t *manager_create(database_t *database)
+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->db = database;
-       this->gateway = NULL;
-       
+       private_manager_t *this;
+
+       INIT(this,
+               .public = {
+                       .login = _login,
+                       .logged_in = _logged_in,
+                       .logout = _logout,
+                       .create_gateway_enumerator = _create_gateway_enumerator,
+                       .select_gateway = _select_gateway,
+                       .context = {
+                               .destroy = _destroy,
+                       },
+               },
+               .store = storage,
+       );
+
        return &this->public;
 }