1 /*
2 * Copyright 2016, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5
6 #include "TargetHost.h"
7
8 #include <AutoLocker.h>
9
10 #include "TeamInfo.h"
11
12
TargetHost(const BString & name)13 TargetHost::TargetHost(const BString& name)
14 :
15 BReferenceable(),
16 fName(name),
17 fLock(),
18 fListeners(),
19 fTeams()
20 {
21 }
22
23
~TargetHost()24 TargetHost::~TargetHost()
25 {
26 while (!fTeams.IsEmpty())
27 delete fTeams.RemoveItemAt((int32)0);
28 }
29
30
31 void
AddListener(Listener * listener)32 TargetHost::AddListener(Listener* listener)
33 {
34 AutoLocker<TargetHost> hostLocker(this);
35 fListeners.Add(listener);
36 }
37
38
39 void
RemoveListener(Listener * listener)40 TargetHost::RemoveListener(Listener* listener)
41 {
42 AutoLocker<TargetHost> hostLocker(this);
43 fListeners.Remove(listener);
44 }
45
46
47 int32
CountTeams() const48 TargetHost::CountTeams() const
49 {
50 return fTeams.CountItems();
51 }
52
53
54 status_t
AddTeam(const team_info & info)55 TargetHost::AddTeam(const team_info& info)
56 {
57 TeamInfo* teamInfo = new (std::nothrow) TeamInfo(info.team, info);
58 if (teamInfo == NULL)
59 return B_NO_MEMORY;
60
61 if (!fTeams.BinaryInsert(teamInfo, &_CompareTeams))
62 return B_NO_MEMORY;
63
64 _NotifyTeamAdded(teamInfo);
65 return B_OK;
66 }
67
68
69 void
RemoveTeam(team_id team)70 TargetHost::RemoveTeam(team_id team)
71 {
72 int32 index = fTeams.BinarySearchIndexByKey(team,
73 &_FindTeamByKey);
74 if (index < 0)
75 return;
76
77 _NotifyTeamRemoved(team);
78 TeamInfo* info = fTeams.RemoveItemAt(index);
79 delete info;
80 }
81
82
83 void
UpdateTeam(const team_info & info)84 TargetHost::UpdateTeam(const team_info& info)
85 {
86 int32 index = fTeams.BinarySearchIndexByKey(info.team,
87 &_FindTeamByKey);
88 if (index < 0)
89 return;
90
91 TeamInfo* teamInfo = fTeams.ItemAt(index);
92 teamInfo->SetTo(info.team, info);
93 _NotifyTeamRenamed(teamInfo);
94 }
95
96
97 TeamInfo*
TeamInfoAt(int32 index) const98 TargetHost::TeamInfoAt(int32 index) const
99 {
100 return fTeams.ItemAt(index);
101 }
102
103
104 TeamInfo*
TeamInfoByID(team_id team) const105 TargetHost::TeamInfoByID(team_id team) const
106 {
107 return fTeams.BinarySearchByKey(team, &_FindTeamByKey);
108 }
109
110
111 /*static*/ int
_CompareTeams(const TeamInfo * a,const TeamInfo * b)112 TargetHost::_CompareTeams(const TeamInfo* a, const TeamInfo* b)
113 {
114 return a->TeamID() < b->TeamID() ? -1 : 1;
115 }
116
117
118 /*static*/ int
_FindTeamByKey(const team_id * id,const TeamInfo * info)119 TargetHost::_FindTeamByKey(const team_id* id, const TeamInfo* info)
120 {
121 if (*id < info->TeamID())
122 return -1;
123 else if (*id > info->TeamID())
124 return 1;
125 return 0;
126 }
127
128
129 void
_NotifyTeamAdded(TeamInfo * info)130 TargetHost::_NotifyTeamAdded(TeamInfo* info)
131 {
132 for (ListenerList::Iterator it = fListeners.GetIterator();
133 Listener* listener = it.Next();) {
134 listener->TeamAdded(info);
135 }
136 }
137
138
139 void
_NotifyTeamRemoved(team_id team)140 TargetHost::_NotifyTeamRemoved(team_id team)
141 {
142 for (ListenerList::Iterator it = fListeners.GetIterator();
143 Listener* listener = it.Next();) {
144 listener->TeamRemoved(team);
145 }
146 }
147
148
149 void
_NotifyTeamRenamed(TeamInfo * info)150 TargetHost::_NotifyTeamRenamed(TeamInfo* info)
151 {
152 for (ListenerList::Iterator it = fListeners.GetIterator();
153 Listener* listener = it.Next();) {
154 listener->TeamRenamed(info);
155 }
156 }
157
158
159 // #pragma mark - TargetHost::Listener
160
161
~Listener()162 TargetHost::Listener::~Listener()
163 {
164 }
165
166
167 void
TeamAdded(TeamInfo * info)168 TargetHost::Listener::TeamAdded(TeamInfo* info)
169 {
170 }
171
172
173 void
TeamRemoved(team_id team)174 TargetHost::Listener::TeamRemoved(team_id team)
175 {
176 }
177
178
179 void
TeamRenamed(TeamInfo * info)180 TargetHost::Listener::TeamRenamed(TeamInfo* info)
181 {
182 }
183