summaryrefslogtreecommitdiff
path: root/src/service_manager.cpp
blob: aa2de8b07baa1d1a9affb05c657b57780235aa96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Anope IRC Services
 *
 * Copyright (C) 2016 Adam <Adam@anope.org>
 * Copyright (C) 2016 Anope Team <team@anope.org>
 *
 * This file is part of Anope. Anope 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, version 2.
 *
 * 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
 * along with this program; if not, see see <http://www.gnu.org/licenses/>.
 */

#include "services.h"
#include "service.h"
#include "logger.h"
#include "modules.h"

ServiceManager *ServiceManager::manager = nullptr;

void ServiceManager::Init()
{
	assert(manager == nullptr);
	manager = new ServiceManager();
}

ServiceManager *ServiceManager::Get()
{
	return manager;
}

void ServiceManager::Destroy()
{
	delete manager;
	manager = nullptr;
}

Service *ServiceManager::FindService(const Anope::string &name)
{
	for (Service *s : services)
		if (s->GetName() == name)
			return s;
	return nullptr;
}

Service *ServiceManager::FindService(const Anope::string &type, const Anope::string &name)
{
	for (Service *s : services)
		if (s->GetType() == type && s->GetName() == name)
			return s;
	return nullptr;
}

std::vector<Service *> ServiceManager::FindServices(const Anope::string &type)
{
	std::vector<Service *> v;

	for (Service *s : services)
		if (s->GetType() == type)
			v.push_back(s);

	return v;
}

void ServiceManager::Register(Service *service)
{
	// assert type
	if (service->GetType().empty())
		throw ModuleException("Service type must be non empty");

	if (service->GetName().empty() == false)
	{
		Service *s = FindService(service->GetType(), service->GetName());

		if (s != nullptr)
			throw ModuleException("Service of type " + service->GetType() + " with name " + service->GetName() + " already exists from " + service->GetOwner()->name);
	}

	Anope::Logger.Debug3("Service registered: {0} {1} address {2} by {3}", service->GetType(), service->GetName(), static_cast<void *>(this), service->GetOwner());

	services.push_back(service);

	LookupAll();
}

void ServiceManager::Unregister(Service *service)
{
	Anope::Logger.Debug3("Service unregistered: {0} {1} address {2} by {3}", service->GetType(), service->GetName(), static_cast<void *>(service), service->GetOwner());

	auto it = std::find(services.begin(), services.end(), service);
	if (it != services.end())
	{
		services.erase(it);

		LookupAll();
	}
}

void ServiceManager::Lookup(ServiceReferenceBase *reference)
{
	if (reference->GetName().empty())
	{
		std::vector<Service *> services = this->FindServices(reference->GetType());
		Anope::Logger.Debug3("Service lookup {0} type {1} name {2}: {3} services", static_cast<void *>(reference), reference->GetType(), reference->GetName(), services.size());
		reference->SetServices(services);
	}
	else
	{
		Service *service = this->FindService(reference->GetType(), reference->GetName());
		Anope::Logger.Debug3("Service lookup {0} type {1} name {2}: {3}", static_cast<void *>(reference), reference->GetType(), reference->GetName(), service);
		reference->SetService(service);
	}
}

void ServiceManager::LookupAll()
{
	for (ServiceReferenceBase *s : references)
		Lookup(s);
}

void ServiceManager::RegisterReference(ServiceReferenceBase *reference)
{
	Lookup(reference);
	this->references.push_back(reference);
}

void ServiceManager::UnregisterReference(ServiceReferenceBase *reference)
{
	auto it = std::find(this->references.begin(), this->references.end(), reference);
	if (it != this->references.end())
		this->references.erase(it);
}