gmyth/src/gmyth_backendinfo.c
author renatofilho
Tue Jul 03 21:26:55 2007 +0100 (2007-07-03)
branchtrunk
changeset 770 800e6485ad15
parent 769 1603bc7804da
child 771 b4480dd29fc8
permissions -rw-r--r--
[svn r776] create function in backend info for otimize socket usage
melunko@117
     1
/**
melunko@117
     2
 * GMyth Library
melunko@117
     3
 *
melunko@117
     4
 * @file gmyth/gmyth_backend_info.c
melunko@117
     5
 * 
rosfran@420
     6
 * @brief <p> This component represents all the MythTV backend server
rosfran@420
     7
 * 						configuration information.
rosfran@420
     8
 *
melunko@117
     9
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
melunko@117
    10
 * @author Hallyson Melo <hallyson.melo@indt.org.br>
rosfran@420
    11
 * @author Rosfran Borges <rosfran.borges@indt.org.br>
melunko@117
    12
 *
rosfran@701
    13
 * 
rosfran@701
    14
 * This program is free software; you can redistribute it and/or modify
rosfran@701
    15
 * it under the terms of the GNU Lesser General Public License as published by
rosfran@701
    16
 * the Free Software Foundation; either version 2 of the License, or
rosfran@701
    17
 * (at your option) any later version.
rosfran@701
    18
 *
rosfran@701
    19
 * This program is distributed in the hope that it will be useful,
rosfran@701
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rosfran@701
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rosfran@701
    22
 * GNU General Public License for more details.
rosfran@701
    23
 *
rosfran@701
    24
 * You should have received a copy of the GNU Lesser General Public License
rosfran@701
    25
 * along with this program; if not, write to the Free Software
rosfran@701
    26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rosfran@701
    27
 */
rosfran@698
    28
leo_sobral@213
    29
#ifdef HAVE_CONFIG_H
leo_sobral@213
    30
#include "config.h"
leo_sobral@213
    31
#endif
melunko@117
    32
melunko@117
    33
#include "gmyth_backendinfo.h"
rosfran@119
    34
#include "gmyth_uri.h"
renatofilho@131
    35
#include "gmyth_debug.h"
melunko@117
    36
renatofilho@754
    37
static void     gmyth_backend_info_class_init(GMythBackendInfoClass *
renatofilho@754
    38
                                              klass);
renatofilho@754
    39
static void     gmyth_backend_info_init(GMythBackendInfo * object);
melunko@117
    40
renatofilho@754
    41
static void     gmyth_backend_info_dispose(GObject * object);
renatofilho@754
    42
static void     gmyth_backend_info_finalize(GObject * object);
melunko@117
    43
renatofilho@750
    44
G_DEFINE_TYPE(GMythBackendInfo, gmyth_backend_info, G_TYPE_OBJECT)
renatofilho@754
    45
    static void     gmyth_backend_info_class_init(GMythBackendInfoClass *
renatofilho@754
    46
                                                  klass)
melunko@117
    47
{
renatofilho@754
    48
    GObjectClass   *gobject_class;
melunko@117
    49
renatofilho@754
    50
    gobject_class = (GObjectClass *) klass;
melunko@117
    51
renatofilho@754
    52
    gobject_class->dispose = gmyth_backend_info_dispose;
renatofilho@754
    53
    gobject_class->finalize = gmyth_backend_info_finalize;
melunko@117
    54
}
melunko@117
    55
melunko@117
    56
static void
renatofilho@750
    57
gmyth_backend_info_init(GMythBackendInfo * backend_info)
melunko@117
    58
{
renatofilho@754
    59
    backend_info->hostname = NULL;
renatofilho@754
    60
    backend_info->username = NULL;
renatofilho@754
    61
    backend_info->password = NULL;
renatofilho@754
    62
    backend_info->db_name = NULL;
renatofilho@754
    63
    backend_info->port = -1;
renatofilho@754
    64
    backend_info->status_port = -1;
melunko@117
    65
}
melunko@117
    66
melunko@117
    67
static void
renatofilho@750
    68
gmyth_backend_info_dispose(GObject * object)
melunko@117
    69
{
renatofilho@754
    70
    GMythBackendInfo *backend_info = GMYTH_BACKEND_INFO(object);
melunko@117
    71
renatofilho@754
    72
    g_free(backend_info->hostname);
renatofilho@754
    73
    g_free(backend_info->username);
renatofilho@754
    74
    g_free(backend_info->password);
renatofilho@754
    75
    g_free(backend_info->db_name);
renatofilho@769
    76
    g_object_unref (backend_info->sock);
melunko@117
    77
renatofilho@754
    78
    backend_info->hostname = NULL;
renatofilho@754
    79
    backend_info->username = NULL;
renatofilho@754
    80
    backend_info->password = NULL;
renatofilho@754
    81
    backend_info->db_name = NULL;
renatofilho@754
    82
    backend_info->port = -1;
renatofilho@754
    83
    backend_info->status_port = -1;
melunko@117
    84
renatofilho@754
    85
    G_OBJECT_CLASS(gmyth_backend_info_parent_class)->dispose(object);
melunko@117
    86
}
melunko@117
    87
melunko@117
    88
static void
renatofilho@750
    89
gmyth_backend_info_finalize(GObject * object)
melunko@117
    90
{
renatofilho@754
    91
    g_signal_handlers_destroy(object);
melunko@117
    92
renatofilho@754
    93
    G_OBJECT_CLASS(gmyth_backend_info_parent_class)->finalize(object);
melunko@117
    94
}
melunko@117
    95
rosfran@421
    96
/** 
rosfran@421
    97
 * Creates a new instance of GMythBackendInfo.
melunko@117
    98
 * 
melunko@117
    99
 * @return a new instance of GMythBackendInfo.
melunko@117
   100
 */
rosfran@698
   101
GMythBackendInfo *
renatofilho@750
   102
gmyth_backend_info_new()
melunko@117
   103
{
renatofilho@754
   104
    GMythBackendInfo *backend_info =
renatofilho@754
   105
        GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
rosfran@698
   106
renatofilho@754
   107
    return backend_info;
melunko@117
   108
}
melunko@117
   109
rosfran@421
   110
/** 
rosfran@421
   111
 * Creates a new instance of GMythBackendInfo, based on a given set of 
rosfran@421
   112
 * configuration parameters.
rosfran@421
   113
 * 
rosfran@421
   114
 * @param hostname The hostname to the MythTV backend server.
rosfran@421
   115
 * @param username The user name to the MythTV backend MySQL server.
rosfran@421
   116
 * @param password The password to the user of the MythTV backend MySQL server.
rosfran@421
   117
 * @param db_name The database name of the MythTV backend, stored on the MySQL server.
rosfran@421
   118
 * @param port The port number of the MythTV backend server (commonly is 6543).
rosfran@421
   119
 * 
rosfran@421
   120
 * @return a new instance of GMythBackendInfo.
rosfran@421
   121
 */
rosfran@698
   122
GMythBackendInfo *
renatofilho@750
   123
gmyth_backend_info_new_full(const gchar * hostname, const gchar * username,
renatofilho@754
   124
                            const gchar * password, const gchar * db_name,
renatofilho@754
   125
                            gint port)
melunko@117
   126
{
renatofilho@754
   127
    GMythBackendInfo *backend_info =
renatofilho@754
   128
        GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
rosfran@242
   129
renatofilho@754
   130
    gmyth_backend_info_set_hostname(backend_info, hostname);
renatofilho@754
   131
    gmyth_backend_info_set_username(backend_info, username);
renatofilho@754
   132
    gmyth_backend_info_set_password(backend_info, password);
renatofilho@754
   133
    gmyth_backend_info_set_db_name(backend_info, db_name);
renatofilho@754
   134
    gmyth_backend_info_set_port(backend_info, port);
melunko@117
   135
renatofilho@754
   136
    return backend_info;
melunko@117
   137
}
melunko@117
   138
rosfran@421
   139
