Radio example implementation

This commit is contained in:
2021-09-28 15:35:56 +02:00
parent 9cdbb84076
commit 2ad5deac3e
26 changed files with 318 additions and 0 deletions

8
Radio/bnd.bnd Normal file
View File

@@ -0,0 +1,8 @@
Bundle-Version: 1.0.0
Private-Package: Radio
Bundle-Activator: Radio.Radio
-buildfeatures:
-buildpath: \
channelapi;version=latest,\
outputapi;version=latest

View File

@@ -0,0 +1,21 @@
package Radio;
import channelapi.Channel;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
public class ChannelSelector {
private final BundleContext context;
public ChannelSelector (BundleContext context){
this.context = context;
}
public Channel selectChannel(){
ServiceTracker<Channel, Channel> serviceTracker = new ServiceTracker<Channel, Channel>(context, Channel.class, null);
serviceTracker.open();
Channel channel = serviceTracker.getService();
serviceTracker.close();
return channel;
}
}

View File

@@ -0,0 +1,21 @@
package Radio;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import outputapi.Output;
public class OutputSelector {
private final BundleContext context;
public OutputSelector(BundleContext context){
this.context = context;
}
public Output selectOutput (){
ServiceTracker<Output, Output> serviceTracker = new ServiceTracker<>(context, Output.class, null);
serviceTracker.open();
Output output = serviceTracker.getService();
serviceTracker.close();
return output;
}
}

View File

@@ -0,0 +1,30 @@
package Radio;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Radio implements BundleActivator {
private ChannelSelector channelSelector = null;
private OutputSelector outputSelector = null;
private Receiver receiver = null;
@Override
public void start(BundleContext context) throws Exception {
System.out.println("Starting radio");
channelSelector = new ChannelSelector(context);
outputSelector = new OutputSelector(context);
receiver = new Receiver();
System.out.println("Selecting channel");
receiver.setChannel(channelSelector.selectChannel());
System.out.println("Selecting output");
receiver.setOutput(outputSelector.selectOutput());
System.out.println("Starting receiver");
receiver.start();
}
@Override
public void stop(BundleContext context) throws Exception {
receiver.stop();
}
}

View File

@@ -0,0 +1,44 @@
package Radio;
import channelapi.Channel;
import channelapi.Stream;
import outputapi.Output;
public class Receiver {
private Channel channel = null;
private Output output = null;
private Thread receiverThread = null;
public Receiver (){
}
public void setChannel(Channel channel) {
this.channel = channel;
}
public void setOutput(Output output) {
this.output = output;
}
public void start (){
System.out.println("Starting receiver");
Stream stream = channel.getStream();
receiverThread = new Thread(() -> {
try{
while (true) {
output.write(stream.read());
Thread.sleep(1000);
}
} catch (InterruptedException e){
System.out.println("Receiver stopped");
}
});
receiverThread.start();
}
public void stop(){
receiverThread.interrupt();
}
}

7
Radio1/bnd.bnd Normal file
View File

@@ -0,0 +1,7 @@
Bundle-Version: 1.0.0
Private-Package: Radio1
Bundle-Activator: Radio1.Radio1
-buildfeatures:
-buildpath: \
channelapi;version=latest

View File

@@ -0,0 +1,25 @@
package Radio1;
import channelapi.Channel;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import java.util.Dictionary;
import java.util.Hashtable;
public class Radio1 implements BundleActivator {
private ServiceRegistration registration;
@Override
public void start(BundleContext context) throws Exception {
Dictionary<String, Object> props = new Hashtable<>();
props.put("name", "Radio 1");
registration = context.registerService( Channel.class, new Radio1Channel(), props);
}
@Override
public void stop(BundleContext context) throws Exception {
registration.unregister();
}
}

View File

@@ -0,0 +1,13 @@
package Radio1;
import channelapi.Channel;
import channelapi.Stream;
public class Radio1Channel implements Channel {
private Stream stream = new Radio1Stream();
@Override
public Stream getStream() {
return stream;
}
}

View File

