Permalink
Browse files

Put Socket, Audio, and Main threads into their own executors and have…

… the socket thread pause for 1 second before testing readiness in its loop.
  • Loading branch information...
1 parent a549125 commit 87c6e331c7979c9c2f80973e6ccadd99ae73a0fb Chris Sams committed Jul 27, 2012
@@ -7,10 +7,8 @@
import javax.swing.JOptionPane;
import java.io.DataOutputStream;
import java.io.PipedOutputStream;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.ArrayBlockingQueue;
-public class AudioInThread extends Thread {
+public class AudioInThread implements Runnable {
private boolean run;
private boolean audioReady;
private TargetDataLine Line;
@@ -19,12 +17,11 @@
private static int VOLUMEBUFFERSIZE=100;
private int volumeBuffer[]=new int[VOLUMEBUFFERSIZE];
private int volumeBufferCounter=0;
- private static int ISIZE=4096;
+ private static int ISIZE=256;
private static int QUEUE_SIZE=4096;
private byte buffer[]=new byte[ISIZE+1];
-// private PipedOutputStream ps=new PipedOutputStream();
-// private DataOutputStream outPipe=new DataOutputStream(ps);
- private volatile BlockingQueue<Integer> outQueue = new ArrayBlockingQueue<Integer>(QUEUE_SIZE, true);
+ private PipedOutputStream ps=new PipedOutputStream();
+ private DataOutputStream outPipe=new DataOutputStream(ps);
// Filter details ..
// filtertype = Raised Cosine
@@ -69,14 +66,13 @@ public AudioInThread (DMRDecode theApp) {
run=false;
audioReady=false;
gettingAudio=false;
- setPriority(Thread.MIN_PRIORITY);
- start();
- Thread.yield();
+ setupAudio();
}
// Main
public void run() {
// Run continously
+ int i = 0;
for (;;) {
// If it hasn't been already then setup the audio device
if (audioReady==false) setupAudio();
@@ -128,8 +124,7 @@ private void getSample () {
try {
// Put this through a root raised filter
// and then put the result into the output pipe
- //outPipe.writeInt(rootRaisedFilter(sample));
- outQueue.put(rootRaisedFilter(sample));
+ outPipe.writeInt(rootRaisedFilter(sample));
}
catch (Exception e) {
String err=e.getMessage();
@@ -197,12 +192,9 @@ public int returnVolumeAverage () {
return volumeAverage;
}
- public BlockingQueue<Integer> getDataQueue(){
- return this.outQueue;
- }
// Return the PipedOutputSteam object so it can be connected to
-// public PipedOutputStream getPipedWriter() {
-// return ps;
-// }
+ public PipedOutputStream getPipedWriter() {
+ return ps;
+ }
}
@@ -38,7 +38,9 @@
import java.io.IOException;
import java.io.PipedInputStream;
import java.util.ArrayList;
-import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadFactory;
public class DMRDecode {
private DisplayModel display_model;
@@ -93,9 +95,8 @@
private int samplesAheadBuffer[]=new int[SAMPLESAHEADSIZE];
private int samplesAheadCounter=0;
private int jitter=-1;
-// private DataInputStream inPipeData;
-// private PipedInputStream inPipe;
- private BlockingQueue<Integer> inPipeData;
+ private DataInputStream inPipeData;
+ private PipedInputStream inPipe;
private int lastSample=0;
private final int JITTERFRAMEADJUST=1;
private final int JITTERCOUNTERSIZE=(JITTERFRAMEADJUST*144);
@@ -122,13 +123,54 @@
public FileWriter quickLogFile;
private int colourCode=0;
private ArrayList<Integer> incomingDataList=new ArrayList<Integer>();
+
+ private int socketThreadPriority = 3;
+ private int audioInputPriority = 3;
+ private int mainThreadPriority = 5;
+
+ private ExecutorService socketExecutor = Executors.newSingleThreadExecutor(
+ new ThreadFactory(){
+ public Thread newThread(Runnable r){
+ Thread thread = new Thread(r);
+ thread.setName("DMRDecode Socket Thread");
+ thread.setPriority(socketThreadPriority);
+ return thread;
+ }
+ }
+ );
+
+ private ExecutorService mainExecutor = Executors.newSingleThreadExecutor(
+ new ThreadFactory(){
+ public Thread newThread(Runnable r){
+ Thread thread = new Thread(r);
+ thread.setName("DMRDecode Main Thread");
+ thread.setPriority(mainThreadPriority);
+ return thread;
+ }
+ }
+ );
+
+ private ExecutorService audioInputExecutor = Executors.newSingleThreadExecutor(
+ new ThreadFactory(){
+ public Thread newThread(Runnable r){
+ Thread thread = new Thread(r);
+ thread.setName("DMRDecode Audio Input Thread");
+ thread.setPriority(audioInputPriority);
+ return thread;
+ }
+ }
+ );
+
public static void main(String[] args) {
- theApp=new DMRDecode();
- SwingUtilities.invokeLater(new Runnable(){public void run(){theApp.createGUI();}});
- theApp.short_lc.setApp(theApp);
// Setup the TCP/IP socket code
try {
+ theApp=new DMRDecode();
+ SwingUtilities.invokeAndWait(new Runnable(){public void run(){theApp.createGUI();}});
+ theApp.short_lc.setApp(theApp);
+ theApp.socketExecutor.submit(theApp.socketThread);
+
+ //this returns a boolean...
theApp.socketThread.setupSocket();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Error in socket setup during main()","DMRDecode", JOptionPane.INFORMATION_MESSAGE);
@@ -139,20 +181,26 @@ public static void main(String[] args) {
// Start the audio thread
theApp.lineInThread.startAudio();
// Connected a piped input stream to the piped output stream in the thread
-// theApp.inPipe=new PipedInputStream(theApp.lineInThread.getPipedWriter());
+ theApp.inPipe=new PipedInputStream(theApp.lineInThread.getPipedWriter(), 16384);
// Now connect a data input stream to the piped input stream
-// theApp.inPipeData=new DataInputStream(theApp.inPipe);
- theApp.inPipeData = theApp.lineInThread.getDataQueue();
- }
+ theApp.inPipeData=new DataInputStream(theApp.inPipe);
+ }
catch (Exception e) {
JOptionPane.showMessageDialog(null,"Error in main()","DMRDecode", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
- }
- // The main routine
- while (RUNNING) {
- if ((theApp.lineInThread.getAudioReady()==true)&&(theApp.pReady==true)) theApp.decode();
- }
- }
+ }
+
+ theApp.audioInputExecutor.submit(theApp.lineInThread);
+
+ theApp.mainExecutor.submit(new Runnable(){
+ public void run(){
+ while (RUNNING) {
+ if ((theApp.lineInThread.getAudioReady()==true)&&(theApp.pReady==true)) theApp.decode();
+ }
+ }
+ });
+
+ }
// Setup the window //
public void createGUI() {
@@ -168,26 +216,25 @@ public void createGUI() {
window.setVisible(true);
// Make certain the program knows the GUI is ready
pReady=true;
- }
+ }
class WindowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
- }
- }
+ }
+ }
public DisplayFrame getWindow() {
return window;
- }
+ }
public DisplayModel getModel() {
return display_model;
- }
+ }
public DisplayView getView() {
return display_view;
- }
+ }
-
// The main routine for decoding DMR data
public void decode() {
noCarrier();
@@ -220,7 +267,7 @@ private void frameCalcs (int lmin,int lmax) {
}
// Pass these settings to the display bar
- window.displayBarParams(max,min,umid,lmid);
+ window.displayBarParams(max,min,umid,lmid);
}
// This code lifted straight from the DSD source code converted to Java
@@ -529,7 +576,7 @@ private int syncCompare(boolean sync) {
}
// Adds a line to the display as long as pause isn't enabled
- public void addLine(String line,Color col,Font font) {
+ public void addLine(final String line, final Color col, final Font font) {
if (pauseScreen==true) return;
else display_view.add_line(line,col,font);
}
@@ -904,7 +951,7 @@ private int getSample (boolean jitmode) {
int sample=0;
// Get the sample from the sound card via the sound thread
try {
- sample=inPipeData.take();
+ sample=inPipeData.readInt();
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,"Error in getSample()","DMRDecode", JOptionPane.INFORMATION_MESSAGE);
@@ -39,7 +39,7 @@
public DisplayView (DMRDecode theApp) {
this.theApp=theApp;
- }
+ }
public void update (Observable o,Object rectangle) {
}
@@ -106,4 +106,4 @@ public String getText() {
return buffer.toString();
}
-}
+}
@@ -5,7 +5,7 @@
import java.net.ServerSocket;
import java.net.Socket;
-public class SocketOut extends Thread {
+public class SocketOut implements Runnable {
private boolean ready;
private static int PORT=17887;
private static int MAXCONNS=10;
@@ -16,9 +16,6 @@
public SocketOut (DMRDecode theApp) {
ready=false;
- setPriority(Thread.MIN_PRIORITY);
- start();
- Thread.yield();
}
// Main
@@ -28,6 +25,13 @@ public void run() {
for (;;) {
// Wait for a socket connection if the listening socket has been setup
// and there is a free socket available
+ if(ready == false){
+ try{
+ Thread.sleep(1000);
+ }catch(InterruptedException ie){
+ }
+ continue;
+ }
if ((ready==true)&&(checkForFreeSockets()==true)) {
// Get the index of the next available socket
next=nextFreeSocket();

0 comments on commit 87c6e33

Please sign in to comment.