/** 
rosfran@421
   140
 * Creates a new instance of GMythBackendInfo, based on the 
rosfran@421
   141
 * MythTV's backend server URI string.
rosfran@421
   142
 * 
rosfran@421
   143
 * @param uri_str The URI string pointing to the MythTV backend server.
rosfran@421
   144
 * 
rosfran@421
   145
 * @return a new instance of GMythBackendInfo.
rosfran@421
   146
 */
rosfran@698
   147
GMythBackendInfo *
renatofilho@750
   148
gmyth_backend_info_new_with_uri(const gchar * uri_str)
rosfran@119
   149
{
renatofilho@754
   150
    GMythBackendInfo *backend_info =
renatofilho@754
   151
        GMYTH_BACKEND_INFO(g_object_new(GMYTH_BACKEND_INFO_TYPE, NULL));
rosfran@698
   152
renatofilho@754
   153
    GMythURI       *uri = gmyth_uri_new_with_value(uri_str);
rosfran@698
   154
renatofilho@754
   155
    gchar         **path_parts =
renatofilho@754
   156
        g_strsplit(gmyth_uri_get_path(uri), "&", -1);
rosfran@698
   157
renatofilho@754
   158
    gmyth_backend_info_set_hostname(backend_info, gmyth_uri_get_host(uri));
renatofilho@754
   159
    gmyth_backend_info_set_username(backend_info, gmyth_uri_get_user(uri));
renatofilho@754
   160
    gmyth_backend_info_set_password(backend_info,
renatofilho@754
   161
                                    gmyth_uri_get_password(uri));
melunko@412
   162
renatofilho@754
   163
    /*
renatofilho@754
   164
     * gets the path info to database name, from the URI, and removes the
renatofilho@754
   165
     * trash chars 
renatofilho@754
   166
     */
renatofilho@754
   167
    gmyth_backend_info_set_db_name(backend_info, path_parts != NULL &&
renatofilho@754
   168
                                   strlen(path_parts[0]) > 0 ?
renatofilho@754
   169
                                   g_strstrip(g_strdelimit
renatofilho@754
   170
                                              (path_parts[0], "/?",
renatofilho@754
   171
                                               ' ')) :
renatofilho@754
   172
                                   gmyth_uri_get_path(uri));
leo_sobral@446
   173
renatofilho@754
   174
    gmyth_backend_info_set_port(backend_info, gmyth_uri_get_port(uri));
rosfran@698
   175
renatofilho@754
   176
    g_object_unref(uri);
renatofilho@754
   177
    g_strfreev(path_parts);
rosfran@698
   178
renatofilho@754
   179
    return backend_info;
rosfran@119
   180
}
rosfran@119
   181
melunko@117
   182
void
renatofilho@750
   183
gmyth_backend_info_set_hostname(GMythBackendInfo * backend_info,
renatofilho@754
   184
                                const gchar * hostname)
melunko@117
   185
{
renatofilho@754
   186
    g_return_if_fail(backend_info != NULL);
rosfran@698
   187
renatofilho@754
   188
    if (NULL == hostname || strlen(hostname) <= 0) {
renatofilho@754
   189
        gmyth_debug("Error trying to set a hostname equals to NULL.");
renatofilho@754
   190
    } else {
renatofilho@754
   191
        backend_info->hostname = g_strdup(hostname);
renatofilho@754
   192
    }
melunko@117
   193
}
melunko@117
   194
melunko@117
   195
void
renatofilho@750
   196
gmyth_backend_info_set_username(GMythBackendInfo * backend_info,
renatofilho@754
   197
                                const gchar * username)
melunko@117
   198
{
renatofilho@754
   199
    g_return_if_fail(backend_info != NULL);
melunko@117
   200
renatofilho@754
   201
    backend_info->username = g_strdup(username);
melunko@117
   202
}
melunko@117
   203
melunko@117
   204
void
renatofilho@750
   205
gmyth_backend_info_set_password(GMythBackendInfo * backend_info,
renatofilho@754
   206
                                const gchar * password)
melunko@117
   207
{
renatofilho@754
   208
    g_return_if_fail(backend_info != NULL);
melunko@117
   209
renatofilho@754
   210
    backend_info->password = g_strdup(password);
melunko@117
   211
}
melunko@117
   212
