![JAR search and dependency download from the Maven repository](/logo.png)
io.joynr.demo.MyRadioProviderApplication Maven / Gradle / Ivy
package io.joynr.demo;
/*
* #%L
* %%
* Copyright (C) 2011 - 2016 BMW Car IT GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import io.joynr.provider.ProviderAnnotations;
import io.joynr.accesscontrol.StaticDomainAccessControlProvisioning;
import io.joynr.accesscontrol.StaticDomainAccessControlProvisioningModule;
import io.joynr.exceptions.JoynrRuntimeException;
import io.joynr.messaging.AtmosphereMessagingModule;
import io.joynr.messaging.MessagingPropertyKeys;
import io.joynr.messaging.mqtt.paho.client.MqttPahoModule;
import io.joynr.messaging.websocket.WebsocketModule;
import io.joynr.runtime.AbstractJoynrApplication;
import io.joynr.runtime.CCInProcessRuntimeModule;
import io.joynr.runtime.CCWebSocketRuntimeModule;
import io.joynr.runtime.JoynrApplication;
import io.joynr.runtime.JoynrApplicationModule;
import io.joynr.runtime.JoynrInjectorFactory;
import io.joynr.runtime.LibjoynrWebSocketRuntimeModule;
import java.io.IOException;
import java.util.Properties;
import jline.console.ConsoleReader;
import joynr.infrastructure.DacTypes.MasterAccessControlEntry;
import joynr.infrastructure.DacTypes.Permission;
import joynr.infrastructure.DacTypes.TrustLevel;
import joynr.types.ProviderScope;
import joynr.types.ProviderQos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import edu.umd.cs.findbugs.annotations.SuppressWarnings;
public class MyRadioProviderApplication extends AbstractJoynrApplication {
private static final Logger LOG = LoggerFactory.getLogger(MyRadioProviderApplication.class);
public static final String STATIC_PERSISTENCE_FILE = "provider-joynr.properties";
private MyRadioProvider provider = null;
@Inject
private ObjectMapper jsonSerializer;
@Inject
private ProviderScope providerScope;
public static void main(String[] args) throws Exception {
// run application from cmd line using Maven:
// mvn exec:java -Dexec.mainClass="io.joynr.demo.MyRadioProviderApplication" -Dexec.args=""
// Get the provider domain from the command line
if (args.length < 1 || args.length > 3) {
LOG.error("\n\nUSAGE: java {} [(websocket | websocketCC):[http]:[mqtt] [local]]\n\n NOTE: Providers are registered on the local domain.",
MyRadioProviderApplication.class.getName());
return;
}
String localDomain = args[0];
Properties joynrConfig = new Properties();
Module runtimeModule = getRuntimeModule(args, joynrConfig);
final ProviderScope providerScope = getProviderScope(args);
LOG.debug("Using the following runtime module: " + runtimeModule.getClass().getSimpleName());
LOG.debug("Registering provider with the following scope: " + providerScope.name());
LOG.debug("Registering provider on domain \"{}\"", localDomain);
// joynr config properties are used to set joynr configuration at
// compile time. They are set on the
// JoynrInjectorFactory.
// Set a custom static persistence file (default is joynr.properties in
// the working dir) to store
// joynr configuration. It allows for changing the joynr configuration
// at runtime. Custom persistence
// files support running the consumer and provider applications from
// within the same directory.
joynrConfig.setProperty(MessagingPropertyKeys.PERSISTENCE_FILE, STATIC_PERSISTENCE_FILE);
// How to use custom infrastructure elements:
// 1) Set them programmatically at compile time using the joynr
// configuration properties at the
// JoynInjectorFactory. E.g. uncomment the following lines to set a
// certain joynr server
// instance.
// joynrConfig.setProperty(MessagingPropertyKeys.BOUNCE_PROXY_URL,
// "http://localhost:8080/bounceproxy/");
// joynrConfig.setProperty(MessagingPropertyKeys.DISCOVERYDIRECTORYURL,
// "http://localhost:8080/discovery/channels/discoverydirectory_channelid/");
// Each joynr instance has a local domain. It identifies the execution
// device/platform, e.g. the
// vehicle. Normally, providers on that instance are registered for the
// local domain.
joynrConfig.setProperty(PROPERTY_JOYNR_DOMAIN_LOCAL, localDomain);
// 2) Or set them in the static persistence file (default:
// joynr.properties in working dir) at
// runtime. If not available in the working dir, it will be created
// during the first launch
// of the application. Copy the following lines to the custom
// persistence file to set a
// certain joynr server instance.
// NOTE: This application uses a custom static persistence file
// provider-joynr.properties.
// Copy the following lines to the custom persistence file to set a
// certain joynr server
// instance.
// joynr.messaging.bounceproxyurl=http://localhost:8080/bounceproxy/
// joynr.messaging.discoverydirectoryurl=http://localhost:8080/discovery/channels/discoverydirectory_channelid/
// 3) Or set them in Java System properties.
// -Djoynr.messaging.bounceProxyUrl=http://localhost:8080/bounceproxy/
// -Djoynr.messaging.capabilitiesDirectoryUrl=http://localhost:8080/discovery/channels/discoverydirectory_channelid/
// NOTE:
// Programmatically set configuration properties override properties set
// in the static persistence file.
// Java system properties override both
// Application-specific configuration properties are injected to the
// application by setting
// them on the JoynApplicationModule.
Properties appConfig = new Properties();
// Use injected static provisioning of access control entries to allow access to anyone to this interface
provisionAccessControl(joynrConfig, localDomain);
JoynrApplication joynrApplication = new JoynrInjectorFactory(joynrConfig,
runtimeModule,
new StaticDomainAccessControlProvisioningModule()).createApplication(new JoynrApplicationModule(MyRadioProviderApplication.class,
appConfig) {
@Override
protected void configure() {
super.configure();
bind(ProviderScope.class).toInstance(providerScope);
}
});
joynrApplication.run();
joynrApplication.shutdown();
}
private static ProviderScope getProviderScope(String[] args) {
if (args.length > 2 && args[2].equalsIgnoreCase("local")) {
return ProviderScope.LOCAL;
}
return ProviderScope.GLOBAL;
}
private static Module getRuntimeModule(String[] args, Properties joynrConfig) {
Module runtimeModule;
if (args.length > 1) {
String transport = args[1].toLowerCase();
if (transport.contains("websocketcc")) {
configureWebSocket(joynrConfig);
runtimeModule = new CCWebSocketRuntimeModule();
} else if (transport.contains("websocket")) {
configureWebSocket(joynrConfig);
runtimeModule = new LibjoynrWebSocketRuntimeModule();
} else {
runtimeModule = new CCInProcessRuntimeModule();
}
Module backendTransportModules = Modules.EMPTY_MODULE;
if (transport.contains("http")) {
backendTransportModules = Modules.combine(backendTransportModules, new AtmosphereMessagingModule());
}
if (transport.contains("mqtt")) {
configureMqtt(joynrConfig);
backendTransportModules = Modules.combine(backendTransportModules, new MqttPahoModule());
}
return Modules.override(runtimeModule).with(backendTransportModules);
}
return Modules.override(new CCInProcessRuntimeModule()).with(new AtmosphereMessagingModule());
}
private static void configureWebSocket(Properties joynrConfig) {
joynrConfig.setProperty(WebsocketModule.PROPERTY_WEBSOCKET_MESSAGING_HOST, "localhost");
joynrConfig.setProperty(WebsocketModule.PROPERTY_WEBSOCKET_MESSAGING_PORT, "4242");
joynrConfig.setProperty(WebsocketModule.PROPERTY_WEBSOCKET_MESSAGING_PROTOCOL, "ws");
joynrConfig.setProperty(WebsocketModule.PROPERTY_WEBSOCKET_MESSAGING_PATH, "");
}
private static void configureMqtt(Properties joynrConfig) {
joynrConfig.put("joynr.messaging.mqtt.brokerUri", "tcp://localhost:1883");
joynrConfig.put(MessagingPropertyKeys.PROPERTY_MESSAGING_PRIMARYGLOBALTRANSPORT, "mqtt");
}
@Override
public void run() {
provider = new MyRadioProvider();
provider.addBroadcastFilter(new TrafficServiceBroadcastFilter());
provider.addBroadcastFilter(new GeocastBroadcastFilter(jsonSerializer));
ProviderQos providerQos = new ProviderQos();
providerQos.setPriority(System.currentTimeMillis());
providerQos.setScope(providerScope);
runtime.registerProvider(localDomain, provider, providerQos);
ConsoleReader console;
try {
console = new ConsoleReader();
int key;
while ((key = console.readCharacter()) != 'q') {
switch (key) {
case 's':
provider.shuffleStations();
break;
case 'p':
provider.fireWeakSignalEventWithPartition();
break;
case 'w':
provider.fireWeakSignalEvent();
break;
case 'n':
provider.fireNewStationDiscoveredEvent();
break;
default:
LOG.info("\n\nUSAGE press\n" + " q\tto quit\n" + " s\tto shuffle stations\n"
+ " w\tto fire weak signal event\n"
+ " p\tto fire weak signal event with country of current station as partition\n"
+ " n\tto fire station discovered event\n");
break;
}
}
} catch (IOException e) {
LOG.error("error reading input from console", e);
}
}
@Override
@SuppressWarnings(value = "DM_EXIT", justification = "WORKAROUND to be removed")
public void shutdown() {
LOG.info("shutting down");
if (provider != null) {
try {
runtime.unregisterProvider(localDomain, provider);
} catch (JoynrRuntimeException e) {
LOG.error("unable to unregister capabilities {}", e.getMessage());
}
}
runtime.shutdown(true);
// TODO currently there is a bug preventing all threads being stopped
// WORKAROUND
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// do nothing; exiting application
}
System.exit(0);
}
private static void provisionAccessControl(Properties properties, String domain) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enableDefaultTypingAsProperty(DefaultTyping.JAVA_LANG_OBJECT, "_typeName");
MasterAccessControlEntry newMasterAccessControlEntry = new MasterAccessControlEntry("*",
domain,
ProviderAnnotations.getInterfaceName(MyRadioProvider.class),
TrustLevel.LOW,
new TrustLevel[]{ TrustLevel.LOW },
TrustLevel.LOW,
new TrustLevel[]{ TrustLevel.LOW },
"*",
Permission.YES,
new Permission[]{ Permission.YES });
MasterAccessControlEntry[] provisionedAccessControlEntries = { newMasterAccessControlEntry };
String provisionedAccessControlEntriesAsJson = objectMapper.writeValueAsString(provisionedAccessControlEntries);
properties.setProperty(StaticDomainAccessControlProvisioning.PROPERTY_PROVISIONED_MASTER_ACCESSCONTROLENTRIES,
provisionedAccessControlEntriesAsJson);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy