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();
}
}