aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/Wasabi/ServiceManager.cpp
blob: 2d6887aa12ddc9263bdbc30dbecc4005c2a8964a (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "ServiceManager.h"
#include "api__wasabi-replicant.h"
#include "service/ifc_servicefactory.h"
#include "service/svccb.h"

using namespace nu;

ServiceManager::ServiceManager()
{
#ifdef _WIN32
	component_wait = CreateSemaphoreW(NULL, 0, LONG_MAX, NULL);
#else
	sem_init(&component_wait, 0, 0);
#endif

}

ServiceManager::~ServiceManager()
{
	#ifdef _WIN32
	if (component_wait)
		CloseHandle(component_wait);
#else
	sem_destroy(&component_wait);
#endif
}

int ServiceManager::Dispatchable_QueryInterface(GUID interface_guid, void **object) 
{
	if (interface_guid == ifc_component_sync::GetInterfaceGUID())
	{
		*object = (ifc_component_sync *)this;
	}
	return NErr_Unknown;
}
//-------------------------------------------
int ServiceManager::GetServiceIndex(GUID key)
{
	for(int idx = 0; idx < services_indexer.size(); idx++)
	{
		if (memcmp(&key, &services_indexer[idx], sizeof(GUID)) == 0)
		{
			return idx;
		}
	}
	
	return -1;
}

int ServiceManager::Service_Register(ifc_serviceFactory *svc)
{
	AutoLock lock(serviceGuard LOCKNAME("ServiceManager::service_register"));
	GUID service_type = svc->GetServiceType();
	GUID service_id = svc->GetGUID();

	// add the service to the master list
	ifc_serviceFactory* new_factory = services[service_id];
	if (new_factory) // if someone already has this GUID, we need to replace
	{
		// replace factory in services_by_type map
		ServiceList* type_list = services_by_type[service_type];

		if (type_list)
		{
			for (ServiceList::iterator itr=type_list->begin();itr!=type_list->end();itr++)
			{
				ifc_serviceFactory *f = *itr;
				if (f->GetGUID() == service_id)
				{
					*itr = svc;
				}
			}
		}
		// tell the old factory we're kicking its ass to the curb.
		new_factory->ServiceNotify(ifc_serviceFactory::ONUNREGISTERED);
		// HAKAN:
		// Should we delete old factory?
		// new_factory = svc;
	}
	else // not used yet, just assign
	{
		//new_factory = svc;
		services_indexer.push_back(service_id);

		// add it to the by-type lookup
		ServiceList *&type_list = services_by_type[service_type];
		if (!type_list)
			type_list = new ServiceList;

		type_list->push_back(svc);
	}

	services[service_id]=svc;

	// send notifications
	svc->ServiceNotify(ifc_serviceFactory::ONREGISTERED);

	WASABI2_API_SYSCB->IssueCallback(Service::event_type,
		Service::on_register,
		(intptr_t)&service_type, reinterpret_cast<intptr_t>(svc));
	return NErr_Success;
}

int ServiceManager::Service_Unregister(ifc_serviceFactory *svc)
{
	AutoLock lock(serviceGuard LOCKNAME("ServiceManager::Service_Unregister"));
	GUID service_type = svc->GetServiceType();
	GUID service_id = svc->GetGUID();

	// remove it from the master list
	ServiceMap::iterator itr = services.find(service_id);
	if (itr != services.end())
		services.erase(itr);

	// and from the type lookup map
	ServiceList *type_list = services_by_type[service_type];
	if (type_list)
	{
		//type_list->eraseObject(svc);
		for (auto it = type_list->begin(); it != type_list->end(); it++)
		{
			if (*it == svc)
			{
				it = type_list->erase(it);
				break;
			}
		}
	}
	WASABI2_API_SYSCB->IssueCallback(Service::event_type,	Service::on_deregister, (intptr_t)&service_type, reinterpret_cast<intptr_t>(svc));
	svc->ServiceNotify(ifc_serviceFactory::ONUNREGISTERED);

	return NErr_Success;
}

size_t ServiceManager::Service_GetServiceCount(GUID svc_type)
{
	AutoLock lock(serviceGuard LOCKNAME("ServiceManager::Service_GetServiceCount"));
	ServiceList *type_list = services_by_type[svc_type];
	if (type_list)
		return type_list->size();
	else
		return 0;
}

ifc_serviceFactory *ServiceManager::Service_EnumService(GUID svc_type, size_t n)
{
	AutoLock lock(serviceGuard LOCKNAME("ServiceManager::Service_EnumService"));
	ServiceList *type_list = services_by_type[svc_type];
	if (type_list && (size_t)n < type_list->size())
		return type_list->at(n);
	else
		return 0;
}

ifc_serviceFactory *ServiceManager::Service_EnumService(size_t n)
{
	AutoLock lock(serviceGuard LOCKNAME("ServiceManager::Service_EnumService"));
	if ((size_t)n < services.size())
	{
		//return services.at(n).second;
		if (n < services_indexer.size())
		{
			GUID g = services_indexer[n];
			return services[g];
		}
	}
	
	return 0;
}

ifc_serviceFactory *ServiceManager::Service_GetServiceByGUID(GUID guid)
{
	AutoLock lock(serviceGuard LOCKNAME("ServiceManager::service_getServiceByGuid"));
	ServiceMap::iterator itr = services.find(guid);
	if (itr != services.end())
		return itr->second;
	else
		return 0;
}

void ServiceManager::Service_ComponentDone()
{
#ifdef _WIN32
		ReleaseSemaphore(component_wait, 1, NULL);
#else
		sem_post(&component_wait);
#endif
}

int ServiceManager::ComponentSync_Wait(size_t count)
{
	while (count--)
	{
#ifdef _WIN32
		WaitForSingleObject(component_wait, INFINITE);
#else
		sem_wait(&component_wait);
#endif
	}
	return NErr_Success;
}