1255a01c7Sbeveloper/*
2255a01c7Sbeveloper * Copyright (c) 2002, 2003 Marcus Overhagen <Marcus@Overhagen.de>
3255a01c7Sbeveloper *
4255a01c7Sbeveloper * Permission is hereby granted, free of charge, to any person obtaining
5255a01c7Sbeveloper * a copy of this software and associated documentation files or portions
6255a01c7Sbeveloper * thereof (the "Software"), to deal in the Software without restriction,
7255a01c7Sbeveloper * including without limitation the rights to use, copy, modify, merge,
8255a01c7Sbeveloper * publish, distribute, sublicense, and/or sell copies of the Software,
9255a01c7Sbeveloper * and to permit persons to whom the Software is furnished to do so, subject
10255a01c7Sbeveloper * to the following conditions:
11255a01c7Sbeveloper *
12255a01c7Sbeveloper *  * Redistributions of source code must retain the above copyright notice,
13255a01c7Sbeveloper *    this list of conditions and the following disclaimer.
14255a01c7Sbeveloper *
15255a01c7Sbeveloper *  * Redistributions in binary form must reproduce the above copyright notice
16255a01c7Sbeveloper *    in the  binary, as well as this list of conditions and the following
17255a01c7Sbeveloper *    disclaimer in the documentation and/or other materials provided with
18255a01c7Sbeveloper *    the distribution.
19255a01c7Sbeveloper *
20255a01c7Sbeveloper * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21255a01c7Sbeveloper * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22255a01c7Sbeveloper * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23255a01c7Sbeveloper * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24255a01c7Sbeveloper * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25255a01c7Sbeveloper * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26255a01c7Sbeveloper * THE SOFTWARE.
27255a01c7Sbeveloper *
281299bfb2Sbeveloper */
29255a01c7Sbeveloper
306fcead13SAxel Dörfler
316fcead13SAxel Dörfler#include "AppManager.h"
326fcead13SAxel Dörfler
336fcead13SAxel Dörfler#include <stdio.h>
346fcead13SAxel Dörfler
356396865dSbeveloper#include <Application.h>
3652a38012Sejakowatz#include <Autolock.h>
376fcead13SAxel Dörfler#include <OS.h>
386fcead13SAxel Dörfler#include <Roster.h>
396fcead13SAxel Dörfler
408f3a6845SBarrett#include <MediaDebug.h>
416fcead13SAxel Dörfler#include <MediaMisc.h>
426fcead13SAxel Dörfler
4341ba915dSbeveloper#include "BufferManager.h"
4441ba915dSbeveloper#include "media_server.h"
456fcead13SAxel Dörfler#include "NodeManager.h"
466fcead13SAxel Dörfler#include "NotificationManager.h"
476fcead13SAxel Dörfler
4852a38012Sejakowatz
4952a38012SejakowatzAppManager::AppManager()
506fcead13SAxel Dörfler	:
516fcead13SAxel Dörfler	BLocker("media app manager")
5252a38012Sejakowatz{
5352a38012Sejakowatz}
5452a38012Sejakowatz
5528a06094Sbeveloper
5652a38012SejakowatzAppManager::~AppManager()
5752a38012Sejakowatz{
5852a38012Sejakowatz}
5952a38012Sejakowatz
6028a06094Sbeveloper
6128a06094Sbeveloperbool
6228a06094SbeveloperAppManager::HasTeam(team_id team)
6352a38012Sejakowatz{
646fcead13SAxel Dörfler	BAutolock lock(this);
656fcead13SAxel Dörfler	return fMap.find(team) != fMap.end();
6652a38012Sejakowatz}
6752a38012Sejakowatz
6828a06094Sbeveloper
6928a06094Sbeveloperstatus_t
7011c7bd86SDario CasalinuovoAppManager::RegisterTeam(team_id team, const BMessenger& messenger)
7152a38012Sejakowatz{
726fcead13SAxel Dörfler	BAutolock lock(this);
736fcead13SAxel Dörfler
74fe1f74abSJérôme Duval	TRACE("AppManager::RegisterTeam %" B_PRId32 "\n", team);
752bb039ccSbeveloper	if (HasTeam(team)) {
76fe1f74abSJérôme Duval		ERROR("AppManager::RegisterTeam: team %" B_PRId32 " already"
77fe1f74abSJérôme Duval			" registered\n", team);
786396865dSbeveloper		return B_ERROR;
792bb039ccSbeveloper	}
806fcead13SAxel Dörfler
816fcead13SAxel Dörfler	try {
826fcead13SAxel Dörfler		fMap.insert(std::make_pair(team, messenger));
836fcead13SAxel Dörfler	} catch (std::bad_alloc& exception) {
846fcead13SAxel Dörfler		return B_NO_MEMORY;
856fcead13SAxel Dörfler	}
866fcead13SAxel Dörfler
876fcead13SAxel Dörfler	return B_OK;
886396865dSbeveloper}
8952a38012Sejakowatz
9028a06094Sbeveloper
9128a06094Sbeveloperstatus_t
9228a06094SbeveloperAppManager::UnregisterTeam(team_id team)
936396865dSbeveloper{
94fe1f74abSJérôme Duval	TRACE("AppManager::UnregisterTeam %" B_PRId32 "\n", team);
9528a06094Sbeveloper
966fcead13SAxel Dörfler	Lock();
976fcead13SAxel Dörfler	bool isRemoved = fMap.erase(team) != 0;
986fcead13SAxel Dörfler	Unlock();
99359ac306Sbeveloper
1006fcead13SAxel Dörfler	_CleanupTeam(team);
1016396865dSbeveloper
1026fcead13SAxel Dörfler	return isRemoved ? B_OK : B_ERROR;
1036396865dSbeveloper}
1046396865dSbeveloper
10528a06094Sbeveloper
10628a06094Sbeveloperteam_id
1079c3b4706SAxel DörflerAppManager::AddOnServerTeam()
1086396865dSbeveloper{
10928a06094Sbeveloper	team_id id = be_roster->TeamFor(B_MEDIA_ADDON_SERVER_SIGNATURE);
11028a06094Sbeveloper	if (id < 0) {
11128a06094Sbeveloper		ERROR("media_server: Trouble, media_addon_server is dead!\n");
11228a06094Sbeveloper		return -1;
11328a06094Sbeveloper	}
11428a06094Sbeveloper	return id;
1156396865dSbeveloper}
11652a38012Sejakowatz
11728a06094Sbeveloper
1186fcead13SAxel Dörflerstatus_t
1196fcead13SAxel DörflerAppManager::SendMessage(team_id team, BMessage* message)
1206396865dSbeveloper{
1216fcead13SAxel Dörfler	BAutolock lock(this);
1226fcead13SAxel Dörfler
1236fcead13SAxel Dörfler	AppMap::iterator found = fMap.find(team);
1246fcead13SAxel Dörfler	if (found == fMap.end())
1256fcead13SAxel Dörfler		return B_NAME_NOT_FOUND;
1266fcead13SAxel Dörfler
1276fcead13SAxel Dörfler	return found->second.SendMessage(message);
12852a38012Sejakowatz}
12952a38012Sejakowatz
13028a06094Sbeveloper
13128a06094Sbevelopervoid
1326fcead13SAxel DörflerAppManager::Dump()
13352a38012Sejakowatz{
1346fcead13SAxel Dörfler	BAutolock lock(this);
1356fcead13SAxel Dörfler
1366fcead13SAxel Dörfler	printf("\n");
1376fcead13SAxel Dörfler	printf("AppManager: list of applications follows:\n");
1386fcead13SAxel Dörfler
1396fcead13SAxel Dörfler	app_info info;
1406fcead13SAxel Dörfler	AppMap::iterator iterator = fMap.begin();
1416fcead13SAxel Dörfler	for (; iterator != fMap.end(); iterator++) {
1426fcead13SAxel Dörfler		app_info info;
1436fcead13SAxel Dörfler		be_roster->GetRunningAppInfo(iterator->first, &info);
144fe1f74abSJérôme Duval		printf(" team %" B_PRId32 " \"%s\", messenger %svalid\n",
145fe1f74abSJérôme Duval			iterator->first, info.ref.name,
146fe1f74abSJérôme Duval			iterator->second.IsValid() ? "" : "NOT ");
1476fcead13SAxel Dörfler	}
1486fcead13SAxel Dörfler
1496fcead13SAxel Dörfler	printf("AppManager: list end\n");
15052a38012Sejakowatz}
15152a38012Sejakowatz
1526396865dSbeveloper
15353c3fa51SDario Casalinuovovoid
15411c7bd86SDario CasalinuovoAppManager::NotifyRosters()
15553c3fa51SDario Casalinuovo{
15611c7bd86SDario Casalinuovo	BAutolock lock(this);
15711c7bd86SDario Casalinuovo
15811c7bd86SDario Casalinuovo	AppMap::iterator iterator = fMap.begin();
15911c7bd86SDario Casalinuovo	for (; iterator != fMap.end(); iterator++)
16011c7bd86SDario Casalinuovo		iterator->second.SendMessage(MEDIA_SERVER_ALIVE);
16153c3fa51SDario Casalinuovo}
16253c3fa51SDario Casalinuovo
16353c3fa51SDario Casalinuovo
16428a06094Sbevelopervoid
1656fcead13SAxel DörflerAppManager::_CleanupTeam(team_id team)
16652a38012Sejakowatz{
1676fcead13SAxel Dörfler	ASSERT(!IsLocked());
1686396865dSbeveloper
169fe1f74abSJérôme Duval	TRACE("AppManager: cleaning up team %" B_PRId32 "\n", team);
1706396865dSbeveloper
17141ba915dSbeveloper	gNodeManager->CleanupTeam(team);
17241ba915dSbeveloper	gBufferManager->CleanupTeam(team);
17341ba915dSbeveloper	gNotificationManager->CleanupTeam(team);
17452a38012Sejakowatz}
175