core.cpp 3.88 KB
Newer Older
Ronan's avatar
Ronan committed
1 2
/*
 * core.cpp
Ronan's avatar
Ronan committed
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
Ronan's avatar
Ronan committed
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * 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.
Ronan's avatar
Ronan committed
9 10 11 12 13 14 15
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Ronan's avatar
Ronan committed
18 19
 */

20
#include <mediastreamer2/mscommon.h>
21
#include <xercesc/util/PlatformUtils.hpp>
22

23
#include "address/address-p.h"
24
#include "call/call.h"
25 26
#include "core/core-listener.h"
#include "core/core-p.h"
27
#include "logger/logger.h"
28
#include "paths/paths.h"
Ronan's avatar
Ronan committed
29

30
// TODO: Remove me later.
31
#include "c-wrapper/c-wrapper.h"
32
#include "private.h"
33

34 35
#define LINPHONE_DB "linphone.db"

36 37
// =============================================================================

38 39
using namespace std;

Ronan's avatar
Ronan committed
40 41
LINPHONE_BEGIN_NAMESPACE

42 43 44
void CorePrivate::init () {
	L_Q();
	mainDb.reset(new MainDb(q->getSharedFromThis()));
45 46

	AbstractDb::Backend backend;
47
	string uri = L_C_TO_STRING(lp_config_get_string(linphone_core_get_config(L_GET_C_BACK_PTR(q)), "storage", "uri", nullptr));
48
	if (!uri.empty())
49
		backend = strcmp(lp_config_get_string(linphone_core_get_config(L_GET_C_BACK_PTR(q)), "storage", "backend", nullptr), "mysql") == 0
50 51
			? MainDb::Mysql
			: MainDb::Sqlite3;
52 53
	else {
		backend = AbstractDb::Sqlite3;
54
		uri = q->getDataPath() + LINPHONE_DB;
55
	}
56

57
	lInfo() << "Opening linphone database: " << uri;
58
	if (!mainDb->connect(backend, uri))
59 60
		lFatal() << "Unable to open linphone database.";

61
	loadChatRooms();
62
}
Ronan's avatar
Ronan committed
63

64 65 66 67 68 69 70 71
void CorePrivate::registerListener (CoreListener *listener) {
	listeners.push_back(listener);
}

void CorePrivate::unregisterListener (CoreListener *listener) {
	listeners.remove(listener);
}

72 73 74 75 76 77 78
void CorePrivate::uninit () {
	L_Q();
	while (!calls.empty()) {
		calls.front()->terminate();
		linphone_core_iterate(L_GET_C_BACK_PTR(q));
		ms_usleep(10000);
	}
79 80

	AddressPrivate::clearSipAddressesCache();
81 82
}

83 84
// -----------------------------------------------------------------------------

Ghislain MARY's avatar
Ghislain MARY committed
85
void CorePrivate::notifyNetworkReachable (bool sipNetworkReachable, bool mediaNetworkReachable) {
86
	for (const auto &listener : listeners)
Ghislain MARY's avatar
Ghislain MARY committed
87
		listener->onNetworkReachable(sipNetworkReachable, mediaNetworkReachable);
88 89 90 91 92 93 94
}

void CorePrivate::notifyRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const string &message) {
	for (const auto &listener : listeners)
		listener->onRegistrationStateChanged(cfg, state, message);
}

95 96
// =============================================================================

97 98 99
Core::Core () : Object(*new CorePrivate) {
	xercesc::XMLPlatformUtils::Initialize();
}
100 101 102

Core::~Core () {
	lInfo() << "Destroying core: " << this;
103
	xercesc::XMLPlatformUtils::Terminate();
104 105 106 107 108 109
}

shared_ptr<Core> Core::create (LinphoneCore *cCore) {
	// Do not use `make_shared` => Private constructor.
	shared_ptr<Core> core = shared_ptr<Core>(new Core);
	L_SET_CPP_PTR_FROM_C_OBJECT(cCore, core);
Ronan's avatar
Ronan committed
110
	return core;
111
}
Ronan's avatar
Ronan committed
112

113
LinphoneCore *Core::getCCore () const {
114
	return L_GET_C_BACK_PTR(this);
115 116
}

117 118 119 120
// -----------------------------------------------------------------------------
// Paths.
// -----------------------------------------------------------------------------

121
string Core::getDataPath() const {
122
	return Paths::getPath(Paths::Data, static_cast<PlatformHelpers *>(L_GET_C_BACK_PTR(this)->platform_helper));
123 124
}

125
string Core::getConfigPath() const {
126
	return Paths::getPath(Paths::Config, static_cast<PlatformHelpers *>(L_GET_C_BACK_PTR(this)->platform_helper));
127 128
}

Ronan's avatar
Ronan committed
129
LINPHONE_END_NAMESPACE