@@ -0,0 +1,10 @@
package Radio1;
import channelapi.Stream;
public class Radio1Stream implements Stream {
@Override
public String read() {
return "Radio 1";
}
}

7
Radio538/bnd.bnd Normal file
View File

@@ -0,0 +1,7 @@
Bundle-Version: 1.0.0
Private-Package: Radio538
Bundle-Activator: Radio538.Radio538
-buildfeatures:
-buildpath: \
channelapi;version=latest

View File

@@ -0,0 +1,26 @@
package Radio538;
import channelapi.Channel;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import java.util.Dictionary;
import java.util.Hashtable;
public class Radio538 implements BundleActivator {
private ServiceRegistration registration;
@Override
public void start(BundleContext context) throws Exception {
Dictionary<String, Object> props = new Hashtable<>();
props.put("name", "Radio 538");
registration = context.registerService( Channel.class, new Radio538Channel(), props);
}
@Override
public void stop(BundleContext context) throws Exception {
registration.unregister();
}
}

View File

@@ -0,0 +1,12 @@
package Radio538;
import channelapi.Channel;
import channelapi.Stream;
public class Radio538Channel implements Channel {
private Stream stream = new Radio538Stream();
@Override
public Stream getStream() {
return stream;
}
}

View File

@@ -0,0 +1,10 @@
package Radio538;
import channelapi.Stream;
public class Radio538Stream implements Stream {
@Override
public String read() {
return "Radio 538";
}
}

1
RadioRunner/bnd.bnd Normal file
View File

@@ -0,0 +1 @@
-nobundles: true

0
RadioRunner/conf/.gitignore vendored Normal file
View File

12
RadioRunner/run.bndrun Normal file
View File

@@ -0,0 +1,12 @@
-runfw: org.apache.felix.framework
-runfeatures: shell
-runbundles: \
channelapi;version=latest,\
outputapi;version=latest,\
Radio538;version=latest,\
speaker;version=latest,\
Radio;version=latest
-runproperties: \
org.ops4j.pax.logging.DefaultServiceLog.level=WARN

5
channelapi/bnd.bnd Normal file
View File

@@ -0,0 +1,5 @@
Bundle-Version: 1.0.0
Export-Package: channelapi
-buildfeatures:
-buildpath:

View File

@@ -0,0 +1,5 @@
package channelapi;
public interface Channel {
Stream getStream();
}

View File

@@ -0,0 +1,5 @@
package channelapi;
public interface Stream {
String read();
}

View File

@@ -0,0 +1,2 @@
@org.osgi.annotation.versioning.Version("1.0.0")
package channelapi;

5
outputapi/bnd.bnd Normal file
View File

@@ -0,0 +1,5 @@
Bundle-Version: 1.0.0
Export-Package: outputapi
-buildfeatures:
-buildpath:

View File

@@ -0,0 +1,5 @@
package outputapi;
public interface Output {
void write (String sound);
}

View File

@@ -0,0 +1,2 @@
@org.osgi.annotation.versioning.Version("1.0.0")
package outputapi;

7
speaker/bnd.bnd Normal file
View File

@@ -0,0 +1,7 @@
Bundle-Version: 1.0.0
Private-Package: speaker
Bundle-Activator: speaker.Speaker
-buildfeatures:
-buildpath: \
outputapi;version=latest

View File

@@ -0,0 +1,25 @@
package speaker;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import outputapi.Output;
import java.util.Dictionary;
import java.util.Hashtable;
public class Speaker implements BundleActivator {
private ServiceRegistration registration;
@Override
public void start(BundleContext context) throws Exception {
Dictionary<String, Object> props = new Hashtable<>();
props.put("type", "Speaker");
registration = context.registerService( Output.class, new SpeakerOutput(), props);
}
@Override
public void stop(BundleContext context) throws Exception {
registration.unregister();
}
}

View File

@@ -0,0 +1,10 @@
package speaker;
import outputapi.Output;
public class SpeakerOutput implements Output {
@Override
public void write(String sound) {
System.out.println(sound);
}
}