forked from 4thline/cling
/
RegistryItems.java
185 lines (158 loc) · 5.71 KB
/
RegistryItems.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2011-2024 4th Line GmbH, Switzerland and others
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License Version 1 or later
* ("CDDL") (collectively, the "License"). You may not use this file
* except in compliance with the License. See LICENSE.txt for more
* information.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* SPDX-License-Identifier: CDDL-1.0
*/
package org.jupnp.registry;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.jupnp.UpnpServiceConfiguration;
import org.jupnp.model.ValidationException;
import org.jupnp.model.gena.GENASubscription;
import org.jupnp.model.meta.Device;
import org.jupnp.model.resource.Resource;
import org.jupnp.model.types.DeviceType;
import org.jupnp.model.types.ServiceType;
import org.jupnp.model.types.UDN;
/**
* Internal class, required by {@link RegistryImpl}.
*
* @author Christian Bauer
*/
abstract class RegistryItems<D extends Device, S extends GENASubscription> {
protected final RegistryImpl registry;
protected final Set<RegistryItem<UDN, D>> deviceItems = new HashSet<>();
protected final Set<RegistryItem<String, S>> subscriptionItems = new HashSet<>();
RegistryItems(RegistryImpl registry) {
this.registry = registry;
}
Set<RegistryItem<UDN, D>> getDeviceItems() {
return deviceItems;
}
Set<RegistryItem<String, S>> getSubscriptionItems() {
return subscriptionItems;
}
abstract void add(D device);
abstract boolean remove(final D device);
abstract void removeAll();
abstract void maintain();
abstract void shutdown();
/**
* Returns root and embedded devices registered under the given UDN.
*
* @param udn A unique device name.
* @param rootOnly Set to true if only root devices (no embedded) should be searched
* @return Any registered root or embedded device under the given UDN, <tt>null</tt> if
* no device with the given UDN has been registered.
*/
D get(UDN udn, boolean rootOnly) {
for (RegistryItem<UDN, D> item : deviceItems) {
D device = item.getItem();
if (device.getIdentity().getUdn().equals(udn)) {
return device;
}
if (!rootOnly) {
D foundDevice = (D) item.getItem().findDevice(udn);
if (foundDevice != null) {
return foundDevice;
}
}
}
return null;
}
/**
* Returns all devices (root or embedded) with a compatible type.
* <p>
* This routine will check compatible versions, as described by the UDA.
* </p>
*
* @param deviceType The minimum device type required.
* @return Any registered root or embedded device with a compatible type.
*/
Collection<D> get(DeviceType deviceType) {
Collection<D> devices = new HashSet<>();
for (RegistryItem<UDN, D> item : deviceItems) {
D[] d = (D[]) item.getItem().findDevices(deviceType);
if (d != null) {
devices.addAll(Arrays.asList(d));
}
}
return devices;
}
/**
* Returns all devices (root or embedded) which have at least one matching service.
*
* @param serviceType The type of service to search for.
* @return Any registered root or embedded device with at least one matching service.
*/
Collection<D> get(ServiceType serviceType) {
Collection<D> devices = new HashSet<>();
for (RegistryItem<UDN, D> item : deviceItems) {
D[] d = (D[]) item.getItem().findDevices(serviceType);
if (d != null) {
devices.addAll(Arrays.asList(d));
}
}
return devices;
}
Collection<D> get() {
Collection<D> devices = new HashSet<>();
for (RegistryItem<UDN, D> item : deviceItems) {
devices.add(item.getItem());
}
return devices;
}
boolean contains(D device) {
return contains(device.getIdentity().getUdn());
}
boolean contains(UDN udn) {
return deviceItems.contains(new RegistryItem<UDN, D>(udn));
}
void addSubscription(S subscription) {
RegistryItem<String, S> subscriptionItem = new RegistryItem<>(subscription.getSubscriptionId(), subscription,
subscription.getActualDurationSeconds());
subscriptionItems.add(subscriptionItem);
}
boolean updateSubscription(S subscription) {
if (removeSubscription(subscription)) {
addSubscription(subscription);
return true;
}
return false;
}
boolean removeSubscription(S subscription) {
return subscriptionItems.remove(new RegistryItem<String, S>(subscription.getSubscriptionId()));
}
S getSubscription(String subscriptionId) {
for (RegistryItem<String, S> registryItem : subscriptionItems) {
if (registryItem.getKey().equals(subscriptionId)) {
return registryItem.getItem();
}
}
return null;
}
Resource[] getResources(Device device) throws RegistrationException {
UpnpServiceConfiguration config = registry.getConfiguration();
if (config != null) {
try {
return config.getNamespace().getResources(device);
} catch (ValidationException e) {
throw new RegistrationException("Resource discover error", e);
}
} else {
return new Resource[0];
}
}
}