1 /* 2 * Copyright 2002, Marcus Overhagen. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #include <OS.h> 6 #include <Messenger.h> 7 #include <Autolock.h> 8 #include <stdio.h> 9 #include "AppManager.h" 10 11 AppManager::AppManager() 12 { 13 } 14 15 AppManager::~AppManager() 16 { 17 } 18 19 bool AppManager::HasTeam(team_id team) 20 { 21 BAutolock lock(mLocker); 22 ListItem *item; 23 for (int32 i = 0; (item = (ListItem *)mList.ItemAt(i)) != NULL; i++) 24 if (item->team == team) 25 return true; 26 return false; 27 } 28 29 status_t AppManager::RegisterTeam(team_id team, BMessenger messenger) 30 { 31 printf("AppManager::RegisterTeam %d\n",(int) team); 32 33 BAutolock lock(mLocker); 34 ListItem *item; 35 36 if (HasTeam(team)) 37 return B_ERROR; 38 39 item = new ListItem; 40 item->team = team; 41 item->messenger = messenger; 42 43 return mList.AddItem(item) ? B_OK : B_ERROR; 44 } 45 46 status_t AppManager::UnregisterTeam(team_id team) 47 { 48 printf("AppManager::UnregisterTeam %d\n",(int) team); 49 50 BAutolock lock(mLocker); 51 ListItem *item; 52 for (int32 i = 0; (item = (ListItem *)mList.ItemAt(i)) != NULL; i++) 53 if (item->team == team) { 54 if (mList.RemoveItem(item)) { 55 delete item; 56 return B_OK; 57 } else { 58 break; 59 } 60 } 61 return B_ERROR; 62 } 63 64 void AppManager::BroadcastMessage(BMessage *msg, bigtime_t timeout) 65 { 66 BAutolock lock(mLocker); 67 ListItem *item; 68 for (int32 i = 0; (item = (ListItem *)mList.ItemAt(i)) != NULL; i++) 69 if (B_OK != item->messenger.SendMessage(msg,(BHandler *)NULL,timeout)) 70 HandleBroadcastError(msg, item->messenger, item->team, timeout); 71 } 72 73 void AppManager::HandleBroadcastError(BMessage *msg, BMessenger &, team_id team, bigtime_t timeout) 74 { 75 BAutolock lock(mLocker); 76 printf("error broadcasting team %d with message after %.3f seconds\n",int(team),timeout / 1000000.0); 77 msg->PrintToStream(); 78 } 79 80 status_t AppManager::LoadState() 81 { 82 return B_ERROR; 83 } 84 85 status_t AppManager::SaveState() 86 { 87 return B_ERROR; 88 } 89