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();
|
||||||
|
}
|
||||||
|
}
|
||||||
7
Radio1/bnd.bnd
Normal file
7
Radio1/bnd.bnd
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
Bundle-Version: 1.0.0
|
||||||
|
Private-Package: Radio1
|
||||||
|
Bundle-Activator: Radio1.Radio1
|
||||||
|
|
||||||
|
-buildfeatures:
|
||||||
|
-buildpath: \
|
||||||
|
channelapi;version=latest
|
||||||
25
Radio1/src/Radio1/Radio1.java
Normal file
25
Radio1/src/Radio1/Radio1.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Radio1/src/Radio1/Radio1Channel.java
Normal file
13
Radio1/src/Radio1/Radio1Channel.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Radio1/src/Radio1/Radio1Stream.java
Normal file
10
Radio1/src/Radio1/Radio1Stream.java
Normal 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
7
Radio538/bnd.bnd
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
Bundle-Version: 1.0.0
|
||||||
|
Private-Package: Radio538
|
||||||
|
Bundle-Activator: Radio538.Radio538
|
||||||
|
|
||||||
|
-buildfeatures:
|
||||||
|
-buildpath: \
|
||||||
|
channelapi;version=latest
|
||||||
26
Radio538/src/Radio538/Radio538.java
Normal file
26
Radio538/src/Radio538/Radio538.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Radio538/src/Radio538/Radio538Channel.java
Normal file
12
Radio538/src/Radio538/Radio538Channel.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Radio538/src/Radio538/Radio538Stream.java
Normal file
10
Radio538/src/Radio538/Radio538Stream.java
Normal 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
1
RadioRunner/bnd.bnd
Normal file
@@ -0,0 +1 @@
|
|||||||
|
-nobundles: true
|
||||||
0
RadioRunner/conf/.gitignore
vendored
Normal file
0
RadioRunner/conf/.gitignore
vendored
Normal file
12
RadioRunner/run.bndrun
Normal file
12
RadioRunner/run.bndrun
Normal 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
5
channelapi/bnd.bnd
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Bundle-Version: 1.0.0
|
||||||
|
Export-Package: channelapi
|
||||||
|
|
||||||
|
-buildfeatures:
|
||||||
|
-buildpath:
|
||||||
5
channelapi/src/channelapi/Channel.java
Normal file
5
channelapi/src/channelapi/Channel.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package channelapi;
|
||||||
|
|
||||||
|
public interface Channel {
|
||||||
|
Stream getStream();
|
||||||
|
}
|
||||||
5
channelapi/src/channelapi/Stream.java
Normal file
5
channelapi/src/channelapi/Stream.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package channelapi;
|
||||||
|
|
||||||
|
public interface Stream {
|
||||||
|
String read();
|
||||||
|
}
|
||||||
2
channelapi/src/channelapi/package-info.java
Normal file
2
channelapi/src/channelapi/package-info.java
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
@org.osgi.annotation.versioning.Version("1.0.0")
|
||||||
|
package channelapi;
|
||||||
5
outputapi/bnd.bnd
Normal file
5
outputapi/bnd.bnd
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Bundle-Version: 1.0.0
|
||||||
|
Export-Package: outputapi
|
||||||
|
|
||||||
|
-buildfeatures:
|
||||||
|
-buildpath:
|
||||||
5
outputapi/src/outputapi/Output.java
Normal file
5
outputapi/src/outputapi/Output.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package outputapi;
|
||||||
|
|
||||||
|
public interface Output {
|
||||||
|
void write (String sound);
|
||||||
|
}
|
||||||
2
outputapi/src/outputapi/package-info.java
Normal file
2
outputapi/src/outputapi/package-info.java
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
@org.osgi.annotation.versioning.Version("1.0.0")
|
||||||
|
package outputapi;
|
||||||
7
speaker/bnd.bnd
Normal file
7
speaker/bnd.bnd
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
Bundle-Version: 1.0.0
|
||||||
|
Private-Package: speaker
|
||||||
|
Bundle-Activator: speaker.Speaker
|
||||||
|
|
||||||
|
-buildfeatures:
|
||||||
|
-buildpath: \
|
||||||
|
outputapi;version=latest
|
||||||
25
speaker/src/speaker/Speaker.java
Normal file
25
speaker/src/speaker/Speaker.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
10
speaker/src/speaker/SpeakerOutput.java
Normal file
10
speaker/src/speaker/SpeakerOutput.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user