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
13TargetHost::TargetHost(const BString& name)
14	:
15	BReferenceable(),
16	fName(name),
17	fLock(),
18	fListeners(),
19	fTeams()
20{
21}
22
23
24TargetHost::~TargetHost()
25{
26	while (!fTeams.IsEmpty())
27		delete fTeams.RemoveItemAt((int32)0);
28}
29
30
31void
32TargetHost::AddListener(Listener* listener)
33{
34	AutoLocker<TargetHost> hostLocker(this);
35	fListeners.Add(listener);
36}
37
38
39void
40TargetHost::RemoveListener(Listener* listener)
41{
42	AutoLocker<TargetHost> hostLocker(this);
43	fListeners.Remove(listener);
44}
45
46
47int32
48TargetHost::CountTeams() const
49{
50	return fTeams.CountItems();
51}
52
53
54status_t
55TargetHost::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
69void
70TargetHost::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
83void
84TargetHost::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
97TeamInfo*
98TargetHost::TeamInfoAt(int32 index) const
99{
100	return fTeams.ItemAt(index);
101}
102
103
104TeamInfo*
105TargetHost::TeamInfoByID(team_id team) const
106{
107	return fTeams.BinarySearchByKey(team, &_FindTeamByKey);
108}
109
110
111/*static*/ int
112TargetHost::_CompareTeams(const TeamInfo* a, const TeamInfo* b)
113{
114	return a->TeamID() < b->TeamID() ? -1 : 1;
115}
116
117
118/*static*/ int
119TargetHost::_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
129void
130TargetHost::_NotifyTeamAdded(TeamInfo* info)
131{
132	for (ListenerList::Iterator it = fListeners.GetIterator();
133			Listener* listener = it.Next();) {
134		listener->TeamAdded(info);
135	}
136}
137
138
139void
140TargetHost::_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
149void
150TargetHost::_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
162TargetHost::Listener::~Listener()
163{
164}
165
166
167void
168TargetHost::Listener::TeamAdded(TeamInfo* info)
169{
170}
171
172
173void
174TargetHost::Listener::TeamRemoved(team_id team)
175{
176}
177
178
179void
180TargetHost::Listener::TeamRenamed(TeamInfo* info)
181{
182}
183