Package: Simulator
Simulator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Simulator(GatewayConfiguration, Transport, Set) |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
connected() |
|
|
|
|
|
||||||||||||||||||||
disconnected() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
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;
14:
15: import java.util.LinkedList;
16: import java.util.List;
17: import java.util.Set;
18:
19: import org.eclipse.kapua.kura.simulator.app.Application;
20: import org.eclipse.kapua.kura.simulator.app.ApplicationController;
21: import org.eclipse.kapua.kura.simulator.birth.BirthCertificateModule;
22: import org.slf4j.Logger;
23: import org.slf4j.LoggerFactory;
24:
25: /**
26: * A default Kura simulator
27: */
28: public class Simulator implements AutoCloseable {
29:
30: private static final Logger logger = LoggerFactory.getLogger(Simulator.class);
31:
32: protected final Transport transport;
33:
34: protected List<Module> modules = new LinkedList<>();
35:
36: public Simulator(final GatewayConfiguration configuration, final Transport transport,
37: final Set<Application> applications) {
38:
39: this.transport = transport;
40:
41: // set up callbacks
42:
43: this.transport.whenConnected(this::connected);
44: this.transport.whenDisconnected(this::disconnected);
45:
46: // set up application controller
47:
48: final ApplicationController applicationController = new ApplicationController(transport, applications);
49: modules.add(applicationController);
50:
51: // set up builder
52:
53: modules.add(new BirthCertificateModule(configuration, applicationController::getApplicationIds));
54:
55: // finally connect
56:
57: this.transport.connect();
58: }
59:
60: @Override
61: public void close() {
62: // we don't close the transport here
63: }
64:
65: protected void connected() {
66: logger.info("Connected ... sending birth certificate ...");
67:• for (final Module module : modules) {
68: try {
69: module.connected(transport);
70: } catch (final Exception e) {
71: logger.warn("Failed to call module: {}", module, e);
72: }
73: }
74: }
75:
76: protected void disconnected() {
77:• for (final Module module : modules) {
78: try {
79: module.disconnected(transport);
80: } catch (final Exception e) {
81: logger.warn("Failed to call module: {}", module, e);
82: }
83: }
84: }
85:
86: }