1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.mock;
17
18 import org.fourthline.cling.UpnpService;
19 import org.fourthline.cling.UpnpServiceConfiguration;
20 import org.fourthline.cling.controlpoint.ControlPoint;
21 import org.fourthline.cling.controlpoint.ControlPointImpl;
22 import org.fourthline.cling.model.message.header.UpnpHeader;
23 import org.fourthline.cling.model.meta.LocalDevice;
24 import org.fourthline.cling.protocol.ProtocolFactory;
25 import org.fourthline.cling.protocol.ProtocolFactoryImpl;
26 import org.fourthline.cling.protocol.async.SendingNotificationAlive;
27 import org.fourthline.cling.protocol.async.SendingSearch;
28 import org.fourthline.cling.registry.Registry;
29 import org.fourthline.cling.registry.RegistryImpl;
30 import org.fourthline.cling.registry.RegistryMaintainer;
31 import org.fourthline.cling.transport.RouterException;
32 import org.fourthline.cling.transport.spi.NetworkAddressFactory;
33
34 import javax.enterprise.inject.Alternative;
35
36
37
38
39
40
41
42
43
44 @Alternative
45 public class MockUpnpService implements UpnpService {
46
47 protected final UpnpServiceConfiguration configuration;
48 protected final ControlPoint controlPoint;
49 protected final ProtocolFactory protocolFactory;
50 protected final Registry registry;
51 protected final MockRouter router;
52
53 protected final NetworkAddressFactory networkAddressFactory;
54
55
56
57
58 public MockUpnpService() {
59 this(false, new MockUpnpServiceConfiguration(false, false));
60 }
61
62
63
64
65 public MockUpnpService(MockUpnpServiceConfiguration configuration) {
66 this(false, configuration);
67 }
68
69
70
71
72 public MockUpnpService(final boolean sendsAlive, final boolean maintainsRegistry) {
73 this(sendsAlive, new MockUpnpServiceConfiguration(maintainsRegistry, false));
74 }
75
76 public MockUpnpService(final boolean sendsAlive, final boolean maintainsRegistry, final boolean multiThreaded) {
77 this(sendsAlive, new MockUpnpServiceConfiguration(maintainsRegistry, multiThreaded));
78 }
79
80 public MockUpnpService(final boolean sendsAlive, final MockUpnpServiceConfiguration configuration) {
81
82 this.configuration = configuration;
83
84 this.protocolFactory = createProtocolFactory(this, sendsAlive);
85
86 this.registry = new RegistryImpl(this) {
87 @Override
88 protected RegistryMaintainer createRegistryMaintainer() {
89 return configuration.isMaintainsRegistry() ? super.createRegistryMaintainer() : null;
90 }
91 };
92
93 this.networkAddressFactory = this.configuration.createNetworkAddressFactory();
94
95 this.router = createRouter();
96
97 this.controlPoint = new ControlPointImpl(configuration, protocolFactory, registry);
98 }
99
100 protected ProtocolFactory createProtocolFactory(UpnpService service, boolean sendsAlive) {
101 return new MockProtocolFactory(service, sendsAlive);
102 }
103
104 protected MockRouter createRouter() {
105 return new MockRouter(getConfiguration(), getProtocolFactory());
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119 public static class MockProtocolFactory extends ProtocolFactoryImpl {
120
121 private boolean sendsAlive;
122
123 public MockProtocolFactory(UpnpService upnpService, boolean sendsAlive) {
124 super(upnpService);
125 this.sendsAlive = sendsAlive;
126 }
127
128 @Override
129 public SendingNotificationAlive createSendingNotificationAlive(LocalDevice localDevice) {
130 return new SendingNotificationAlive(getUpnpService(), localDevice) {
131 @Override
132 protected void execute() throws RouterException {
133 if (sendsAlive) super.execute();
134 }
135 };
136 }
137
138 @Override
139 public SendingSearch createSendingSearch(UpnpHeader searchTarget, int mxSeconds) {
140 return new SendingSearch(getUpnpService(), searchTarget, mxSeconds) {
141 @Override
142 public int getBulkIntervalMilliseconds() {
143 return 0;
144 }
145 };
146 }
147 }
148
149 public UpnpServiceConfiguration getConfiguration() {
150 return configuration;
151 }
152
153 public ControlPoint getControlPoint() {
154 return controlPoint;
155 }
156
157 public ProtocolFactory getProtocolFactory() {
158 return protocolFactory;
159 }
160
161 public Registry getRegistry() {
162 return registry;
163 }
164
165 public MockRouter getRouter() {
166 return router;
167 }
168
169 public void shutdown() {
170 getRegistry().shutdown();
171 getConfiguration().shutdown();
172 }
173 }