1/* 2 * Copyright (c) 2002, 2003 Marcus Overhagen <Marcus@Overhagen.de> 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files or portions 6 * thereof (the "Software"), to deal in the Software without restriction, 7 * including without limitation the rights to use, copy, modify, merge, 8 * publish, distribute, sublicense, and/or sell copies of the Software, 9 * and to permit persons to whom the Software is furnished to do so, subject 10 * to the following conditions: 11 * 12 * * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * * Redistributions in binary form must reproduce the above copyright notice 16 * in the binary, as well as this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided with 18 * the distribution. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 * THE SOFTWARE. 27 * 28 */ 29 30 31#include "AppManager.h" 32 33#include <stdio.h> 34 35#include <Application.h> 36#include <Autolock.h> 37#include <OS.h> 38#include <Roster.h> 39 40#include <MediaDebug.h> 41#include <MediaMisc.h> 42 43#include "BufferManager.h" 44#include "media_server.h" 45#include "NodeManager.h" 46#include "NotificationManager.h" 47 48 49AppManager::AppManager() 50 : 51 BLocker("media app manager") 52{ 53} 54 55 56AppManager::~AppManager() 57{ 58} 59 60 61bool 62AppManager::HasTeam(team_id team) 63{ 64 BAutolock lock(this); 65 return fMap.find(team) != fMap.end(); 66} 67 68 69status_t 70AppManager::RegisterTeam(team_id team, const BMessenger& messenger) 71{ 72 BAutolock lock(this); 73 74 TRACE("AppManager::RegisterTeam %" B_PRId32 "\n", team); 75 if (HasTeam(team)) { 76 ERROR("AppManager::RegisterTeam: team %" B_PRId32 " already" 77 " registered\n", team); 78 return B_ERROR; 79 } 80 81 try { 82 fMap.insert(std::make_pair(team, messenger)); 83 } catch (std::bad_alloc& exception) { 84 return B_NO_MEMORY; 85 } 86 87 return B_OK; 88} 89 90 91status_t 92AppManager::UnregisterTeam(team_id team) 93{ 94 TRACE("AppManager::UnregisterTeam %" B_PRId32 "\n", team); 95 96 Lock(); 97 bool isRemoved = fMap.erase(team) != 0; 98 Unlock(); 99 100 _CleanupTeam(team); 101 102 return isRemoved ? B_OK : B_ERROR; 103} 104 105 106team_id 107AppManager::AddOnServerTeam() 108{ 109 team_id id = be_roster->TeamFor(B_MEDIA_ADDON_SERVER_SIGNATURE); 110 if (id < 0) { 111 ERROR("media_server: Trouble, media_addon_server is dead!\n"); 112 return -1; 113 } 114 return id; 115} 116 117 118status_t 119AppManager::SendMessage(team_id team, BMessage* message) 120{ 121 BAutolock lock(this); 122 123 AppMap::iterator found = fMap.find(team); 124 if (found == fMap.end()) 125 return B_NAME_NOT_FOUND; 126 127 return found->second.SendMessage(message); 128} 129 130 131void 132AppManager::Dump() 133{ 134 BAutolock lock(this); 135 136 printf("\n"); 137 printf("AppManager: list of applications follows:\n"); 138 139 app_info info; 140 AppMap::iterator iterator = fMap.begin(); 141 for (; iterator != fMap.end(); iterator++) { 142 app_info info; 143 be_roster->GetRunningAppInfo(iterator->first, &info); 144 printf(" team %" B_PRId32 " \"%s\", messenger %svalid\n", 145 iterator->first, info.ref.name, 146 iterator->second.IsValid() ? "" : "NOT "); 147 } 148 149 printf("AppManager: list end\n"); 150} 151 152 153void 154AppManager::NotifyRosters() 155{ 156 BAutolock lock(this); 157 158 AppMap::iterator iterator = fMap.begin(); 159 for (; iterator != fMap.end(); iterator++) 160 iterator->second.SendMessage(MEDIA_SERVER_ALIVE); 161} 162 163 164void 165AppManager::_CleanupTeam(team_id team) 166{ 167 ASSERT(!IsLocked()); 168 169 TRACE("AppManager: cleaning up team %" B_PRId32 "\n", team); 170 171 gNodeManager->CleanupTeam(team); 172 gBufferManager->CleanupTeam(team); 173 gNotificationManager->CleanupTeam(team); 174} 175