test_connection.c 3.88 KB
Newer Older
Slava Monich's avatar
Slava Monich committed
1
/*
2
 * Copyright (C) 2013-2017 Jolla Ltd.
3
 * Contact: Slava Monich <slava.monich@jolla.com>
Slava Monich's avatar
Slava Monich committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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 "test_connection.h"

/* Logging */
18 19 20
#define GLOG_MODULE_NAME mms_connection_log
#include <gutil_log.h>
GLOG_MODULE_DEFINE("mms-connection-test");
Slava Monich's avatar
Slava Monich committed
21 22

typedef MMSConnectionClass MMSConnectionTestClass;
Slava Monich's avatar
Slava Monich committed
23 24 25 26 27 28 29
typedef struct mms_connection_test {
    MMSConnection connection;
    char* imsi;
    char* mmsc;
    char* mmsproxy;
    char* netif;
} MMSConnectionTest;
Slava Monich's avatar
Slava Monich committed
30

31
G_DEFINE_TYPE(MMSConnectionTest, mms_connection_test, MMS_TYPE_CONNECTION)
Slava Monich's avatar
Slava Monich committed
32 33 34 35
#define MMS_TYPE_CONNECTION_TEST (mms_connection_test_get_type())
#define MMS_CONNECTION_TEST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
   MMS_TYPE_CONNECTION_TEST, MMSConnectionTest))

36
typedef struct test_connection_state_change {
Slava Monich's avatar
Slava Monich committed
37
    MMSConnection* conn;
38 39 40
    MMS_CONNECTION_STATE state;
} MMSConnectionStateChange;

Slava Monich's avatar
Slava Monich committed
41 42
static
gboolean
43 44
test_connection_test_state_change_cb(
    void* param)
Slava Monich's avatar
Slava Monich committed
45
{
46
    MMSConnectionStateChange* change = param;
Slava Monich's avatar
Slava Monich committed
47 48 49 50 51 52
    MMSConnection* conn = change->conn;
    if (conn->state != MMS_CONNECTION_STATE_CLOSED &&
        conn->state != MMS_CONNECTION_STATE_FAILED &&
        conn->state != change->state) {
        conn->state = change->state;
        mms_connection_signal_state_change(conn);
Slava Monich's avatar
Slava Monich committed
53
    }
Slava Monich's avatar
Slava Monich committed
54
    mms_connection_unref(conn);
55 56
    g_free(change);
    return FALSE;
Slava Monich's avatar
Slava Monich committed
57 58 59 60
}

static
gboolean
61
mms_connection_test_set_state(
Slava Monich's avatar
Slava Monich committed
62
    MMSConnection* conn,
63
    MMS_CONNECTION_STATE state)
Slava Monich's avatar
Slava Monich committed
64
{
Slava Monich's avatar
Slava Monich committed
65
    if (conn->state != MMS_CONNECTION_STATE_CLOSED) {
66 67
        MMSConnectionStateChange* change = g_new0(MMSConnectionStateChange,1);
        change->state = state;
Slava Monich's avatar
Slava Monich committed
68
        change->conn = mms_connection_ref(conn);
69 70 71
        g_idle_add(test_connection_test_state_change_cb, change);
    }
    return TRUE;
Slava Monich's avatar
Slava Monich committed
72 73 74 75 76
}

MMSConnection*
mms_connection_test_new(
    const char* imsi,
77 78
    const char* proxy,
    unsigned short port)
Slava Monich's avatar
Slava Monich committed
79 80
{
    MMSConnectionTest* test = g_object_new(MMS_TYPE_CONNECTION_TEST, NULL);
Slava Monich's avatar
Slava Monich committed
81 82
    MMSConnection* conn = &test->connection;
    conn->imsi = test->imsi = g_strdup(imsi);
Slava Monich's avatar
Slava Monich committed
83
    if (port) {
Slava Monich's avatar
Slava Monich committed
84
        conn->netif = test->netif = g_strdup("lo");
Slava Monich's avatar
Slava Monich committed
85 86
        if (proxy) {
            test->mmsc = g_strdup("http://mmsc");
87
            test->mmsproxy = g_strdup_printf("%s:%hu", proxy, port);
Slava Monich's avatar
Slava Monich committed
88 89 90
        } else {
            test->mmsc = g_strdup_printf("http://127.0.0.1:%hu", port);
        }
Slava Monich's avatar
Slava Monich committed
91 92
        conn->mmsc = test->mmsc;
        conn->mmsproxy = test->mmsproxy;
Slava Monich's avatar
Slava Monich committed
93
    }
Slava Monich's avatar
Slava Monich committed
94 95 96
    conn->type = MMS_CONNECTION_TYPE_AUTO;
    conn->state = MMS_CONNECTION_STATE_OPENING;
    mms_connection_test_set_state(conn, conn->netif ?
97
        MMS_CONNECTION_STATE_OPEN : MMS_CONNECTION_STATE_FAILED);
Slava Monich's avatar
Slava Monich committed
98
    return conn;
Slava Monich's avatar
Slava Monich committed
99 100 101 102 103 104 105 106 107
}

static
void
mms_connection_test_close(
    MMSConnection* test)
{
    mms_connection_test_set_state(test, MMS_CONNECTION_STATE_CLOSED);
}
Slava Monich's avatar
Slava Monich committed
108 109 110 111 112 113 114 115 116 117 118 119
static
void
mms_connection_test_finalize(
    GObject* object)
{
    MMSConnectionTest* test = MMS_CONNECTION_TEST(object);
    g_free(test->imsi);
    g_free(test->mmsc);
    g_free(test->mmsproxy);
    g_free(test->netif);
    G_OBJECT_CLASS(mms_connection_test_parent_class)->finalize(object);
}
Slava Monich's avatar
Slava Monich committed
120 121 122 123 124 125 126 127 128 129

/**
 * Per class initializer
 */
static
void
mms_connection_test_class_init(
    MMSConnectionTestClass* klass)
{
    klass->fn_close = mms_connection_test_close;
Slava Monich's avatar
Slava Monich committed
130
    G_OBJECT_CLASS(klass)->finalize = mms_connection_test_finalize;
Slava Monich's avatar
Slava Monich committed
131 132 133 134 135 136 137 138 139 140
}

/**
 * Per instance initializer
 */
static
void
mms_connection_test_init(
    MMSConnectionTest* conn)
{
141
    GVERBOSE_("%p", conn);
Slava Monich's avatar
Slava Monich committed
142 143 144 145 146 147 148 149 150
}

/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */