Radio example implementation
This commit is contained in:
8
Radio/bnd.bnd
Normal file
8
Radio/bnd.bnd
Normal 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
|
||||
21
Radio/src/Radio/ChannelSelector.java
Normal file
21
Radio/src/Radio/ChannelSelector.java
Normal 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;
|
||||
}
|
||||
}
|
||||
21
Radio/src/Radio/OutputSelector.java
Normal file
21
Radio/src/Radio/OutputSelector.java
Normal 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;
|
||||
}
|
||||
}
|
||||
30
Radio/src/Radio/Radio.java
Normal file
30
Radio/src/Radio/Radio.java
Normal 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();
|
||||
}
|
||||
}
|
||||
44
Radio/src/Radio/Receiver.java
Normal file
44
Radio/src/Radio/Receiver.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user