-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAbstractService.java
More file actions
267 lines (241 loc) · 8.64 KB
/
AbstractService.java
File metadata and controls
267 lines (241 loc) · 8.64 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright 2007 David Wheeler
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tensin.sonos.control;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.teleal.cling.UpnpService;
import org.teleal.cling.controlpoint.ActionCallback;
import org.teleal.cling.model.action.ActionArgumentValue;
import org.teleal.cling.model.action.ActionInvocation;
import org.teleal.cling.model.message.UpnpResponse;
import org.teleal.cling.model.meta.RemoteDevice;
import org.teleal.cling.model.meta.Service;
import org.teleal.cling.model.types.UDAServiceId;
/**
* An abstract class that wraps a UpnpService. Intended to be subclassed to have
* functionality added.
*
* @author David WHEELER
* @author Serge SIMON
*
*/
public abstract class AbstractService {
// private class EventingRefreshTask extends TimerTask {
// private final ThreadChangingEventHandlerWrapper handler;
//
// protected EventingRefreshTask(final ThreadChangingEventHandlerWrapper handler) {
// this.handler = handler;
// }
//
// @Override
// public void run() {
// try {
// refreshServiceEventing(DEFAULT_EVENT_PERIOD, handler);
// } catch (IOException e) {
// LOGGER.warn("Could not refresh eventing: ", e);
// }
// }
// }
/**
* A ServiceEventHandler that adapts the event to occur in the SonosController thread.
*
* @author David WHEELER
* @author Serge SIMON
* @author Serge SIMON
*
*/
// private static final class ThreadChangingEventHandlerWrapper implements ServiceEventHandler {
//
// private final ServiceEventHandler handler;
//
// public ThreadChangingEventHandlerWrapper(final ServiceEventHandler handler) {
// this.handler = handler;
// }
//
// public ServiceEventHandler getWrappedHandler() {
// return handler;
// }
//
// public void handleStateVariableEvent(final String varName, final String newValue) {
// SonosController.getInstance().getControllerExecutor().execute(new Runnable() {
// @Override
// public void run() {
// handler.handleStateVariableEvent(varName, newValue);
// }
// });
// }
// }
//
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractService.class);
/**
* The default length of time in seconds to register events for.
*/
protected static final int DEFAULT_EVENT_PERIOD = 3600; // 1 hour
/**
* Find service.
*
* @param device
* the device
* @param serviceName
* the service name
* @return the service
*/
public static Service findService(final RemoteDevice device, final String serviceName) {
UDAServiceId uadServiceId = UDAServiceId.valueOf(serviceName.replace(":1", ""));
return device.findService(uadServiceId);
}
/** The service. */
protected final Service service;
/** The upnp service. */
protected UpnpService upnpService = null;
// private static final Timer timer = new Timer("ServiceEventRefresher", true);
// private final List<EventingRefreshTask> tasks = new ArrayList<EventingRefreshTask>();
/** The message factory. */
protected final UpnpMessageFactory messageFactory;
/**
* Instantiates a new abstract service.
*
* @param upnpService
* the upnp service
* @param service
* the service
* @param type
* the type
*/
public AbstractService(final UpnpService upnpService, final Service service, final String type) {
if (service != null && !service.getServiceType().toString().equals(type)) {
throw new IllegalArgumentException("service must be " + type + ", not " + service.getServiceType());
}
this.upnpService = upnpService;
this.service = service;
messageFactory = UpnpMessageFactory.getNewInstance();
}
/**
* Dispose.
*/
public void dispose() {
// LOGGER.info("Unregistering event listeners for " + getClass());
// List<EventingRefreshTask> tasksCopy = new ArrayList<EventingRefreshTask>(tasks);
// for (EventingRefreshTask task : tasksCopy) {
// unregisterServiceEventing(task.handler);
// }
}
/**
* Execute.
*
* @param message
* the message
*/
protected void execute(final ActionInvocation message) {
ActionCallback callback = new ActionCallback(message) {
@Override
public void failure(final ActionInvocation invocation, final UpnpResponse operation, final String defaultMsg) {
LOGGER.error(defaultMsg);
}
@Override
public void success(final ActionInvocation invocation) {
ActionArgumentValue[] output = invocation.getOutput();
// assertEquals(output.length, 0);
}
};
getUpnpService().getControlPoint().execute(callback);
}
/**
* Execute immediate.
*
* @param message
* the message
*/
protected void executeImmediate(final ActionInvocation message) {
new ActionCallback.Default(message, getUpnpService().getControlPoint()).run();
}
/**
* Gets the service.
*
* @return the UpnpService wrapped by this class.
*/
public Service getService() {
return service;
}
/**
* Gets the upnp service.
*
* @return the UpnpService wrapped by this class.
*/
public UpnpService getUpnpService() {
return upnpService;
}
/**
* Adds or refreshes the registration of service event notifications to the
* given handler
*
* @param duration
* the requested length of time for event notifications
* @param handler
* the object that handles the notifications
* @return the length of time that the handler has been registered for.
* @
* if the device could not be contacted for some reason.
*/
// private int refreshServiceEventing(final int duration, final ServiceEventHandler handler) {
// ServicesEventing eventing = ServicesEventing.getInstance();
// int i = eventing.register(service, handler, duration);
// LOGGER.info("Registered " + getClass() + " for eventing for " + i + "s");
// return i;
// }
/**
* Registers this service as a ServiceEventListener on its UpnpService. This
* should be cleaned up by calling unregisterServiceEventing()
*
* @param handler
* @return true if the registration process completed successfully. if false
* is returned, registration will still be attempted periodically
* until unregisterServiceEventing() is called.
*/
// protected boolean registerServiceEventing(final ServiceEventHandler handler) {
// ThreadChangingEventHandlerWrapper handlerWrapper = new ThreadChangingEventHandlerWrapper(handler);
// try {
// refreshServiceEventing(DEFAULT_EVENT_PERIOD, handlerWrapper);
// EventingRefreshTask task = new EventingRefreshTask(handlerWrapper);
// tasks.add(task);
// // schedule to refresh 1 minute less than the event period (in milis, not seconds)
// timer.schedule(task, (DEFAULT_EVENT_PERIOD - 60) * 1000, (DEFAULT_EVENT_PERIOD - 60) * 1000);
// return true;
// } catch (IOException e) {
// LOGGER.warn("Could not register service eventing: ", e);
// return false;
// }
// }
/**
* Removes the given handler from Service Event registration. It will recieve
* no further events.
*
* @param handler
* @
*/
// protected void unregisterServiceEventing(final ServiceEventHandler handler) {
// ServicesEventing eventing = ServicesEventing.getInstance();
// for (ListIterator<EventingRefreshTask> i = tasks.listIterator(); i.hasNext();) {
// EventingRefreshTask task = i.next();
// if (task.handler.getWrappedHandler() == handler) {
// task.cancel();
// i.remove();
// try {
// eventing.unRegister(service, task.handler);
// } catch (IOException e) {
// LOGGER.error("Could not unregister eventing from " + service, e);
// }
// }
// }
// }
}