melunko@117
   213
void
renatofilho@750
   214
gmyth_backend_info_set_db_name(GMythBackendInfo * backend_info,
renatofilho@754
   215
                               const gchar * db_name)
melunko@117
   216
{
renatofilho@754
   217
    g_return_if_fail(backend_info != NULL);
melunko@117
   218
renatofilho@754
   219
    backend_info->db_name = g_strdup(db_name);
melunko@117
   220
}
melunko@117
   221
rosfran@119
   222
void
renatofilho@750
   223
gmyth_backend_info_set_port(GMythBackendInfo * backend_info, gint port)
rosfran@119
   224
{
renatofilho@754
   225
    g_return_if_fail(backend_info != NULL);
rosfran@119
   226
renatofilho@754
   227
    if (port <= 0) {
renatofilho@754
   228
        gmyth_debug("Error trying to set a port less than 0.");
renatofilho@754
   229
    } else {
renatofilho@754
   230
        backend_info->port = port;
renatofilho@754
   231
    }
rosfran@119
   232
}
rosfran@119
   233
morphbr@352
   234
void
renatofilho@754
   235
gmyth_backend_info_set_status_port(GMythBackendInfo * backend_info,
renatofilho@754
   236
                                   gint port)
morphbr@352
   237
{
renatofilho@754
   238
    g_return_if_fail(backend_info != NULL);
morphbr@352
   239
renatofilho@754
   240
    if (port <= 0) {
renatofilho@754
   241
        gmyth_debug
renatofilho@754
   242
            ("Error trying to set the status port to less than zero.");
renatofilho@754
   243
    } else {
renatofilho@754
   244
        backend_info->status_port = port;
renatofilho@754
   245
    }
morphbr@352
   246
}
morphbr@352
   247
renatofilho@754
   248
const gchar    *
renatofilho@750
   249
gmyth_backend_info_get_hostname(GMythBackendInfo * backend_info)
melunko@117
   250
{
renatofilho@754
   251
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   252
renatofilho@754
   253
    return backend_info->hostname;
melunko@117
   254
}
melunko@117
   255
renatofilho@754
   256
const gchar    *
renatofilho@750
   257
gmyth_backend_info_get_username(GMythBackendInfo * backend_info)
melunko@117
   258
{
renatofilho@754
   259
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   260
renatofilho@754
   261
    return backend_info->username;
melunko@117
   262
}
melunko@117
   263
renatofilho@754
   264
const gchar    *
renatofilho@750
   265
gmyth_backend_info_get_password(GMythBackendInfo * backend_info)
melunko@117
   266
{
renatofilho@754
   267
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   268
renatofilho@754
   269
    return backend_info->password;
melunko@117
   270
}
melunko@117
   271
renatofilho@754
   272
const gchar    *
renatofilho@750
   273
gmyth_backend_info_get_db_name(GMythBackendInfo * backend_info)
melunko@117
   274
{
renatofilho@754
   275
    g_return_val_if_fail(backend_info != NULL, NULL);
melunko@117
   276
renatofilho@754
   277
    return backend_info->db_name;
melunko@117
   278
}
melunko@117
   279
melunko@117
   280
gint
renatofilho@750
   281
gmyth_backend_info_get_port(GMythBackendInfo * backend_info)
melunko@117
   282
{
renatofilho@754
   283
    g_return_val_if_fail(backend_info != NULL, -1);
melunko@117
   284
renatofilho@754
   285
    return backend_info->port;
melunko@117
   286
}
rosfran@178
   287
rosfran@421
   288
/** 
rosfran@421
   289
 * Creates a new instance of GMythURI, based on the GMythBackendInfo instance to the 
rosfran@421
   290
 * MythTV's backend server.
rosfran@421
   291
 * 
rosfran@421
   292
 * @param backend_info The GMythBackendInfo instance.
rosfran@421
   293
 * 
rosfran@421
   294
 * @return an instance of GMythURI, created from a GMythBackendInfo.
rosfran@421
   295
 */
renatofilho@754
   296
