Skip to content

Package: ApplicationController$1

ApplicationController$1

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