Skip to content

Package: ApplicationController$Entry

ApplicationController$Entry

nameinstructionbranchcomplexitylinemethod
ApplicationController.Entry(String, ApplicationContext, Handler)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getHandler()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getId()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.kura.simulator.app;
14:
15: import org.eclipse.kapua.kura.simulator.Module;
16: import org.eclipse.kapua.kura.simulator.Transport;
17: import org.eclipse.kapua.kura.simulator.payload.Message;
18: import org.eclipse.kapua.kura.simulator.topic.Topic;
19: import org.eclipse.kapua.kura.simulator.topic.Topic.Segment;
20:
21: import java.util.Collection;
22: import java.util.Collections;
23: import java.util.HashMap;
24: import java.util.Map;
25: import java.util.Set;
26:
27: public class ApplicationController implements Module {
28:
29: private final Map<String, Entry> applications = new HashMap<>();
30: private final Transport transport;
31:
32: private static class Entry {
33:
34: private final String id;
35: private final Handler handler;
36:
37: public Entry(final String id, final ApplicationContext context, final Handler handler) {
38: this.id = id;
39: this.handler = handler;
40: }
41:
42: public String getId() {
43: return this.id;
44: }
45:
46: public Handler getHandler() {
47: return this.handler;
48: }
49: }
50:
51: public ApplicationController(final Transport transport) {
52: this.transport = transport;
53: }
54:
55: public ApplicationController(final Transport transport, final Collection<Application> applications) {
56: this.transport = transport;
57: applications.forEach(this::add);
58: }
59:
60: public void add(final Application application) {
61: final Descriptor desc = application.getDescriptor();
62: final String id = desc.getId();
63:
64: remove(application);
65:
66: final ApplicationContext context = new ApplicationContext() {
67:
68: @Override
69: public Sender sender(final Topic topic) {
70: topic.attach("application-id", id);
71: return Sender.transportSender(topic, ApplicationController.this.transport);
72: }
73: };
74:
75: final Handler handler = application.createHandler(context);
76:
77: final Entry entry = new Entry(id, context, handler);
78: this.applications.put(id, entry);
79:
80: subscribeEntry(entry);
81: }
82:
83: public void remove(final Application application) {
84: final String id = application.getDescriptor().getId();
85: final Entry entry = this.applications.remove(id);
86: if (entry == null) {
87: return;
88: }
89:
90: this.transport.unsubscribe(Topic.application(id).append(Segment.wildcard()));
91: }
92:
93: private void subscribeEntry(final Entry entry) {
94: this.transport.subscribe(Topic.application(entry.getId()).append(Segment.wildcard()), msg -> {
95: // try to cut off application id prefix
96: final Message message = msg.localize(Topic.application(entry.getId()));
97: if (message != null) {
98: // matches our pattern
99: entry.getHandler().processMessage(message);
100: }
101: });
102: }
103:
104: @Override
105: public void connected(final Transport transport) {
106: for (final Entry entry : this.applications.values()) {
107: entry.getHandler().connected();
108: subscribeEntry(entry);
109: }
110: }
111:
112: @Override
113: public void disconnected(final Transport transport) {
114: for (final Entry entry : this.applications.values()) {
115: entry.getHandler().disconnected();
116: }
117: }
118:
119: public Set<String> getApplicationIds() {
120: return Collections.unmodifiableSet(this.applications.keySet());
121: }
122:
123: }