GMythURI       *
renatofilho@750
   297
gmyth_backend_info_get_uri(GMythBackendInfo * backend_info)
rosfran@307
   298
{
renatofilho@754
   299
    GMythURI       *uri = NULL;
renatofilho@754
   300
    gchar          *uri_str = NULL;
renatofilho@754
   301
    gchar          *user_info = NULL;
renatofilho@754
   302
    gchar          *db_data = NULL;
rosfran@529
   303
renatofilho@754
   304
    if ((backend_info->username != NULL
renatofilho@754
   305
         && strlen(backend_info->username) > 0))
renatofilho@754
   306
        user_info =
renatofilho@754
   307
            g_strdup_printf("%s:%s@", backend_info->username,
renatofilho@754
   308
                            backend_info->password);
rosfran@698
   309
renatofilho@754
   310
    if (backend_info->db_name != NULL && strlen(backend_info->db_name) > 0) {
renatofilho@754
   311
        if ((g_strrstr(backend_info->db_name, "_") != NULL))
renatofilho@754
   312
            db_data = g_strdup(backend_info->db_name);
renatofilho@754
   313
        else
renatofilho@754
   314
            db_data = g_strdup_printf("?%s&", backend_info->db_name);
renatofilho@754
   315
    }
renatofilho@754
   316
    // else if ( ( ( g_strrstr( backend_info->path, "livetv" ) != NULL )
renatofilho@754
   317
    // || 
renatofilho@754
   318
    // ( g_strrstr( backend_info->path, "/?" ) != NULL ) )
rosfran@698
   319
renatofilho@754
   320
    uri_str = g_strdup_printf("myth://%s%s:%d/%s", user_info != NULL
renatofilho@754
   321
                              && strlen(user_info) > 0 ? user_info : "",
renatofilho@754
   322
                              backend_info->hostname, backend_info->port,
renatofilho@754
   323
                              db_data != NULL
renatofilho@754
   324
                              && strlen(db_data) > 0 ? db_data : "");
renatofilho@754
   325
    uri = gmyth_uri_new_with_value(uri_str);
melunko@412
   326
renatofilho@754
   327
    if (user_info != NULL)
renatofilho@754
   328
        g_free(user_info);
rosfran@529
   329
renatofilho@754
   330
    if (db_data != NULL)
renatofilho@754
   331
        g_free(db_data);
rosfran@698
   332
renatofilho@754
   333
    g_free(uri_str);
rosfran@698
   334
renatofilho@754
   335
    return uri;
rosfran@307
   336
}
rosfran@307
   337
rosfran@529
   338
gboolean
renatofilho@750
   339
gmyth_backend_info_is_local_file(GMythBackendInfo * backend_info)
rosfran@529
   340
{
renatofilho@754
   341
    g_return_val_if_fail(backend_info != NULL, FALSE);
rosfran@698
   342
renatofilho@754
   343
    return
renatofilho@754
   344
        gmyth_uri_is_local_file(gmyth_backend_info_get_uri(backend_info));
rosfran@529
   345
}
renatofilho@769
   346
renatofilho@769
   347
GMythSocket*
renatofilho@769
   348
gmyth_backend_info_get_connected_socket (GMythBackendInfo *backend_info)
renatofilho@769
   349
{
renatofilho@770
   350
    if (backend_info->sock == NULL) {
renatofilho@769
   351
        gboolean res;
renatofilho@769
   352
        backend_info->sock = gmyth_socket_new ();
renatofilho@769
   353
        res =  gmyth_socket_connect_to_backend (socket,
renatofilho@769
   354
                                                backend_info->hostname,
renatofilho@769
   355
                                                backend_info->port,
renatofilho@769
   356
                                                TRUE);
renatofilho@769
   357
        if (res == FALSE) {
renatofilho@769
   358
            g_object_unref (backend_info->sock);
renatofilho@769
   359
            backend_info->sock = NULL;
renatofilho@769
   360
        }
renatofilho@769
   361
    }
renatofilho@769
   362
renatofilho@769
   363
    return g_object_ref (backend_info->sock);
renatofilho@769
   364
}