DefaultCachingComponent.java 9.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of Simonstrator.KOM.
 *
 * Simonstrator.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * PeerfactSim.KOM 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

21
package de.tud.kom.p2psim.impl.vehicular.caching;
22
23
24
25
26
27

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

28
import de.tudarmstadt.maki.simonstrator.api.Host;
29
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
30
31
32
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName;
import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener;
33
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.VehicleInformationComponent;
34
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent;
Tobias Meuser's avatar
Tobias Meuser committed
35
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
36
37
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
38
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
39
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.JamInformation;
40
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
41
42
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
43

44
45
public class DefaultCachingComponent
implements CachingComponent, ConnectivityListener {
46
	private Map<Class<? extends Object>, List<PointInformation>> _cache = new HashMap<>();
47
48
49

	private Map<Integer, Integer> _lastColorValues = new HashMap<>();

50
51
	private Host _host;

Tobias Meuser's avatar
Tobias Meuser committed
52
	private CacheInvalidationStrategy _invalidationStrategy;
53

Tobias Meuser's avatar
Tobias Meuser committed
54
	private CacheReplacementStrategy _replacementStrategy;
55

Tobias Meuser's avatar
Tobias Meuser committed
56
	private CacheDecisionStrategy _decisionStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
57

58
59
	private int _minObservations = 0;
	
60
61
62
63
64
65
	private double[] informationRatios = new double[] {1, 0.75, 0.5, 0.25, 0};

	public DefaultCachingComponent(Host pHost,
			CacheInvalidationStrategy pInvalidationStrategy,
			CacheReplacementStrategy pReplacementStrategy,
			CacheDecisionStrategy pDecisionStrategy) {
66
67
		_host = pHost;

Tobias Meuser's avatar
Tobias Meuser committed
68
		if (_host != null) {
69
70
71
72
73
74
75
			NetInterface wifi = _host.getNetworkComponent().getByName(NetInterfaceName.WIFI);
			if (wifi != null) {
				wifi.addConnectivityListener(this);
			} else {
				NetInterface cellular = _host.getNetworkComponent().getByName(NetInterfaceName.MOBILE);
				cellular.addConnectivityListener(this);
			}
Tobias Meuser's avatar
Tobias Meuser committed
76
		}
Tobias Meuser's avatar
Tobias Meuser committed
77
78
79

		_invalidationStrategy = pInvalidationStrategy;
		_replacementStrategy = pReplacementStrategy;
Tobias Meuser's avatar
Tobias Meuser committed
80
81
82
83
		_decisionStrategy = pDecisionStrategy;
	}

	@Override
84
85
	public <T extends PointInformation> List<T> getDecidedCacheEntries(
			Class<T> pCacheEntryClass) {
Tobias Meuser's avatar
Tobias Meuser committed
86
87
88
89
90
91
		List<T> cacheEntries = getCacheEntries(pCacheEntryClass);

		if (cacheEntries == null) {
			return null;
		}

92
		Map<Object, Map<Class<? extends Object>, List<PointInformation>>> similarCacheEntries = new HashMap<>();
Tobias Meuser's avatar
Tobias Meuser committed
93
94
95
96

		for (T t : cacheEntries) {
			Object position = getEdgeOrPosition(t);
			if (!similarCacheEntries.containsKey(position)) {
97
				similarCacheEntries.put(position, new HashMap<>());
Tobias Meuser's avatar
Tobias Meuser committed
98
			}
99
100
101
102
			if (!similarCacheEntries.get(position).containsKey(t.getValue().getClass())) {
				similarCacheEntries.get(position).put(t.getValue().getClass(), new ArrayList<PointInformation>());
			}
			similarCacheEntries.get(position).get(t.getValue().getClass()).add(t);
Tobias Meuser's avatar
Tobias Meuser committed
103
104
105
		}

		List<T> decidedInformation = new ArrayList<>();
106
		for (Map<Class<? extends Object>, List<PointInformation>> similarEdges : similarCacheEntries
107
				.values()) {
108
			for (List<PointInformation> similarInformation : similarEdges.values()) {
109
110
111
112
113
114
				if (similarInformation.size() >= _minObservations) {
					PointInformation correctInformation = _decisionStrategy
							.decideOnCorrectInformation(similarInformation);
					
					decidedInformation.add((T) correctInformation);
				}
115
			}
Tobias Meuser's avatar
Tobias Meuser committed
116
117
118
		}

		return decidedInformation;
119
	}
120
121
122
123
	
	public void setMinObservations(int pMinObservations) {
		this._minObservations = pMinObservations;
	}
124
125

	@Override
126
127
	public <T extends PointInformation> List<T> getCacheEntries(
			Class<T> pCacheEntryClass) {
128
		if (_cache.containsKey(pCacheEntryClass)) {
129
130
			List<? extends PointInformation> cacheEntries = _cache
					.get(pCacheEntryClass);
131
132
			List<T> results = new ArrayList<>();

133
134
135
136
137
138
			for (int i = 0; i < cacheEntries.size(); i++) {
				PointInformation object = cacheEntries.get(i);
				if (_invalidationStrategy.checkInformation(object)) {
					cacheEntries.remove(i--);
					continue;
				}
139
140
141
142
143
144
145
146
				results.add((T) object);
			}

			return results;
		}
		return null;
	}

147
	@Override
148
	public <T extends PointInformation> boolean containsEntry(T pCacheEntry) {
149
150
151
152
153
154
155
		if (_cache.containsKey(pCacheEntry)) {
			List<? extends Object> cacheEntries = _cache.get(pCacheEntry);
			return cacheEntries.contains(pCacheEntry);
		}
		return false;
	}

156
	@Override
157
	public <T extends PointInformation> void storeCacheEntry(T pCacheEntry) {
158
159
160
		if (!_cache.containsKey(pCacheEntry.getClass())) {
			_cache.put(pCacheEntry.getClass(), new ArrayList<>());
		}
161

162
		List<PointInformation> entries = _cache.get(pCacheEntry.getClass());
Tobias Meuser's avatar
Tobias Meuser committed
163
		entries.add(pCacheEntry);
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
	@Override
	public void initialize() {
		_cache.clear();
	}

	@Override
	public void shutdown() {
	}

	@Override
	public Host getHost() {
		return _host;
	}

	@Override
	public void wentOnline(Host pHost, NetInterface pNetInterface) {
		_cache.clear();
	}

	@Override
	public void wentOffline(Host pHost, NetInterface pNetInterface) {

	}

190
191
192
193
194
	@Override
	public void clear() {
		_cache.clear();
	}

Tobias Meuser's avatar
Tobias Meuser committed
195
196
	public Object getEdgeOrPosition(PointInformation information) {
		if (information.hasAttribute(AvailableInformationAttributes.EDGE)) {
197
198
			return information
					.getAttribute(AvailableInformationAttributes.EDGE);
Tobias Meuser's avatar
Tobias Meuser committed
199
200
201
202
203
		} else {
			return information.getLocation();
		}
	}

204
205
206
207
	public CacheDecisionStrategy getDecisionStrategy() {
		return _decisionStrategy;
	}

208
209
	@Override
	public String getNodeDescription() {
Tobias Meuser's avatar
Tobias Meuser committed
210
		return "    " + getHost().getId();
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
	}

	@Override
	public int getNodeColorDimensions() {
		return 2;
	}

	@Override
	public String[] getNodeColorDimensionDescriptions() {
		return new String[] {"Route Information", "Number of jam information"};
	}

	@Override
	public String[] getNodeColorDescriptions(int pDimension) {
		if (pDimension == 0) {
			String[] labels = new String[informationRatios.length];
			for (int i = 0; i < informationRatios.length; i++) {
				labels[i] = informationRatios[i] * 100 + "%";
			}
			return labels;
		} else if (pDimension == 1) {
			String[] numbers = new String[10];
			for (int i = 0; i < numbers.length; i++) {
				numbers[i] = String.valueOf(i);
			}
			return numbers;
		}
		return new String[0];
	}

	@Override
	public int getNodeColor(int pDimension) {
		if (pDimension == 0) {
			List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
			try {
				VehicleInformationComponent vehicleInformationComponent = getHost().getComponent(VehicleInformationComponent.class);
				if (vehicleInformationComponent.isValid()) {
					RoadNetworkRoute currentRoute = vehicleInformationComponent.getCurrentRoute();
					if (currentRoute != null) {
						int count = 0;
						int active = 0;
						for (RoadNetworkEdge edge : currentRoute.getRoute()) {
							if (edge.isActive()) {
								if (decidedCacheEntries != null) {
									for (JamInformation jamInformation : decidedCacheEntries) {
										if (jamInformation.getEdge().equals(edge) && jamInformation.getValue()) {
											count++;
											break;
										}
									}
								}
								active++;
							}
						}
						if (active != 0) {
							double ratio = count / ((double) active);
							for (int i = 0; i < informationRatios.length; i++) {
								if (informationRatios[i] <= ratio) {
									_lastColorValues.put(pDimension, i);
									break;
								}
							}
						} else {
							_lastColorValues.put(pDimension, 0);
						}
					}
				}
			} catch (ComponentNotAvailableException e) {
			}
			if (_lastColorValues.containsKey(pDimension)) {
				return _lastColorValues.get(pDimension);
			}
			return 0;
		}
		if (pDimension == 1) {
			List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
			if (decidedCacheEntries != null) {
				_lastColorValues.put(pDimension, decidedCacheEntries.size());
			}
			if (_lastColorValues.containsKey(pDimension)) {
				return Math.min(9, _lastColorValues.get(pDimension));
			}
			return 0;
		}
		return 0;
	}

	@Override
	public boolean isActive() {
		return getHost().getNetworkComponent().getByName(NetInterfaceName.MOBILE).isUp()
				|| getHost().getNetworkComponent().getByName(NetInterfaceName.WIFI).isUp();
	}
303
}