/
ReceivingAsync.java
104 lines (87 loc) · 3.29 KB
/
ReceivingAsync.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
/*
* Copyright (C) 2013 4th Line GmbH, Switzerland
*
* The contents of this file are subject to the terms of either the GNU
* Lesser General Public License Version 2 or later ("LGPL") or 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.
*/
package org.fourthline.cling.protocol;
import org.fourthline.cling.UpnpService;
import org.fourthline.cling.model.message.UpnpMessage;
import org.fourthline.cling.model.message.header.UpnpHeader;
import org.seamless.util.Exceptions;
import java.util.logging.Logger;
/**
* Supertype for all asynchronously executing protocols, handling reception of UPnP messages.
* <p>
* After instantiation by the {@link ProtocolFactory}, this protocol <code>run()</code>s and
* calls its own {@link #waitBeforeExecution()} method. By default, the protocol does not wait
* before then proceeding with {@link #execute()}.
* </p>
*
* @param <M> The type of UPnP message handled by this protocol.
*
* @author Christian Bauer
*/
public abstract class ReceivingAsync<M extends UpnpMessage> implements Runnable {
final private static Logger log = Logger.getLogger(ReceivingAsync.class.getName());
private final UpnpService upnpService;
private M inputMessage;
protected ReceivingAsync(UpnpService upnpService, M inputMessage) {
this.upnpService = upnpService;
this.inputMessage = inputMessage;
}
public UpnpService getUpnpService() {
return upnpService;
}
public M getInputMessage() {
return inputMessage;
}
public void run() {
boolean proceed;
try {
proceed = waitBeforeExecution();
} catch (InterruptedException ex) {
log.info("Protocol wait before execution interrupted (on shutdown?): " + getClass().getSimpleName());
proceed = false;
}
if (proceed) {
try {
execute();
} catch (RuntimeException ex) {
Throwable cause = Exceptions.unwrap(ex);
if (cause instanceof InterruptedException) {
log.info("Protocol execution interrupted (on shutdown?): " + ex);
} else {
throw ex;
}
}
}
}
/**
* Provides an opportunity to pause before executing the protocol.
*
* @return <code>true</code> (default) if execution should continue after waiting.
*
* @throws InterruptedException If waiting has been interrupted, which also stops execution.
*/
protected boolean waitBeforeExecution() throws InterruptedException {
// Don't wait by default
return true;
}
protected abstract void execute();
protected <H extends UpnpHeader> H getFirstHeader(UpnpHeader.Type headerType, Class<H> subtype) {
return getInputMessage().getHeaders().getFirstHeader(headerType, subtype);
}
@Override
public String toString() {
return "(" + getClass().getSimpleName() + ")";
}
}